frustrated. starting over

main
Elijah Voigt 2 years ago
parent f47d32b54d
commit bb9b37ef17

@ -105,13 +105,12 @@ fn initialize_ui(mut commands: Commands) {
Name::new("GLTFs"), Name::new("GLTFs"),
NodeBundle { NodeBundle {
style: Style { style: Style {
flex_direction: FlexDirection::Row, flex_direction: FlexDirection::Column,
..default() ..default()
}, },
..default() ..default()
}, },
GltfsUi, GltfsUi,
UiCollapse::Show,
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(( parent.spawn((
@ -119,21 +118,18 @@ fn initialize_ui(mut commands: Commands) {
Name::new("Scenes"), Name::new("Scenes"),
NodeBundle { ..default() }, NodeBundle { ..default() },
ScenesUi, ScenesUi,
UiCollapse::Show,
)); ));
parent.spawn(( parent.spawn((
GameUiList, GameUiList,
Name::new("Cameras"), Name::new("Cameras"),
NodeBundle { ..default() }, NodeBundle { ..default() },
CamerasUi, CamerasUi,
UiCollapse::Show,
)); ));
parent.spawn(( parent.spawn((
GameUiList, GameUiList,
Name::new("Animations"), Name::new("Animations"),
NodeBundle { ..default() }, NodeBundle { ..default() },
AnimationsUi, AnimationsUi,
UiCollapse::Show,
)); ));
}); });
parent.spawn(( parent.spawn((
@ -147,7 +143,6 @@ fn initialize_ui(mut commands: Commands) {
..default() ..default()
}, },
AudioClipsUi, AudioClipsUi,
UiCollapse::Show,
)); ));
}); });
} }

