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...

master
mitchellhansen 5 years ago
parent 5551581a3e
commit a403d13f41

@ -1,18 +1,20 @@
use std::collections::HashSet; use std::collections::HashSet;
use sfml::window::{Key, Event}; use sfml::window::{Key, Event, mouse::Button};
pub struct Input { pub struct Input {
held_keys: HashSet<Key> held_keys: HashSet<Key>,
held_mouse: HashSet<u8>,
} }
impl Input { impl Input {
pub fn new() -> Input { pub fn new() -> Input {
let mut container = HashSet::new(); let mut key_container = HashSet::new();
let mut mouse_container = HashSet::new();
Input { Input {
held_keys: container, held_keys: key_container,
held_mouse: mouse_container,
} }
} }
@ -20,6 +22,10 @@ impl Input {
self.held_keys.contains(&key) 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) { pub fn ingest(&mut self, event: &Event) {
match event { match event {
Event::KeyPressed { code, .. } => { Event::KeyPressed { code, .. } => {
@ -28,6 +34,12 @@ impl Input {
Event::KeyReleased { code, .. } => { Event::KeyReleased { code, .. } => {
self.held_keys.remove(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);
},
_ => {} _ => {}
} }
} }

@ -92,21 +92,30 @@ fn main() {
if code == Key::Escape { if code == Key::Escape {
return; return;
} }
} },
Event::MouseButtonPressed { button, x, y } => { 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) input.ingest(&event)
} }
if input.is_held(Key::W) {} if input.is_mousebutton_held(Button::Left) {
if input.is_held(Key::A) {}
if input.is_held(Key::S) {} }
if input.is_held(Key::D) {}
elapsed_time = timer.elap_time(); elapsed_time = timer.elap_time();
delta_time = elapsed_time - current_time; delta_time = elapsed_time - current_time;

Loading…
Cancel
Save