bevy_vista 0.17.1

A visual UI editor plugin for Bevy with inspector-driven editing and .vista.ron serialization.
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
use std::collections::{HashMap, HashSet};

use bevy::prelude::*;

use crate::core::widget::layout::spawn_tree_node;
use crate::core::widget::{TreeNodeBuilder, TreeNodeHeader, TreeNodeItemId, TreeViewBuilder};

use super::*;

#[derive(Resource)]
pub(crate) struct HierarchyState {
    pub dirty: bool,
}

impl Default for HierarchyState {
    fn default() -> Self {
        Self { dirty: true }
    }
}

#[derive(Resource, Default)]
pub(super) struct HierarchyDragState {
    dragging_node: Option<blueprint::BlueprintNodeId>,
    hover_target: Option<blueprint::BlueprintNodeId>,
    ghost_entity: Option<Entity>,
    source_header: Option<Entity>,
}

#[derive(Resource, Default)]
pub(super) struct HierarchyTreeCache {
    tree_content: Option<Entity>,
    nodes: HashMap<blueprint::BlueprintNodeId, HierarchyNodeUi>,
}

#[derive(Clone, Copy)]
struct HierarchyNodeUi {
    node: Entity,
    children_container: Entity,
    label: Entity,
}

#[derive(Component)]
pub(super) struct HierarchyContentRoot;

#[derive(Component)]
pub(super) struct HierarchyItemObserverBound;

pub(super) fn init_hierarchy_panel(
    mut commands: Commands,
    hierarchy: Single<Entity, With<Hierarchy>>,
    editor_theme: Res<EditorTheme>,
    mut cache: ResMut<HierarchyTreeCache>,
) {
    commands
        .entity(*hierarchy)
        .insert(BackgroundColor(editor_theme.0.palette.surface));

    let content = commands
        .spawn((
            Name::new("Hierarchy Content"),
            Node {
                width: percent(100.0),
                flex_direction: FlexDirection::Column,
                row_gap: px(2.0),
                padding: UiRect::all(px(4.0)),
                ..default()
            },
            HierarchyContentRoot,
        ))
        .observe(on_hierarchy_content_drag_end)
        .observe(on_hierarchy_content_drag_cancel)
        .id();

    let tree = TreeViewBuilder::new()
        .width(percent(100.0))
        .height(percent(100.0))
        .item_gap(2.0)
        .padding(UiRect::all(px(0.0)))
        .build_with_result(
            &mut commands,
            std::iter::empty::<TreeNodeBuilder>(),
            Some(&editor_theme.0),
        );
    cache.tree_content = Some(tree.content);

    commands.entity(*hierarchy).add_child(content);
    commands.entity(content).add_child(tree.root);
}

pub(super) fn refresh_hierarchy_panel(
    mut commands: Commands,
    mut state: ResMut<HierarchyState>,
    drag_state: Res<HierarchyDragState>,
    document: Res<blueprint::WidgetBlueprintDocument>,
    editor_theme: Res<EditorTheme>,
    mut cache: ResMut<HierarchyTreeCache>,
    parents: Query<&ChildOf>,
) {
    if drag_state.dragging_node.is_some() || !state.dirty {
        return;
    }
    let Some(tree_content) = cache.tree_content else {
        return;
    };

    let mut alive = HashSet::new();
    let mut root_entities = Vec::with_capacity(document.roots.len());
    for root in document.roots.iter().copied() {
        if let Some(entity) = ensure_hierarchy_node(
            &mut commands,
            &document,
            &mut cache,
            root,
            &mut alive,
            Some(&editor_theme.0),
        ) {
            root_entities.push(entity);
        }
    }
    commands
        .entity(tree_content)
        .replace_children(&root_entities);

    let stale = cache
        .nodes
        .keys()
        .copied()
        .filter(|id| !alive.contains(id))
        .collect::<Vec<_>>();
    let stale_entities = stale
        .iter()
        .filter_map(|id| cache.nodes.get(id).map(|ui| ui.node))
        .collect::<HashSet<_>>();
    for id in stale {
        if let Some(ui) = cache.nodes.remove(&id) {
            let is_child_of_stale = parents
                .get(ui.node)
                .map(|parent| stale_entities.contains(&parent.parent()))
                .unwrap_or(false);
            if !is_child_of_stale {
                commands.entity(ui.node).despawn();
            }
        }
    }

    state.dirty = false;
}

