Fix botched mouse handling

This commit is contained in:
shivshank 2018-08-25 16:08:23 -04:00
parent f71d48b814
commit 8df4325917
5 changed files with 25 additions and 4 deletions

11
CHANGELOG.md Normal file
View file

@ -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

View file

@ -4,6 +4,7 @@ use mini_gl_fb::{Config, BufferFormat};
use mini_gl_fb::glutin::{MouseButton, VirtualKeyCode}; use mini_gl_fb::glutin::{MouseButton, VirtualKeyCode};
use std::time::SystemTime; use std::time::SystemTime;
use std::cmp;
const WIDTH: usize = 200; const WIDTH: usize = 200;
const HEIGHT: usize = 200; const HEIGHT: usize = 200;
@ -45,7 +46,9 @@ fn main() {
if input.mouse_is_down(MouseButton::Left) { if input.mouse_is_down(MouseButton::Left) {
// Mouse was pressed // 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; cells[y * WIDTH + x] = true;
fb.update_buffer(&cells); fb.update_buffer(&cells);
// Give the user extra time to make something pretty each time they click // Give the user extra time to make something pretty each time they click

View file

@ -16,7 +16,9 @@ pub struct GlutinBreakout {
} }
pub struct BasicInput { 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), pub mouse_pos: (usize, usize),
/// Stores whether a mouse button was down and is down, in that order. /// Stores whether a mouse button was down and is down, in that order.
/// ///

View file

@ -307,7 +307,11 @@ impl Internal {
let (x, y): (f64, f64) = pos.to_physical(dpi_factor).into(); 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 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 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; input.mouse_pos = mouse_pos;
} }

View file

@ -307,7 +307,8 @@ impl MiniGlFb {
/// Provides an easy interface for rudimentary input handling. /// Provides an easy interface for rudimentary input handling.
/// ///
/// Automatically handles close events and partially handles resizes (the caller chooses if /// 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 /// 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 /// `BasicInput` for the information that is provided to you. You will need to use some