Skip to main content

cranpose_render_common/scene_store/
placement.rs

1// Where a record sits in its parent's layer: its placement, its origin and
2// its content offset, as the store keeps and reads them.
3
4use cranpose_core::RecordId;
5use cranpose_ui::{Point, Rect};
6use cranpose_ui_graphics::ProjectiveTransform;
7
8use super::{RecordKind, SceneStore, TransformId};
9
10/// Where a record sits in its parent's layer: at a point, where layout
11/// placed a record that nothing transforms, or by a transform. A transform
12/// that only moves a record is a point, so two equal placements compare
13/// equal.
14#[derive(Clone, Copy, Debug, PartialEq)]
15pub enum Placement {
16    /// At the point, past the parent's content offset.
17    At(Point),
18    /// By the transform, which takes the record's space to its parent's.
19    By(ProjectiveTransform),
20}
21
22impl Placement {
23    /// Returns the transform from the record's space to the space that
24    /// `parent` maps the record's parent into.
25    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    /// The bounds in the parent's layer of `rect`, in the record's space:
33    /// [`ProjectiveTransform::bounds_for_rect`] of the placement's
34    /// transform, to the bit.
35    #[inline]
36    pub fn bounds_for_rect(self, rect: Rect) -> Rect {
37        match self {
38            // The arithmetic of a translation's transform, whose unit scale
39            // leaves each coordinate as it is.
40            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    /// The point the placement moves the record's origin to, when it only
57    /// moves the record.
58    #[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    /// The offset of the content box of `id` from its origin.
70    #[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    /// Sets the offset of the content box of `id`, and returns whether it
77    /// changed.
78    #[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    /// Where layout placed `id` in its parent's content, before the
92    /// parent's content offset.
93    #[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    /// The top left of `id` in its parent's layer: its placement and the
100    /// parent's content offset.
101    #[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    // Where the placement of the record at `index` puts it in its parent's
113    // layer, past the parent's content offset.
114    #[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            // A layer sums its origin as `placed_transform` does, with its
121            // graphics layer's translation, zero where the store keeps no
122            // transform: the sum turns a negative zero positive, so the
123            // origin keeps the bits a transform of the layer would hold.
124            RecordKind::Layer | RecordKind::Wrapper => {
125                Point::new(x + 0.0 + offset.x, y + 0.0 + offset.y)
126            }
127            // A draw group lies in its node's own space, where the content
128            // offset does not move it.
129            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    /// The transform from `id` to its parent's content.
145    #[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    /// Where `id` sits in its parent's layer.
161    #[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    /// Places `id` at `origin_in_parent` in its parent's content by
170    /// `transform`, or by a translation there alone, past the parent's
171    /// content offset, for `None`. A transform that only moves the record
172    /// there takes no entry of its own. The parent's content offset is set
173    /// before. Returns whether the placement changed.
174    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        // An entry holds a transform that does more than place the record.
185        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}