mgui_core/render_object/
transform.rs1use super::*;
2
3pub struct TransformRenderObject {
16 pub child: Option<SharedRO>,
17 pub element_id: Option<ElementId>,
19 pub size: Size,
21 pub transform: crate::Transform2D,
23}
24
25impl TransformRenderObject {
26 pub fn new(child: Box<dyn RenderObject>, transform: crate::Transform2D) -> Self {
27 Self { child: Some(make_ro_shared(child)), element_id: None, size: Size::ZERO, transform }
28 }
29 pub fn empty(transform: crate::Transform2D) -> Self {
30 Self { child: None, element_id: None, size: Size::ZERO, transform }
31 }
32}
33
34impl RenderObject for TransformRenderObject {
35 fn layout(&mut self, constraints: BoxConstraints, ctx: &mut LayoutContext) -> Size {
36 self.size = self.child.as_mut().map(|c| c.layout_cached(constraints, ctx)).unwrap_or(Size::ZERO);
37 self.size
38 }
39 fn paint(&self, _offset: Point, _painter: &mut PaintList) {
40 }
42 fn paint_as_layer_root(&self, offset: Point, painter: &mut PaintList) {
43 if let Some(c) = &self.child {
46 c.borrow().paint(offset, painter);
47 }
48 }
49 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
50 let child = self.child.as_ref()?;
55 let s = self.size;
56 if s.width <= 0.0 || s.height <= 0.0 {
57 return child.borrow().hit_test(position, offset);
58 }
59 let cx = offset.x + s.width * 0.5;
60 let cy = offset.y + s.height * 0.5;
61 let local = inverse_apply_transform(position, self.transform, cx, cy);
63 child.borrow().hit_test(local, offset)
66 }
67 fn set_element_id(&mut self, _id: ElementId) {
68 }
70 fn child_slots(&self) -> Vec<(Point, SharedRO)> {
73 self.child.as_ref().map(|c| vec![(Point::ZERO, c.clone())]).unwrap_or_default()
74 }
75
76 fn for_each_child(&self, f: &mut dyn FnMut(Point, &SharedRO)) {
77 if let Some(c) = &self.child {
78 f(Point::ZERO, c);
79 }
80 }
81 fn propagate_element_ids(&mut self, child_ids: &[ElementId], stateful_ros: &mut [SharedRO]) {
82 if let (Some(child_ro), Some(&cid)) = (self.child.as_mut(), child_ids.first()) {
83 if let Some(new_ro) = stateful_ros.first_mut() {
84 *child_ro = new_ro.clone();
85 }
86 child_ro.borrow_mut().set_element_id(cid);
87 }
88 }
89 fn content_size(&self) -> Size {
90 self.size
91 }
92 fn layer_layout_size(&self) -> Option<Size> {
93 Some(self.size)
94 }
95 fn push_child(&mut self, _offset: Point, _child: SharedRO) {
96 }
98 fn children_count(&self) -> usize {
99 if self.child.is_some() {
100 1
101 } else {
102 0
103 }
104 }
105 fn layer_effect(&self) -> crate::paint::LayerEffect {
106 crate::paint::LayerEffect::Transform(self.transform)
107 }
108
109 fn apply_state(&mut self, state: &mut dyn std::any::Any) -> StateApply {
115 let s = match state.downcast_ref::<crate::widget::animated::AnimatedScaleState>() {
116 Some(s) => s,
117 None => return StateApply::Unsupported,
118 };
119 let (sx, sy) = s.presented_scale();
120 if (sx - self.transform.scale_x).abs() < f32::EPSILON && (sy - self.transform.scale_y).abs() < f32::EPSILON {
121 return StateApply::Unchanged;
122 }
123 self.transform.scale_x = sx;
124 self.transform.scale_y = sy;
125 StateApply::Updated
126 }
127}