rstnode: basic mouse input handling

wip/yesman
Katharina Fey 3 years ago
parent 0472f0fb50
commit f9d4d7ec01
Signed by: kookie
GPG Key ID: 90734A9E619C8A6C
  1. 56
      games/rstnode/rst-client/src/input.rs
  2. 1
      games/rstnode/rst-client/src/main.rs
  3. 19
      games/rstnode/rst-client/src/state.rs
  4. 4
      games/rstnode/rst-core/src/data.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<f32>,
/// 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<Point2<f32>>,
}
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<f32> {
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!");
}
}

@ -10,6 +10,7 @@ mod constants;
mod ctx;
mod error;
mod graphics;
mod input;
mod log;
mod settings;
mod state;

@ -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(())
}

@ -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 {

Loading…
Cancel
Save