From b30c49f409144307a0f9fe176b76562e0637fd9b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 23 May 2020 11:39:25 +0200 Subject: [PATCH] [input] keep track of mouse press origin --- emigui/src/containers/window.rs | 4 +--- emigui/src/input.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/emigui/src/containers/window.rs b/emigui/src/containers/window.rs index a3cd7f3e..6f5281af 100644 --- a/emigui/src/containers/window.rs +++ b/emigui/src/containers/window.rs @@ -267,7 +267,6 @@ struct PossibleInteractions { pub(crate) struct WindowInteraction { pub(crate) area_layer: Layer, pub(crate) start_rect: Rect, - pub(crate) start_mouse_pos: Pos2, pub(crate) left: bool, pub(crate) right: bool, pub(crate) top: bool, @@ -345,7 +344,7 @@ fn resize_window(ctx: &Context, window_interaction: &WindowInteraction) -> Optio } } else { // movevement - rect = rect.translate(mouse_pos - window_interaction.start_mouse_pos); + rect = rect.translate(mouse_pos - ctx.input().mouse.press_origin?); } return Some(rect); @@ -444,7 +443,6 @@ fn resize_hover( Some(WindowInteraction { area_layer, start_rect: rect, - start_mouse_pos: mouse_pos, left, right, top, diff --git a/emigui/src/input.rs b/emigui/src/input.rs index 52ba998b..4e0c1b9e 100644 --- a/emigui/src/input.rs +++ b/emigui/src/input.rs @@ -81,6 +81,9 @@ pub struct MouseInput { /// None for touch screens when finger is not down. pub pos: Option, + /// Where did the current click/drag originate? + pub press_origin: Option, + /// How much the mouse moved compared to last frame, in points. pub delta: Vec2, @@ -99,6 +102,7 @@ impl Default for MouseInput { pressed: false, released: false, pos: None, + press_origin: None, delta: Vec2::zero(), velocity: Vec2::zero(), pos_tracker: MovementTracker::new(1000, 0.1), @@ -172,6 +176,13 @@ impl MouseInput { .unwrap_or_default(); let pressed = !self.down && new.mouse_down; + let mut press_origin = self.press_origin; + if pressed { + press_origin = new.mouse_pos; + } else if !self.down || self.pos.is_none() { + press_origin = None; + } + if let Some(mouse_pos) = new.mouse_pos { self.pos_tracker.add(new.time, mouse_pos); } else { @@ -187,6 +198,7 @@ impl MouseInput { pressed, released: self.down && !new.mouse_down, pos: new.mouse_pos, + press_origin, delta, velocity, pos_tracker: self.pos_tracker, @@ -253,6 +265,7 @@ impl MouseInput { ui.add(label!("pressed: {}", self.pressed)); ui.add(label!("released: {}", self.released)); ui.add(label!("pos: {:?}", self.pos)); + ui.add(label!("press_origin: {:?}", self.press_origin)); ui.add(label!("delta: {:?}", self.delta)); ui.add(label!( "velocity: [{:3.0} {:3.0}] points/sec",