Add `Response::total_drag_delta` and `PointerState::total_drag_delta` (#7708)

Useful in many cases. In a follow-up PR I will use it to prevent drift
when dragging/resizing windows
This commit is contained in:
Emil Ernerfeldt 2025-11-12 22:26:37 +01:00
parent 74dce787af
commit 5ae6d6d901
2 changed files with 22 additions and 2 deletions

View File

@ -1336,6 +1336,11 @@ impl PointerState {
self.press_origin self.press_origin
} }
/// How far has the pointer moved since the start of the drag (if any)?
pub fn total_drag_delta(&self) -> Option<Vec2> {
Some(self.latest_pos? - self.press_origin?)
}
/// When did the current click/drag originate? /// When did the current click/drag originate?
/// `None` if no mouse button is down. /// `None` if no mouse button is down.
#[inline(always)] #[inline(always)]

View File

@ -396,7 +396,7 @@ impl Response {
self.drag_stopped() && self.ctx.input(|i| i.pointer.button_released(button)) self.drag_stopped() && self.ctx.input(|i| i.pointer.button_released(button))
} }
/// If dragged, how many points were we dragged and in what direction? /// If dragged, how many points were we dragged in since last frame?
#[inline] #[inline]
pub fn drag_delta(&self) -> Vec2 { pub fn drag_delta(&self) -> Vec2 {
if self.dragged() { if self.dragged() {
@ -410,7 +410,22 @@ impl Response {
} }
} }
/// If dragged, how far did the mouse move? /// If dragged, how many points have we been dragged since the start of the drag?
#[inline]
pub fn total_drag_delta(&self) -> Option<Vec2> {
if self.dragged() {
let mut delta = self.ctx.input(|i| i.pointer.total_drag_delta())?;
if let Some(from_global) = self.ctx.layer_transform_from_global(self.layer_id) {
delta *= from_global.scaling;
}
Some(delta)
} else {
None
}
}
/// If dragged, how far did the mouse move since last frame?
///
/// This will use raw mouse movement if provided by the integration, otherwise will fall back to [`Response::drag_delta`] /// This will use raw mouse movement if provided by the integration, otherwise will fall back to [`Response::drag_delta`]
/// Raw mouse movement is unaccelerated and unclamped by screen boundaries, and does not relate to any position on the screen. /// Raw mouse movement is unaccelerated and unclamped by screen boundaries, and does not relate to any position on the screen.
/// This may be useful in certain situations such as draggable values and 3D cameras, where screen position does not matter. /// This may be useful in certain situations such as draggable values and 3D cameras, where screen position does not matter.