bevy_animation_graph_editor 0.9.0

Animation graph editor for the Bevy game engine
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
use bevy::{
    asset::Assets,
    ecs::{entity::Entity, observer::On, system::Commands},
    prelude::{AppTypeRegistry, World},
};
use bevy_animation_graph::{
    builtin_nodes::fsm_node::FsmNode,
    core::{
        animation_graph::AnimationGraph,
        animation_node::AnimationNode,
        context::{graph_context::GraphState, graph_context_arena::GraphContextId},
        state_machine::high_level::{DirectTransition, State, StateMachine},
    },
};
use egui::Widget;
use egui_dock::egui;

use super::EditorWindowRegistrationContext;
use crate::ui::{
    actions::{
        EditorAction,
        graph::{EditNode, GraphAction, RenameNode, UpdateDefaultData, UpdateGraphSpec},
    },
    generic_widgets::{
        data_value::DataValueWidget,
        fsm::{
            direct_transition::DirectTransitionWidget, state::StateWidget,
            state_id_mut::StateIdWidget,
        },
        graph_input_pin::GraphInputPinWidget,
        hashmap::HashMapWidget,
        io_spec::IoSpecWidget,
    },
    native_windows::{EditorWindowContext, NativeEditorWindowExtension},
    node_editors::{ReflectEditable, reflect_editor::ReflectNodeEditor},
    state_management::{
        global::{
            active_fsm::ActiveFsm,
            active_fsm_state::ActiveFsmState,
            active_fsm_transition::ActiveFsmTransition,
            active_graph::ActiveGraph,
            active_graph_context::{ActiveContexts, SetActiveContext},
            active_graph_node::ActiveGraphNode,
            fsm::{SetFsmNodeSpec, SetFsmStartState, UpdateDirectTransition, UpdateState},
            get_global_state,
            inspector_selection::{InspectorSelection, SetInspectorSelection},
        },
        window::buffers::ClearBuffers,
    },
    utils::{self, with_assets_all},
};

#[derive(Debug)]
pub struct InspectorWindow;

impl NativeEditorWindowExtension for InspectorWindow {
    fn init(&self, world: &mut World, ctx: &EditorWindowRegistrationContext) {
        let window = ctx.window;
        world.add_observer(
            move |_: On<SetInspectorSelection>, mut commands: Commands| {
                commands.trigger(ClearBuffers(window));
            },
        );
    }

    fn ui(&self, ui: &mut egui::Ui, world: &mut World, ctx: &mut EditorWindowContext) {
        let inspector_selection = get_global_state::<InspectorSelection>(world)
            .cloned()
            .unwrap_or_default();

        ui.push_id(&inspector_selection, |ui| match inspector_selection {
            InspectorSelection::ActiveFsm => {
                fsm_inspector(world, ui, ctx);
            }
            InspectorSelection::ActiveFsmTransition => {
                transition_inspector(world, ui, ctx);
            }
            InspectorSelection::ActiveFsmState => {
                state_inspector(world, ui, ctx);
            }
            InspectorSelection::ActiveGraph => {
                graph_inspector(world, ui, ctx);
            }
            InspectorSelection::ActiveNode => {
                node_inspector(world, ui, ctx);
            }
            InspectorSelection::Nothing => {}
        });
    }

    fn display_name(&self) -> String {
        "Inspector".to_string()
    }
}

fn node_inspector(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    ui.heading("Graph node");

    let active_node = get_global_state::<ActiveGraphNode>(world).cloned()?;

    with_assets_all(
        world,
        [active_node.handle.id()],
        |world: &mut World, [graph]| -> Option<_> {
            let node = graph.nodes.get(&active_node.node)?;

            let node_buffer_id = ui.id().with("graph node buffer");

            let node_buffer = ctx.buffers.get_mut_or_insert_with_condition(
                node_buffer_id,
                |n: &AnimationNode| n.id != active_node.node,
                || node.clone(),
            );

            let response = ui.text_edit_singleline(&mut node_buffer.name);
            let mut clear = false;

            if response.lost_focus() {
                ctx.editor_actions
                    .push(EditorAction::Graph(GraphAction::RenameNode(RenameNode {
                        graph: active_node.handle.clone(),
                        node: active_node.node,
                        new_name: node_buffer.name.clone(),
                    })));
                clear = true;
            }

            let editor = if let Some(editable) = world
                .resource::<AppTypeRegistry>()
                .0
                .clone()
                .read()
                .get_type_data::<ReflectEditable>(node.inner.type_id())
            {
                (editable.get_editor)(node.inner.as_ref())
            } else {
                Box::new(ReflectNodeEditor)
            };

            let inner_edit_response = editor.show_dyn(ui, world, node_buffer.inner.as_mut());

            if inner_edit_response.changed() {
                ctx.editor_actions
                    .push(EditorAction::Graph(GraphAction::EditNode(EditNode {
                        graph: active_node.handle.clone(),
                        node: node.id,
                        new_inner: node_buffer.inner.clone(),
                    })));
                clear = true;
            }

            if clear {
                ctx.buffers.clear::<AnimationNode>(node_buffer_id);
            }

            Some(())
        },
    )
    .flatten()
}

