From 6d4088a17328603cbc7c85c8bacb49d064875e48 Mon Sep 17 00:00:00 2001 From: Serge Bazanski Date: Sun, 4 Apr 2021 15:46:29 +0000 Subject: [PATCH] engine: add input module --- engine/BUILD | 2 + engine/src/input.rs | 68 +++++++++++++++++++++++++++++++ engine/src/main.rs | 10 ++++- engine/src/render/mod.rs | 88 ++++++++++++++++++++++++++++++++++++---- 4 files changed, 158 insertions(+), 10 deletions(-) create mode 100644 engine/src/input.rs diff --git a/engine/BUILD b/engine/BUILD index 43bf509..c2cadca 100644 --- a/engine/BUILD +++ b/engine/BUILD @@ -13,6 +13,8 @@ rust_binary( srcs = [ "src/main.rs", + "src/input.rs", + "src/physics/mod.rs", "src/physics/color.rs", diff --git a/engine/src/input.rs b/engine/src/input.rs new file mode 100644 index 0000000..f7a4aff --- /dev/null +++ b/engine/src/input.rs @@ -0,0 +1,68 @@ +use std::collections::{BTreeMap,BTreeSet}; + +#[derive(Clone, Debug)] +pub struct Input { + pub devices: BTreeMap, + highest_no: u64, +} +impl ecs::Global for Input {} + +impl Input { + pub fn allocate_device(&mut self) -> u64 { + self.highest_no += 1; + self.highest_no + } + pub fn new() -> Self { + Self { + devices: BTreeMap::new(), + highest_no: 0, + } + } + pub fn mouse_cursor(&self) -> Option<&MouseCursor> { + for dev in self.devices.values() { + if let &Device::MouseCursor(cursor) = &dev { + return Some(&cursor); + } + } + None + } +} + +#[derive(Clone, Debug)] +pub enum Device { + MouseCursor(MouseCursor), +} + +#[derive(Clone, Debug)] +pub struct MouseCursor { + // x and y coordinates [0.0, 1.0), top left at (0, 0). + pub x: f32, + pub y: f32, + + pressed: BTreeSet, +} + +impl MouseCursor { + pub fn new() -> Self { + Self { + x: 0., y: 0., + pressed: BTreeSet::new(), + } + } + + pub fn set_mouse_pressed(&mut self, button: MouseButton) { + self.pressed.insert(button); + } + + pub fn set_mouse_released(&mut self, button: MouseButton) { + self.pressed.remove(&button); + } +} + +#[derive(Clone, Copy, Debug, PartialOrd, Ord, PartialEq, Eq)] +pub enum MouseButton { + Left, + Middle, + Right, + Other, +} diff --git a/engine/src/main.rs b/engine/src/main.rs index abc2315..6bdaa04 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -21,6 +21,7 @@ use std::time; use cgmath as cgm; +pub mod input; mod render; mod util; mod physics; @@ -158,13 +159,17 @@ impl Main { struct MainData<'a> { scene_info: ecs::ReadWriteGlobal<'a, render::SceneInfo>, time: ecs::ReadGlobal<'a, Time>, + input: ecs::ReadGlobal<'a, input::Input>, transforms: ecs::ReadWriteComponent<'a, Transform>, } impl<'a> ecs::System <'a> for Main { type SystemData = MainData<'a>; fn run(&mut self, sd: Self::SystemData) { - let position = (sd.time.get().instant() / 10.0) * 3.14 * 2.0; + let position: f32 = match sd.input.get().mouse_cursor() { + Some(cursor) => cursor.x, + _ => (sd.time.get().instant() / 10.0) * 3.14 * 2.0, + }; let camera = cgm::Point3::new( 7.0 + (position / 4.0).sin(), @@ -209,10 +214,11 @@ fn main() { p.add_system(renderer); let start = time::Instant::now(); - world.set_global(Time{ + world.set_global(Time { start, now: start, }); + world.set_global(input::Input::new()); loop { world.global_mut::