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;
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
33#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
34impl SceneStore {
35    /// The offset of the content box of `id` from its origin.
36    #[inline]
37    pub fn content_offset(&self, id: RecordId) -> Point {
38        let [x, y] = self.warm[id.index()].content_offset;
39        Point::new(x, y)
40    }
41
42    /// Sets the offset of the content box of `id`, and returns whether it
43    /// changed.
44    #[inline]
45    pub fn set_content_offset(&mut self, id: RecordId, offset: Point) -> bool {
46        let index = id.index();
47        let offset = [offset.x, offset.y];
48        let changed = !super::records::same_pair(self.warm[index].content_offset, offset);
49        if changed {
50            self.warm[index].content_offset = offset;
51            self.stale(index);
52            self.moved(index);
53        }
54        changed
55    }
56
57    /// Where layout placed `id` in its parent's content, before the
58    /// parent's content offset.
59    #[inline]
60    pub fn origin_in_parent(&self, id: RecordId) -> Point {
61        let [x, y] = self.geo[id.index()].placement;
62        Point::new(x, y)
63    }
64
65    /// The top left of `id` in its parent's layer: its placement and the
66    /// parent's content offset.
67    #[inline]
68    pub fn origin(&self, id: RecordId) -> Point {
69        let index = id.index();
70        match self.tree[index].transform {
71            Some(transform) => self.transforms[transform.index()]
72                .as_translation()
73                .unwrap_or_else(|| self.placed_origin(index)),
74            None => self.placed_origin(index),
75        }
76    }
77
78    // Where the placement of the record at `index` puts it in its parent's
79    // layer, past the parent's content offset.
80    #[inline]
81    fn placed_origin(&self, index: usize) -> Point {
82        let [x, y] = self.geo[index].placement;
83        let offset = self.parent_content_offset(index);
84        match self.tree[index].kind {
85            RecordKind::Leaf => Point::new(x + offset.x, y + offset.y),
86            // A layer sums its origin as `placed_transform` does, with its
87            // graphics layer's translation, zero where the store keeps no
88            // transform: the sum turns a negative zero positive, so the
89            // origin keeps the bits a transform of the layer would hold.
90            RecordKind::Layer | RecordKind::Wrapper => {
91                Point::new(x + 0.0 + offset.x, y + 0.0 + offset.y)
92            }
93            // A draw group lies in its node's own space, where the content
94            // offset does not move it.
95            RecordKind::DrawGroup => Point::new(x, y),
96        }
97    }
98
99    #[inline]
100    fn parent_content_offset(&self, index: usize) -> Point {
101        self.tree[index]
102            .parent
103            .map(|parent| self.content_offset(parent))
104            .unwrap_or_default()
105    }
106
107    #[expect(
108        clippy::inline_always,
109        reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
110    )]
111    /// The transform from `id` to its parent's content.
112    #[inline(always)]
113    pub fn transform_to_parent(&self, id: RecordId) -> ProjectiveTransform {
114        match self.tree[id.index()].transform {
115            Some(transform) => self.transforms[transform.index()],
116            None => {
117                let origin = self.placed_origin(id.index());
118                ProjectiveTransform::translation(origin.x, origin.y)
119            }
120        }
121    }
122
123    #[expect(
124        clippy::inline_always,
125        reason = "the pixel push reads it for every record; out of line it built the placement in memory"
126    )]
127    /// Where `id` sits in its parent's layer.
128    #[inline(always)]
129    pub fn placement(&self, id: RecordId) -> Placement {
130        match self.tree[id.index()].transform {
131            Some(transform) => Placement::By(self.transforms[transform.index()]),
132            None => Placement::At(self.placed_origin(id.index())),
133        }
134    }
135
136    /// Places `id` at `origin_in_parent` in its parent's content by
137    /// `transform`, or by a translation there alone, past the parent's
138    /// content offset, for `None`. A transform that only moves the record
139    /// there takes no entry of its own. The parent's content offset is set
140    /// before. Returns whether the placement changed.
141    pub fn set_placement(
142        &mut self,
143        id: RecordId,
144        origin_in_parent: Point,
145        transform: Option<ProjectiveTransform>,
146    ) -> bool {
147        let index = id.index();
148        let placement = [origin_in_parent.x, origin_in_parent.y];
149        let moved = !super::records::same_pair(self.geo[index].placement, placement);
150        self.geo[index].placement = placement;
151        // An entry holds a transform that does more than place the record.
152        let transform = transform.filter(|transform| {
153            let origin = self.placed_origin(index);
154            !super::records::same_bits(
155                *transform,
156                ProjectiveTransform::translation(origin.x, origin.y),
157            )
158        });
159        let entry = self.tree[index].transform;
160        let unchanged = !moved
161            && match (entry, transform) {
162                (Some(entry), Some(transform)) => {
163                    super::records::same_bits(self.transforms[entry.index()], transform)
164                }
165                (None, None) => true,
166                (Some(_), None) | (None, Some(_)) => false,
167            };
168        if unchanged {
169            return false;
170        }
171        self.moved(index);
172        if let Some(parent) = self.tree[index].parent {
173            self.stale(parent.index());
174        }
175        match (transform, entry) {
176            (None, Some(old)) => {
177                self.tree[index].transform = None;
178                self.transforms.remove(old.index());
179            }
180            (None, None) => {}
181            (Some(transform), Some(old)) => self.transforms[old.index()] = transform,
182            (Some(transform), None) => {
183                let entry = TransformId::from_index(self.transforms.insert(transform));
184                self.tree[index].transform = Some(entry);
185            }
186        }
187        true
188    }
189}