Skip to content

Commit 6cddee8

Browse files
committed
move graph creations to assets menu
1 parent 8c5078a commit 6cddee8

7 files changed

Lines changed: 127 additions & 32 deletions

File tree

crates/bevy_animation_graph_editor/src/ui/actions/graph.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ use bevy_animation_graph::core::{
1818
state_machine::high_level::StateMachine,
1919
};
2020

21-
use super::{DynamicAction, run_handler, saving::DirtyAssets};
22-
use crate::{
23-
graph_show::{GraphIndicesMap, make_graph_indices},
24-
ui::actions::ActionContext,
25-
};
21+
use super::saving::DirtyAssets;
22+
use crate::graph_show::{GraphIndicesMap, make_graph_indices};
2623

2724
pub enum GraphAction {
2825
CreateLink(CreateLink),
@@ -358,22 +355,3 @@ impl GraphAndContext<'_> {
358355
}
359356
}
360357
}
361-
362-
pub struct CreateGraphAction;
363-
364-
impl DynamicAction for CreateGraphAction {
365-
fn handle(self: Box<Self>, world: &mut World, _: &mut ActionContext) {
366-
run_handler(world, "Could not create clip preview")(
367-
|In(_),
368-
mut graph_assets: ResMut<Assets<AnimationGraph>>,
369-
mut dirty_assets: ResMut<DirtyAssets>| {
370-
let new_handle = graph_assets.add(AnimationGraph::default());
371-
info!("Creating graph with id: {:?}", new_handle.id());
372-
dirty_assets
373-
.assets
374-
.insert(new_handle.id().untyped(), new_handle.untyped());
375-
},
376-
*self,
377-
)
378-
}
379-
}