fn state_inspector(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    ui.heading("FSM State");

    let active_state = get_global_state::<ActiveFsmState>(world)?.clone();

    with_assets_all(world, [active_state.handle.id()], |world, [fsm]| {
        let state = fsm.states.get(&active_state.state)?;

        let buffer_id = ui.id().with("state buffer");
        let buffer = ctx.buffers.get_mut_or_insert_with_condition(
            buffer_id,
            |v: &State| v.id != active_state.state,
            || state.clone(),
        );

        let r = ui.add(StateWidget::new_salted(buffer, world, "state widget"));
        if r.changed() {
            let state = buffer.clone();
            ctx.trigger(UpdateState {
                fsm: active_state.handle.clone(),
                state,
            });
        }

        Some(())
    })
    .flatten()
}

fn transition_inspector(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    ui.heading("FSM Transition");

    let active_transition = get_global_state::<ActiveFsmTransition>(world)?.clone();

    with_assets_all(world, [active_transition.handle.id()], |world, [fsm]| {
        let transition = fsm.transitions.get(&active_transition.transition)?;

        let buffer_id = ui.id().with("transition buffer");
        let buffer = ctx.buffers.get_mut_or_insert_with_condition(
            buffer_id,
            |v: &DirectTransition| v.id != transition.id,
            || transition.clone(),
        );

        let r = ui.add(
            DirectTransitionWidget::new(buffer, world)
                .salted("state widget")
                .with_fsm(Some(fsm)),
        );
        if r.changed() {
            let transition = buffer.clone();
            ctx.trigger(UpdateDirectTransition {
                fsm: active_transition.handle.clone(),
                transition,
            });
        }

        Some(())
    })
    .flatten()
}

fn graph_inspector(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    ui.heading("Animation graph");

    select_graph_context(world, ui, ctx);

    let active_graph = get_global_state::<ActiveGraph>(world)?.clone();

    world.resource_scope::<Assets<AnimationGraph>, _>(|_, graph_assets| {
        let graph = graph_assets.get(&active_graph.handle)?;

        let graph_spec_buffer = ctx
            .buffers
            .get_mut_or_insert_with(ui.id().with("graph_spec"), || graph.io_spec.clone());

        let spec_response = IoSpecWidget::new_salted(graph_spec_buffer, "graph_spec_widget")
            .show(ui, |ui, i| {
                GraphInputPinWidget::new_salted(i, "graph input pin edit").ui(ui)
            });

        if spec_response.changed() {
            ctx.editor_actions
                .push(EditorAction::Graph(GraphAction::UpdateGraphSpec(
                    UpdateGraphSpec {
                        graph: active_graph.handle.clone(),
                        new_spec: graph_spec_buffer.clone(),
                    },
                )));
        }

        let default_values_buffer = ctx
            .buffers
            .get_mut_or_insert_with(ui.id().with("graph_default_values"), || {
                graph.default_data.clone()
            });

        ui.heading("Default data");

        let default_values_response =
            HashMapWidget::new_salted(default_values_buffer, "graph_default_values").ui(
                ui,
                |ui, key| {
                    ui.add(GraphInputPinWidget::new_salted(
                        key,
                        "default value graph input pin",
                    ))
                },
                |ui, key| ui.label(format!("{:?}", key)),
                |ui, value| ui.add(DataValueWidget::new_salted(value, "default value widget")),
            );

        if default_values_response.changed() {
            ctx.editor_actions
                .push(EditorAction::Graph(GraphAction::UpdateDefaultData(
                    UpdateDefaultData {
                        graph: active_graph.handle.clone(),
                        input_data: default_values_buffer.clone(),
                    },
                )));
        }

        Some(())
    })
}

