From 398718a3c80856b49422dc61169fe7b40189ee7d Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Tue, 12 Feb 2019 12:49:35 -0800 Subject: [PATCH] Allow the rotation slider to be dragged --- demo/src/main.rs | 5 +++++ demo/src/ui.rs | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/demo/src/main.rs b/demo/src/main.rs index c5b5c68d..b7a21abf 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -247,6 +247,11 @@ impl DemoApp { self.camera_pitch -= yrel as f32 * MOUSELOOK_ROTATION_SPEED; self.dirty = true; } + Event::MouseMotion { x, y, mousestate, .. } if mousestate.left() => { + let point = Point2DI32::new(x, y).scale(self.scale_factor as i32); + ui_event = UIEvent::MouseDragged(point); + self.dirty = true; + } Event::KeyDown { keycode: Some(Keycode::W), .. } => { self.camera_velocity.set_z(-CAMERA_VELOCITY); self.dirty = true; diff --git a/demo/src/ui.rs b/demo/src/ui.rs index 5cd40997..afbb1075 100644 --- a/demo/src/ui.rs +++ b/demo/src/ui.rs @@ -178,7 +178,7 @@ impl DemoUI { let (widget_x, widget_y) = (ROTATE_PANEL_X + PADDING, rotate_panel_y + PADDING); let widget_rect = RectI32::new(Point2DI32::new(widget_x, widget_y), Point2DI32::new(SLIDER_WIDTH, SLIDER_KNOB_HEIGHT)); - if let Some(position) = event.handle_mouse_down_in_rect(widget_rect) { + if let Some(position) = event.handle_mouse_down_or_dragged_in_rect(widget_rect) { self.rotation = position.x(); } @@ -267,6 +267,7 @@ impl DemoUI { pub enum UIEvent { None, MouseDown(Point2DI32), + MouseDragged(Point2DI32), } impl UIEvent { @@ -283,4 +284,15 @@ impl UIEvent { } None } + + fn handle_mouse_down_or_dragged_in_rect(&mut self, rect: RectI32) -> Option { + match *self { + UIEvent::MouseDown(point) | + UIEvent::MouseDragged(point) if rect.contains_point(point) => { + *self = UIEvent::None; + Some(point - rect.origin()) + } + _ => None, + } + } }