From aa333b0fe42c3eafbb1ec1e43997d9944046c963 Mon Sep 17 00:00:00 2001 From: mars Date: Thu, 3 Nov 2022 19:07:40 -0600 Subject: [PATCH] Add Magpie cursor input (closes #23) --- apps/magpie/src/service/window.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/apps/magpie/src/service/window.rs b/apps/magpie/src/service/window.rs index 983730f..c30dde9 100644 --- a/apps/magpie/src/service/window.rs +++ b/apps/magpie/src/service/window.rs @@ -2,10 +2,10 @@ use std::collections::HashMap; use std::path::PathBuf; 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::{glutin, Surface}; -use glutin::event::{Event, WindowEvent}; +use glutin::event::{ElementState, Event, MouseButton, WindowEvent}; use glutin::event_loop::{ControlFlow, EventLoop, EventLoopProxy, EventLoopWindowTarget}; use glutin::window::WindowId; @@ -26,6 +26,7 @@ pub struct Window { pub graphics: Graphics, pub panel: Panel, pub last_update: Instant, + pub cursor_pos: Vec2, } impl Window { @@ -42,6 +43,7 @@ impl Window { graphics, panel, last_update, + cursor_pos: Vec2::ZERO, }) } @@ -75,9 +77,26 @@ impl Window { pub fn on_event(&mut self, event: WindowEvent) { match event { 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() } + 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); + } _ => {} } }