Add Magpie cursor input (closes #23)

This commit is contained in:
mars 2022-11-03 19:07:40 -06:00
parent 325a85eb39
commit aa333b0fe4
1 changed files with 22 additions and 3 deletions

View File

@ -2,10 +2,10 @@ use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::Instant; use std::time::Instant;
use canary::{Panel, Runtime, Vec2}; use canary::{CursorEventKind, Panel, Runtime, Vec2, PX_PER_MM};
use glium::backend::glutin::DisplayCreationError; use glium::backend::glutin::DisplayCreationError;
use glium::{glutin, Surface}; use glium::{glutin, Surface};
use glutin::event::{Event, WindowEvent}; use glutin::event::{ElementState, Event, MouseButton, WindowEvent};
use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget}; use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget};
use glutin::window::WindowId; use glutin::window::WindowId;
@ -26,6 +26,7 @@ pub struct Window {
pub graphics: Graphics, pub graphics: Graphics,
pub panel: Panel, pub panel: Panel,
pub last_update: Instant, pub last_update: Instant,
pub cursor_pos: Vec2,
} }
impl Window { impl Window {
@ -42,6 +43,7 @@ impl Window {
graphics, graphics,
panel, panel,
last_update, last_update,
cursor_pos: Vec2::ZERO,
}) })
} }
@ -75,9 +77,26 @@ impl Window {
pub fn on_event(&mut self, event: WindowEvent) { pub fn on_event(&mut self, event: WindowEvent) {
match event { match event {
WindowEvent::Resized(size) => { WindowEvent::Resized(size) => {
self.resize(Vec2::new(size.width as f32, size.height as f32) * canary::PX_PER_MM); self.resize(Vec2::new(size.width as f32, size.height as f32) * PX_PER_MM);
self.request_redraw() self.request_redraw()
} }
WindowEvent::CursorMoved { position, .. } => {
let x = position.x as f32 * PX_PER_MM;
let y = position.y as f32 * PX_PER_MM;
self.cursor_pos = Vec2::new(x, y);
}
WindowEvent::MouseInput {
state,
button: MouseButton::Left,
..
} => {
let event = match state {
ElementState::Pressed => CursorEventKind::Select,
ElementState::Released => CursorEventKind::Deselect,
};
self.panel.on_cursor_event(event, self.cursor_pos);
}
_ => {} _ => {}
} }
} }