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};
#[derive(Debug)]
pub enum ControlFlag {
Quit
}
#[derive(Debug)]
pub struct GameContext {
pub renderer: RefCell<HackedRaylibHandle>,
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 {

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
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 raylib_thread;
{
@ -168,6 +171,7 @@ pub async fn game_begin(game_config: &mut GameConfig) -> Result<(), Box<dyn std:
renderer: RefCell::new(rl.into()),
config: game_config.clone(),
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;
}
}
// 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(())
}

View File

@ -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;
}
}

View File

@ -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_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<Scenes, ScreenError, GameContext> 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(
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",
83,
300,
25,
Color::RED,
);
raylib.draw_text(
"START GAME",
77,
300,
25,
Color::BLUE,
);
raylib.draw_text(
"START GAME",
80,
300,
25,
hovering_start_game,
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,
);
}
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(
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",
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,
hovering_htp,
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,
);
}
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(
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",
83,
400,
25,
Color::RED,
);
raylib.draw_text(
"OPTIONS",
77,
400,
25,
Color::BLUE,
);
raylib.draw_text(
"OPTIONS",
80,
400,
25,
hovering_options,
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,
);
}
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(
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",
83,
450,
25,
Color::RED,
);
raylib.draw_text(
"QUIT",
77,
450,
25,
Color::BLUE,
);
raylib.draw_text(
"QUIT",
80,
450,
25,
hovering_quit,
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,
);
}
self.is_quit_pressed = mouse_pressed && hovering_quit;
}
}

View File

@ -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<Scenes, ScreenError, GameContext> 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,
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,
);
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,
);
}
self.is_btm_pressed = mouse_pressed && hovering_back;
}
}

View File

@ -62,13 +62,11 @@ impl Action<Scenes, ScreenError, GameContext> 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<Scenes, ScreenError, GameContext> 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,
//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,
);
}
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_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,
);
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,
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,
);
}
}
}