Skip to content

Commit

Permalink
refactor: improved hud and ui 👀
Browse files Browse the repository at this point in the history
  • Loading branch information
eerii committed Dec 11, 2023
1 parent bc773c8 commit 2c7ed25
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 80 deletions.
1 change: 1 addition & 0 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ mod only_in_debug {
..default()
}),
FpsText,
UI_LAYER,
));
});
}
Expand Down
12 changes: 7 additions & 5 deletions src/end.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
config::{GameOptions, GameScore},
menu::MenuButton,
menu::MenuState,
ui::*,
GameState,
};
Expand Down Expand Up @@ -34,8 +34,8 @@ fn init_end_screen(
if let Ok(node) = node.get_single_mut() {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(&style, "Your journey has ended").add(parent);
UIText::new(
UIText::simple(&style, "Your journey has ended").add(parent);
UIText::simple(
&style,
&format!(
"You helped {} entities find their way home",
Expand All @@ -48,15 +48,16 @@ fn init_end_screen(
),
)
.add(parent);
UIText::new(&style, "Thank you").add(parent);
UIText::simple(&style, "Thank you").add(parent);

UIButton::new(&style, "Try again", MenuButton::Other).add(parent);
UIButton::<UiNone>::new(&style, "Try again", None).add(parent);
});
}
}
}

fn handle_buttons(
mut menu_state: ResMut<NextState<MenuState>>,
mut game_state: ResMut<NextState<GameState>>,
mut text: Query<&mut Text>,
mut buttons: Query<(&Interaction, &Children, &mut BackgroundColor), Changed<Interaction>>,
Expand All @@ -70,6 +71,7 @@ fn handle_buttons(
bg.0 = opts.color.dark;
text.sections[0].style.color = opts.color.light;
// Go to the main menu
menu_state.set(MenuState::Main);
game_state.set(GameState::Menu);
}
Interaction::Hovered => {
Expand Down
71 changes: 31 additions & 40 deletions src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ struct TilesText;
// Systems
// ·······

fn init_hud(mut cmd: Commands, assets: Res<GameAssets>, mut node: Query<Entity, With<UiNode>>) {
fn init_hud(
mut cmd: Commands,
assets: Res<GameAssets>,
style: Res<UIStyle>,
mut node: Query<Entity, With<UiNode>>,
) {
// Main menu layout
if let Ok(node) = node.get_single_mut() {
if let Some(mut node) = cmd.get_entity(node) {
Expand All @@ -58,29 +63,22 @@ fn init_hud(mut cmd: Commands, assets: Res<GameAssets>, mut node: Query<Entity,
..default()
})
.with_children(|tiles| {
tiles.spawn(ImageBundle {
image: UiImage {
texture: assets.river_icon.clone(),
..default()
},
style: Style {
width: Val::Px(24.),
..default()
},
..default()
});

tiles.spawn((
TextBundle::from_section(
"0",
TextStyle {
font: assets.font.clone(),
font_size: 24.0,
color: Color::WHITE,
ImageBundle {
image: UiImage {
texture: assets.river_icon.clone(),
..default()
},
style: Style {
width: Val::Px(style.text.font_size + 4.),
..default()
},
),
TilesText,
..default()
},
UI_LAYER,
));

UIText::new(&style, "0", Some(TilesText)).add(tiles);
});

parent
Expand All @@ -98,29 +96,22 @@ fn init_hud(mut cmd: Commands, assets: Res<GameAssets>, mut node: Query<Entity,
..default()
})
.with_children(|score| {
UIText::new(&style, "0", Some(ScoreText)).add(score);

score.spawn((
TextBundle::from_section(
"0",
TextStyle {
font: assets.font.clone(),
font_size: 24.0,
color: Color::WHITE,
ImageBundle {
image: UiImage {
texture: assets.coin_icon.clone(),
..default()
},
style: Style {
width: Val::Px(style.text.font_size + 4.),
..default()
},
),
ScoreText,
));

score.spawn(ImageBundle {
image: UiImage {
texture: assets.coin_icon.clone(),
..default()
},
style: Style {
width: Val::Px(24.),
..default()
},
..default()
});
UI_LAYER,
));
});
});
}
Expand Down
36 changes: 17 additions & 19 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ pub enum MenuButton {
RemapKeybind(String),
ResetKeybinds,
ChangeFont(String),
Other,
}

// ·······
Expand Down Expand Up @@ -174,7 +173,6 @@ fn handle_buttons(
})
.unwrap_or_else(|e| error!("Failed to change font size: {}", e));
}
MenuButton::Other => {}
}
}
Interaction::Hovered => {
Expand Down Expand Up @@ -321,36 +319,36 @@ fn remap_keybind(
fn layout_main(mut cmd: Commands, node: Entity, style: &UIStyle, best_score: u32) {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(style, "Entities' repose")
UIText::simple(style, "Entities' repose")
.with_title()
.add(parent);
if best_score > 0 {
UIText::new(style, &format!("Most saved: {}", best_score)).add(parent);
UIText::simple(style, &format!("Most saved: {}", best_score)).add(parent);
}

UIButton::new(style, "Play", MenuButton::Play).add(parent);
UIButton::new(style, "Settings", MenuButton::GoSettings).add(parent);
UIButton::new(style, "Play", Some(MenuButton::Play)).add(parent);
UIButton::new(style, "Settings", Some(MenuButton::GoSettings)).add(parent);
});
}
}