fn fsm_inspector(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    ui.heading("State machine");

    select_graph_context_fsm(world, ui, ctx);

    let active_fsm = get_global_state::<ActiveFsm>(world)?.clone();

    world.resource_scope::<Assets<StateMachine>, _>(|_, fsm_assets| {
        // We should make sure to include the fsm id in the buffer id salt to avoid reusing buffers
        // when active FSM changes
        let buffer_id = |ui: &mut egui::Ui, s: &str| ui.id().with(s).with(active_fsm.handle.id());

        let fsm = fsm_assets.get(&active_fsm.handle)?;
        let spec_buffer = ctx
            .buffers
            .get_mut_or_insert_with(buffer_id(ui, "fsm input spec"), || fsm.node_spec.clone());

        let spec_response = IoSpecWidget::new_salted(spec_buffer, "fsm input spec widget")
            .show(ui, |ui, i| ui.text_edit_singleline(i));

        if spec_response.changed() {
            let new = spec_buffer.clone();
            ctx.trigger(SetFsmNodeSpec {
                fsm: active_fsm.handle.clone(),
                new,
            });
        }

        let start_state_buffer = ctx
            .buffers
            .get_mut_or_insert_with(buffer_id(ui, "fsm start state"), || fsm.start_state);

        let r = ui
            .horizontal(|ui| {
                ui.label("start state:");
                ui.add(
                    StateIdWidget::new(start_state_buffer)
                        .salted("fsm start state")
                        .with_fsm(Some(fsm)),
                )
            })
            .inner;

        if r.changed() {
            let new = *start_state_buffer;
            ctx.trigger(SetFsmStartState {
                fsm: active_fsm.handle.clone(),
                new,
            });
        }

        Some(())
    })
}

fn select_graph_context(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    let active_graph = get_global_state::<ActiveGraph>(world)?;
    let active_contexts = get_global_state::<ActiveContexts>(world)?;

    let available_contexts =
        list_graph_contexts(world, |ctx| ctx.get_graph_id() == active_graph.handle.id());

    let mut selected = active_contexts
        .by_asset
        .get(&active_graph.handle.id().untyped())
        .copied();

    let response = egui::ComboBox::from_label("Active context")
        .selected_text(format!("{selected:?}"))
        .show_ui(ui, |ui| {
            ui.selectable_value(
                &mut selected,
                None,
                format!("{:?}", None::<(Entity, GraphContextId)>),
            );
            for (entity, id) in available_contexts {
                ui.selectable_value(
                    &mut selected,
                    Some((entity, id)),
                    format!("{:?} - {:?}", entity, id),
                );
            }
        });

    if let Some((selected_entity, selected_id)) = selected
        && response.response.changed()
    {
        ctx.trigger(SetActiveContext {
            asset_id: active_graph.handle.id().untyped(),
            entity: selected_entity,
            id: selected_id,
        });
    }

    Some(())
}

fn select_graph_context_fsm(
    world: &mut World,
    ui: &mut egui::Ui,
    ctx: &mut EditorWindowContext,
) -> Option<()> {
    let active_fsm = get_global_state::<ActiveFsm>(world)?;
    let active_contexts = get_global_state::<ActiveContexts>(world)?;
    let graph_assets = world.resource::<Assets<AnimationGraph>>();

    let available_contexts = list_graph_contexts(world, |ctx| {
        let graph_id = ctx.get_graph_id();
        let Some(graph) = graph_assets.get(graph_id) else {
            return false;
        };
        graph
            .contains_node_that::<FsmNode>(|n| n.fsm == active_fsm.handle)
            .is_some()
    });

    let mut selected = active_contexts
        .by_asset
        .get(&active_fsm.handle.id().untyped())
        .copied();

    let response = egui::ComboBox::from_label("Active context")
        .selected_text(format!("{selected:?}"))
        .show_ui(ui, |ui| {
            ui.selectable_value(
                &mut selected,
                None,
                format!("{:?}", None::<(Entity, GraphContextId)>),
            );
            for (entity, id) in available_contexts {
                ui.selectable_value(
                    &mut selected,
                    Some((entity, id)),
                    format!("{:?} - {:?}", entity, id),
                );
            }
        });

    if let Some((selected_entity, selected_id)) = selected
        && response.response.changed()
    {
        ctx.trigger(SetActiveContext {
            asset_id: active_fsm.handle.id().untyped(),
            entity: selected_entity,
            id: selected_id,
        });
    }

    Some(())
}

fn list_graph_contexts(
    world: &World,
    filter: impl Fn(&GraphState) -> bool,
) -> Vec<(Entity, GraphContextId)> {
    let players = utils::iter_animation_graph_players(world);
    players
        .iter()
        .filter_map(|(entity, player)| Some((entity, player.get_context_arena()?)))
        .flat_map(|(entity, arena)| {
            arena
                .iter_context_ids()
                .filter(|id| {
                    let context = arena.get_context(*id).unwrap();
                    filter(context)
                })
                .map(|id| (*entity, id))
        })
        .collect()
}