From 4087f3c4c3ffd43e4ea001fcd5a629f468e72d0e Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Tue, 2 Aug 2016 18:01:21 +0200 Subject: [PATCH] Added get_unscaled_mouse_pos --- examples/mouse.rs | 5 +++-- src/lib.rs | 18 ++++++++++++++++++ src/os/macos/mod.rs | 13 +++++++++++++ src/os/unix/mod.rs | 10 +++++++++- src/os/windows/mod.rs | 9 +++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/examples/mouse.rs b/examples/mouse.rs index 8e9d785..0cb606e 100644 --- a/examples/mouse.rs +++ b/examples/mouse.rs @@ -8,8 +8,8 @@ const HEIGHT: usize = 360; fn main() { let mut buffer: Vec = vec![0; WIDTH * HEIGHT]; - let mut window = match Window::new("Mouse Draw - Press ESC to exit", WIDTH, HEIGHT, - WindowOptions { + let mut window = match Window::new("Mouse Draw - Press ESC to exit", WIDTH, HEIGHT, + WindowOptions { scale: Scale::X2, ..WindowOptions::default() }) { @@ -23,6 +23,7 @@ fn main() { while window.is_open() && !window.is_key_down(Key::Escape) { window.get_mouse_pos(MouseMode::Discard).map(|mouse| { let screen_pos = ((mouse.1 as usize) * WIDTH) + mouse.0 as usize; + println!("{:?}", window.get_unscaled_mouse_pos(MouseMode::Discard).unwrap()); if window.get_mouse_down(MouseButton::Left) { buffer[screen_pos] = 0x00ffffff; diff --git a/src/lib.rs b/src/lib.rs index 0cb9d75..c97f0a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -310,6 +310,24 @@ impl Window { self.0.get_mouse_pos(mode) } + /// + /// Get the current position of the mouse relative to the current window + /// The coordinate system is as 0, 0 as the upper left corner and ignores + /// any scaling set to the window. + /// + /// # Examples + /// + /// ```ignore + /// window.get_unscaled_mouse_pos(MouseMode::Clamp).map(|mouse| { + /// println!("x {} y {}", mouse.0, mouse.1); + /// }); + /// ``` + /// + #[inline] + pub fn get_unscaled_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { + self.0.get_unscaled_mouse_pos(mode) + } + /// /// Check if a mouse button is down or not /// diff --git a/src/os/macos/mod.rs b/src/os/macos/mod.rs index a4649e6..dbc4c76 100644 --- a/src/os/macos/mod.rs +++ b/src/os/macos/mod.rs @@ -359,6 +359,19 @@ impl Window { h) } + pub fn get_unscaled_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { + let s = 1.0; + let w = self.shared_data.width as f32; + let h = self.shared_data.height as f32; + + mouse_handler::get_pos(mode, + self.shared_data.mouse_x, + self.shared_data.mouse_y, + s, + w, + h) + } + #[inline] pub fn set_cursor_style(&mut self, cursor: CursorStyle) { unsafe { diff --git a/src/os/unix/mod.rs b/src/os/unix/mod.rs index aac97da..5e3c573 100644 --- a/src/os/unix/mod.rs +++ b/src/os/unix/mod.rs @@ -30,7 +30,7 @@ extern { fn mfb_update(window: *mut c_void); fn mfb_update_with_buffer(window: *mut c_void, buffer: *const c_uchar); fn mfb_set_position(window: *mut c_void, x: i32, y: i32); - fn mfb_set_key_callback(window: *mut c_void, target: *mut c_void, + fn mfb_set_key_callback(window: *mut c_void, target: *mut c_void, kb: unsafe extern fn(*mut c_void, i32, i32), cb: unsafe extern fn(*mut c_void, u32)); fn mfb_set_shared_data(window: *mut c_void, target: *mut SharedData); @@ -281,6 +281,14 @@ impl Window { mouse_handler::get_pos(mode, self.shared_data.mouse_x, self.shared_data.mouse_y, s, w, h) } + pub fn get_unscaled_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { + let s = self.shared_data.scale as f32; + let w = self.shared_data.width as f32; + let h = self.shared_data.height as f32; + + mouse_handler::get_pos(mode, self.shared_data.mouse_x, self.shared_data.mouse_y, 1.0, w, h) + } + pub fn get_mouse_down(&self, button: MouseButton) -> bool { match button { MouseButton::Left => self.shared_data.state[0] > 0, diff --git a/src/os/windows/mod.rs b/src/os/windows/mod.rs index d05fa0f..2cb3bdf 100644 --- a/src/os/windows/mod.rs +++ b/src/os/windows/mod.rs @@ -521,6 +521,15 @@ impl Window { mouse_handler::get_pos(mode, self.mouse.x, self.mouse.y, s, w * s, h * s) } + pub fn get_unscaled_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { + let s = self.scale_factor as f32; + let w = self.width as f32; + let h = self.height as f32; + + // TODO: Needs to be fixed with resize support + mouse_handler::get_pos(mode, self.mouse.x, self.mouse.y, 1.0, w * s, h * s) + } + pub fn get_mouse_down(&self, button: MouseButton) -> bool { match button { MouseButton::Left => self.mouse.state[0],