pub(super) fn attach_hierarchy_tree_item_observers(
    mut commands: Commands,
    hierarchy_content: Single<Entity, With<HierarchyContentRoot>>,
    parents: Query<&ChildOf>,
    headers: Query<
        Entity,
        (
            With<TreeNodeHeader>,
            With<TreeNodeItemId>,
            Without<HierarchyItemObserverBound>,
        ),
    >,
) {
    for header in headers.iter() {
        if !is_descendant_of(header, *hierarchy_content, &parents) {
            continue;
        }
        commands
            .entity(header)
            .observe(on_hierarchy_item_click)
            .observe(on_hierarchy_item_drag_start)
            .observe(on_hierarchy_item_drag)
            .observe(on_hierarchy_item_over)
            .observe(on_hierarchy_item_out)
            .observe(on_hierarchy_item_drag_end)
            .observe(on_hierarchy_item_drag_cancel)
            .insert(HierarchyItemObserverBound);
    }
}

fn on_hierarchy_item_click(
    mut event: On<Pointer<Click>>,
    options: Res<VistaEditorViewOptions>,
    runtime_map: Res<blueprint::BlueprintRuntimeMap>,
    parents: Query<&ChildOf>,
    headers: Query<(), (With<TreeNodeHeader>, With<TreeNodeItemId>)>,
    item_ids: Query<&TreeNodeItemId>,
    mut selection: ResMut<VistaEditorSelection>,
) {
    if options.is_preview_mode {
        return;
    }
    let Some(header) = find_ancestor_with(event.event_target(), &parents, |entity| {
        headers.contains(entity)
    }) else {
        return;
    };
    let Ok(item_id) = item_ids.get(header) else {
        return;
    };
    selection.selected_entity = runtime_map.node_to_entity.get(&item_id.0).copied();
    event.propagate(false);
}

fn on_hierarchy_item_drag_start(
    mut event: On<Pointer<DragStart>>,
    mut commands: Commands,
    options: Res<VistaEditorViewOptions>,
    parents: Query<&ChildOf>,
    headers: Query<(), (With<TreeNodeHeader>, With<TreeNodeItemId>)>,
    item_ids: Query<&TreeNodeItemId>,
    mut row_bg: Query<&mut BackgroundColor>,
    mut drag: ResMut<HierarchyDragState>,
    document: Res<blueprint::WidgetBlueprintDocument>,
    editor_root: Single<Entity, With<EditorRoot>>,
    editor_theme: Res<EditorTheme>,
) {
    if options.is_preview_mode {
        return;
    }
    let Some(header) = find_ancestor_with(event.event_target(), &parents, |entity| {
        headers.contains(entity)
    }) else {
        return;
    };
    let Ok(item_id) = item_ids.get(header) else {
        return;
    };

    if let Some(ghost) = drag.ghost_entity.take() {
        commands.entity(ghost).despawn();
    }

    let label = document
        .nodes
        .get(&item_id.0)
        .map(|node| node.name.clone())
        .unwrap_or_else(|| "Node".to_owned());
    let theme = &editor_theme.0;
    let ghost_bg = theme.palette.surface_variant.lighter(0.08).with_alpha(0.9);
    let text_color = theme.palette.on_surface;
    let font = theme.typography.body_medium.font.clone();
    let pointer = event.event().pointer_location.position;
    let ghost = commands
        .spawn((
            Name::new("Hierarchy Drag Ghost"),
            Node {
                position_type: PositionType::Absolute,
                left: px(pointer.x + 12.0),
                top: px(pointer.y + 12.0),
                padding: UiRect::axes(px(8.0), px(4.0)),
                ..default()
            },
            BackgroundColor(ghost_bg),
            BorderRadius::all(px(5.0)),
            GlobalZIndex(EDITOR_DEFAULT_Z_INDEX + 100),
            children![(Text::new(label), font, TextColor(text_color),)],
        ))
        .id();
    commands.entity(*editor_root).add_child(ghost);

    drag.dragging_node = Some(item_id.0);
    drag.hover_target = None;
    drag.ghost_entity = Some(ghost);
    drag.source_header = Some(header);
    if let Ok(mut bg) = row_bg.get_mut(header) {
        bg.0 = bg.0.with_alpha(0.5);
    }
    event.propagate(false);
}

