Add basic block selection with mouse

This commit is contained in:
crumblingstatue 2023-04-03 18:12:36 +02:00
parent 98965a8c86
commit f07bd9c713
4 changed files with 66 additions and 26 deletions

View file

@ -1,22 +1,51 @@
use fnv::FnvHashSet;
use sfml::window::{Event, Key};
use sfml::window::{mouse, Event, Key};
use crate::graphics::ScreenPos;
#[derive(Default)]
pub struct KbInput {
pub struct Input {
down: FnvHashSet<Key>,
pressed: FnvHashSet<Key>,
pub lmb_down: bool,
pub rmb_down: bool,
pub mouse_down_loc: ScreenPos,
}
impl KbInput {
impl Input {
pub fn update_from_event(&mut self, ev: &Event) {
match ev {
Event::KeyPressed { code, .. } => {
self.pressed.insert(*code);
self.down.insert(*code);
&Event::KeyPressed { code, .. } => {
self.pressed.insert(code);
self.down.insert(code);
}
Event::KeyReleased { code, .. } => {
self.down.remove(code);
}
&Event::MouseButtonPressed { button, x, y } => {
self.mouse_down_loc = ScreenPos {
x: x as i16,
y: y as i16,
};
if button == mouse::Button::Left {
self.lmb_down = true;
}
if button == mouse::Button::Right {
self.rmb_down = true;
}
}
&Event::MouseButtonReleased { button, .. } => {
if button == mouse::Button::Left {
self.lmb_down = false;
}
if button == mouse::Button::Right {
self.rmb_down = false;
}
}
&Event::MouseMoved { x, y } => {
self.mouse_down_loc.x = x as i16;
self.mouse_down_loc.y = y as i16;
}
_ => {}
}
}