From f9d4d7ec0198f192f53e583c259e35350fe9fb62 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Sun, 7 Feb 2021 21:12:43 +0100 Subject: rstnode: basic mouse input handling --- games/rstnode/rst-client/src/input.rs | 56 +++++++++++++++++++++++++++++++++++ games/rstnode/rst-client/src/main.rs | 1 + games/rstnode/rst-client/src/state.rs | 19 ++++++++---- games/rstnode/rst-core/src/data.rs | 4 ++- 4 files changed, 74 insertions(+), 6 deletions(-) create mode 100644 games/rstnode/rst-client/src/input.rs diff --git a/games/rstnode/rst-client/src/input.rs b/games/rstnode/rst-client/src/input.rs new file mode 100644 index 000000000000..4e901b8ade06 --- /dev/null +++ b/games/rstnode/rst-client/src/input.rs @@ -0,0 +1,56 @@ +//! Advanced input handler + +use ggez::{ + event::EventHandler, + input::mouse::{self, MouseButton}, + Context, GameResult, +}; +use mint::Point2; + +pub struct InputHandle { + /// The mouse position on the viewport + pub mouse_pos: Point2, + /// Whether the left mouse button is pressed this frame + pub left_pressed: bool, + /// Whether the right mouse button is pressed this frame + pub right_pressed: bool, + /// Set when pressing left mouse and unset when releasing it + pub drag_point: Option>, +} + +impl InputHandle { + pub fn new() -> Self { + Self { + mouse_pos: [0.0, 0.0].into(), + left_pressed: false, + right_pressed: false, + drag_point: None, + } + } + + /// Get the unprojected mouse coordinates + pub fn unproject(&self) -> Point2 { + self.mouse_pos.clone(); + todo!() + } +} + +impl EventHandler for InputHandle { + fn update(&mut self, ctx: &mut Context) -> GameResult<()> { + self.mouse_pos = mouse::position(&ctx); + self.left_pressed = mouse::button_pressed(ctx, MouseButton::Left); + self.right_pressed = mouse::button_pressed(ctx, MouseButton::Right); + + // Only set the drag_point once and unset when we release Left button + if self.left_pressed && self.drag_point.is_none() { + self.drag_point = Some(self.mouse_pos.clone()); + } else if !self.left_pressed { + self.drag_point = None; + } + + Ok(()) + } + fn draw(&mut self, _: &mut Context) -> GameResult<()> { + panic!("Don't draw the input handle!"); + } +} diff --git a/games/rstnode/rst-client/src/main.rs b/games/rstnode/rst-client/src/main.rs index 240f302c55df..aa86de26c952 100644 --- a/games/rstnode/rst-client/src/main.rs +++ b/games/rstnode/rst-client/src/main.rs @@ -10,6 +10,7 @@ mod constants; mod ctx; mod error; mod graphics; +mod input; mod log; mod settings; mod state; diff --git a/games/rstnode/rst-client/src/state.rs b/games/rstnode/rst-client/src/state.rs index 6b7312f13d7f..b1b5e89f3d13 100644 --- a/games/rstnode/rst-client/src/state.rs +++ b/games/rstnode/rst-client/src/state.rs @@ -6,15 +6,17 @@ use crate::{ entities::{Coordinates, NodeRndr}, Renderer, }, + input::InputHandle, GameSettings, }; use ggez::{event::EventHandler, graphics, Context, GameResult}; -use rst_core::data::{Node, Owner, Upgrade}; +use rst_core::data::{Color, Level, Node, Owner, Player, Upgrade}; use std::sync::Arc; pub struct ClientState { assets: Assets, settings: GameSettings, + input: InputHandle, // Game state node: NodeRndr, @@ -25,14 +27,20 @@ impl ClientState { Self { assets, settings, + input: InputHandle::new(), node: NodeRndr { - loc: Coordinates(250.0, 250.0), + loc: Coordinates(512.0, 512.0), inner: Arc::new(Node { id: 0, health: 100.into(), max_health: 100.into(), - owner: Owner::Neutral, - type_: Upgrade::Base, + owner: Owner::Player(Player { + id: 0, + name: "kookie".into(), + color: Color::blue(), + money: 1000.into(), + }), + type_: Upgrade::Relay(Level::One), links: 0, link_states: vec![], buffer: vec![], @@ -47,7 +55,8 @@ impl ClientState { } impl EventHandler for ClientState { - fn update(&mut self, _ctx: &mut Context) -> GameResult<()> { + fn update(&mut self, ctx: &mut Context) -> GameResult<()> { + self.input.update(ctx)?; Ok(()) } diff --git a/games/rstnode/rst-core/src/data.rs b/games/rstnode/rst-core/src/data.rs index c41ec6d77e32..d0494fdef3e2 100644 --- a/games/rstnode/rst-core/src/data.rs +++ b/games/rstnode/rst-core/src/data.rs @@ -99,7 +99,7 @@ pub struct Team { /// An RGB color without alpha #[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct Color(u8, u8, u8); +pub struct Color(pub u8, pub u8, pub u8); impl Color { pub fn black() -> Self { @@ -189,6 +189,8 @@ pub enum Owner { Player(Player), } + + /// Encodes upgrade level without numbers #[derive(Copy, Clone, Serialize, Deserialize)] pub enum Level { -- cgit v1.2.3