-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode_graph.rs
319 lines (297 loc) · 12.2 KB
/
node_graph.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
use narui::{
layout::Maximal,
re_export::{
lyon::{
lyon_tessellation::{path::geom::Point, FillTessellator, StrokeTessellator},
tessellation::{path::path::Builder, LineCap, StrokeOptions},
},
palette::Shade,
},
renderer::ColoredBuffersBuilder,
*,
};
use std::sync::Arc;
#[widget]
pub fn drag_detector(
#[default] on_drag: impl Fn(&CallbackContext, Vec2) + Clone + Sync + Send + 'static,
#[default] on_start: impl Fn(&CallbackContext, Fragment) + Clone + Sync + Send + 'static,
#[default] on_end: impl Fn(&CallbackContext, Fragment) + Clone + Sync + Send + 'static,
#[default] relative: bool,
children: Fragment,
context: &mut WidgetContext,
) -> Fragment {
let click_start_position = context.listenable(Vec2::zero());
let click_started = context.listenable(false);
let clicked = context.listenable(false);
let key = context.widget_local.idx;
let on_click = move |context: &CallbackContext, clicked_current, _, _| {
context.shout(clicked, clicked_current);
if clicked_current {
context.shout(click_started, true);
on_start(context, key)
} else {
on_end(context, key)
}
};
let on_move = move |context: &CallbackContext, position, _| {
if context.spy(click_started) {
context.shout(click_start_position, position);
context.shout(click_started, false);
} else if context.spy(clicked) {
on_drag(
context,
position
- if relative {
context.spy(click_start_position)
} else {
context.measure_size(key).unwrap().logical / 2.
},
)
}
};
rsx! {
<input on_move=on_move on_click=on_click>
<fragment>{children.into()}</fragment>
</input>
}
}
#[widget]
pub fn hr(color: Color, context: &mut WidgetContext) -> Fragment {
rsx! {
<sized constraint={BoxConstraints::min_height(10.)}>
<rect_leaf fill=Some(color) />
</sized>
}
}
#[widget]
pub fn handle(
color: Color,
#[default(20.0)] size: f32,
graph_root: Fragment,
parent_node: Fragment,
on_drag: impl Fn(&CallbackContext, Vec2, Vec2, Color) + Clone + Sync + Send + 'static,
on_drag_end: impl Fn(&CallbackContext, Fragment, Fragment) + Clone + Sync + Send + 'static,
on_drag_start: impl Fn(&CallbackContext, Fragment, Fragment) + Clone + Sync + Send + 'static,
context: &mut WidgetContext,
) -> Fragment {
let this_key = context.widget_local.idx;
let on_drag = move |context: &CallbackContext, pos: Vec2| {
let size = context.measure_size(this_key).unwrap().logical;
let position = context.measure_offset(graph_root, this_key).unwrap().logical;
let start = position + (size / 2.);
let end = start + pos;
on_drag(context, start, end, color);
};
rsx! {
<sized constraint={BoxConstraints::tight(size, size)}>
<drag_detector
on_drag=on_drag
on_end=(move |context, key| {on_drag_end(context, key, parent_node)})
on_start=(move |context, key| {on_drag_start(context, key, parent_node)})
>
<rect_leaf
fill=Some(color)
border_radius=Fraction(1.0)
/>
</drag_detector>
</sized>
}
}
#[widget]
pub fn connection(
start: Vec2,
end: Vec2,
color: Color,
context: &mut WidgetContext,
) -> FragmentInner {
let path_gen = Arc::new(
move |_size: Vec2,
_fill_tess: &mut FillTessellator,
stroke_tess: &mut StrokeTessellator,
mut buffers_builder: ColoredBuffersBuilder| {
let mut builder = Builder::new();
builder.begin(start.into());
builder.cubic_bezier_to(
Point::new((start.x + end.x) / 2.0, start.y),
Point::new((start.x + end.x) / 2.0, end.y),
end.into(),
);
builder.end(false);
stroke_tess
.tessellate_path(
&builder.build(),
&StrokeOptions::default().with_line_width(5.0),
&mut buffers_builder.with_color(color),
)
.unwrap();
},
);
let mut stroke_options = StrokeOptions::default();
stroke_options.line_width = 5.;
stroke_options.end_cap = LineCap::Round;
stroke_options.start_cap = LineCap::Round;
FragmentInner::Leaf {
render_object: RenderObject::Path { path_gen },
layout: Box::new(Maximal),
}
}
fn get_handle_offset(
context: &CallbackContext,
handle: Fragment,
node: Fragment,
) -> Result<Vec2, MeasureError> {
Ok(context.measure_offset(node, handle)?.logical + (context.measure_size(handle)?.logical / 2.))
}
#[widget]
pub fn node(
name: impl ToString + Clone + Send + Sync + 'static,
on_drag: impl Fn(&CallbackContext, Vec2) + Clone + Sync + Send + 'static,
on_handle_drag: impl Fn(&CallbackContext, Vec2, Vec2, Color) + Clone + Sync + Send + 'static,
on_handle_drag_start: impl Fn(&CallbackContext, Fragment, Fragment) + Clone + Sync + Send + 'static,
on_handle_drag_end: impl Fn(&CallbackContext, Fragment, Fragment) + Clone + Sync + Send + 'static,
graph_root: Fragment,
context: &mut WidgetContext,
) -> Fragment {
let key = context.widget_local.idx;
let fill_color = Color::from_linear(Shade::lighten(&theme::BG_DARK.into_linear(), 0.1));
let stroke_color = Color::from_linear(Shade::lighten(&theme::BG_LIGHT.into_linear(), 0.2));
rsx! {
<sized constraint=BoxConstraints::tight(250., 150.)>
<stack>
<padding padding=EdgeInsets::horizontal(10.0)>
<rect_leaf border_radius=Paxel(10.0) fill=Some(fill_color) stroke=Some((stroke_color, 2.0)) />
</padding>
<column>
<padding padding=EdgeInsets::horizontal(10.0)>
<drag_detector on_drag=on_drag relative=true>
<column main_axis_size=MainAxisSize::Min>
<text>{name}</text>
<hr color=stroke_color />
</column>
</drag_detector>
</padding>
<flexible fit=FlexFit::Tight>
<stack>
<align alignment=Alignment::center_left()>
<column main_axis_alignment=MainAxisAlignment::SpaceEvenly>
<handle on_drag_end=on_handle_drag_end.clone() on_drag_start=on_handle_drag_start.clone() graph_root=graph_root parent_node=key on_drag=on_handle_drag.clone() color={Color::new(1., 1., 0., 1.)} />
<handle on_drag_end=on_handle_drag_end.clone() on_drag_start=on_handle_drag_start.clone() graph_root=graph_root parent_node=key on_drag=on_handle_drag.clone() color={Color::new(0., 1., 1., 1.)} />
</column>
</align>
<align alignment=Alignment::center_right()>
<column>
<handle on_drag_end=on_handle_drag_end on_drag_start=on_handle_drag_start graph_root=graph_root parent_node=key on_drag=on_handle_drag color={Color::new(1., 0., 1., 1.)} />
</column>
</align>
/* TODO: add controls, etc */
</stack>
</flexible>
</column>
</stack>
</sized>
}
}
#[widget]
pub fn node_graph(context: &mut WidgetContext) -> Fragment {
let this_key = context.widget_local.idx;
let nodes = context.listenable(vec![
("narui", Vec2::zero()),
("rocks", Vec2::new(300., 400.)),
("hard", Vec2::new(600., 400.)),
]);
let current_nodes = context.listen(nodes);
let current_nodes_clone = current_nodes.clone();
let current_connection = context.listenable(None);
let settled_connections: Listenable<Vec<((usize, Vec2), (usize, Vec2), Color)>> =
context.listenable(vec![]);
let on_handle_drag = move |context: &CallbackContext, start: Vec2, end: Vec2, color: Color| {
context.shout(current_connection, Some((start, end, color)))
};
let drop_handle: Listenable<Option<(Fragment, Fragment, usize)>> = context.listenable(None);
let drag_handle: Listenable<Option<(Fragment, Fragment, usize)>> = context.listenable(None);
context.after_frame(move |context| {
if context.spy(drop_handle).is_some() {
let start = context.spy(drag_handle).unwrap();
let end = context.spy(drop_handle).unwrap();
let connection = (
(start.2, get_handle_offset(context, start.0, start.1).unwrap()),
(end.2, get_handle_offset(context, end.0, end.1).unwrap()),
Color::new(1., 1., 1., 1.),
);
let mut connections = context.spy(settled_connections);
if let Some(i) = connections.iter().position(|x| x == &connection) {
connections.remove(i);
} else {
connections.push(connection);
}
context.shout(settled_connections, connections);
context.shout(drag_handle, None);
context.shout(drop_handle, None);
}
});
rsx! {
<stack>
<stack fit=StackFit::Tight>
{current_nodes.iter().cloned().enumerate().map(|(i, (name, position))| {
let current_nodes_clone = current_nodes_clone.clone();
rsx! {
<positioned pos=AbsolutePosition::from_offset(position.into()) key=i>
<node
name=name
graph_root=this_key
on_drag={move |context: &CallbackContext, pos: Vec2| {
let mut new_positions = current_nodes_clone.clone();
new_positions[i].1 = position + pos;
context.shout(nodes, new_positions);
}}
on_handle_drag=on_handle_drag
on_handle_drag_end={move |context: &CallbackContext, handle: Fragment, node: Fragment| {
if handle != context.spy(drag_handle).unwrap().0 {
context.shout(drop_handle, Some((handle, node, i)));
}
context.shout(current_connection, None);
}}
on_handle_drag_start={move |context: &CallbackContext, handle, node| {
context.shout(drag_handle, Some((handle, node, i)));
}}
/>
</positioned>
}
}).collect()}
</stack>
<fragment>
{
if let Some((start, end, color)) = context.listen(current_connection) {
Some(rsx! { <connection start=start end=end color=color /> })
} else {
None
}
}
</fragment>
<stack>
{
context.listen(settled_connections).iter().enumerate().map(|(i, (start, end, color))| {
let start = {
let (i, vec) = start;
context.listen(nodes)[*i].1 + *vec
};
let end = {
let (i, vec) = end;
context.listen(nodes)[*i].1 + *vec
};
rsx! {<connection key=i start=start end=end color=*color />}
}).collect()
}
</stack>
</stack>
}
}
fn main() {
env_logger::init();
app::render(
app::WindowBuilder::new().with_title("narui node graph demo"),
rsx_toplevel! {
<node_graph />
},
);
}