redo a bunch of ui code

This commit is contained in:
Evan Pratten 2021-10-02 20:26:28 -04:00
parent 9b74b42554
commit 44927d3afa
6 changed files with 156 additions and 491 deletions

View File

@ -5,11 +5,17 @@ use discord_sdk::activity::ActivityBuilder;
use crate::{GameConfig, utilities::non_ref_raylib::HackedRaylibHandle}; use crate::{GameConfig, utilities::non_ref_raylib::HackedRaylibHandle};
#[derive(Debug)]
pub enum ControlFlag {
Quit
}
#[derive(Debug)] #[derive(Debug)]
pub struct GameContext { pub struct GameContext {
pub renderer: RefCell<HackedRaylibHandle>, pub renderer: RefCell<HackedRaylibHandle>,
pub config: GameConfig, pub config: GameConfig,
pub discord_rpc_send: Sender<Option<ActivityBuilder>> pub discord_rpc_send: Sender<Option<ActivityBuilder>>,
pub flag_send: Sender<Option<ControlFlag>>
} }
// impl GameContext { // impl GameContext {

View File

@ -144,6 +144,9 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
// Build an MPSC for the game to send rich presence data to discord // Build an MPSC for the game to send rich presence data to discord
let (send_discord_rpc, recv_discord_rpc) = std::sync::mpsc::channel(); let (send_discord_rpc, recv_discord_rpc) = std::sync::mpsc::channel();
// Build an MPSC for signaling the control thread
let (send_control_signal, recv_control_signal) = std::sync::mpsc::channel();
let context; let context;
let raylib_thread; let raylib_thread;
{ {
@ -168,6 +171,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
renderer: RefCell::new(rl.into()), renderer: RefCell::new(rl.into()),
config: game_config.clone(), config: game_config.clone(),
discord_rpc_send: send_discord_rpc, discord_rpc_send: send_discord_rpc,
flag_send: send_control_signal,
}); });
} }
@ -304,6 +308,21 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
continue; continue;
} }
} }
// Handle control flags
match recv_control_signal.try_recv() {
Ok(flag) => {
if let Some(flag) = flag {
match flag {
context::ControlFlag::Quit => break,
}
}
}
Err(TryRecvError::Empty) => {}
Err(TryRecvError::Disconnected) => {
break;
}
}
} }
Ok(()) Ok(())
} }

View File

@ -113,7 +113,7 @@ impl ScreenSpaceRender for HowToPlayScreen {
//Back to Menu //Back to Menu
let hovering_back_button = Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0) 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( raylib.draw_rgb_split_text(
Vector2::new(25.0, screen_size.y - 50.0), Vector2::new(25.0, screen_size.y - 50.0),
"BACK TO MENU", "BACK TO MENU",
@ -121,9 +121,6 @@ impl ScreenSpaceRender for HowToPlayScreen {
hovering_back_button, hovering_back_button,
Color::WHITE, Color::WHITE,
); );
if hovering_back_button && mouse_pressed { self.is_btm_pressed = hovering_back_button && mouse_pressed;
self.is_btm_pressed = true;
}
} }
} }

View File

