From ec8793180d264d4a18f9d6606bb82f1573bc7053 Mon Sep 17 00:00:00 2001 From: Llywelwyn Date: Sat, 30 Sep 2023 03:24:30 +0100 Subject: [PATCH] mainmenu draw --- src/gui/main_menu.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ src/gui/mod.rs | 2 ++ src/main.rs | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/gui/main_menu.rs diff --git a/src/gui/main_menu.rs b/src/gui/main_menu.rs new file mode 100644 index 0000000..da012ad --- /dev/null +++ b/src/gui/main_menu.rs @@ -0,0 +1,46 @@ +use notan::prelude::*; +use notan::draw::{ Draw, CreateDraw, DrawTextSection, Font }; +use specs::prelude::*; +use std::collections::HashMap; +use super::{ FONTSIZE, RunState, DISPLAYWIDTH, TILESIZE, MainMenuSelection }; +use crate::consts::DISPLAYHEIGHT; + +pub fn draw_mainmenu(ecs: &World, draw: &mut Draw, atlas: &HashMap, font: &Font) { + let runstate = ecs.read_resource::(); + let selected = match *runstate { + RunState::MainMenu { menu_selection } => menu_selection, + _ => unreachable!("draw_mainmenu() called outside of MainMenu runstate."), + }; + let save_exists = crate::saveload_system::does_save_exist(); + const MID_X: f32 = ((DISPLAYWIDTH as f32) * TILESIZE) / 2.0; + + let (x, mut y) = (MID_X, ((DISPLAYHEIGHT as f32) * TILESIZE) / 3.0); + draw.text(font, "RUST-RL") + .size(FONTSIZE * 2.0) + .position(x, y) + .h_align_center(); + y = draw.last_text_bounds().max_y(); + draw.text(font, "New Game") + .size(FONTSIZE) + .position(x, y) + .h_align_center() + .color(get_colour(selected, MainMenuSelection::NewGame)); + if save_exists { + y = draw.last_text_bounds().max_y(); + draw.text(font, "Load Game") + .size(FONTSIZE) + .position(x, y) + .h_align_center() + .color(get_colour(selected, MainMenuSelection::LoadGame)); + } + y = draw.last_text_bounds().max_y(); + draw.text(font, "Quit") + .size(FONTSIZE) + .position(x, y) + .h_align_center() + .color(get_colour(selected, MainMenuSelection::Quit)); +} + +fn get_colour(selected: MainMenuSelection, desired: MainMenuSelection) -> Color { + if selected == desired { Color::from_rgb(0.0, 1.0, 0.0) } else { Color::WHITE } +} diff --git a/src/gui/mod.rs b/src/gui/mod.rs index e9cd3a4..b0a8341 100644 --- a/src/gui/mod.rs +++ b/src/gui/mod.rs @@ -64,6 +64,8 @@ pub use cheat_menu::*; use crate::consts::events::*; mod farlook; pub use farlook::*; +mod main_menu; +pub use main_menu::*; /// Gives a popup box with a message and a title, and waits for a keypress. #[allow(unused)] diff --git a/src/main.rs b/src/main.rs index 8f48353..54564e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -493,7 +493,7 @@ fn draw(_app: &mut App, gfx: &mut Graphics, gs: &mut State) { let runstate = *gs.ecs.fetch::(); match runstate { RunState::MainMenu { .. } => { - // Draw main menu + gui::draw_mainmenu(&gs.ecs, &mut draw, &gs.atlas, &gs.font); } RunState::CharacterCreation { .. } => { // Draw character creation