From 8df43259171fc0cca48f7d85112db40f8e652a88 Mon Sep 17 00:00:00 2001 From: shivshank Date: Sat, 25 Aug 2018 16:08:23 -0400 Subject: [PATCH] Fix botched mouse handling --- CHANGELOG.md | 11 +++++++++++ examples/game_of_life.rs | 5 ++++- src/breakout.rs | 4 +++- src/core.rs | 6 +++++- src/lib.rs | 3 ++- 5 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..ff15a09 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,11 @@ +# Change Log + +## [v0.5.1] - 2018-08-25 + +Started a change log! + +### Fixed + + - Vertical mouse position was wrong, v0.5.0 changed coordinate systems from a window-like one + to a UV-like one (0, 0 is now the lower left) but the glutin basic input feature did not + respect this and reported old style mouse positions diff --git a/examples/game_of_life.rs b/examples/game_of_life.rs index 6f4e47e..8868345 100644 --- a/examples/game_of_life.rs +++ b/examples/game_of_life.rs @@ -4,6 +4,7 @@ use mini_gl_fb::{Config, BufferFormat}; use mini_gl_fb::glutin::{MouseButton, VirtualKeyCode}; use std::time::SystemTime; +use std::cmp; const WIDTH: usize = 200; const HEIGHT: usize = 200; @@ -45,7 +46,9 @@ fn main() { if input.mouse_is_down(MouseButton::Left) { // Mouse was pressed - let (x, y) = input.mouse_pos; + let (mut x, mut y) = input.mouse_pos; + x = cmp::min(x, WIDTH - 1); + y = cmp::min(y, HEIGHT - 1); cells[y * WIDTH + x] = true; fb.update_buffer(&cells); // Give the user extra time to make something pretty each time they click diff --git a/src/breakout.rs b/src/breakout.rs index 6374a9c..ff9069f 100644 --- a/src/breakout.rs +++ b/src/breakout.rs @@ -16,7 +16,9 @@ pub struct GlutinBreakout { } pub struct BasicInput { - /// The mouse position in buffer coordinates + /// The mouse position in buffer coordinates. + /// + /// The bottom left of the window is (0, 0). pub mouse_pos: (usize, usize), /// Stores whether a mouse button was down and is down, in that order. /// diff --git a/src/core.rs b/src/core.rs index 62450b9..f8bd4ec 100644 --- a/src/core.rs +++ b/src/core.rs @@ -307,7 +307,11 @@ impl Internal { let (x, y): (f64, f64) = pos.to_physical(dpi_factor).into(); let x_scale = self.fb.buffer_width as f64 / (self.fb.vp_width as f64); let y_scale = self.fb.buffer_height as f64 / (self.fb.vp_height as f64); - let mouse_pos = ((x * x_scale).floor() as usize, (y * y_scale).floor() as usize); + let mut mouse_pos = ( + (x * x_scale).floor() as usize, + // use the OpenGL texture coordinate system instead of window coordinates + (self.fb.buffer_height as f64 - y * y_scale).floor() as usize + ); input.mouse_pos = mouse_pos; } diff --git a/src/lib.rs b/src/lib.rs index f603b10..9e9543b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -307,7 +307,8 @@ impl MiniGlFb { /// Provides an easy interface for rudimentary input handling. /// /// Automatically handles close events and partially handles resizes (the caller chooses if - /// a redraw is necessary). + /// a redraw is necessary; and the window will only actually physically change size if it is + /// a resizable window). /// /// Polls for window events and summarizes the input events for you each frame. See /// `BasicInput` for the information that is provided to you. You will need to use some