crates/bevy_animation_graph_editor/src/ui/core.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::ui::{
2121
native_views::{EditorView, EditorViewContext, EditorViewUiState},
2222
native_windows::{NativeEditorWindow, NativeEditorWindowExtension},
2323
state_management::global::{
24-
GlobalState, clip::RequestCreateClip, fsm::RequestCreateFsm, ragdoll::RequestCreateRagdoll,
24+
GlobalState, animation_graph::RequestCreateAnimationGraph, clip::RequestCreateClip,
25+
fsm::RequestCreateFsm, ragdoll::RequestCreateRagdoll,
2526
ragdoll_bone_map::RequestCreateRagdollBoneMap, skeleton::RequestCreateSkeleton,
2627
},
2728
};
@@ -152,6 +153,9 @@ fn menu_bar(ctx: &mut egui::Context, command_queue: &mut CommandQueue) {
152153
if ui.button("Animation").clicked() {
153154
command_queue.push(trigger(RequestCreateClip));
154155
}
156+
if ui.button("Animation graph").clicked() {
157+
command_queue.push(trigger(RequestCreateAnimationGraph));
158+
}
155159
if ui.button("State machine").clicked() {
156160
command_queue.push(trigger(RequestCreateFsm));
157161
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use bevy_animation_graph::core::animation_graph::AnimationGraph;
2+
3+
use crate::ui::{
4+
native_windows::asset_creation::generic_with_path::CreateAssetFromPath,
5+
state_management::global::animation_graph::CreateAnimationGraph,
6+
};
7+
8+
#[derive(Debug, Clone, Copy)]
9+
pub struct CreateAnimationGraphWindow;
10+
11+
impl CreateAssetFromPath for CreateAnimationGraphWindow {
12+
const NAME: &'static str = "Create animation graph";
13+
const EXTENSION: &'static str = ".animgraph.ron";
14+
15+
fn create(&self, path: String, queue: &mut crate::ui::native_windows::OwnedQueue) {
16+
queue.trigger(CreateAnimationGraph {
17+
animation_graph: AnimationGraph::default(),
18+
virtual_path: path.to_string(),
19+
});
20+
}
21+
}

crates/bevy_animation_graph_editor/src/ui/native_windows/asset_creation/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod anim;
2+
pub mod animation_graph;
23
pub mod fsm;
34
pub mod generic_with_path;
45
pub mod ragdoll;

crates/bevy_animation_graph_editor/src/ui/native_windows/graph_picker.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use bevy::{asset::Handle, prelude::World};
22
use egui_dock::egui;
33

44
use crate::ui::{
5-
actions::graph::CreateGraphAction,
65
generic_widgets::asset_picker::AssetPicker,
76
native_windows::{EditorWindowContext, NativeEditorWindowExtension},
87
state_management::global::{
@@ -35,10 +34,6 @@ impl NativeEditorWindowExtension for GraphPickerWindow {
3534
selection: InspectorSelection::ActiveGraph,
3635
})
3736
}
38-
39-
if ui.button("New Graph").clicked() {
40-
ctx.editor_actions.dynamic(CreateGraphAction);
41-
}
4237
}
4338

4439
fn display_name(&self) -> String {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
use bevy::ecs::{
2+
component::Component,
3+
entity::Entity,
4+
event::Event,
5+
observer::On,
6+
reflect::AppTypeRegistry,
7+
system::{Commands, Res, ResMut},
8+
world::World,
9+
};
10+
use bevy_animation_graph::core::animation_graph::{
11+
AnimationGraph, serial::AnimationGraphSerializer,
12+
};
13+
14+
use crate::{
15+
Cli,
16+
scanner::RescanAssets,
17+
ui::{
18+
UiState,
19+
core::EguiWindow,
20+
native_windows::{
21+
NativeEditorWindow, asset_creation::animation_graph::CreateAnimationGraphWindow,
22+
},
23+
state_management::global::RegisterStateComponent,
24+
},
25+
};
26+
27+
#[derive(Debug, Component, Default, Clone)]
28+
pub struct AnimationGraphManager;
29+
30+
impl RegisterStateComponent for AnimationGraphManager {
31+
fn register(world: &mut World, _global_state_entity: Entity) {
32+
world.add_observer(RequestCreateAnimationGraph::observe);
33+
world.add_observer(CreateAnimationGraph::observe);
34+
}
35+
}
36+
37+
/// Will open a "create FSM" window popup
38+
#[derive(Event)]
39+
pub struct RequestCreateAnimationGraph;
40+
41+
impl RequestCreateAnimationGraph {
42+
pub fn observe(
43+
_: On<RequestCreateAnimationGraph>,
44+
mut commands: Commands,
45+
ui_state: ResMut<UiState>,
46+
) {
47+
if let Some(active_view_idx) = ui_state.active_view {
48+
let view = &ui_state.views[active_view_idx];
49+
let win = NativeEditorWindow::create_cmd(
50+
&mut commands,
51+
view.entity,
52+
CreateAnimationGraphWindow,
53+
);
54+
55+
let UiState { views, .. } = ui_state.into_inner();
56+
57+
views[active_view_idx]
58+
.dock_state
59+
.add_window(vec![EguiWindow::EntityWindow(win)]);
60+
}
61+
}
62+
}
63+
64+
#[derive(Event)]
65+
pub struct CreateAnimationGraph {
66+
pub virtual_path: String,
67+
pub animation_graph: AnimationGraph,
68+
}
69+
70+
impl CreateAnimationGraph {
71+
pub fn observe(
72+
event: On<CreateAnimationGraph>,
73+
cli: Res<Cli>,
74+
registry: Res<AppTypeRegistry>,
75+
mut commands: Commands,
76+
) {
77+
let type_registry = registry.0.read();
78+
let graph_serial = AnimationGraphSerializer::new(&event.animation_graph, &type_registry);
79+
80+
let mut final_path = cli.asset_source.clone();
81+
final_path.push(&event.virtual_path);
82+
bevy::log::info!("Creating animation graph metadata at {:?}", final_path);
83+
ron::Options::default()
84+
.to_io_writer_pretty(
85+
std::fs::File::create(final_path).unwrap(),
86+
&graph_serial,
87+
ron::ser::PrettyConfig::default(),
88+
)
89+
.unwrap();
90+
91+
commands.trigger(RescanAssets);
92+
}
93+
}

crates/bevy_animation_graph_editor/src/ui/state_management/global/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ pub mod active_graph_node;
77
pub mod active_ragdoll;
88
pub mod active_scene;
99
pub mod active_skeleton;
10+
pub mod animation_graph;
1011
pub mod clip;
1112
pub mod fsm;
1213
pub mod inspector_selection;
@@ -32,8 +33,9 @@ use crate::ui::state_management::global::{
3233
active_fsm_transition::ActiveFsmTransition, active_graph::ActiveGraph,
3334
active_graph_context::ActiveContexts, active_graph_node::ActiveGraphNode,
3435
active_ragdoll::ActiveRagdoll, active_scene::ActiveScene, active_skeleton::ActiveSkeleton,
35-
clip::ClipManager, fsm::FsmManager, inspector_selection::InspectorSelection,
36-
ragdoll::RagdollManager, ragdoll_bone_map::RagdollBoneMapManager, skeleton::SkeletonManager,
36+
animation_graph::AnimationGraphManager, clip::ClipManager, fsm::FsmManager,
37+
inspector_selection::InspectorSelection, ragdoll::RagdollManager,
38+
ragdoll_bone_map::RagdollBoneMapManager, skeleton::SkeletonManager,
3739
};
3840

3941
#[derive(Component)]
@@ -58,6 +60,7 @@ impl GlobalState {
5860
RagdollManager::register(world, entity);
5961
RagdollBoneMapManager::register(world, entity);
6062
SkeletonManager::register(world, entity);
63+
AnimationGraphManager::register(world, entity);
6164

6265
world.add_observer(CloseWindow::observe);
6366
}

0 commit comments

Comments
 (0)