From 44927d3afa8e21bf61acdbb36be7c78550ac7321 Mon Sep 17 00:00:00 2001 From: Evan Pratten Date: Sat, 2 Oct 2021 20:26:28 -0400 Subject: [PATCH] redo a bunch of ui code --- game/src/context.rs | 8 +- game/src/lib.rs | 19 ++ game/src/scenes/how_to_play_screen.rs | 7 +- game/src/scenes/main_menu_screen.rs | 337 +++++--------------------- game/src/scenes/options_screen.rs | 120 +++------ game/src/scenes/pause_screen.rs | 156 +++--------- 6 files changed, 156 insertions(+), 491 deletions(-) diff --git a/game/src/context.rs b/game/src/context.rs index a3e0ac3..a37ebde 100644 --- a/game/src/context.rs +++ b/game/src/context.rs @@ -5,11 +5,17 @@ use discord_sdk::activity::ActivityBuilder; use crate::{GameConfig, utilities::non_ref_raylib::HackedRaylibHandle}; +#[derive(Debug)] +pub enum ControlFlag { + Quit +} + #[derive(Debug)] pub struct GameContext { pub renderer: RefCell, pub config: GameConfig, - pub discord_rpc_send: Sender> + pub discord_rpc_send: Sender>, + pub flag_send: Sender> } // impl GameContext { diff --git a/game/src/lib.rs b/game/src/lib.rs index 4adb624..7b053c4 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -144,6 +144,9 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box Result<(), Box Result<(), Box { + if let Some(flag) = flag { + match flag { + context::ControlFlag::Quit => break, + } + } + } + Err(TryRecvError::Empty) => {} + Err(TryRecvError::Disconnected) => { + break; + } + } } Ok(()) } diff --git a/game/src/scenes/how_to_play_screen.rs b/game/src/scenes/how_to_play_screen.rs index 2dcd658..e69ba4b 100644 --- a/game/src/scenes/how_to_play_screen.rs +++ b/game/src/scenes/how_to_play_screen.rs @@ -113,7 +113,7 @@ impl ScreenSpaceRender for HowToPlayScreen { //Back to Menu let hovering_back_button = Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0) - .check_collision_point_rec(mouse_position); + .check_collision_point_rec(mouse_position); raylib.draw_rgb_split_text( Vector2::new(25.0, screen_size.y - 50.0), "BACK TO MENU", @@ -121,9 +121,6 @@ impl ScreenSpaceRender for HowToPlayScreen { hovering_back_button, Color::WHITE, ); - if hovering_back_button && mouse_pressed { - self.is_btm_pressed = true; - } - + self.is_btm_pressed = hovering_back_button && mouse_pressed; } } diff --git a/game/src/scenes/main_menu_screen.rs b/game/src/scenes/main_menu_screen.rs index 80a2fe2..8910661 100644 --- a/game/src/scenes/main_menu_screen.rs +++ b/game/src/scenes/main_menu_screen.rs @@ -6,29 +6,23 @@ use discord_sdk::activity::{ActivityBuilder, Assets}; use pkg_version::pkg_version_major; use raylib::prelude::*; -use crate::{ - context::GameContext, - utilities::{ +use crate::{GameConfig, context::{ControlFlag, GameContext}, utilities::{ datastore::{load_texture_from_internal_data, ResourceLoadError}, game_version::get_version_string, math::interpolate_exp, non_ref_raylib::HackedRaylibHandle, render_layer::ScreenSpaceRender, - }, - GameConfig, -}; + }}; use super::{Scenes, ScreenError}; use tracing::{debug, error, info, trace}; #[derive(Debug)] pub struct MainMenuScreen { - - is_start_pressed: bool, //Is start button pressed - is_htp_pressed: bool, //Is how to play button pressed + is_start_pressed: bool, //Is start button pressed + is_htp_pressed: bool, //Is how to play button pressed is_options_pressed: bool, //Is options button pressed - is_quit_pressed: bool //Is quit button pressed - + is_quit_pressed: bool, //Is quit button pressed } impl MainMenuScreen { @@ -38,7 +32,7 @@ impl MainMenuScreen { is_start_pressed: false, is_htp_pressed: false, is_options_pressed: false, - is_quit_pressed: false + is_quit_pressed: false, } } } @@ -74,17 +68,14 @@ impl Action for MainMenuScreen { if self.is_start_pressed { Ok(ActionFlag::SwitchState(Scenes::InGameScene)) - } - else if self.is_htp_pressed { + } else if self.is_htp_pressed { Ok(ActionFlag::SwitchState(Scenes::HowToPlayScreen)) - } - else if self.is_options_pressed { + } else if self.is_options_pressed { Ok(ActionFlag::SwitchState(Scenes::OptionsScreen)) - } - else if self.is_quit_pressed { - panic!(); - } - else { + } else if self.is_quit_pressed { + context.flag_send.send(Some(ControlFlag::Quit)).unwrap(); + Ok(ActionFlag::Continue) + } else { Ok(ActionFlag::Continue) } } @@ -109,7 +100,13 @@ impl ScreenSpaceRender for MainMenuScreen { // Render the background raylib.clear_background(Color::BLACK); - raylib.draw_rectangle_lines(0, 0, screen_size.x as i32, screen_size.y as i32, config.colors.white); + raylib.draw_rectangle_lines( + 0, + 0, + screen_size.x as i32, + screen_size.y as i32, + config.colors.white, + ); // Calculate the logo position let screen_size = raylib.get_screen_size(); @@ -153,269 +150,61 @@ impl ScreenSpaceRender for MainMenuScreen { Color::WHITE, ); - raylib.draw_text( - + // Render the title + raylib.draw_rgb_split_text( + Vector2::new(37.0, 80.0), &format!("[{}]", config.name), - 37, - 80, - 70, - Color::BLUE, - ); - - raylib.draw_text( - - &format!("[{}]", config.name), - 43, - 80, - 70, - Color::RED, - ); - - raylib.draw_text( - - &format!("[{}]", config.name), - 40, - 80, 70, + true, Color::WHITE, ); // Start Game - if Rectangle::new(80.0, 300.0, 170.0, 20.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - - "START GAME", - 83, - 300, - 25, - Color::RED, - ); - raylib.draw_text( - - "START GAME", - 77, - 300, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "START GAME", - 80, - 300, - 25, - Color::WHITE, - ); - - if mouse_pressed{ - self.is_start_pressed = true; - } - } - else{ - raylib.draw_text( - - "START GAME", - 81, - 300, - 25, - Color::RED, - ); - raylib.draw_text( - - "START GAME", - 79, - 300, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "START GAME", - 80, - 300, - 25, - Color::WHITE, - ); - } + let hovering_start_game = + Rectangle::new(80.0, 300.0, 170.0, 20.0).check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(80.0, 300.0), + "START GAME", + 25, + hovering_start_game, + Color::WHITE, + ); + self.is_start_pressed = mouse_pressed && hovering_start_game; // How to Play - if Rectangle::new(80.0, 350.0, 170.0, 20.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - - "HOW TO PLAY", - 83, - 350, - 25, - Color::RED, - ); - raylib.draw_text( - - "HOW TO PLAY", - 77, - 350, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "HOW TO PLAY", - 80, - 350, - 25, - Color::WHITE, - ); - - if mouse_pressed{ - self.is_htp_pressed = true; - } - } - else{ - raylib.draw_text( - - "HOW TO PLAY", - 81, - 350, - 25, - Color::RED, - ); - raylib.draw_text( - - "HOW TO PLAY", - 79, - 350, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "HOW TO PLAY", - 80, - 350, - 25, - Color::WHITE, - ); - } + let hovering_htp = + Rectangle::new(80.0, 350.0, 170.0, 20.0).check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(80.0, 350.0), + "HOW TO PLAY", + 25, + hovering_htp, + Color::WHITE, + ); + self.is_htp_pressed = mouse_pressed && hovering_htp; // OPTIONS - if Rectangle::new(80.0, 400.0, 135.0, 20.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - - "OPTIONS", - 83, - 400, - 25, - Color::RED, - ); - raylib.draw_text( - - "OPTIONS", - 77, - 400, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "OPTIONS", - 80, - 400, - 25, - Color::WHITE, - ); - - if mouse_pressed{ - self.is_options_pressed = true; - } - - } - else{ - raylib.draw_text( - - "OPTIONS", - 81, - 400, - 25, - Color::RED, - ); - raylib.draw_text( - - "OPTIONS", - 79, - 400, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "OPTIONS", - 80, - 400, - 25, - Color::WHITE, - ); - - - } + let hovering_options = + Rectangle::new(80.0, 400.0, 135.0, 20.0).check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(80.0, 400.0), + "OPTIONS", + 25, + hovering_options, + Color::WHITE, + ); + self.is_options_pressed = mouse_pressed && hovering_options; // QUIT - if Rectangle::new(80.0, 445.0, 65.0, 20.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - - "QUIT", - 83, - 450, - 25, - Color::RED, - ); - raylib.draw_text( - - "QUIT", - 77, - 450, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "QUIT", - 80, - 450, - 25, - Color::WHITE, - ); - - if mouse_pressed{ - self.is_quit_pressed = true; - } - - } - else{ - raylib.draw_text( - - "QUIT", - 81, - 450, - 25, - Color::RED, - ); - raylib.draw_text( - - "QUIT", - 79, - 450, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "QUIT", - 80, - 450, - 25, - Color::WHITE, - ); - - - } + let hovering_quit = + Rectangle::new(80.0, 445.0, 65.0, 20.0).check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(80.0, 450.0), + "QUIT", + 25, + hovering_quit, + Color::WHITE, + ); + self.is_quit_pressed = mouse_pressed && hovering_quit; } } diff --git a/game/src/scenes/options_screen.rs b/game/src/scenes/options_screen.rs index a8a952e..84a9a40 100644 --- a/game/src/scenes/options_screen.rs +++ b/game/src/scenes/options_screen.rs @@ -5,27 +5,31 @@ use dirty_fsm::{Action, ActionFlag}; use pkg_version::pkg_version_major; use raylib::prelude::*; -use crate::{GameConfig, context::GameContext, utilities::{ +use crate::{ + context::GameContext, + utilities::{ datastore::{load_texture_from_internal_data, ResourceLoadError}, game_version::get_version_string, math::interpolate_exp, non_ref_raylib::HackedRaylibHandle, render_layer::ScreenSpaceRender, - }}; + }, + GameConfig, +}; use super::{Scenes, ScreenError}; use tracing::{debug, info, trace}; #[derive(Debug)] pub struct OptionsScreen { - is_btm_pressed: bool //Is back to menu button pressed + is_btm_pressed: bool, //Is back to menu button pressed } impl OptionsScreen { /// Construct a new `OptionsScreen` pub fn new() -> Self { Self { - is_btm_pressed: false + is_btm_pressed: false, } } } @@ -52,8 +56,7 @@ impl Action for OptionsScreen { if self.is_btm_pressed { Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen)) - } - else{ + } else { Ok(ActionFlag::Continue) } } @@ -69,13 +72,19 @@ impl ScreenSpaceRender for OptionsScreen { fn render_screen_space( &mut self, raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle, - config: &GameConfig + config: &GameConfig, ) { let screen_size = raylib.get_screen_size(); // Render the background raylib.clear_background(Color::BLACK); - raylib.draw_rectangle_lines(0, 0, screen_size.x as i32, screen_size.y as i32, config.colors.white); + raylib.draw_rectangle_lines( + 0, + 0, + screen_size.x as i32, + screen_size.y as i32, + config.colors.white, + ); let screen_size = raylib.get_screen_size(); @@ -84,90 +93,19 @@ impl ScreenSpaceRender for OptionsScreen { let mouse_pressed: bool = raylib.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON); - raylib.draw_text( - - "Options", - 37, - 80, - 70, - Color::BLUE, - ); - - raylib.draw_text( - - "Options", - 43, - 80, - 70, - Color::RED, - ); - - raylib.draw_text( - - "Options", - 40, - 80, - 70, - Color::WHITE, - ); + // Render the title + raylib.draw_rgb_split_text(Vector2::new(40.0, 80.0), "Options", 70, true, Color::WHITE); //Back to Menu - if Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0).check_collision_point_rec(mouse_position){ - raylib.draw_text( - - "BACK TO MENU", - 28, - screen_size.y as i32 - 50, - 25, - Color::RED, - ); - raylib.draw_text( - - "BACK TO MENU", - 22, - screen_size.y as i32 - 50, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "BACK TO MENU", - 25, - screen_size.y as i32 - 50, - 25, - Color::WHITE, - ); - - if mouse_pressed{ - self.is_btm_pressed = true; - - } - } - else { - raylib.draw_text( - - "BACK TO MENU", - 26, - screen_size.y as i32 - 50, - 25, - Color::RED, - ); - raylib.draw_text( - - "BACK TO MENU", - 24, - screen_size.y as i32 - 50, - 25, - Color::BLUE, - ); - raylib.draw_text( - - "BACK TO MENU", - 25, - screen_size.y as i32 - 50, - 25, - Color::WHITE, - ); - } + let hovering_back = Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0) + .check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new(25.0, screen_size.y - 50.0), + "Options", + 25, + hovering_back, + Color::WHITE, + ); + self.is_btm_pressed = mouse_pressed && hovering_back; } } diff --git a/game/src/scenes/pause_screen.rs b/game/src/scenes/pause_screen.rs index ea4fe28..1dde2e6 100644 --- a/game/src/scenes/pause_screen.rs +++ b/game/src/scenes/pause_screen.rs @@ -62,13 +62,11 @@ impl Action for PauseScreen { //Screen Size let screen_size = context.renderer.borrow_mut().get_screen_size(); - let centered_x_menu = (screen_size.x as f32 / 2.0) - 120.0; let centered_y_menu = (screen_size.y as f32 / 2.0) + 100.0; let centered_x_paused = (screen_size.x as f32 / 2.0) - 220.0; let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0; - //Mouse Position let mouse_position: Vector2 = context.renderer.borrow_mut().get_mouse_position(); //Mouse Input @@ -81,13 +79,15 @@ impl Action for PauseScreen { //For Paused if is_left_click - && Rectangle::new(centered_x_paused, centered_y_paused, 435.0, 80.0).check_collision_point_rec(mouse_position) + && Rectangle::new(centered_x_paused, centered_y_paused, 435.0, 80.0) + .check_collision_point_rec(mouse_position) { return Ok(ActionFlag::SwitchState(Scenes::InGameScene)); } //For Menu if is_left_click - && Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0).check_collision_point_rec(mouse_position) + && Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0) + .check_collision_point_rec(mouse_position) { return Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen)); } @@ -125,74 +125,13 @@ impl ScreenSpaceRender for PauseScreen { //Mouse Input let is_left_click = raylib.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON); - raylib.draw_rectangle_lines(0, 0, screen_size.x as i32, screen_size.y as i32, config.colors.white); - - //Pause Menu Texts With Glitchy Effect - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 223, - (screen_size.y as i32 / 2) - 40, - 120, - Color::RED, + raylib.draw_rectangle_lines( + 0, + 0, + screen_size.x as i32, + screen_size.y as i32, + config.colors.white, ); - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 217, - (screen_size.y as i32 / 2) - 40, - 120, - Color::BLUE, - ); - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 220, - (screen_size.y as i32 / 2) - 40, - 120, - Color::WHITE, - ); - raylib.draw_text( - "Click To Resume", - (screen_size.x as i32 / 2) - 80, - (screen_size.y as i32 / 2) + 60, - 20, - Color::RED, - ); - raylib.draw_text( - "Click To Resume", - (screen_size.x as i32 / 2) - 80, - (screen_size.y as i32 / 2) + 60, - 20, - Color::BLUE, - ); - raylib.draw_text( - "Click To Resume", - (screen_size.x as i32 / 2) - 80, - (screen_size.y as i32 / 2) + 60, - 20, - Color::WHITE, - ); - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 123, - (screen_size.y as i32 / 2) + 100, - 50, - Color::RED, - ); - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 117, - (screen_size.y as i32 / 2) + 100, - 50, - Color::BLUE, - ); - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 120, - (screen_size.y as i32 / 2) + 100, - 50, - Color::WHITE, - ); - - //Push comment //Variables for centering let centered_x_menu = (screen_size.x as f32 / 2.0) - 120.0; @@ -200,54 +139,31 @@ impl ScreenSpaceRender for PauseScreen { let centered_x_paused = (screen_size.x as f32 / 2.0) - 220.0; let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0; - - if Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 116, - (screen_size.y as i32 / 2) + 100, - 50, - Color::RED, - ); - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 124, - (screen_size.y as i32 / 2) + 100, - 50, - Color::BLUE, - ); - raylib.draw_text( - "Main Menu", - (screen_size.x as i32 / 2) - 120, - (screen_size.y as i32 / 2) + 100, - 50, - Color::WHITE, - ); - - } - - if Rectangle::new(centered_x_paused, centered_y_paused, 435.0, 80.0).check_collision_point_rec(mouse_position) { - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 215, - (screen_size.y as i32 / 2) - 40, - 120, - Color::BLUE, - ); - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 225, - (screen_size.y as i32 / 2) - 40, - 120, - Color::RED, - ); - raylib.draw_text( - "Paused", - (screen_size.x as i32 / 2) - 220, - (screen_size.y as i32 / 2) - 40, - 120, - Color::WHITE, - ); - } + //Pause Menu Texts With Glitchy Effect + let hovering_pause = Rectangle::new(centered_x_paused, centered_y_paused, 435.0, 80.0) + .check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new((screen_size.x / 2.0) - 220.0, (screen_size.y / 2.0) - 40.0), + "Paused", + 120, + hovering_pause, + Color::WHITE, + ); + raylib.draw_rgb_split_text( + Vector2::new((screen_size.x / 2.0) - 80.0, (screen_size.y / 2.0) + 60.0), + "Click To Resume", + 20, + false, + Color::WHITE, + ); + let hovering_main_menu = Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0) + .check_collision_point_rec(mouse_position); + raylib.draw_rgb_split_text( + Vector2::new((screen_size.x / 2.0) - 120.0, (screen_size.y / 2.0) + 100.0), + "Main Menu", + 50, + hovering_main_menu, + Color::WHITE, + ); } }