cranpose_render_common/scene_store/
placement.rs1use cranpose_core::RecordId;
5use cranpose_ui::{Point, Rect};
6use cranpose_ui_graphics::ProjectiveTransform;
7
8use super::{RecordKind, SceneStore, TransformId};
9
10#[derive(Clone, Copy, Debug, PartialEq)]
15pub enum Placement {
16 At(Point),
18 By(ProjectiveTransform),
20}
21
22impl Placement {
23 pub fn then(self, parent: ProjectiveTransform) -> ProjectiveTransform {
26 match self {
27 Self::At(origin) => parent.after_translation(origin.x, origin.y),
28 Self::By(transform) => transform.then(parent),
29 }
30 }
31
32 #[inline]
36 pub fn bounds_for_rect(self, rect: Rect) -> Rect {
37 match self {
38 Self::At(origin) => {
41 let x0 = rect.x + origin.x;
42 let x1 = (rect.x + rect.width) + origin.x;
43 let y0 = rect.y + origin.y;
44 let y1 = (rect.y + rect.height) + origin.y;
45 Rect {
46 x: x0.min(x1),
47 y: y0.min(y1),
48 width: (x1 - x0).abs(),
49 height: (y1 - y0).abs(),
50 }
51 }
52 Self::By(transform) => transform.bounds_for_rect(rect),
53 }
54 }
55
56 #[inline]
59 pub fn as_translation(self) -> Option<Point> {
60 match self {
61 Self::At(origin) => Some(origin),
62 Self::By(transform) => transform.as_translation(),
63 }
64 }
65}
66
67#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
68impl SceneStore {
69 #[inline]
71 pub fn content_offset(&self, id: RecordId) -> Point {
72 let [x, y] = self.warm[id.index()].content_offset;
73 Point::new(x, y)
74 }
75
76 #[inline]
79 pub fn set_content_offset(&mut self, id: RecordId, offset: Point) -> bool {
80 let index = id.index();
81 let offset = [offset.x, offset.y];
82 let changed = !super::records::same_pair(self.warm[index].content_offset, offset);
83 if changed {
84 self.warm[index].content_offset = offset;
85 self.stale(index);
86 self.moved(index);
87 }
88 changed
89 }
90
91 #[inline]
94 pub fn origin_in_parent(&self, id: RecordId) -> Point {
95 let [x, y] = self.geo[id.index()].placement;
96 Point::new(x, y)
97 }
98
99 #[inline]
102 pub fn origin(&self, id: RecordId) -> Point {
103 let index = id.index();
104 match self.tree[index].transform {
105 Some(transform) => self.transforms[transform.index()]
106 .as_translation()
107 .unwrap_or_else(|| self.placed_origin(index)),
108 None => self.placed_origin(index),
109 }
110 }
111
112 #[inline]
115 fn placed_origin(&self, index: usize) -> Point {
116 let [x, y] = self.geo[index].placement;
117 let offset = self.parent_content_offset(index);
118 match self.tree[index].kind {
119 RecordKind::Leaf => Point::new(x + offset.x, y + offset.y),
120 RecordKind::Layer | RecordKind::Wrapper => {
125 Point::new(x + 0.0 + offset.x, y + 0.0 + offset.y)
126 }
127 RecordKind::DrawGroup => Point::new(x, y),
130 }
131 }
132
133 #[inline]
134 fn parent_content_offset(&self, index: usize) -> Point {
135 self.tree[index]
136 .parent
137 .map_or_default(|parent| self.content_offset(parent))
138 }
139
140 #[expect(
141 clippy::inline_always,
142 reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
143 )]
144 #[inline(always)]
146 pub fn transform_to_parent(&self, id: RecordId) -> ProjectiveTransform {
147 match self.tree[id.index()].transform {
148 Some(transform) => self.transforms[transform.index()],
149 None => {
150 let origin = self.placed_origin(id.index());
151 ProjectiveTransform::translation(origin.x, origin.y)
152 }
153 }
154 }
155
156 #[expect(
157 clippy::inline_always,
158 reason = "the pixel push reads it for every record; out of line it built the placement in memory"
159 )]
160 #[inline(always)]
162 pub fn placement(&self, id: RecordId) -> Placement {
163 match self.tree[id.index()].transform {
164 Some(transform) => Placement::By(self.transforms[transform.index()]),
165 None => Placement::At(self.placed_origin(id.index())),
166 }
167 }
168
169 pub fn set_placement(
175 &mut self,
176 id: RecordId,
177 origin_in_parent: Point,
178 transform: Option<ProjectiveTransform>,
179 ) -> bool {
180 let index = id.index();
181 let placement = [origin_in_parent.x, origin_in_parent.y];
182 let moved = !super::records::same_pair(self.geo[index].placement, placement);
183 self.geo[index].placement = placement;
184 let transform = transform.filter(|transform| {
186 let origin = self.placed_origin(index);
187 !super::records::same_bits(
188 *transform,
189 ProjectiveTransform::translation(origin.x, origin.y),
190 )
191 });
192 let entry = self.tree[index].transform;
193 let unchanged = !moved
194 && match (entry, transform) {
195 (Some(entry), Some(transform)) => {
196 super::records::same_bits(self.transforms[entry.index()], transform)
197 }
198 (None, None) => true,
199 (Some(_), None) | (None, Some(_)) => false,
200 };
201 if unchanged {
202 return false;
203 }
204 self.moved(index);
205 if let Some(parent) = self.tree[index].parent {
206 self.stale(parent.index());
207 }
208 match (transform, entry) {
209 (None, Some(old)) => {
210 self.tree[index].transform = None;
211 self.transforms.remove(old.index());
212 }
213 (None, None) => {}
214 (Some(transform), Some(old)) => self.transforms[old.index()] = transform,
215 (Some(transform), None) => {
216 let entry = TransformId::from_index(self.transforms.insert(transform));
217 self.tree[index].transform = Some(entry);
218 }
219 }
220 true
221 }
222}