Don't take mouse events from an unfocused window

Former-commit-id: 99b60171c86109606318c6f39b45370105e3a7d9
This commit is contained in:
Imbris
2019-05-25 07:52:43 -04:00
parent aa6abf705b
commit d18f93f8da

View File

@ -17,6 +17,7 @@ pub struct Window {
needs_refresh_resize: bool, needs_refresh_resize: bool,
key_map: HashMap<glutin::VirtualKeyCode, Key>, key_map: HashMap<glutin::VirtualKeyCode, Key>,
supplement_events: Vec<Event>, supplement_events: Vec<Event>,
focused: bool,
} }
impl Window { impl Window {
@ -75,6 +76,7 @@ impl Window {
needs_refresh_resize: false, needs_refresh_resize: false,
key_map, key_map,
supplement_events: vec![], supplement_events: vec![],
focused: true,
}) })
} }
@ -99,6 +101,7 @@ impl Window {
let cursor_grabbed = self.cursor_grabbed; let cursor_grabbed = self.cursor_grabbed;
let renderer = &mut self.renderer; let renderer = &mut self.renderer;
let window = &mut self.window; let window = &mut self.window;
let focused = &mut self.focused;
let key_map = &self.key_map; let key_map = &self.key_map;
let pan_sensitivity = self.pan_sensitivity; let pan_sensitivity = self.pan_sensitivity;
let zoom_sensitivity = self.zoom_sensitivity; let zoom_sensitivity = self.zoom_sensitivity;
@ -143,19 +146,22 @@ impl Window {
}, },
_ => {} _ => {}
}, },
glutin::WindowEvent::Focused(new_focus) => *focused = new_focus,
_ => {} _ => {}
}, },
glutin::Event::DeviceEvent { event, .. } => match event { glutin::Event::DeviceEvent { event, .. } => match event {
glutin::DeviceEvent::MouseMotion { glutin::DeviceEvent::MouseMotion {
delta: (dx, dy), .. delta: (dx, dy), ..
} if cursor_grabbed => events.push(Event::CursorPan(Vec2::new( } if cursor_grabbed && *focused => events.push(Event::CursorPan(Vec2::new(
dx as f32 * pan_sensitivity, dx as f32 * pan_sensitivity,
dy as f32 * pan_sensitivity, dy as f32 * pan_sensitivity,
))), ))),
glutin::DeviceEvent::MouseWheel { glutin::DeviceEvent::MouseWheel {
delta: glutin::MouseScrollDelta::LineDelta(_x, y), delta: glutin::MouseScrollDelta::LineDelta(_x, y),
.. ..
} if cursor_grabbed => events.push(Event::Zoom(y as f32 * zoom_sensitivity)), } if cursor_grabbed && *focused => {
events.push(Event::Zoom(y as f32 * zoom_sensitivity))
}
_ => {} _ => {}
}, },
_ => {} _ => {}