Mid set-state button action implementation

main
Elijah C. Voigt 1 year ago
parent 3b7f626394
commit ae5bfb45d0

@ -1,6 +1,7 @@
use crate::prelude::*; use crate::prelude::*;
/// Menu Plugin; empty struct for Plugin impl /// Menu Plugin; empty struct for Plugin impl
pub(crate) struct DicePlugin; pub(crate) struct DicePlugin;
impl Plugin for DicePlugin { impl Plugin for DicePlugin {
@ -9,6 +10,13 @@ impl Plugin for DicePlugin {
} }
} }
/// Create UI for the Dice game at startup
fn init_dice_ui(mut commands: Commands) { fn init_dice_ui(mut commands: Commands) {
todo!() commands
.spawn((
GameChoice::Dice,
SetState(GameChoice::Dice),
SetState(MenuState::Closed),
))
.add(UiTitle { text: "Dice" });
} }

@ -10,11 +10,12 @@ impl Plugin for GamePlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.init_state::<GameChoice>(); app.init_state::<GameChoice>();
app.add_systems(Startup, init_camera); app.add_systems(Startup, init_camera);
app.add_plugins(dice::DicePlugin);
} }
} }
#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default)] #[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)]
enum GameChoice { pub(crate) enum GameChoice {
#[default] #[default]
None, None,
Dice, Dice,

@ -16,7 +16,7 @@ impl Plugin for MenuPlugin {
/// State tracking if the menu is open or closed /// State tracking if the menu is open or closed
#[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)] #[derive(States, Debug, Clone, PartialEq, Eq, Hash, Default, Component)]
enum MenuState { pub(crate) enum MenuState {
#[default] #[default]
Open, Open,
Closed, Closed,
@ -25,36 +25,11 @@ enum MenuState {
/// Initialize menu UI nodes at startup /// Initialize menu UI nodes at startup
fn init_menu_ui(mut commands: Commands) { fn init_menu_ui(mut commands: Commands) {
commands commands
.spawn(( .spawn(MenuState::Open)
MenuState::Open, .add(UiContainer)
NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
align_content: AlignContent::Center,
justify_items: JustifyItems::Center,
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Column,
..default()
},
..default()
},
))
.with_children(|parent| { .with_children(|parent| {
let title_text_style = TextStyle { parent.spawn_empty().add(UiTitle {
color: Color::BLACK, text: "Game Jam Casino",
font_size: 24.0,
..default()
};
parent.spawn(TextBundle {
text: Text::from_section("Game Jam Casino", title_text_style),
style: Style {
margin: UiRect::all(Val::Px(5.0)),
..default()
},
..default()
}); });
parent.spawn_empty().add(UiButton { label: "Dice" }); parent.spawn_empty().add(UiButton { label: "Dice" });
}); });

@ -3,5 +3,9 @@ pub(crate) use bevy::{ecs::system::EntityCommand, prelude::*};
/// Intra-project imports /// Intra-project imports
pub(crate) use crate::ecs::schedule::common_conditions::*; pub(crate) use crate::ecs::schedule::common_conditions::*;
pub(crate) use crate::game::GameChoice;
pub(crate) use crate::manage_visibility; pub(crate) use crate::manage_visibility;
pub(crate) use crate::ui::button::UiButton; pub(crate) use crate::ui::button::UiButton;
pub(crate) use crate::ui::container::UiContainer;
pub(crate) use crate::ui::title::UiTitle;
pub(crate) use crate::ui::SetState;

@ -20,6 +20,18 @@ 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: States>(S);
fn button_state_actions<S: States>(
q: Query<(&mut S, &SetState<S>, &Interaction), Changed<Interaction>>,
s: Res<State<S>>,
n: Res<NextState<S>>,
) {
todo!("Change state when button is clicked")
}
pub(crate) mod button { pub(crate) mod button {
use super::*; use super::*;
@ -55,3 +67,54 @@ pub(crate) mod button {
} }
} }
} }
pub(crate) mod title {
use super::*;
pub(crate) struct UiTitle {
pub text: &'static str,
}
impl EntityCommand for UiTitle {
fn apply(self, id: Entity, world: &mut World) {
let title_text_style = TextStyle {
color: Color::BLACK,
font_size: 24.0,
..default()
};
world.entity_mut(id).insert(TextBundle {
text: Text::from_section(self.text, title_text_style),
style: Style {
margin: UiRect::all(Val::Px(5.0)),
..default()
},
..default()
});
}
}
}
pub(crate) mod container {
use super::*;
pub(crate) struct UiContainer;
impl EntityCommand for UiContainer {
fn apply(self, id: Entity, world: &mut World) {
world.entity_mut(id).insert(NodeBundle {
style: Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
align_content: AlignContent::Center,
justify_items: JustifyItems::Center,
justify_content: JustifyContent::Center,
flex_direction: FlexDirection::Column,
position_type: PositionType::Absolute,
..default()
},
..default()
});
}
}
}

Loading…
Cancel
Save