diff --git a/src/game/dice.rs b/src/game/dice.rs index 76dfa08..474e779 100644 --- a/src/game/dice.rs +++ b/src/game/dice.rs @@ -1,7 +1,6 @@ use crate::prelude::*; /// Menu Plugin; empty struct for Plugin impl - pub(crate) struct DicePlugin; impl Plugin for DicePlugin { @@ -13,10 +12,32 @@ impl Plugin for DicePlugin { /// Create UI for the Dice game at startup fn init_dice_ui(mut commands: Commands) { commands - .spawn(( - GameChoice::Dice, - SetState(GameChoice::Dice), - SetState(MenuState::Closed), - )) - .add(UiTitle { text: "Dice" }); + .spawn((GameChoice::Dice, MenuState::Closed)) + .add(UiContainer) + .with_children(|parent| { + parent + .spawn_empty() + .add(UiTitle { text: "Dice" }) + .add(UiStyle(Style { + position_type: PositionType::Absolute, + top: Val::Px(0.0), + left: Val::Px(0.0), + margin: UiRect::all(Val::Px(5.0)), + border: UiRect::all(Val::Px(1.0)), + ..default() + })); + + parent + .spawn((SetState(GameChoice::None), SetState(MenuState::Open))) + .add(UiButton { label: "Menu" }) + .add(UiStyle(Style { + position_type: PositionType::Absolute, + top: Val::Px(0.0), + right: Val::Px(0.0), + margin: UiRect::all(Val::Px(5.0)), + padding: UiRect::all(Val::Px(5.0)), + border: UiRect::all(Val::Px(1.0)), + ..default() + })); + }); } diff --git a/src/game/mod.rs b/src/game/mod.rs index 112d9e6..9e9eeff 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -11,6 +11,14 @@ impl Plugin for GamePlugin { app.init_state::(); app.add_systems(Startup, init_camera); app.add_plugins(dice::DicePlugin); + app.add_systems( + Update, + manage_visibility::.run_if(state_changed::), + ); + app.add_systems( + Update, + button_state_action::.run_if(any_component_changed::), + ); } } diff --git a/src/menu.rs b/src/menu.rs index 586ae9d..9dd0fb7 100644 --- a/src/menu.rs +++ b/src/menu.rs @@ -1,4 +1,4 @@ -use crate::{manage_visibility, prelude::*}; +use crate::prelude::*; /// Menu Plugin; empty struct for Plugin impl pub(crate) struct MenuPlugin; @@ -11,6 +11,10 @@ impl Plugin for MenuPlugin { Update, manage_visibility::.run_if(state_changed::), ); + app.add_systems( + Update, + button_state_action::.run_if(any_component_changed::), + ); } } @@ -25,12 +29,14 @@ pub(crate) enum MenuState { /// Initialize menu UI nodes at startup fn init_menu_ui(mut commands: Commands) { commands - .spawn(MenuState::Open) + .spawn((MenuState::Open, GameChoice::None)) .add(UiContainer) .with_children(|parent| { parent.spawn_empty().add(UiTitle { text: "Game Jam Casino", }); - parent.spawn_empty().add(UiButton { label: "Dice" }); + parent + .spawn(SetState(GameChoice::Dice)) + .add(UiButton { label: "Dice" }); }); } diff --git a/src/prelude.rs b/src/prelude.rs index 26f344c..2763431 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -1,3 +1,5 @@ +pub(crate) use std::fmt::Debug; + /// Bevy imports pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*}; @@ -5,7 +7,10 @@ pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*}; pub(crate) use crate::ecs::schedule::common_conditions::*; pub(crate) use crate::game::GameChoice; pub(crate) use crate::manage_visibility; +pub(crate) use crate::menu::MenuState; pub(crate) use crate::ui::button::UiButton; +pub(crate) use crate::ui::button_state_action; pub(crate) use crate::ui::container::UiContainer; +pub(crate) use crate::ui::style::UiStyle; pub(crate) use crate::ui::title::UiTitle; pub(crate) use crate::ui::SetState; diff --git a/src/ui.rs b/src/ui.rs index 3c64b3a..78fedbf 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -22,14 +22,19 @@ fn button_interaction(mut query: Query<(&mut BackgroundColor, &Interaction)>) { /// Component for marking a button as a "Set State" action #[derive(Component)] -pub(crate) struct SetState(S); +pub(crate) struct SetState(pub S); -fn button_state_actions( - q: Query<(&mut S, &SetState, &Interaction), Changed>, - s: Res>, - n: Res>, +pub(crate) fn button_state_action( + q: Query<(&SetState, &Interaction), Changed>, + mut n: ResMut>, ) { - todo!("Change state when button is clicked") + q.iter() + .for_each(|(SetState(s), interaction)| match interaction { + Interaction::Pressed => { + n.set(s.clone()); + } + _ => (), + }); } pub(crate) mod button { @@ -113,8 +118,23 @@ pub(crate) mod container { position_type: PositionType::Absolute, ..default() }, + border_color: BorderColor(Color::BLACK), ..default() }); } } } + +pub(crate) mod style { + use super::*; + + pub(crate) struct UiStyle(pub Style); + + impl EntityCommand for UiStyle { + fn apply(self, id: Entity, world: &mut World) { + if let Some(mut s) = world.entity_mut(id).get_mut::