From 227bd6a2027d2c0c3e52b42e7e2f93f0d8399d1b Mon Sep 17 00:00:00 2001 From: Francis Chua Date: Thu, 1 Feb 2024 09:56:08 -0800 Subject: [PATCH] add invert method for TSTransform --- crates/egui/src/context.rs | 2 +- crates/egui_demo_lib/src/demo/pan_zoom.rs | 4 ++-- crates/emath/src/ts_transform.rs | 25 +++++------------------ 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 90e72ff2145..b9a233273aa 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2181,7 +2181,7 @@ impl Context { let pointer_pos = viewport.input.pointer.interact_pos(); if let Some(pointer_pos) = pointer_pos { // Apply the inverse transformation of this layer to the pointer pos. - let pointer_pos = transform.invert_pos(pointer_pos); + let pointer_pos = transform.inverse() * pointer_pos; if let Some(rects) = viewport.layer_rects_prev_frame.get(&layer_id) { for blocking in rects.iter().rev() { if blocking.id == id { diff --git a/crates/egui_demo_lib/src/demo/pan_zoom.rs b/crates/egui_demo_lib/src/demo/pan_zoom.rs index 7b1c83e45b7..224323194b7 100644 --- a/crates/egui_demo_lib/src/demo/pan_zoom.rs +++ b/crates/egui_demo_lib/src/demo/pan_zoom.rs @@ -42,7 +42,7 @@ impl super::View for PanZoom { let original_zoom = self.transform.scaling; let new_zoom = original_zoom * ui.ctx().input(|i| i.zoom_delta()); - let layer_pos = self.transform.invert_pos(pointer); + let layer_pos = self.transform.inverse() * pointer; let new_transform = TSTransform::new(self.transform.translation, new_zoom); let new_pos = new_transform * layer_pos; @@ -83,7 +83,7 @@ impl super::View for PanZoom { // but may also cover over other windows. .order(egui::Order::Foreground) .show(ui.ctx(), |ui| { - ui.set_clip_rect(self.transform.invert_rect(rect)); + ui.set_clip_rect(self.transform.inverse() * rect); egui::Frame::default() .rounding(egui::Rounding::same(4.0)) .inner_margin(egui::Margin::same(8.0)) diff --git a/crates/emath/src/ts_transform.rs b/crates/emath/src/ts_transform.rs index 014b3f0060a..e6f02cfc4ca 100644 --- a/crates/emath/src/ts_transform.rs +++ b/crates/emath/src/ts_transform.rs @@ -57,28 +57,13 @@ impl TSTransform { /// let p1 = pos2(2.0, 3.0); /// let p2 = pos2(12.0, 5.0); /// let ts = TSTransform::new(vec2(2.0, 3.0), 2.0); - /// assert_eq!(ts.invert_pos(p1), pos2(0.0, 0.0)); - /// assert_eq!(ts.invert_pos(p2), pos2(5.0, 1.0)); + /// let inv = ts.inverse(); + /// assert_eq!(inv.mul_pos(p1), pos2(0.0, 0.0)); + /// assert_eq!(inv.mul_pos(p2), pos2(5.0, 1.0)); /// ``` #[inline] - pub fn invert_pos(&self, pos: Pos2) -> Pos2 { - // First, reverse translation, then reverse scaling. - (pos - self.translation) / self.scaling - } - - /// Reverses the transformation from screen space to layer space. - /// - /// ``` - /// # use emath::{pos2, vec2, Rect, TSTransform}; - /// let rect = Rect::from_min_max(pos2(16.0, 15.0), pos2(46.0, 30.0)); - /// let ts = TSTransform::new(vec2(1.0, 0.0), 3.0); - /// let inverted = ts.invert_rect(rect); - /// assert_eq!(inverted.min, pos2(5.0, 5.0)); - /// assert_eq!(inverted.max, pos2(15.0, 10.0)); - /// ``` - #[inline] - pub fn invert_rect(&self, rect: Rect) -> Rect { - Rect::from_min_max(self.invert_pos(rect.min), self.invert_pos(rect.max)) + pub fn inverse(&self) -> Self { + Self::new(-self.translation / self.scaling, 1.0 / self.scaling) } /// Transforms the given coordinate by translation then scaling.