Added get_unscaled_mouse_pos

This commit is contained in:
Daniel Collin 2016-08-02 18:01:21 +02:00
parent 9cd8c97694
commit 4087f3c4c3
5 changed files with 52 additions and 3 deletions

View file

@ -8,8 +8,8 @@ const HEIGHT: usize = 360;
fn main() { fn main() {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT]; let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
let mut window = match Window::new("Mouse Draw - Press ESC to exit", WIDTH, HEIGHT, let mut window = match Window::new("Mouse Draw - Press ESC to exit", WIDTH, HEIGHT,
WindowOptions { WindowOptions {
scale: Scale::X2, scale: Scale::X2,
..WindowOptions::default() ..WindowOptions::default()
}) { }) {
@ -23,6 +23,7 @@ fn main() {
while window.is_open() && !window.is_key_down(Key::Escape) { while window.is_open() && !window.is_key_down(Key::Escape) {
window.get_mouse_pos(MouseMode::Discard).map(|mouse| { window.get_mouse_pos(MouseMode::Discard).map(|mouse| {
let screen_pos = ((mouse.1 as usize) * WIDTH) + mouse.0 as usize; 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) { if window.get_mouse_down(MouseButton::Left) {
buffer[screen_pos] = 0x00ffffff; buffer[screen_pos] = 0x00ffffff;

View file

@ -310,6 +310,24 @@ impl Window {
self.0.get_mouse_pos(mode) 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 /// Check if a mouse button is down or not
/// ///

View file

@ -359,6 +359,19 @@ impl Window {
h) 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] #[inline]
pub fn set_cursor_style(&mut self, cursor: CursorStyle) { pub fn set_cursor_style(&mut self, cursor: CursorStyle) {
unsafe { unsafe {

View file

@ -30,7 +30,7 @@ extern {
fn mfb_update(window: *mut c_void); fn mfb_update(window: *mut c_void);
fn mfb_update_with_buffer(window: *mut c_void, buffer: *const c_uchar); 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_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), kb: unsafe extern fn(*mut c_void, i32, i32),
cb: unsafe extern fn(*mut c_void, u32)); cb: unsafe extern fn(*mut c_void, u32));
fn mfb_set_shared_data(window: *mut c_void, target: *mut SharedData); 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) 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 { pub fn get_mouse_down(&self, button: MouseButton) -> bool {
match button { match button {
MouseButton::Left => self.shared_data.state[0] > 0, MouseButton::Left => self.shared_data.state[0] > 0,

View file

@ -521,6 +521,15 @@ impl Window {
mouse_handler::get_pos(mode, self.mouse.x, self.mouse.y, s, w * s, h * s) 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 { pub fn get_mouse_down(&self, button: MouseButton) -> bool {
match button { match button {
MouseButton::Left => self.mouse.state[0], MouseButton::Left => self.mouse.state[0],