fn on_hierarchy_item_drag(
    event: On<Pointer<Drag>>,
    drag: Res<HierarchyDragState>,
    mut nodes: Query<&mut Node>,
) {
    let Some(ghost) = drag.ghost_entity else {
        return;
    };
    let Ok(mut node) = nodes.get_mut(ghost) else {
        return;
    };
    let pointer = event.event().pointer_location.position;
    node.left = px(pointer.x + 12.0);
    node.top = px(pointer.y + 12.0);
}

fn on_hierarchy_item_over(
    event: On<Pointer<Over>>,
    mut drag: ResMut<HierarchyDragState>,
    parents: Query<&ChildOf>,
    headers: Query<(), (With<TreeNodeHeader>, With<TreeNodeItemId>)>,
    item_ids: Query<&TreeNodeItemId>,
    mut row_bg: Query<&mut BackgroundColor>,
    editor_theme: Res<EditorTheme>,
) {
    if drag.dragging_node.is_none() {
        return;
    }
    let Some(header) = find_ancestor_with(event.event_target(), &parents, |entity| {
        headers.contains(entity)
    }) else {
        return;
    };
    let Ok(item_id) = item_ids.get(header) else {
        return;
    };
    if drag.dragging_node == Some(item_id.0) {
        drag.hover_target = None;
        return;
    }
    drag.hover_target = Some(item_id.0);
    let hover_color = editor_theme.0.palette.outline_variant;
    if let Ok(mut bg) = row_bg.get_mut(header) {
        bg.0 = hover_color;
    }
}

fn on_hierarchy_item_out(
    event: On<Pointer<Out>>,
    mut drag: ResMut<HierarchyDragState>,
    parents: Query<&ChildOf>,
    headers: Query<(), (With<TreeNodeHeader>, With<TreeNodeItemId>)>,
    item_ids: Query<&TreeNodeItemId>,
    mut row_bg: Query<&mut BackgroundColor>,
    editor_theme: Res<EditorTheme>,
) {
    if drag.dragging_node.is_none() {
        return;
    }
    let Some(header) = find_ancestor_with(event.event_target(), &parents, |entity| {
        headers.contains(entity)
    }) else {
        return;
    };
    let Ok(item_id) = item_ids.get(header) else {
        return;
    };
    if drag.hover_target == Some(item_id.0) {
        drag.hover_target = None;
    }
    let normal_bg = editor_theme.0.palette.surface_variant;
    if let Ok(mut bg) = row_bg.get_mut(header) {
        bg.0 = normal_bg;
    }
}

fn on_hierarchy_item_drag_end(
    mut event: On<Pointer<DragEnd>>,
    mut commands: Commands,
    options: Res<VistaEditorViewOptions>,
    parents: Query<&ChildOf>,
    headers: Query<(), (With<TreeNodeHeader>, With<TreeNodeItemId>)>,
    item_ids: Query<&TreeNodeItemId>,
    mut row_bg: Query<&mut BackgroundColor>,
    mut drag: ResMut<HierarchyDragState>,
    widget_registry: Res<WidgetRegistry>,
    mut document: ResMut<blueprint::WidgetBlueprintDocument>,
    mut state: ResMut<HierarchyState>,
) {
    if options.is_preview_mode {
        return;
    }
    let header = find_ancestor_with(event.event_target(), &parents, |entity| {
        headers.contains(entity)
    });
    let source = drag
        .dragging_node
        .or_else(|| header.and_then(|entity| item_ids.get(entity).ok().map(|item| item.0)));
    let Some(source) = source else {
        cleanup_hierarchy_drag_visual(&mut commands, &mut drag, &mut row_bg);
        state.dirty = true;
        return;
    };

    match drag.hover_target {
        Some(target) if target != source => {
            let _ = blueprint::apply_blueprint_command(
                blueprint::BlueprintCommand::MoveNode {
                    node: source,
                    new_parent: Some(target),
                    index: None,
                },
                &mut document,
                &widget_registry,
            );
            state.dirty = true;
        }
        None => {
            let _ = blueprint::apply_blueprint_command(
                blueprint::BlueprintCommand::MoveNode {
                    node: source,
                    new_parent: None,
                    index: None,
                },
                &mut document,
                &widget_registry,
            );
            state.dirty = true;
        }
        _ => {}
    }

    cleanup_hierarchy_drag_visual(&mut commands, &mut drag, &mut row_bg);
    event.propagate(false);
}