fn layout_options(mut cmd: Commands, node: Entity, style: &UIStyle) {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(style, "Settings").with_title().add(parent);
UIText::simple(style, "Settings").with_title().add(parent);

UIButton::new(style, "Keybinds", MenuButton::GoKeybinds).add(parent);
UIButton::new(style, "Visual", MenuButton::GoVisual).add(parent);
UIButton::new(style, "Keybinds", Some(MenuButton::GoKeybinds)).add(parent);
UIButton::new(style, "Visual", Some(MenuButton::GoVisual)).add(parent);

UIButton::new(style, "Back", MenuButton::GoMain).add(parent);
UIButton::new(style, "Back", Some(MenuButton::GoMain)).add(parent);
});
}
}

fn layout_keybinds(mut cmd: Commands, node: Entity, style: &UIStyle, keybinds: &Keybinds) {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(style, "Keybinds").with_title().add(parent);
UIText::simple(style, "Keybinds").with_title().add(parent);

for (i, value) in keybinds.iter_fields().enumerate() {
let field_name = keybinds.name_at(i).unwrap();
Expand All @@ -365,7 +363,7 @@ fn layout_keybinds(mut cmd: Commands, node: Entity, style: &UIStyle, keybinds: &
UIButton::new(
style,
&keys,
MenuButton::RemapKeybind(field_name.to_string()),
Some(MenuButton::RemapKeybind(field_name.to_string())),
)
.with_width(Val::Px(128.))
.with_font_scale(0.7)
Expand All @@ -374,31 +372,31 @@ fn layout_keybinds(mut cmd: Commands, node: Entity, style: &UIStyle, keybinds: &
}
}

UIButton::new(style, "Reset", MenuButton::ResetKeybinds).add(parent);
UIButton::new(style, "Reset", Some(MenuButton::ResetKeybinds)).add(parent);

UIButton::new(style, "Back", MenuButton::GoSettings).add(parent);
UIButton::new(style, "Back", Some(MenuButton::GoSettings)).add(parent);
});
}
}

fn layout_rebinding(mut cmd: Commands, node: Entity, style: &UIStyle, key: &str) {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(
UIText::simple(
style,
&format!("Press a key or button for {}", snake_to_upper(key)),
)
.add(parent);

UIButton::new(style, "Back", MenuButton::GoKeybinds).add(parent);
UIButton::new(style, "Back", Some(MenuButton::GoKeybinds)).add(parent);
});
}
}

