diff --git a/bin/editor.rs b/bin/editor.rs index 91488fd..bc6879f 100644 --- a/bin/editor.rs +++ b/bin/editor.rs @@ -2,6 +2,13 @@ // // Editor for creating Monologue Trees levels // +// REFACTOR: +// * Monologue -> Event -> Active -> UI +// * Scene -> Event -> Active -> UI +// * Animation -> Event -> Active -> UI +// * Font -> Event -> Active -> UI +// * Gltf -> Event -> Active -> UI +// // BUGS: // * When Handle is loaded, the button for TargetAsset should load as well // * Exported level should preserve active camera @@ -18,12 +25,15 @@ use bevy::{ asset::{Asset, AssetLoader, Assets, ChangeWatcher, LoadContext, LoadedAsset}, - audio::PlaybackMode, gltf::Gltf, prelude::*, utils::{BoxedFuture, Duration}, }; -use monologue_trees::{debug::*, ui}; +use monologue_trees::{ + debug::*, + editor::{assets::*, audio::*, *}, + ui, +}; const WELCOME_MESSAGES: &'static [&'static str] = &[ "Welcome to the Monologue Trees editor!", @@ -82,7 +92,16 @@ fn main() { (remove_scenes_ui, add_scenes_ui, control_active_scenes), ) .add_systems(Update, (cameras_ui, manage_active_camera, fallback_camera)) - .add_systems(Update, (audio_ui, play_audio, pause_audio, control_audio)) + .add_systems( + Update, + ( + audio_ui, + ui_control_audio, + ui_active::, + ui_inactive::, + control_audio, + ), + ) .add_systems( Update, ( @@ -167,9 +186,6 @@ fn main() { .run(); } -#[derive(Resource, Default)] -pub struct AssetRegistry(Vec); - #[derive(Debug, Component)] pub struct TabRoot; @@ -177,10 +193,6 @@ pub struct TabRoot; #[reflect(Component)] pub struct LevelRoot; -#[derive(Debug, Component, Reflect, Default)] -#[reflect(Component)] -pub struct AudioRoot; - #[derive(Debug, Component)] pub struct EditorCamera; @@ -271,46 +283,22 @@ fn initialize_ui(mut commands: Commands) { ui::Sorting(2), )) .with_children(|parent| { - content_containers.push(spawn_tab_container::( - "Font", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Monologue", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Audio", - parent, - ui::Select::Multi, - )); - content_containers.push(spawn_tab_container::( - "Level", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Gltf", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Scene", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Camera", - parent, - ui::Select::Single, - )); - content_containers.push(spawn_tab_container::( - "Animation", - parent, - ui::Select::Multi, - )); + content_containers + .push(spawn_tab_container::("Font", parent)); + content_containers + .push(spawn_tab_container::("Monologue", parent)); + content_containers + .push(spawn_tab_container::("Audio", parent)); + content_containers + .push(spawn_tab_container::("Level", parent)); + content_containers + .push(spawn_tab_container::("Gltf", parent)); + content_containers + .push(spawn_tab_container::("Scene", parent)); + content_containers + .push(spawn_tab_container::("Camera", parent)); + content_containers + .push(spawn_tab_container::("Animation", parent)); }); // Container for tabs that open/close containers @@ -548,7 +536,6 @@ fn welcome_message(mut writer: EventWriter) { fn spawn_tab_container( title: &'static str, parent: &mut ChildBuilder, - select: ui::Select, ) -> (String, Entity) { ( title.into(), @@ -571,277 +558,11 @@ fn spawn_tab_container( T::default(), ui::Scroll, Interaction::default(), - select, )) .id(), ) } -use audio::*; -mod audio { - - use super::*; - - #[derive(Debug, Component, Default)] - pub struct AudioWidget; - - pub fn audio_ui( - mut events: EventReader>, - mut commands: Commands, - widget: Query>, - current: Query<(Entity, &ui::TargetAsset)>, - server: Res, - ) { - events.iter().for_each(|event| match event { - AssetEvent::Created { handle } => { - info!("Asset created! {:?}", event); - create_asset_button( - &widget, - &mut commands, - ui::TargetAsset { - handle: handle.clone(), - }, - get_asset_name(&server, handle.clone()), - None, - ); - } - AssetEvent::Removed { handle } => { - info!("Asset removed! {:?}", event); - destroy_asset_button( - ¤t, - &mut commands, - &ui::TargetAsset { - handle: handle.clone(), - }, - ); - } - AssetEvent::Modified { handle } => { - info!("Asset modified! {:?}", event); - destroy_asset_button( - ¤t, - &mut commands, - &ui::TargetAsset { - handle: handle.clone(), - }, - ); - create_asset_button( - &widget, - &mut commands, - ui::TargetAsset { - handle: handle.clone(), - }, - get_asset_name(&server, handle.clone()), - None, - ); - } - }); - } - - #[derive(Debug, Event)] - pub enum ControlAudio { - Play(Handle), - Loop(Handle), - Stop(Handle), - } - - pub fn control_audio( - mut events: EventReader, - root: Query>, - mut commands: Commands, - sources: Query<(Entity, &Handle), With>, - ) { - events.iter().for_each(|event| match event { - ControlAudio::Play(handle) => (), - ControlAudio::Loop(handle) => { - info!("Looping audio {:?}", handle); - let root = if let Ok(entity) = root.get_single() { - entity - } else { - commands.spawn(AudioRoot).id() - }; - commands.entity(root).with_children(|parent| { - parent.spawn(AudioSourceBundle { - source: handle.clone(), - settings: PlaybackSettings { - mode: PlaybackMode::Loop, - paused: false, - ..default() - }, - }); - info!("Done spawning"); - }); - } - ControlAudio::Stop(handle) => { - info!("Stopping audio {:?}", handle); - sources - .iter() - .find_map(|(entity, source_handle)| { - if source_handle == handle { - Some(entity) - } else { - None - } - }) - .iter() - .for_each(|&entity| { - commands.entity(entity).despawn_recursive(); - info!("Done despawning"); - }); - } - }); - } - - pub fn play_audio( - events: Query<&ui::TargetAsset, (With