@ -6,29 +6,23 @@ use discord_sdk::activity::{ActivityBuilder, Assets};
use pkg_version::pkg_version_major; use pkg_version::pkg_version_major;
use raylib::prelude::*; use raylib::prelude::*;
use crate::{ use crate::{GameConfig, context::{ControlFlag, GameContext}, utilities::{
context::GameContext,
utilities::{
datastore::{load_texture_from_internal_data, ResourceLoadError}, datastore::{load_texture_from_internal_data, ResourceLoadError},
game_version::get_version_string, game_version::get_version_string,
math::interpolate_exp, math::interpolate_exp,
non_ref_raylib::HackedRaylibHandle, non_ref_raylib::HackedRaylibHandle,
render_layer::ScreenSpaceRender, render_layer::ScreenSpaceRender,
}, }};
GameConfig,
};
use super::{Scenes, ScreenError}; use super::{Scenes, ScreenError};
use tracing::{debug, error, info, trace}; use tracing::{debug, error, info, trace};
#[derive(Debug)] #[derive(Debug)]
pub struct MainMenuScreen { pub struct MainMenuScreen {
is_start_pressed: bool, //Is start button pressed
is_start_pressed: bool, //Is start button pressed is_htp_pressed: bool, //Is how to play button pressed
is_htp_pressed: bool, //Is how to play button pressed
is_options_pressed: bool, //Is options 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 { impl MainMenuScreen {
@ -38,7 +32,7 @@ impl MainMenuScreen {
is_start_pressed: false, is_start_pressed: false,
is_htp_pressed: false, is_htp_pressed: false,
is_options_pressed: false, is_options_pressed: false,
is_quit_pressed: false is_quit_pressed: false,
} }
} }
} }
@ -74,17 +68,14 @@ impl Action<Scenes, ScreenError, GameContext> for MainMenuScreen {
if self.is_start_pressed { if self.is_start_pressed {
Ok(ActionFlag::SwitchState(Scenes::InGameScene)) Ok(ActionFlag::SwitchState(Scenes::InGameScene))
} } else if self.is_htp_pressed {
else if self.is_htp_pressed {
Ok(ActionFlag::SwitchState(Scenes::HowToPlayScreen)) Ok(ActionFlag::SwitchState(Scenes::HowToPlayScreen))
} } else if self.is_options_pressed {
else if self.is_options_pressed {
Ok(ActionFlag::SwitchState(Scenes::OptionsScreen)) Ok(ActionFlag::SwitchState(Scenes::OptionsScreen))
} } else if self.is_quit_pressed {
else if self.is_quit_pressed { context.flag_send.send(Some(ControlFlag::Quit)).unwrap();
panic!(); Ok(ActionFlag::Continue)
} } else {
else {
Ok(ActionFlag::Continue) Ok(ActionFlag::Continue)
} }
} }
@ -109,7 +100,13 @@ impl ScreenSpaceRender for MainMenuScreen {
// Render the background // Render the background
raylib.clear_background(Color::BLACK); 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 // Calculate the logo position
let screen_size = raylib.get_screen_size(); let screen_size = raylib.get_screen_size();
@ -153,269 +150,61 @@ impl ScreenSpaceRender for MainMenuScreen {
Color::WHITE, Color::WHITE,
); );
raylib.draw_text( // Render the title
raylib.draw_rgb_split_text(
Vector2::new(37.0, 80.0),
&format!("[{}]", config.name), &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, 70,
true,
Color::WHITE, Color::WHITE,
); );
// Start Game // Start Game
if Rectangle::new(80.0, 300.0, 170.0, 20.0).check_collision_point_rec(mouse_position) { let hovering_start_game =
raylib.draw_text( Rectangle::new(80.0, 300.0, 170.0, 20.0).check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
"START GAME", Vector2::new(80.0, 300.0),
83, "START GAME",
300, 25,
25, hovering_start_game,
Color::RED, Color::WHITE,
); );
raylib.draw_text( self.is_start_pressed = mouse_pressed && hovering_start_game;
"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,
);
}
// How to Play // How to Play
if Rectangle::new(80.0, 350.0, 170.0, 20.0).check_collision_point_rec(mouse_position) { let hovering_htp =
raylib.draw_text( Rectangle::new(80.0, 350.0, 170.0, 20.0).check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
"HOW TO PLAY", Vector2::new(80.0, 350.0),
83, "HOW TO PLAY",
350, 25,
25, hovering_htp,
Color::RED, Color::WHITE,
); );
raylib.draw_text( self.is_htp_pressed = mouse_pressed && hovering_htp;
"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,
);
}
// OPTIONS // OPTIONS
if Rectangle::new(80.0, 400.0, 135.0, 20.0).check_collision_point_rec(mouse_position) { let hovering_options =
raylib.draw_text( Rectangle::new(80.0, 400.0, 135.0, 20.0).check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
"OPTIONS", Vector2::new(80.0, 400.0),
83, "OPTIONS",
400, 25,
25, hovering_options,
Color::RED, Color::WHITE,
); );
raylib.draw_text( self.is_options_pressed = mouse_pressed && hovering_options;
"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,
);
}
// QUIT // QUIT
if Rectangle::new(80.0, 445.0, 65.0, 20.0).check_collision_point_rec(mouse_position) { let hovering_quit =
raylib.draw_text( Rectangle::new(80.0, 445.0, 65.0, 20.0).check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
"QUIT", Vector2::new(80.0, 450.0),
83, "QUIT",
450, 25,
25, hovering_quit,
Color::RED, Color::WHITE,
); );
raylib.draw_text( self.is_quit_pressed = mouse_pressed && hovering_quit;
"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,
);
}
} }
} }

View File

