Skip to content

Commit

Permalink
add invert method for TSTransform
Browse files Browse the repository at this point in the history
  • Loading branch information
Tweoss committed Feb 1, 2024
1 parent e593bbf commit 227bd6a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_lib/src/demo/pan_zoom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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))
Expand Down
25 changes: 5 additions & 20 deletions crates/emath/src/ts_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 227bd6a

Please sign in to comment.