fn layout_visual(mut cmd: Commands, node: Entity, style: &UIStyle, opts: &GameOptions) {
if let Some(mut node) = cmd.get_entity(node) {
node.with_children(|parent| {
UIText::new(style, "Visual settings")
UIText::simple(style, "Visual settings")
.with_title()
.add(parent);

Expand All @@ -409,15 +407,15 @@ fn layout_visual(mut cmd: Commands, node: Entity, style: &UIStyle, opts: &GameOp
UIButton::new(
style,
&format!("{}", value),
MenuButton::ChangeFont(field_name),
Some(MenuButton::ChangeFont(field_name)),
)
.with_width(Val::Px(40.))
.add(row);
});
}
}

UIButton::new(style, "Back", MenuButton::GoSettings).add(parent);
UIButton::new(style, "Back", Some(MenuButton::GoSettings)).add(parent);
});
}
}
52 changes: 36 additions & 16 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Plugin for UIPlugin {
#[derive(Resource, Default)]
pub struct UIStyle {
title: TextStyle,
text: TextStyle,
pub text: TextStyle,
button_text: TextStyle,

button: Style,
Expand All @@ -49,6 +49,9 @@ pub struct UiCam;
#[derive(Component)]
pub struct UiNode;

#[derive(Component)]
pub struct UiNone;

// ·······
// Systems
// ·······
Expand Down Expand Up @@ -142,16 +145,18 @@ fn change_style(

// Text

pub struct UIText<'a> {
pub struct UIText<'a, T: Component> {
text: TextBundle,
style: &'a UIStyle,
action: Option<T>,
}

impl<'a> UIText<'a> {
pub fn new(style: &'a UIStyle, text: &str) -> Self {
impl<'a, T: Component> UIText<'a, T> {
pub fn new(style: &'a UIStyle, text: &str, action: Option<T>) -> Self {
Self {
text: TextBundle::from_section(text, style.text.clone()),
style,
action,
}
}

Expand All @@ -166,7 +171,21 @@ impl<'a> UIText<'a> {
}

pub fn add(self, parent: &mut ChildBuilder) {
parent.spawn((self.text, UI_LAYER));
if let Some(action) = self.action {
parent.spawn((self.text, action, UI_LAYER));
} else {
parent.spawn((self.text, UI_LAYER));
}
}
}

impl<'a> UIText<'a, UiNone> {
pub fn simple(style: &'a UIStyle, text: &str) -> Self {
Self {
text: TextBundle::from_section(text, style.text.clone()),
style,
action: None,
}
}
}

Expand All @@ -175,11 +194,11 @@ impl<'a> UIText<'a> {
pub struct UIButton<T: Component> {
button: ButtonBundle,
text: TextBundle,
action: T,
action: Option<T>,
}

impl<T: Component> UIButton<T> {
pub fn new(style: &UIStyle, text: &str, action: T) -> Self {
pub fn new(style: &UIStyle, text: &str, action: Option<T>) -> Self {
Self {
button: ButtonBundle {
style: style.button.clone(),
Expand All @@ -202,21 +221,22 @@ impl<T: Component> UIButton<T> {
}

pub fn add(self, parent: &mut ChildBuilder) {
let _text = self.text.text.sections[0].value.clone();
let _id = parent
.spawn((self.button, self.action, UI_LAYER))
.with_children(|button| {
button.spawn((self.text, UI_LAYER));
})
.id();
if let Some(action) = self.action {
parent.spawn((self.button, action, UI_LAYER))
} else {
parent.spawn((self.button, UI_LAYER))
}
.with_children(|button| {
button.spawn((self.text, UI_LAYER));
});
}
}

// Option row (label text + widget)

pub struct UIOption<'a> {
row: NodeBundle,
label: UIText<'a>,
label: UIText<'a, UiNone>,
}

impl<'a> UIOption<'a> {
Expand All @@ -233,7 +253,7 @@ impl<'a> UIOption<'a> {
},
..default()
},
label: UIText::new(style, &snake_to_upper(label)).with_style(Style {
label: UIText::new(style, &snake_to_upper(label), None).with_style(Style {
flex_grow: 1.,
..default()
}),
Expand Down

0 comments on commit 2c7ed25

Please sign in to comment.