@ -5,27 +5,31 @@ use dirty_fsm::{Action, ActionFlag};
use pkg_version::pkg_version_major; use pkg_version::pkg_version_major;
use raylib::prelude::*; use raylib::prelude::*;
use crate::{GameConfig, context::GameContext, utilities::{ use crate::{
context::GameContext,
utilities::{
datastore::{load_texture_from_internal_data, ResourceLoadError}, datastore::{load_texture_from_internal_data, ResourceLoadError},
game_version::get_version_string, game_version::get_version_string,
math::interpolate_exp, math::interpolate_exp,
non_ref_raylib::HackedRaylibHandle, non_ref_raylib::HackedRaylibHandle,
render_layer::ScreenSpaceRender, render_layer::ScreenSpaceRender,
}}; },
GameConfig,
};
use super::{Scenes, ScreenError}; use super::{Scenes, ScreenError};
use tracing::{debug, info, trace}; use tracing::{debug, info, trace};
#[derive(Debug)] #[derive(Debug)]
pub struct OptionsScreen { 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 { impl OptionsScreen {
/// Construct a new `OptionsScreen` /// Construct a new `OptionsScreen`
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
is_btm_pressed: false is_btm_pressed: false,
} }
} }
} }
@ -52,8 +56,7 @@ impl Action<Scenes, ScreenError, GameContext> for OptionsScreen {
if self.is_btm_pressed { if self.is_btm_pressed {
Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen)) Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen))
} } else {
else{
Ok(ActionFlag::Continue) Ok(ActionFlag::Continue)
} }
} }
@ -69,13 +72,19 @@ impl ScreenSpaceRender for OptionsScreen {
fn render_screen_space( fn render_screen_space(
&mut self, &mut self,
raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle, raylib: &mut crate::utilities::non_ref_raylib::HackedRaylibHandle,
config: &GameConfig config: &GameConfig,
) { ) {
let screen_size = raylib.get_screen_size(); let screen_size = raylib.get_screen_size();
// Render the background // Render the background
raylib.clear_background(Color::BLACK); 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(); 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); let mouse_pressed: bool = raylib.is_mouse_button_pressed(MouseButton::MOUSE_LEFT_BUTTON);
raylib.draw_text( // Render the title
raylib.draw_rgb_split_text(Vector2::new(40.0, 80.0), "Options", 70, true, Color::WHITE);
"Options",
37,
80,
70,
Color::BLUE,
);
raylib.draw_text(
"Options",
43,
80,
70,
Color::RED,
);
raylib.draw_text(
"Options",
40,
80,
70,
Color::WHITE,
);
//Back to Menu //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){ let hovering_back = Rectangle::new(35.0, screen_size.y as f32 - 80.0, 200.0, 40.0)
raylib.draw_text( .check_collision_point_rec(mouse_position);
raylib.draw_rgb_split_text(
"BACK TO MENU", Vector2::new(25.0, screen_size.y - 50.0),
28, "Options",
screen_size.y as i32 - 50, 25,
25, hovering_back,
Color::RED, Color::WHITE,
); );
raylib.draw_text( self.is_btm_pressed = mouse_pressed && hovering_back;
"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,
);
}
} }
} }

View File

@ -62,13 +62,11 @@ impl Action<Scenes, ScreenError, GameContext> for PauseScreen {
//Screen Size //Screen Size
let screen_size = context.renderer.borrow_mut().get_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_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_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_x_paused = (screen_size.x as f32 / 2.0) - 220.0;
let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0; let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0;
//Mouse Position //Mouse Position
let mouse_position: Vector2 = context.renderer.borrow_mut().get_mouse_position(); let mouse_position: Vector2 = context.renderer.borrow_mut().get_mouse_position();
//Mouse Input //Mouse Input
@ -81,13 +79,15 @@ impl Action<Scenes, ScreenError, GameContext> for PauseScreen {
//For Paused //For Paused
if is_left_click 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)); return Ok(ActionFlag::SwitchState(Scenes::InGameScene));
} }
//For Menu //For Menu
if is_left_click 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)); return Ok(ActionFlag::SwitchState(Scenes::MainMenuScreen));
} }
@ -125,74 +125,13 @@ impl ScreenSpaceRender for PauseScreen {
//Mouse Input //Mouse Input
let is_left_click = raylib.is_mouse_button_down(MouseButton::MOUSE_LEFT_BUTTON); 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); raylib.draw_rectangle_lines(
0,
//Pause Menu Texts With Glitchy Effect 0,
raylib.draw_text( screen_size.x as i32,
"Paused", screen_size.y as i32,
(screen_size.x as i32 / 2) - 223, config.colors.white,
(screen_size.y as i32 / 2) - 40,
120,
Color::RED,
); );
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 //Variables for centering
let centered_x_menu = (screen_size.x as f32 / 2.0) - 120.0; 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_x_paused = (screen_size.x as f32 / 2.0) - 220.0;
let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0; let centered_y_paused = (screen_size.y as f32 / 2.0) - 40.0;
//Pause Menu Texts With Glitchy Effect
if Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0).check_collision_point_rec(mouse_position) { let hovering_pause = Rectangle::new(centered_x_paused, centered_y_paused, 435.0, 80.0)
raylib.draw_text( .check_collision_point_rec(mouse_position);
"Main Menu", raylib.draw_rgb_split_text(
(screen_size.x as i32 / 2) - 116, Vector2::new((screen_size.x / 2.0) - 220.0, (screen_size.y / 2.0) - 40.0),
(screen_size.y as i32 / 2) + 100, "Paused",
50, 120,
Color::RED, hovering_pause,
); Color::WHITE,
raylib.draw_text( );
"Main Menu", raylib.draw_rgb_split_text(
(screen_size.x as i32 / 2) - 124, Vector2::new((screen_size.x / 2.0) - 80.0, (screen_size.y / 2.0) + 60.0),
(screen_size.y as i32 / 2) + 100, "Click To Resume",
50, 20,
Color::BLUE, false,
); Color::WHITE,
raylib.draw_text( );
"Main Menu", let hovering_main_menu = Rectangle::new(centered_x_menu, centered_y_menu, 200.0, 50.0)
(screen_size.x as i32 / 2) - 120, .check_collision_point_rec(mouse_position);
(screen_size.y as i32 / 2) + 100, raylib.draw_rgb_split_text(
50, Vector2::new((screen_size.x / 2.0) - 120.0, (screen_size.y / 2.0) + 100.0),
Color::WHITE, "Main Menu",
); 50,
hovering_main_menu,
} 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,
);
}
} }
} }