1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Where a record sits in its parent's layer: its placement, its origin and
// its content offset, as the store keeps and reads them.
use cranpose_core::RecordId;
use cranpose_ui::{Point, Rect};
use cranpose_ui_graphics::ProjectiveTransform;
use super::{RecordKind, SceneStore, TransformId};
/// Where a record sits in its parent's layer: at a point, where layout
/// placed a record that nothing transforms, or by a transform. A transform
/// that only moves a record is a point, so two equal placements compare
/// equal.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Placement {
/// At the point, past the parent's content offset.
At(Point),
/// By the transform, which takes the record's space to its parent's.
By(ProjectiveTransform),
}
impl Placement {
/// Returns the transform from the record's space to the space that
/// `parent` maps the record's parent into.
pub fn then(self, parent: ProjectiveTransform) -> ProjectiveTransform {
match self {
Self::At(origin) => parent.after_translation(origin.x, origin.y),
Self::By(transform) => transform.then(parent),
}
}
/// The bounds in the parent's layer of `rect`, in the record's space:
/// [`ProjectiveTransform::bounds_for_rect`] of the placement's
/// transform, to the bit.
#[inline]
pub fn bounds_for_rect(self, rect: Rect) -> Rect {
match self {
// The arithmetic of a translation's transform, whose unit scale
// leaves each coordinate as it is.
Self::At(origin) => {
let x0 = rect.x + origin.x;
let x1 = (rect.x + rect.width) + origin.x;
let y0 = rect.y + origin.y;
let y1 = (rect.y + rect.height) + origin.y;
Rect {
x: x0.min(x1),
y: y0.min(y1),
width: (x1 - x0).abs(),
height: (y1 - y0).abs(),
}
}
Self::By(transform) => transform.bounds_for_rect(rect),
}
}
/// The point the placement moves the record's origin to, when it only
/// moves the record.
#[inline]
pub fn as_translation(self) -> Option<Point> {
match self {
Self::At(origin) => Some(origin),
Self::By(transform) => transform.as_translation(),
}
}
}
#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
impl SceneStore {
/// The offset of the content box of `id` from its origin.
#[inline]
pub fn content_offset(&self, id: RecordId) -> Point {
let [x, y] = self.warm[id.index()].content_offset;
Point::new(x, y)
}
/// Sets the offset of the content box of `id`, and returns whether it
/// changed.
#[inline]
pub fn set_content_offset(&mut self, id: RecordId, offset: Point) -> bool {
let index = id.index();
let offset = [offset.x, offset.y];
let changed = !super::records::same_pair(self.warm[index].content_offset, offset);
if changed {
self.warm[index].content_offset = offset;
self.stale(index);
self.moved(index);
}
changed
}
/// Where layout placed `id` in its parent's content, before the
/// parent's content offset.
#[inline]
pub fn origin_in_parent(&self, id: RecordId) -> Point {
let [x, y] = self.geo[id.index()].placement;
Point::new(x, y)
}
/// The top left of `id` in its parent's layer: its placement and the
/// parent's content offset.
#[inline]
pub fn origin(&self, id: RecordId) -> Point {
let index = id.index();
match self.tree[index].transform {
Some(transform) => self.transforms[transform.index()]
.as_translation()
.unwrap_or_else(|| self.placed_origin(index)),
None => self.placed_origin(index),
}
}
// Where the placement of the record at `index` puts it in its parent's
// layer, past the parent's content offset.
#[inline]
fn placed_origin(&self, index: usize) -> Point {
let [x, y] = self.geo[index].placement;
let offset = self.parent_content_offset(index);
match self.tree[index].kind {
RecordKind::Leaf => Point::new(x + offset.x, y + offset.y),
// A layer sums its origin as `placed_transform` does, with its
// graphics layer's translation, zero where the store keeps no
// transform: the sum turns a negative zero positive, so the
// origin keeps the bits a transform of the layer would hold.
RecordKind::Layer | RecordKind::Wrapper => {
Point::new(x + 0.0 + offset.x, y + 0.0 + offset.y)
}
// A draw group lies in its node's own space, where the content
// offset does not move it.
RecordKind::DrawGroup => Point::new(x, y),
}
}
#[inline]
fn parent_content_offset(&self, index: usize) -> Point {
self.tree[index]
.parent
.map_or_default(|parent| self.content_offset(parent))
}
#[expect(
clippy::inline_always,
reason = "the collect walk and the update call it for every record; out of line the gauntlet frame took 2% more instructions"
)]
/// The transform from `id` to its parent's content.
#[inline(always)]
pub fn transform_to_parent(&self, id: RecordId) -> ProjectiveTransform {
match self.tree[id.index()].transform {
Some(transform) => self.transforms[transform.index()],
None => {
let origin = self.placed_origin(id.index());
ProjectiveTransform::translation(origin.x, origin.y)
}
}
}
#[expect(
clippy::inline_always,
reason = "the pixel push reads it for every record; out of line it built the placement in memory"
)]
/// Where `id` sits in its parent's layer.
#[inline(always)]
pub fn placement(&self, id: RecordId) -> Placement {
match self.tree[id.index()].transform {
Some(transform) => Placement::By(self.transforms[transform.index()]),
None => Placement::At(self.placed_origin(id.index())),
}
}
/// Places `id` at `origin_in_parent` in its parent's content by
/// `transform`, or by a translation there alone, past the parent's
/// content offset, for `None`. A transform that only moves the record
/// there takes no entry of its own. The parent's content offset is set
/// before. Returns whether the placement changed.
pub fn set_placement(
&mut self,
id: RecordId,
origin_in_parent: Point,
transform: Option<ProjectiveTransform>,
) -> bool {
let index = id.index();
let placement = [origin_in_parent.x, origin_in_parent.y];
let moved = !super::records::same_pair(self.geo[index].placement, placement);
self.geo[index].placement = placement;
// An entry holds a transform that does more than place the record.
let transform = transform.filter(|transform| {
let origin = self.placed_origin(index);
!super::records::same_bits(
*transform,
ProjectiveTransform::translation(origin.x, origin.y),
)
});
let entry = self.tree[index].transform;
let unchanged = !moved
&& match (entry, transform) {
(Some(entry), Some(transform)) => {
super::records::same_bits(self.transforms[entry.index()], transform)
}
(None, None) => true,
(Some(_), None) | (None, Some(_)) => false,
};
if unchanged {
return false;
}
self.moved(index);
if let Some(parent) = self.tree[index].parent {
self.stale(parent.index());
}
match (transform, entry) {
(None, Some(old)) => {
self.tree[index].transform = None;
self.transforms.remove(old.index());
}
(None, None) => {}
(Some(transform), Some(old)) => self.transforms[old.index()] = transform,
(Some(transform), None) => {
let entry = TransformId::from_index(self.transforms.insert(transform));
self.tree[index].transform = Some(entry);
}
}
true
}
}