|
| 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 | +} |
0 commit comments