1pub 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 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 assert_eq!(wheel_zoom(0.9, 200.0), 1.0);
127 assert_eq!(wheel_zoom(1.2, -200.0), 1.0);
128 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 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}