@ -3,19 +3,23 @@ use bevy::{
prelude::*, prelude::*,
window::PrimaryWindow, window::PrimaryWindow,
}; };
use monologue_trees::{debug::*, ui::*};
fn main() { fn main() {
App::new() App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin { .add_plugins((
DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window { primary_window: Some(Window {
title: "UI WTF".into(), title: "UI WTF".into(),
resolution: (640., 480.).into(), resolution: (640., 480.).into(),
..default() ..default()
}), }),
..default() ..default()
})) }),
GameUiPlugin,
))
.init_resource::<Icon>() .init_resource::<Icon>()
.add_systems(Startup, (init, init_cursors, init_container)) .add_systems(Startup, init_ui)
.add_systems(Update, (cursors, container)) .add_systems(Update, (cursors, container))
.run(); .run();
} }
@ -61,67 +65,50 @@ const CURSORS: [CursorIcon; 35] = [
#[derive(Debug, Component, Resource, Default)] #[derive(Debug, Component, Resource, Default)]
struct Icon(CursorIcon); struct Icon(CursorIcon);
fn init(mut commands: Commands) { fn init_ui(mut commands: Commands) {
info!("Spawning camera");
commands.spawn(( commands.spawn((
Camera2dBundle { ..default() }, Camera2dBundle { ..default() },
UiCameraConfig { show_ui: true }, UiCameraConfig { show_ui: true },
)); ));
}
fn init_cursors(mut commands: Commands) {
info!("Spawning Cursor Icons");
commands commands
.spawn(NodeBundle { .spawn((GameUiNav, NodeBundle { ..default() }))
style: Style {
flex_direction: FlexDirection::Column,
..default()
},
background_color: BackgroundColor(Color::BLACK),
..default()
})
.with_children(|parent| { .with_children(|parent| {
parent.spawn( parent
TextBundle::from_section("Cursor Icons", TextStyle { ..default() }) .spawn((
.with_style(Style { GameUiTab,
max_width: Val::Percent(100.0), Name::new("Grow/Shrink Container"),
justify_self: JustifySelf::Stretch, NodeBundle { ..default() },
..default() ))
}) .with_children(|parent| {
.with_background_color(Color::PINK), parent.spawn((Container, GameUiSet, NodeBundle { ..default() }));
); });
parent parent
.spawn(NodeBundle { .spawn((
style: Style { GameUiTab,
align_items: AlignItems::FlexStart, Name::new("Cursor Icons"),
flex_direction: FlexDirection::Column, NodeBundle {
flex_wrap: FlexWrap::Wrap, background_color: BackgroundColor(Color::BLACK),
..default() ..default()
}, },
..default() ))
})
.with_children(|parent| { .with_children(|parent| {
CURSORS.iter().for_each(|&icon| {
parent parent
.spawn(( .spawn((
ButtonBundle { GameUiTab,
style: Style { NodeBundle {
padding: UiRect::all(Val::Px(5.0)), background_color: BackgroundColor(Color::BLACK),
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() ..default()
}, },
Icon(icon),
)) ))
.with_children(|parent| { .with_children(|parent| {
parent.spawn(TextBundle::from_section( CURSORS.iter().for_each(|&icon| {
format!("{:?}", icon), parent.spawn((
TextStyle { ..default() }, GameUiButton,
Name::new(format!("{:?}", icon)),
NodeBundle { ..default() },
Icon(icon),
)); ));
}); });
}); });
@ -154,36 +141,6 @@ fn cursors(
#[derive(Debug, Component)] #[derive(Debug, Component)]
struct Container; 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( fn container(
mut events: EventReader<KeyboardInput>, mut events: EventReader<KeyboardInput>,
mut commands: Commands, mut commands: Commands,
@ -197,23 +154,12 @@ fn container(
match (key_code, state) { match (key_code, state) {
(Some(KeyCode::Up), ButtonState::Pressed) => { (Some(KeyCode::Up), ButtonState::Pressed) => {
commands.entity(root.single()).with_children(|parent| { commands.entity(root.single()).with_children(|parent| {
parent parent.spawn((
.spawn(ButtonBundle { GameUiButton,
style: Style { Name::new("asdfwtf"),
padding: UiRect::all(Val::Px(10.0)), NodeBundle { ..default() },
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() },
)); ));
}); });
});
} }
(Some(KeyCode::Down), ButtonState::Pressed) => { (Some(KeyCode::Down), ButtonState::Pressed) => {
children.single().iter().last().iter().for_each(|&&child| { children.single().iter().last().iter().for_each(|&&child| {

@ -9,6 +9,8 @@ impl Plugin for GameUiPlugin {
app.add_systems( app.add_systems(
Update, Update,
( (
init_ui_nav,
init_ui_tab,
// UI lists // UI lists
init_ui_list, init_ui_list,
// UI Set // UI Set
@ -16,108 +18,98 @@ impl Plugin for GameUiPlugin {
// Buttons // Buttons
init_ui_button, init_ui_button,
manage_button_interaction, manage_button_interaction,
manage_button_title,
// Collapse systems
init_ui_collapse,
manage_collapse,
toggle_collapse,
// Cursor // Cursor
manage_cursor, manage_cursor,
// Initialize name labels
init_name,
), ),
); );
} }
} }
/// Ui Navigation Element fn init_name(
#[derive(Debug, Component)] events: Query<
struct UiNav; (Entity, &Name),
(
/// Collapsed UI element Added<Name>,
#[derive(Debug, Component)] Or<(
pub enum UiCollapse { With<GameUiNav>,
Show, With<GameUiTab>,
Hide, With<GameUiSet>,
} With<GameUiList>,
With<GameUiButton>,
/// 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<UiCollapse>>,
mut commands: Commands, mut commands: Commands,
) { ) {
events.iter().for_each(|(entity, collapse)| { events.iter().for_each(|(entity, name)| {
commands.entity(entity).with_children(|parent| { commands.entity(entity).with_children(|parent| {
let name = match collapse { parent.spawn(
UiCollapse::Show => Name::new("Hide"), TextBundle::from_section(name, TextStyle { ..default() }).with_style(Style {
UiCollapse::Hide => Name::new("Show"),
};
parent.spawn((
GameUiButton,
name,
UiCollapseButton,
NodeBundle {
style: Style {
top: Val::Px(0.0), top: Val::Px(0.0),
right: Val::Px(0.0), left: Val::Px(0.0),
..default()
},
..default() ..default()
}, }),
)); );
}); });
}); });
} }
fn manage_collapse( /// Ui Navigation
events: Query<(&UiCollapse, &Children), Changed<UiCollapse>>, #[derive(Debug, Component)]
mut visibility: Query<(&mut Visibility, &mut Style), Without<UiCollapseButton>>, pub struct GameUiNav;
) {
events.iter().for_each(|(collapse, children)| { fn init_ui_nav(events: Query<Entity, Added<GameUiNav>>, mut commands: Commands) {
children.iter().for_each(|&child| { events.iter().for_each(|entity| {
if let Ok((mut vis, mut style)) = visibility.get_mut(child) { let parent = commands
match collapse { .spawn(NodeBundle {
UiCollapse::Show => { style: Style {
*vis = Visibility::Inherited; flex_direction: FlexDirection::Row,
style.display = Display::Flex; ..default()
} },
UiCollapse::Hide => { background_color: BackgroundColor(Color::PINK),
*vis = Visibility::Hidden; ..default()
style.display = Display::None; })
} .id();
};
} commands
}); .entity(entity)
.insert(NodeBundle {
style: Style { ..default() },
..default()
})
.set_parent(parent);
}); });
} }
fn toggle_collapse( /// Ui Tab Element
mut events: Query< #[derive(Debug, Component)]
(&Interaction, &Parent, &mut Name), pub struct GameUiTab;
(Changed<Interaction>, With<UiCollapseButton>),
>, fn init_ui_tab(events: Query<Entity, Added<GameUiTab>>, mut commands: Commands) {
mut collapses: Query<&mut UiCollapse>, events.iter().for_each(|entity| {
) { let parent = commands
events .spawn(NodeBundle {
.iter_mut() style: Style {
.for_each(|(interaction, parent, mut name)| match interaction { flex_direction: FlexDirection::Column,
Interaction::Pressed => { ..default()
if let Ok(mut collapse) = collapses.get_mut(parent.get()) { },
match *collapse { ..default()
UiCollapse::Show => { })
*name = Name::new("Show"); .id();
*collapse = UiCollapse::Hide;
} commands
UiCollapse::Hide => { .entity(entity)
*name = Name::new("Hide"); .insert(NodeBundle {
*collapse = UiCollapse::Show style: Style {
} flex_wrap: FlexWrap::Wrap,
} ..default()
} },
} background_color: BackgroundColor(Color::TEAL),
_ => (), ..default()
})
.set_parent(parent);
}); });
} }
@ -135,11 +127,9 @@ pub enum UiElementState {
pub struct GameUiList; pub struct GameUiList;
/// Manage UI Lists: lists of UI entities. /// Manage UI Lists: lists of UI entities.
fn init_ui_list(events: Query<(Entity, &Name), Added<GameUiList>>, mut commands: Commands) { fn init_ui_list(events: Query<Entity, Added<GameUiList>>, mut commands: Commands) {
events.iter().for_each(|(entity, name)| { events.iter().for_each(|entity| {
commands commands.entity(entity).insert(NodeBundle {
.entity(entity)
.insert(NodeBundle {
style: Style { style: Style {
flex_direction: FlexDirection::Column, flex_direction: FlexDirection::Column,
justify_items: JustifyItems::Center, justify_items: JustifyItems::Center,
@ -151,15 +141,6 @@ fn init_ui_list(events: Query<(Entity, &Name), Added<GameUiList>>, mut commands:
background_color: BackgroundColor(Color::RED), background_color: BackgroundColor(Color::RED),
border_color: BorderColor(Color::BLACK), border_color: BorderColor(Color::BLACK),
..default() ..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()
}),
);
}); });
}); });
} }
@ -169,8 +150,22 @@ fn init_ui_list(events: Query<(Entity, &Name), Added<GameUiList>>, mut commands:
pub struct GameUiSet; pub struct GameUiSet;
/// Manage UI Sets: collections of UI entities. /// Manage UI Sets: collections of UI entities.
fn init_ui_set(events: Query<(Entity, &Name), Added<GameUiSet>>, mut commands: Commands) { fn init_ui_set(events: Query<Entity, Added<GameUiSet>>, mut commands: Commands) {
events.iter().for_each(|(entity, name)| { 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 commands
.entity(entity) .entity(entity)
.insert(NodeBundle { .insert(NodeBundle {
@ -186,15 +181,7 @@ fn init_ui_set(events: Query<(Entity, &Name), Added<GameUiSet>>, mut commands: C
border_color: BorderColor(Color::BLACK), border_color: BorderColor(Color::BLACK),
..default() ..default()
}) })
.with_children(|parent| { .set_parent(parent);
parent.spawn(
TextBundle::from_section(name, TextStyle { ..default() }).with_style(Style {
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
}),
);
});
}); });
} }
@ -203,11 +190,9 @@ fn init_ui_set(events: Query<(Entity, &Name), Added<GameUiSet>>, mut commands: C
pub struct GameUiButton; pub struct GameUiButton;
/// Manage UI Buttons. interactive buttons. /// Manage UI Buttons. interactive buttons.
fn init_ui_button(events: Query<(Entity, &Name), Added<GameUiButton>>, mut commands: Commands) { fn init_ui_button(events: Query<Entity, Added<GameUiButton>>, mut commands: Commands) {
events.iter().for_each(|(entity, name)| { events.iter().for_each(|entity| {
commands commands.entity(entity).insert((
.entity(entity)
.insert((
ButtonBundle { ButtonBundle {
style: Style { style: Style {
margin: UiRect::all(Val::Px(2.0)), margin: UiRect::all(Val::Px(2.0)),
@ -220,10 +205,7 @@ fn init_ui_button(events: Query<(Entity, &Name), Added<GameUiButton>>, mut comma
..default() ..default()
}, },
UiElementState::Enabled, UiElementState::Enabled,
)) ));
.with_children(|parent| {
parent.spawn(TextBundle::from_section(name, TextStyle::default()));
});
}); });
} }
@ -250,19 +232,6 @@ fn manage_button_interaction(
}); });
} }
fn manage_button_title(
events: Query<(&Name, &Children), (Changed<Name>, With<Button>)>,
mut texts: Query<&mut Text, With<Parent>>,
) {
events.iter().for_each(|(name, children)| {
children.iter().for_each(|&child| {
if let Ok(mut text) = texts.get_mut(child) {
text.sections[0].value = name.into();
}
});
});
}
/// Manage the cursor icon for better immersion /// Manage the cursor icon for better immersion
fn manage_cursor( fn manage_cursor(
mut primary_window: Query<&mut Window, With<PrimaryWindow>>, mut primary_window: Query<&mut Window, With<PrimaryWindow>>,

Loading…
Cancel
Save