fn on_hierarchy_item_drag_cancel(
    mut event: On<Pointer<Cancel>>,
    mut commands: Commands,
    mut row_bg: Query<&mut BackgroundColor>,
    mut state: ResMut<HierarchyState>,
    mut drag: ResMut<HierarchyDragState>,
) {
    cleanup_hierarchy_drag_visual(&mut commands, &mut drag, &mut row_bg);
    state.dirty = true;
    event.propagate(false);
}

fn on_hierarchy_content_drag_end(
    mut event: On<Pointer<DragEnd>>,
    mut commands: Commands,
    options: Res<VistaEditorViewOptions>,
    mut row_bg: Query<&mut BackgroundColor>,
    mut drag: ResMut<HierarchyDragState>,
    widget_registry: Res<WidgetRegistry>,
    mut document: ResMut<blueprint::WidgetBlueprintDocument>,
    mut state: ResMut<HierarchyState>,
) {
    if options.is_preview_mode {
        return;
    }
    let Some(source) = drag.dragging_node else {
        return;
    };

    if drag.hover_target.is_none() {
        let _ = blueprint::apply_blueprint_command(
            blueprint::BlueprintCommand::MoveNode {
                node: source,
                new_parent: None,
                index: None,
            },
            &mut document,
            &widget_registry,
        );
    }

    cleanup_hierarchy_drag_visual(&mut commands, &mut drag, &mut row_bg);
    state.dirty = true;
    event.propagate(false);
}

fn on_hierarchy_content_drag_cancel(
    mut event: On<Pointer<Cancel>>,
    mut commands: Commands,
    mut row_bg: Query<&mut BackgroundColor>,
    mut state: ResMut<HierarchyState>,
    mut drag: ResMut<HierarchyDragState>,
) {
    cleanup_hierarchy_drag_visual(&mut commands, &mut drag, &mut row_bg);
    state.dirty = true;
    event.propagate(false);
}

fn cleanup_hierarchy_drag_visual(
    commands: &mut Commands,
    drag: &mut HierarchyDragState,
    row_bg: &mut Query<&mut BackgroundColor>,
) {
    if let Some(ghost) = drag.ghost_entity.take() {
        commands.entity(ghost).despawn();
    }
    if let Some(header) = drag.source_header.take()
        && let Ok(mut bg) = row_bg.get_mut(header)
    {
        bg.0 = bg.0.with_alpha(1.0);
    }
    drag.dragging_node = None;
    drag.hover_target = None;
}

fn ensure_hierarchy_node(
    commands: &mut Commands,
    document: &blueprint::WidgetBlueprintDocument,
    cache: &mut HierarchyTreeCache,
    node_id: blueprint::BlueprintNodeId,
    alive: &mut HashSet<blueprint::BlueprintNodeId>,
    theme: Option<&Theme>,
) -> Option<Entity> {
    let node = document.nodes.get(&node_id)?;
    alive.insert(node_id);

    let label = node.name.clone();

    let ui = if let Some(ui) = cache.nodes.get(&node_id).copied() {
        commands.entity(ui.label).insert(Text::new(label));
        ui
    } else {
        let builder = if node.children.is_empty() {
            TreeNodeBuilder::leaf(label)
        } else {
            TreeNodeBuilder::branch(label, true, Vec::new())
        }
        .with_item_id(node_id);
        let spawned = spawn_tree_node(commands, builder, theme, 14.0, 2.0);
        let ui = HierarchyNodeUi {
            node: spawned.node,
            children_container: spawned.children_container,
            label: spawned.label,
        };
        cache.nodes.insert(node_id, ui);
        ui
    };

    let mut child_entities = Vec::with_capacity(node.children.len());
    for child in node.children.iter().copied() {
        if let Some(entity) = ensure_hierarchy_node(commands, document, cache, child, alive, theme)
        {
            child_entities.push(entity);
        }
    }
    commands
        .entity(ui.children_container)
        .replace_children(&child_entities);
    Some(ui.node)
}

fn is_descendant_of(entity: Entity, ancestor: Entity, parents: &Query<&ChildOf>) -> bool {
    find_ancestor_with(entity, parents, |e| e == ancestor).is_some()
}

fn find_ancestor_with<F>(
    mut current: Entity,
    parents: &Query<&ChildOf>,
    mut predicate: F,
) -> Option<Entity>
where
    F: FnMut(Entity) -> bool,
{
    loop {
        if predicate(current) {
            return Some(current);
        }
        let Ok(parent) = parents.get(current) else {
            return None;
        };
        current = parent.parent();
    }
}