Skip to main content

xrack_runtime/
scale_rotate.rs

1use crate::{Length, Property};
2use std::{fmt, rc::Rc};
3
4/// A cumulative scale and clockwise rotation in degrees, with a local centroid.
5#[derive(Clone, Copy, Debug, PartialEq)]
6pub struct ScaleRotateSample {
7    pub scale: f64,
8    pub rotation: f64,
9    pub center_x: Length,
10    pub center_y: Length,
11}
12
13impl Default for ScaleRotateSample {
14    fn default() -> Self {
15        Self {
16            scale: 1.0,
17            rotation: 0.0,
18            center_x: Length(0.0),
19            center_y: Length(0.0),
20        }
21    }
22}
23
24#[derive(Clone, Default)]
25pub struct ScaleRotateHandle(Rc<Properties>);
26
27#[derive(Default)]
28struct Properties {
29    active: Property<bool>,
30    scale: Property<f64>,
31    rotation: Property<f64>,
32    center_x: Property<Length>,
33    center_y: Property<Length>,
34}
35
36impl fmt::Debug for ScaleRotateHandle {
37    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
38        formatter
39            .debug_struct("ScaleRotateHandle")
40            .finish_non_exhaustive()
41    }
42}
43
44impl PartialEq for ScaleRotateHandle {
45    fn eq(&self, other: &Self) -> bool {
46        Rc::ptr_eq(&self.0, &other.0)
47    }
48}
49
50impl ScaleRotateHandle {
51    pub fn active(&self) -> bool {
52        self.0.active.get(|| false)
53    }
54    pub fn scale(&self) -> f64 {
55        self.0.scale.get(|| 1.0)
56    }
57    pub fn rotation(&self) -> f64 {
58        self.0.rotation.get(f64::default)
59    }
60    pub fn center_x(&self) -> Length {
61        self.0.center_x.get(Length::default)
62    }
63    pub fn center_y(&self) -> Length {
64        self.0.center_y.get(Length::default)
65    }
66
67    #[doc(hidden)]
68    pub fn publish(&self, sample: Option<ScaleRotateSample>) {
69        self.0.active.set_if_changed(sample.is_some());
70        let sample = sample.unwrap_or_default();
71        self.0.scale.set_if_changed(sample.scale);
72        self.0.rotation.set_if_changed(sample.rotation);
73        self.0.center_x.set_if_changed(sample.center_x);
74        self.0.center_y.set_if_changed(sample.center_y);
75    }
76}