From a403d13f41fba8587d0562cd8e4368885d142d47 Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Sat, 6 Jul 2019 20:24:15 -0700 Subject: [PATCH] Working on zooming and scrolling of the image. Want to do a full input chain like I did the raycaster, but I don't know if I want to commit to SFML for my windowing... --- src/input.rs | 24 ++++++++++++++++++------ src/main.rs | 25 +++++++++++++++++-------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/input.rs b/src/input.rs index 0f1daf81..9964f826 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,25 +1,31 @@ use std::collections::HashSet; -use sfml::window::{Key, Event}; +use sfml::window::{Key, Event, mouse::Button}; pub struct Input { - held_keys: HashSet + held_keys: HashSet, + held_mouse: HashSet, } impl Input { pub fn new() -> Input { - let mut container = HashSet::new(); - + let mut key_container = HashSet::new(); + let mut mouse_container = HashSet::new(); Input { - held_keys: container, + held_keys: key_container, + held_mouse: mouse_container, } } - pub fn is_held(&self, key: Key) -> bool{ + pub fn is_held(&self, key: Key) -> bool { self.held_keys.contains(&key) } + pub fn is_mousebutton_held(&self, button: Button) -> bool { + self.held_mouse.contains(&(button as u8)) + } + pub fn ingest(&mut self, event: &Event) { match event { Event::KeyPressed { code, .. } => { @@ -28,6 +34,12 @@ impl Input { Event::KeyReleased { code, .. } => { self.held_keys.remove(code); } + Event::MouseButtonPressed { button, x, y } => { + self.held_mouse.insert(button.clone() as u8); + }, + Event::MouseButtonReleased { button, x, y } => { + self.held_mouse.insert(button.clone() as u8); + }, _ => {} } } diff --git a/src/main.rs b/src/main.rs index b4991251..649707ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,21 +92,30 @@ fn main() { if code == Key::Escape { return; } - } + }, Event::MouseButtonPressed { button, x, y } => { - if button == Button::Left { - return; + + }, + Event::MouseWheelScrolled { wheel, delta, x, y } => { + + if delta > 0.0 { + println!("{:?}", delta); + println!("{:?}", background_sprite.get_scale()); + background_sprite.set_scale(background_sprite.get_scale()+Vector2f::new(0.1,0.1)); + } else { + println!("{:?}", delta); + println!("{:?}", background_sprite.get_scale()+Vector2f::new(-0.1,-0.1)); + background_sprite.set_scale(background_sprite.get_scale()+Vector2f::new(-0.1,-0.1)); } - } + }, _ => {} } input.ingest(&event) } - if input.is_held(Key::W) {} - if input.is_held(Key::A) {} - if input.is_held(Key::S) {} - if input.is_held(Key::D) {} + if input.is_mousebutton_held(Button::Left) { + + } elapsed_time = timer.elap_time(); delta_time = elapsed_time - current_time;