From bb9b37ef174143f20159e28fd2353888c6b3e253 Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Thu, 10 Aug 2023 20:49:47 -0700 Subject: [PATCH] frustrated. starting over --- bin/editor.rs | 7 +- bin/ui-wtf.rs | 152 +++++++++------------------- src/ui.rs | 275 ++++++++++++++++++++++---------------------------- 3 files changed, 172 insertions(+), 262 deletions(-) diff --git a/bin/editor.rs b/bin/editor.rs index 03eae89..42fb70f 100644 --- a/bin/editor.rs +++ b/bin/editor.rs @@ -105,13 +105,12 @@ fn initialize_ui(mut commands: Commands) { Name::new("GLTFs"), NodeBundle { style: Style { - flex_direction: FlexDirection::Row, + flex_direction: FlexDirection::Column, ..default() }, ..default() }, GltfsUi, - UiCollapse::Show, )) .with_children(|parent| { parent.spawn(( @@ -119,21 +118,18 @@ fn initialize_ui(mut commands: Commands) { Name::new("Scenes"), NodeBundle { ..default() }, ScenesUi, - UiCollapse::Show, )); parent.spawn(( GameUiList, Name::new("Cameras"), NodeBundle { ..default() }, CamerasUi, - UiCollapse::Show, )); parent.spawn(( GameUiList, Name::new("Animations"), NodeBundle { ..default() }, AnimationsUi, - UiCollapse::Show, )); }); parent.spawn(( @@ -147,7 +143,6 @@ fn initialize_ui(mut commands: Commands) { ..default() }, AudioClipsUi, - UiCollapse::Show, )); }); } diff --git a/bin/ui-wtf.rs b/bin/ui-wtf.rs index 9c8e36a..30f4298 100644 --- a/bin/ui-wtf.rs +++ b/bin/ui-wtf.rs @@ -3,19 +3,23 @@ use bevy::{ prelude::*, window::PrimaryWindow, }; +use monologue_trees::{debug::*, ui::*}; fn main() { App::new() - .add_plugins(DefaultPlugins.set(WindowPlugin { - primary_window: Some(Window { - title: "UI WTF".into(), - resolution: (640., 480.).into(), + .add_plugins(( + DefaultPlugins.set(WindowPlugin { + primary_window: Some(Window { + title: "UI WTF".into(), + resolution: (640., 480.).into(), + ..default() + }), ..default() }), - ..default() - })) + GameUiPlugin, + )) .init_resource::() - .add_systems(Startup, (init, init_cursors, init_container)) + .add_systems(Startup, init_ui) .add_systems(Update, (cursors, container)) .run(); } @@ -61,70 +65,53 @@ const CURSORS: [CursorIcon; 35] = [ #[derive(Debug, Component, Resource, Default)] struct Icon(CursorIcon); -fn init(mut commands: Commands) { - info!("Spawning camera"); +fn init_ui(mut commands: Commands) { commands.spawn(( Camera2dBundle { ..default() }, UiCameraConfig { show_ui: true }, )); -} -fn init_cursors(mut commands: Commands) { - info!("Spawning Cursor Icons"); commands - .spawn(NodeBundle { - style: Style { - flex_direction: FlexDirection::Column, - ..default() - }, - background_color: BackgroundColor(Color::BLACK), - ..default() - }) + .spawn((GameUiNav, NodeBundle { ..default() })) .with_children(|parent| { - parent.spawn( - TextBundle::from_section("Cursor Icons", TextStyle { ..default() }) - .with_style(Style { - max_width: Val::Percent(100.0), - justify_self: JustifySelf::Stretch, - ..default() - }) - .with_background_color(Color::PINK), - ); + parent + .spawn(( + GameUiTab, + Name::new("Grow/Shrink Container"), + NodeBundle { ..default() }, + )) + .with_children(|parent| { + parent.spawn((Container, GameUiSet, NodeBundle { ..default() })); + }); parent - .spawn(NodeBundle { - style: Style { - align_items: AlignItems::FlexStart, - flex_direction: FlexDirection::Column, - flex_wrap: FlexWrap::Wrap, + .spawn(( + GameUiTab, + Name::new("Cursor Icons"), + NodeBundle { + background_color: BackgroundColor(Color::BLACK), ..default() }, - ..default() - }) + )) .with_children(|parent| { - CURSORS.iter().for_each(|&icon| { - parent - .spawn(( - ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(5.0)), - margin: UiRect::all(Val::Px(5.0)), - border: UiRect::all(Val::Px(2.0)), - ..default() - }, - background_color: BackgroundColor(Color::GRAY), - border_color: BorderColor(Color::WHITE), - ..default() - }, - Icon(icon), - )) - .with_children(|parent| { - parent.spawn(TextBundle::from_section( - format!("{:?}", icon), - TextStyle { ..default() }, + parent + .spawn(( + GameUiTab, + NodeBundle { + background_color: BackgroundColor(Color::BLACK), + ..default() + }, + )) + .with_children(|parent| { + CURSORS.iter().for_each(|&icon| { + parent.spawn(( + GameUiButton, + Name::new(format!("{:?}", icon)), + NodeBundle { ..default() }, + Icon(icon), )); }); - }); + }); }); }); } @@ -154,36 +141,6 @@ fn cursors( #[derive(Debug, Component)] struct Container; -fn init_container(mut commands: Commands) { - commands - .spawn(NodeBundle { - style: Style { - flex_direction: FlexDirection::Column, - ..default() - }, - ..default() - }) - .with_children(|parent| { - parent.spawn( - TextBundle::from_section("Grow/Shrink Container", TextStyle { ..default() }) - .with_background_color(Color::PURPLE), - ); - parent.spawn(( - Container, - NodeBundle { - style: Style { - flex_direction: FlexDirection::Row, - flex_wrap: FlexWrap::Wrap, - max_width: Val::Px(250.0), - ..default() - }, - background_color: BackgroundColor(Color::PINK), - ..default() - }, - )); - }); -} - fn container( mut events: EventReader, mut commands: Commands, @@ -197,22 +154,11 @@ fn container( match (key_code, state) { (Some(KeyCode::Up), ButtonState::Pressed) => { commands.entity(root.single()).with_children(|parent| { - parent - .spawn(ButtonBundle { - style: Style { - padding: UiRect::all(Val::Px(10.0)), - margin: UiRect::all(Val::Px(10.0)), - ..default() - }, - background_color: BackgroundColor(Color::PURPLE), - ..default() - }) - .with_children(|parent| { - parent.spawn(TextBundle::from_section( - "asdfwtf", - TextStyle { ..default() }, - )); - }); + parent.spawn(( + GameUiButton, + Name::new("asdfwtf"), + NodeBundle { ..default() }, + )); }); } (Some(KeyCode::Down), ButtonState::Pressed) => { diff --git a/src/ui.rs b/src/ui.rs index 32b786f..de7df98 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -9,6 +9,8 @@ impl Plugin for GameUiPlugin { app.add_systems( Update, ( + init_ui_nav, + init_ui_tab, // UI lists init_ui_list, // UI Set @@ -16,109 +18,99 @@ impl Plugin for GameUiPlugin { // Buttons init_ui_button, manage_button_interaction, - manage_button_title, - // Collapse systems - init_ui_collapse, - manage_collapse, - toggle_collapse, // Cursor manage_cursor, + // Initialize name labels + init_name, ), ); } } -/// Ui Navigation Element -#[derive(Debug, Component)] -struct UiNav; - -/// Collapsed UI element -#[derive(Debug, Component)] -pub enum UiCollapse { - Show, - Hide, -} - -/// Marker for UiCollapse Button -#[derive(Debug, Component)] -struct UiCollapseButton; - -/// When a UiCollapse entity is created, populate the open/close button -fn init_ui_collapse( - events: Query<(Entity, &UiCollapse), Added>, +fn init_name( + events: Query< + (Entity, &Name), + ( + Added, + Or<( + With, + With, + With, + With, + With, + )>, + ), + >, mut commands: Commands, ) { - events.iter().for_each(|(entity, collapse)| { + events.iter().for_each(|(entity, name)| { commands.entity(entity).with_children(|parent| { - let name = match collapse { - UiCollapse::Show => Name::new("Hide"), - UiCollapse::Hide => Name::new("Show"), - }; - parent.spawn(( - GameUiButton, - name, - UiCollapseButton, - NodeBundle { - style: Style { - top: Val::Px(0.0), - right: Val::Px(0.0), - ..default() - }, + parent.spawn( + TextBundle::from_section(name, TextStyle { ..default() }).with_style(Style { + top: Val::Px(0.0), + left: Val::Px(0.0), ..default() - }, - )); + }), + ); }); }); } -fn manage_collapse( - events: Query<(&UiCollapse, &Children), Changed>, - mut visibility: Query<(&mut Visibility, &mut Style), Without>, -) { - events.iter().for_each(|(collapse, children)| { - children.iter().for_each(|&child| { - if let Ok((mut vis, mut style)) = visibility.get_mut(child) { - match collapse { - UiCollapse::Show => { - *vis = Visibility::Inherited; - style.display = Display::Flex; - } - UiCollapse::Hide => { - *vis = Visibility::Hidden; - style.display = Display::None; - } - }; - } - }); +/// Ui Navigation +#[derive(Debug, Component)] +pub struct GameUiNav; + +fn init_ui_nav(events: Query>, mut commands: Commands) { + events.iter().for_each(|entity| { + let parent = commands + .spawn(NodeBundle { + style: Style { + flex_direction: FlexDirection::Row, + ..default() + }, + background_color: BackgroundColor(Color::PINK), + ..default() + }) + .id(); + + commands + .entity(entity) + .insert(NodeBundle { + style: Style { ..default() }, + ..default() + }) + .set_parent(parent); }); } -fn toggle_collapse( - mut events: Query< - (&Interaction, &Parent, &mut Name), - (Changed, With), - >, - mut collapses: Query<&mut UiCollapse>, -) { - events - .iter_mut() - .for_each(|(interaction, parent, mut name)| match interaction { - Interaction::Pressed => { - if let Ok(mut collapse) = collapses.get_mut(parent.get()) { - match *collapse { - UiCollapse::Show => { - *name = Name::new("Show"); - *collapse = UiCollapse::Hide; - } - UiCollapse::Hide => { - *name = Name::new("Hide"); - *collapse = UiCollapse::Show - } - } - } - } - _ => (), - }); +/// Ui Tab Element +#[derive(Debug, Component)] +pub struct GameUiTab; + +fn init_ui_tab(events: Query>, mut commands: Commands) { + events.iter().for_each(|entity| { + let parent = commands + .spawn(NodeBundle { + style: Style { + flex_direction: FlexDirection::Column, + ..default() + }, + ..default() + }) + .id(); + + commands + .entity(entity) + .insert(NodeBundle { + style: Style { + flex_wrap: FlexWrap::Wrap, + ..default() + }, + background_color: BackgroundColor(Color::TEAL), + ..default() + }) + .set_parent(parent); + }); } /// Describes the state of an element @@ -135,32 +127,21 @@ pub enum UiElementState { pub struct GameUiList; /// Manage UI Lists: lists of UI entities. -fn init_ui_list(events: Query<(Entity, &Name), Added>, mut commands: Commands) { - events.iter().for_each(|(entity, name)| { - commands - .entity(entity) - .insert(NodeBundle { - style: Style { - flex_direction: FlexDirection::Column, - justify_items: JustifyItems::Center, - border: UiRect::all(Val::Px(2.0)), - margin: UiRect::all(Val::Px(2.0)), - padding: UiRect::all(Val::Px(2.0)), - ..default() - }, - background_color: BackgroundColor(Color::RED), - border_color: BorderColor(Color::BLACK), +fn init_ui_list(events: Query>, mut commands: Commands) { + events.iter().for_each(|entity| { + commands.entity(entity).insert(NodeBundle { + style: Style { + flex_direction: FlexDirection::Column, + justify_items: JustifyItems::Center, + border: UiRect::all(Val::Px(2.0)), + margin: UiRect::all(Val::Px(2.0)), + padding: UiRect::all(Val::Px(2.0)), ..default() - }) - .with_children(|parent| { - parent.spawn( - TextBundle::from_section(name, TextStyle { ..default() }).with_style(Style { - top: Val::Px(0.0), - left: Val::Px(0.0), - ..default() - }), - ); - }); + }, + background_color: BackgroundColor(Color::RED), + border_color: BorderColor(Color::BLACK), + ..default() + }); }); } @@ -169,8 +150,22 @@ fn init_ui_list(events: Query<(Entity, &Name), Added>, mut commands: pub struct GameUiSet; /// Manage UI Sets: collections of UI entities. -fn init_ui_set(events: Query<(Entity, &Name), Added>, mut commands: Commands) { - events.iter().for_each(|(entity, name)| { +fn init_ui_set(events: Query>, mut commands: Commands) { + events.iter().for_each(|entity| { + let parent = commands + .spawn(NodeBundle { + style: Style { + flex_direction: FlexDirection::Column, + padding: UiRect::all(Val::Px(5.0)), + margin: UiRect::all(Val::Px(5.0)), + ..default() + }, + background_color: BackgroundColor(Color::BLUE), + border_color: BorderColor(Color::BLACK), + ..default() + }) + .id(); + commands .entity(entity) .insert(NodeBundle { @@ -186,15 +181,7 @@ fn init_ui_set(events: Query<(Entity, &Name), Added>, mut commands: C border_color: BorderColor(Color::BLACK), ..default() }) - .with_children(|parent| { - parent.spawn( - TextBundle::from_section(name, TextStyle { ..default() }).with_style(Style { - top: Val::Px(0.0), - left: Val::Px(0.0), - ..default() - }), - ); - }); + .set_parent(parent); }); } @@ -203,27 +190,22 @@ fn init_ui_set(events: Query<(Entity, &Name), Added>, mut commands: C pub struct GameUiButton; /// Manage UI Buttons. interactive buttons. -fn init_ui_button(events: Query<(Entity, &Name), Added>, mut commands: Commands) { - events.iter().for_each(|(entity, name)| { - commands - .entity(entity) - .insert(( - ButtonBundle { - style: Style { - margin: UiRect::all(Val::Px(2.0)), - padding: UiRect::all(Val::Px(2.0)), - border: UiRect::all(Val::Px(2.0)), - ..default() - }, - background_color: BackgroundColor(Color::GREEN), - border_color: BorderColor(Color::BLACK), +fn init_ui_button(events: Query>, mut commands: Commands) { + events.iter().for_each(|entity| { + commands.entity(entity).insert(( + ButtonBundle { + style: Style { + margin: UiRect::all(Val::Px(2.0)), + padding: UiRect::all(Val::Px(2.0)), + border: UiRect::all(Val::Px(2.0)), ..default() }, - UiElementState::Enabled, - )) - .with_children(|parent| { - parent.spawn(TextBundle::from_section(name, TextStyle::default())); - }); + background_color: BackgroundColor(Color::GREEN), + border_color: BorderColor(Color::BLACK), + ..default() + }, + UiElementState::Enabled, + )); }); } @@ -250,19 +232,6 @@ fn manage_button_interaction( }); } -fn manage_button_title( - events: Query<(&Name, &Children), (Changed, With