xrack_layout/
scale_rotate.rs1use crate::{Interaction, LayoutNode, Point};
2use xrack_runtime::{Length, Node, RowKey, ScaleRotateHandle, ScaleRotateSample};
3
4#[derive(Clone, Debug, PartialEq)]
5pub struct ScaleRotateTarget {
6 pub path: Vec<usize>,
7 pub handle: ScaleRotateHandle,
8 row: Option<RowKey>,
9}
10
11impl ScaleRotateTarget {
12 pub(crate) fn new(scene: &Node, path: &[usize]) -> Option<Self> {
13 let handle = scene
14 .interactive_at_path(path)?
15 .scale_rotate_handle
16 .clone()?;
17 Some(Self {
18 path: path.to_vec(),
19 handle,
20 row: scene.row_key_at(path),
21 })
22 }
23
24 pub fn is_live(&self, scene: &Node, layout: &LayoutNode, input: &Interaction) -> bool {
25 gesture_target_live(scene, layout, input, &self.path, self.row)
26 && scene
27 .interactive_at_path(&self.path)
28 .and_then(|node| node.scale_rotate_handle.as_ref())
29 == Some(&self.handle)
30 }
31
32 pub fn sample(
33 &self,
34 layout: &LayoutNode,
35 center: Point,
36 scale: f64,
37 rotation: f64,
38 ) -> Option<ScaleRotateSample> {
39 let item = layout.at_path(&self.path)?;
40 let center = item.map_from_window(center)?;
41 Some(ScaleRotateSample {
42 scale,
43 rotation,
44 center_x: Length(f64::from(center.x - item.bounds.x)),
45 center_y: Length(f64::from(center.y - item.bounds.y)),
46 })
47 }
48}
49
50pub(crate) fn gesture_target_live(
51 scene: &Node,
52 layout: &LayoutNode,
53 input: &Interaction,
54 path: &[usize],
55 row: Option<RowKey>,
56) -> bool {
57 !input.menu_open()
58 && !input.choice_open()
59 && input.allows_popup(layout, path)
60 && scene.row_key_at(path) == row
61 && layout.at_path(path).is_some_and(|item| {
62 let bounds = item.visible_window_bounds(item.bounds);
63 item.visible && item.enabled && bounds.width > 0.0 && bounds.height > 0.0
64 })
65}
66
67pub struct TouchTransform {
69 initial_span: f64,
70 last_angle: f64,
71 rotation: f64,
72}
73
74impl TouchTransform {
75 pub fn new(points: [Point; 2]) -> Option<Self> {
76 let (span, angle) = polar(points)?;
77 Some(Self {
78 initial_span: span,
79 last_angle: angle,
80 rotation: 0.0,
81 })
82 }
83
84 pub fn update(&mut self, points: [Point; 2]) -> Option<(f64, f64)> {
85 let (span, angle) = polar(points)?;
86 let delta = (angle - self.last_angle + 180.0).rem_euclid(360.0) - 180.0;
87 self.last_angle = angle;
88 self.rotation += delta;
89 Some((span / self.initial_span, self.rotation))
90 }
91}
92
93pub fn touch_centroid([a, b]: [Point; 2]) -> Point {
94 Point {
95 x: a.x * 0.5 + b.x * 0.5,
96 y: a.y * 0.5 + b.y * 0.5,
97 }
98}
99
100fn polar([a, b]: [Point; 2]) -> Option<(f64, f64)> {
101 let x = f64::from(b.x) - f64::from(a.x);
102 let y = f64::from(b.y) - f64::from(a.y);
103 let span = x.hypot(y);
104 (span >= 1.0 && span.is_finite()).then(|| (span, y.atan2(x).to_degrees()))
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn touch_rotation_accumulates_through_the_angle_boundary() {
113 let pair = |angle: f64| {
114 let angle = angle.to_radians();
115 [
116 Point::default(),
117 Point {
118 x: (40.0 * angle.cos()) as f32,
119 y: (40.0 * angle.sin()) as f32,
120 },
121 ]
122 };
123 let mut transform = TouchTransform::new(pair(170.0)).unwrap();
124 assert!(transform.update([Point::default(); 2]).is_none());
125 let (scale, rotation) = transform.update(pair(-170.0)).unwrap();
126 assert!((scale - 1.0).abs() < 1e-6);
127 assert!((rotation - 20.0).abs() < 1e-5);
128 for angle in [-90.0, 0.0, 90.0, 170.0] {
129 transform.update(pair(angle));
130 }
131 let (_, rotation) = transform.update(pair(-170.0)).unwrap();
132 assert!((rotation - 380.0).abs() < 1e-5);
133 }
134}