Skip to main content

egui_pop_viewport/
view.rs

1//! The view model: what part of the image is shown, at what zoom, and the
2//! one mapping between image and screen that drawing, hit testing and the
3//! pixel readout all use.
4//!
5//! <div class="warning">Experimental: may change in any release
6//! (docs/stability.md).</div>
7//!
8//! - **Zoom is physical**: [`View::zoom`] counts physical screen pixels
9//!   per image pixel, so 100 % is exactly `1.0` and means one image pixel
10//!   on one pixel of the window's surface, whatever the window's scale.
11//!   Each window converts with its own `pixels_per_point`, so the same
12//!   view shown in two windows is 1:1 in both.
13//! - **Whole-pixel offsets**: [`Mapping`] snaps the image's origin to a
14//!   whole physical pixel, so at 100 %, at whole zooms (200 %, 300 %) and
15//!   at their inverses no column is repeated or skipped.
16//! - **The ladder**: [`zoom_in`] and [`zoom_out`] walk [`ZOOM_LADDER`];
17//!   the wheel ([`wheel_zoom`]) is continuous but stops at 100 % when it
18//!   crosses it, so exact pixels are always one notch away.
19//!
20//! ```
21//! use egui_pop_viewport::view::{Mapping, View};
22//!
23//! let rect = egui::Rect::from_min_size(egui::pos2(10.0, 10.0), egui::vec2(400.0, 300.0));
24//! // 100 % on a window at 150 %: one image pixel per physical pixel.
25//! let view = View::actual([500.0, 400.0]);
26//! let m = Mapping::new(&view, [1000.0, 800.0], rect, 1.5);
27//! assert_eq!(m.zoom, 1.0);
28//! let p = m.image_to_screen([500.0, 400.0]);
29//! assert_eq!(m.pixel_at(p), Some([500, 400]));
30//! ```
31//!
32//! The model lives in `egui_pop_core::pointer::view`, shared with every
33//! zoomable image view (the library's comparison), so a gesture changes
34//! the same [`View`] in each.
35
36pub use egui_pop_core::pointer::view::{
37    MAX_ZOOM, MIN_ZOOM, Mapping, View, WHEEL_RATE, ZOOM_LADDER, Zoom, fit_zoom, wheel_zoom,
38    zoom_in, zoom_out,
39};
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44    use egui::{Rect, Vec2, pos2};
45
46    const SCALES: [f32; 5] = [1.0, 1.25, 1.5, 1.75, 2.0];
47
48    fn rect() -> Rect {
49        Rect::from_min_size(pos2(13.0, 7.0), Vec2::new(301.0, 203.0))
50    }
51
52    #[test]
53    fn hundred_percent_maps_each_physical_column_to_one_image_column() {
54        for ppp in SCALES {
55            for center in [[100.0, 80.0], [100.5, 80.25], [100.37, 79.91]] {
56                let m = Mapping::new(&View::actual(center), [400.0, 300.0], rect(), ppp);
57                assert_eq!(m.zoom, 1.0);
58                assert_eq!(m.origin, [m.origin[0].round(), m.origin[1].round()]);
59                let mut last = None;
60                for n in (m.rect_px[0] as i64)..(m.rect_px[2] as i64) {
61                    let pos = pos2(((n as f64 + 0.5) / f64::from(ppp)) as f32, 100.0);
62                    let Some([x, _]) = m.pixel_at(pos) else {
63                        continue;
64                    };
65                    if let Some(l) = last {
66                        assert_eq!(x, l + 1, "ppp {ppp}, column {n}");
67                    }
68                    last = Some(x);
69                }
70                assert!(last.is_some());
71            }
72        }
73    }
74
75    #[test]
76    fn whole_zooms_repeat_each_image_column_exactly_n_times() {
77        for ppp in SCALES {
78            for zoom in [2.0, 3.0, 4.0] {
79                let m = Mapping::new(&View::at(zoom, [50.3, 40.7]), [400.0, 300.0], rect(), ppp);
80                let mut runs: Vec<(i64, u32)> = Vec::new();
81                for n in (m.rect_px[0] as i64)..(m.rect_px[2] as i64) {
82                    let pos = pos2(((n as f64 + 0.5) / f64::from(ppp)) as f32, 100.0);
83                    let Some([x, _]) = m.pixel_at(pos) else {
84                        continue;
85                    };
86                    match runs.last_mut() {
87                        Some((l, c)) if *l == x => *c += 1,
88                        _ => runs.push((x, 1)),
89                    }
90                }
91                // The first and last runs may be cut by the view's edges.
92                for (_, c) in &runs[1..runs.len() - 1] {
93                    assert_eq!(f64::from(*c), zoom, "ppp {ppp}");
94                }
95            }
96        }
97    }
98
99    #[test]
100    fn image_and_screen_round_trip() {
101        for ppp in SCALES {
102            let m = Mapping::new(&View::at(0.37, [123.0, 45.0]), [800.0, 600.0], rect(), ppp);
103            let p = [321.25, 456.5];
104            let back = m.screen_to_image(m.image_to_screen(p));
105            assert!((back[0] - p[0]).abs() < 1e-3 && (back[1] - p[1]).abs() < 1e-3);
106        }
107    }
108
109    #[test]
110    fn fit_shows_the_whole_image_centred() {
111        let m = Mapping::new(&View::fit(), [1000.0, 500.0], rect(), 2.0);
112        assert!((m.zoom - 602.0 / 1000.0).abs() < 1e-9);
113        let v = m.visible().unwrap();
114        assert_eq!(v, [0.0, 0.0, 1000.0, 500.0]);
115    }
116
117    #[test]
118    fn the_ladder_walks_whole_ratios_and_the_wheel_stops_at_100() {
119        assert_eq!(zoom_in(1.0), 2.0);
120        assert_eq!(zoom_out(1.0), 2.0 / 3.0);
121        assert_eq!(zoom_in(0.9), 1.0);
122        assert_eq!(zoom_out(1.1), 1.0);
123        assert_eq!(zoom_in(32.0), 32.0);
124        assert_eq!(zoom_out(1.0 / 16.0), 1.0 / 16.0);
125        // Crossing 100 % stops on it, from either side.
126        assert_eq!(wheel_zoom(0.9, 200.0), 1.0);
127        assert_eq!(wheel_zoom(1.2, -200.0), 1.0);
128        // From 100 %, the wheel leaves it.
129        assert!(wheel_zoom(1.0, 50.0) > 1.0);
130        assert!(wheel_zoom(1.0, -50.0) < 1.0);
131    }
132
133    #[test]
134    fn zooming_around_a_point_keeps_it_under_the_pointer() {
135        for ppp in SCALES {
136            let m = Mapping::new(&View::at(0.5, [200.0, 150.0]), [400.0, 300.0], rect(), ppp);
137            let pos = pos2(100.0, 60.0);
138            let at = m.screen_to_image(pos);
139            let v = m.zoom_around(pos, 2.0);
140            let m2 = Mapping::new(&v, [400.0, 300.0], rect(), ppp);
141            let back = m2.screen_to_image(pos);
142            // Within the origin's snap of half a physical pixel.
143            assert!((back[0] - at[0]).abs() <= 0.5 / 2.0 + 1e-9);
144            assert!((back[1] - at[1]).abs() <= 0.5 / 2.0 + 1e-9);
145        }
146    }
147}