diff --git a/bin/editor.rs b/bin/editor.rs index 42fb70f..2173af1 100644 --- a/bin/editor.rs +++ b/bin/editor.rs @@ -22,7 +22,7 @@ use bevy::{ prelude::*, utils::HashSet, }; -use monologue_trees::{debug::*, ui::*}; +use monologue_trees::debug::*; fn main() { App::new() @@ -36,7 +36,6 @@ fn main() { ..default() }), DebugInfoPlugin, - GameUiPlugin, )) .init_resource::() .add_systems(Startup, (initialize_ui,)) @@ -63,7 +62,6 @@ fn main() { export_level, import_level, // Misc/Debug Systems - load_bogus, ), ) .run(); @@ -101,8 +99,8 @@ fn initialize_ui(mut commands: Commands) { .with_children(|parent| { parent .spawn(( - GameUiList, Name::new("GLTFs"), + GltfsUi, NodeBundle { style: Style { flex_direction: FlexDirection::Column, @@ -110,31 +108,19 @@ fn initialize_ui(mut commands: Commands) { }, ..default() }, - GltfsUi, )) .with_children(|parent| { + parent.spawn((Name::new("Scenes"), ScenesUi, NodeBundle { ..default() })); + parent.spawn((Name::new("Cameras"), CamerasUi, NodeBundle { ..default() })); parent.spawn(( - GameUiList, - Name::new("Scenes"), - NodeBundle { ..default() }, - ScenesUi, - )); - parent.spawn(( - GameUiList, - Name::new("Cameras"), - NodeBundle { ..default() }, - CamerasUi, - )); - parent.spawn(( - GameUiList, Name::new("Animations"), - NodeBundle { ..default() }, AnimationsUi, + NodeBundle { ..default() }, )); }); parent.spawn(( - GameUiSet, Name::new("Audio Clips"), + AudioClipsUi, NodeBundle { style: Style { flex_direction: FlexDirection::Column, @@ -142,30 +128,10 @@ fn initialize_ui(mut commands: Commands) { }, ..default() }, - AudioClipsUi, )); }); } -fn load_bogus( - mut events: EventReader, - root: Query, Without>>)>, - mut commands: Commands, -) { - events - .iter() - .filter( - |&KeyboardInput { - key_code, state, .. - }| *key_code == Some(KeyCode::Space) && *state == ButtonState::Pressed, - ) - .for_each(|_| { - commands - .spawn((GameUiButton, Name::new("bogus"), NodeBundle { ..default() })) - .set_parent(root.single()); - }) -} - #[derive(Resource, Default, Debug)] struct AssetRegistry(HashSet); @@ -233,10 +199,9 @@ fn manage_gltf_ui( }); commands .spawn(( - GameUiButton, Name::new(name), - NodeBundle { ..default() }, GltfsUi, + ButtonBundle { ..default() }, UiRef::Handle(handle.clone()), )) .set_parent(root.single()); @@ -283,10 +248,9 @@ fn manage_scene_ui( .for_each(|(handle, name)| { commands .spawn(( - GameUiButton, Name::new(name), - NodeBundle { ..default() }, ScenesUi, + ButtonBundle { ..default() }, UiRef::Handle(handle.clone()), )) .set_parent(root.single()); @@ -336,10 +300,9 @@ fn manage_animation_ui( .for_each(|(handle, name)| { commands .spawn(( - GameUiButton, Name::new(name), - NodeBundle { ..default() }, AnimationsUi, + ButtonBundle { ..default() }, UiRef::Handle(handle.clone()), )) .set_parent(root.single()); @@ -390,9 +353,8 @@ fn manage_audio_ui( .for_each(|(handle, name)| { commands .spawn(( - GameUiButton, Name::new(name), - NodeBundle { ..default() }, + ButtonBundle { ..default() }, AudioClipsUi, AudioBundle { source: handle.clone(), @@ -409,21 +371,13 @@ fn manage_audio_ui( /// Play/Loop Audio fn play_audio( - mut events: Query< - (&Interaction, &AudioSink, &mut UiElementState), - (Changed, With), - >, + mut events: Query<(&Interaction, &AudioSink), (Changed, With)>, ) { events .iter_mut() - .for_each(|(interaction, sink, mut state)| match interaction { + .for_each(|(interaction, sink)| match interaction { Interaction::Pressed => { sink.toggle(); - - *state = match *state { - UiElementState::Enabled => UiElementState::Active, - _ => UiElementState::Enabled, - } } _ => (), }); diff --git a/bin/ui-wtf.rs b/bin/ui-wtf.rs index a60af75..7202fbd 100644 --- a/bin/ui-wtf.rs +++ b/bin/ui-wtf.rs @@ -68,140 +68,27 @@ fn spawn_tree(parent: &mut ChildBuilder, pos: UiKitPosition, depth: u8, length: }; (0..length).for_each(|_| { parent - .spawn(UiKitButton::new(Color::PINK)) + .spawn(( + UiKitButton { color: Color::PINK }, + UiKitSelect::default(), + UiKitLabel { + name: "Button".into(), + }, + )) .with_children(|parent| { if depth > 1 { parent - .spawn(UiKitContainer::new(pos)) + .spawn(( + UiKitContainer { position: pos }, + UiKitSelect::default(), + UiKitLabel { name: "Tab".into() }, + )) .with_children(|parent| { spawn_tree(parent, pos2, depth - 1, length); }); } }); }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos)) - // .with_children(|parent| { - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos)) - // .with_children(|parent| { - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos)) - // .with_children(|parent| { - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // parent - // .spawn(UiKitButton::new(Color::PINK)) - // .with_children(|parent| { - // parent - // .spawn(UiKitContainer::new(pos2)) - // .with_children(|parent| { - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // parent.spawn(UiKitButton::new(Color::PINK)); - // }); - // }); - // }); - // }); } fn init_ui2(mut commands: Commands) { @@ -226,8 +113,8 @@ fn init_ui2(mut commands: Commands) { .spawn(ButtonBundle { style: Style { border: UiRect::all(Val::Px(1.0)), - width: Val::Px(200.0), - height: Val::Px(150.0), + // width: Val::Px(200.0), + // height: Val::Px(150.0), align_self: AlignSelf::Center, ..default() }, @@ -254,7 +141,7 @@ fn init_ui2(mut commands: Commands) { ..default() }) .with_children(|parent| { - spawn_tree(parent, UiKitPosition::Left, 3, 7); + spawn_tree(parent, UiKitPosition::Left, 3, 4); }); parent @@ -289,7 +176,7 @@ fn init_ui2(mut commands: Commands) { ..default() }) .with_children(|parent| { - spawn_tree(parent, UiKitPosition::Top, 3, 7); + spawn_tree(parent, UiKitPosition::Top, 3, 3); }); }); } diff --git a/src/ui.rs b/src/ui.rs index 77c2a3b..bae5b07 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -22,156 +22,349 @@ pub struct GameUiPlugin; impl Plugin for GameUiPlugin { fn build(&self, app: &mut App) { app.add_systems(Update, (select_tab, select_textbox, text_editor, scroll)) - .add_systems(PostUpdate, selection); + .add_systems( + PreUpdate, + ( + spawn_container, + spawn_button, + /* manage_labels */ selection, + ), + ); } } -#[derive(Debug, Bundle)] -pub struct UiKitContainer { - node_bundle: NodeBundle, - select: UiKitSelect, -} +pub use self::container::*; +mod container { + use super::*; -#[derive(Copy, Clone)] -pub enum UiKitPosition { - Top, - Left, - Right, -} + #[derive(Debug, Component)] + pub struct UiKitContainer { + pub position: UiKitPosition, + } -impl UiKitContainer { - pub fn new(position: UiKitPosition) -> Self { - let style = match position { - UiKitPosition::Top => Style { - border: UiRect::all(Val::Px(1.0)), - top: Val::Percent(102.0), - left: Val::Px(-2.0), - flex_direction: FlexDirection::Column, - align_items: AlignItems::FlexStart, - display: Display::None, - ..default() - }, - UiKitPosition::Left => Style { - border: UiRect::all(Val::Px(1.0)), - left: Val::Percent(100.0), - top: Val::Px(-2.0), - flex_direction: FlexDirection::Column, - justify_items: JustifyItems::Start, - display: Display::None, - ..default() - }, - UiKitPosition::Right => Style { + #[derive(Debug, Copy, Clone)] + pub enum UiKitPosition { + Top, + Left, + Right, + } + + pub fn spawn_container( + events: Query<(Entity, &UiKitContainer, Option<&UiKitLabel>), Added>, + mut commands: Commands, + ) { + events.iter().for_each(|(entity, container, label)| { + let base_style = Style { border: UiRect::all(Val::Px(1.0)), - right: Val::Percent(104.0), - top: Val::Px(-2.0), flex_direction: FlexDirection::Column, - justify_items: JustifyItems::Start, display: Display::None, ..default() - }, - }; - UiKitContainer { - node_bundle: NodeBundle { - style, - background_color: BackgroundColor(Color::PURPLE), - border_color: BorderColor(Color::BLACK), - ..default() - }, - select: UiKitSelect::None, - } + }; + let style = match container.position { + UiKitPosition::Top => Style { + top: Val::Percent(100.0), + align_items: AlignItems::Start, + ..base_style + }, + UiKitPosition::Left => Style { + left: Val::Percent(100.0), + justify_items: JustifyItems::Start, + ..base_style + }, + UiKitPosition::Right => Style { + right: Val::Percent(104.0), + justify_items: JustifyItems::Start, + ..base_style + }, + }; + commands + .entity(entity) + .insert(NodeBundle { + style, + background_color: BackgroundColor(Color::PURPLE), + border_color: BorderColor(Color::BLACK), + ..default() + }) + .with_children(|parent| { + if let Some(label) = label { + parent.spawn( + TextBundle::from_section( + label.name.clone(), + TextStyle { + color: Color::BLACK, + ..default() + }, + ) + .with_style(Style { + align_self: AlignSelf::Center, + justify_self: JustifySelf::Center, + ..default() + }), + ); + } + }); + }) } } -#[derive(Debug, Bundle)] -pub struct UiKitButton { - button_bundle: ButtonBundle, - select: UiKitSelect, +pub use self::label::*; +mod label { + use super::*; + + #[derive(Debug, Component)] + pub struct UiKitLabel { + pub name: String, + } + + // TODO: Handle modified labels + pub fn manage_labels( + events: Query<(Entity, &UiKitLabel), Added>, + mut commands: Commands, + ) { + events.iter().for_each(|(entity, label)| { + commands.entity(entity).with_children(|parent| { + parent.spawn( + TextBundle::from_section( + label.name.clone(), + TextStyle { + color: Color::BLACK, + ..default() + }, + ) + .with_style(Style { + align_self: AlignSelf::Center, + justify_self: JustifySelf::Center, + ..default() + }), + ); + }); + }); + } } -impl UiKitButton { - pub fn new(color: Color) -> Self { - UiKitButton { - button_bundle: ButtonBundle { - style: Style { - border: UiRect::all(Val::Px(1.0)), - width: Val::Px(100.0), - height: Val::Px(50.0), - flex_direction: FlexDirection::Column, +pub use self::button::*; +mod button { + use super::*; + + #[derive(Debug, Component)] + pub struct UiKitButton { + pub color: Color, + } + + pub fn spawn_button( + events: Query<(Entity, &UiKitButton, Option<&UiKitLabel>), Added>, + mut commands: Commands, + ) { + events.iter().for_each(|(entity, button, label)| { + commands + .entity(entity) + .insert(ButtonBundle { + style: Style { + border: UiRect::all(Val::Px(1.0)), + // width: Val::Px(100.0), + // height: Val::Px(50.0), + flex_direction: FlexDirection::Column, + ..default() + }, + background_color: BackgroundColor(button.color), + border_color: BorderColor(Color::BLACK), ..default() - }, - background_color: BackgroundColor(color), - border_color: BorderColor(Color::BLACK), - ..default() - }, - select: UiKitSelect::None, - } + }) + .with_children(|parent| { + if let Some(label) = label { + parent.spawn( + TextBundle::from_section( + label.name.clone(), + TextStyle { + color: Color::BLACK, + ..default() + }, + ) + .with_style(Style { + align_self: AlignSelf::Center, + justify_self: JustifySelf::Center, + ..default() + }), + ); + } + }); + }); } } -#[derive(Debug, Component, Copy, Clone, PartialEq)] -pub enum UiKitSelect { - Active, - None, -} +pub use self::select::*; +mod select { + use super::*; -#[derive(Debug, Bundle)] -pub struct UiKitTextInput { - text_bundle: TextBundle, - interaction: Interaction, - select: UiKitSelect, + #[derive(Debug, Component, Copy, Clone, PartialEq)] + pub enum UiKitSelect { + Active, + None, + } + + impl Default for UiKitSelect { + fn default() -> Self { + UiKitSelect::None + } + } + + /// When an item is selected/de-selected change it's display accordingly + pub fn selection( + mut events: Query< + (&mut BackgroundColor, &UiKitSelect, &Children), + (Changed, With