Skip to main content

cranpose_render_common/scene_store/
record_props.rs

1// The properties a record keeps beyond its fixed-size parts: a graphics
2// layer, pointer handling, layer bounds and what else `LayerProps` holds.
3
4use cranpose_core::RecordId;
5use cranpose_ui::{GraphicsLayer, Rect};
6
7use super::{GraphicsLayerId, LayerProps, PropsId, RecordFlags, SceneStore};
8use crate::graph::HitTestNode;
9
10#[expect(clippy::multiple_inherent_impl, reason = "one file per concern")]
11impl SceneStore {
12    /// The properties of `id`, when it has any.
13    #[inline]
14    pub fn props(&self, id: RecordId) -> Option<&LayerProps> {
15        let index = id.index();
16        if !self.tree[index].flags.contains(RecordFlags::HAS_PROPS) {
17            return None;
18        }
19        self.warm[index]
20            .props
21            .map(|props| &self.props[props.index()])
22    }
23
24    // Removes the properties and the graphics layer of `id` for the scene
25    // builder, which notes the change itself.
26    pub(crate) fn strip_props(&mut self, id: RecordId) {
27        let index = id.index();
28        if self.warm[index].props.is_some() || self.warm[index].graphics_layer.is_some() {
29            self.drop_props(index);
30            super::facts::forget_children(self, index);
31            self.stale_with_parent(index);
32        }
33    }
34
35    // Removes the properties and the graphics layer of the record at
36    // `index`.
37    pub(super) fn drop_props(&mut self, index: usize) {
38        if let Some(entry) = self.warm[index].props.take() {
39            self.props.remove(entry.index());
40        }
41        if let Some(slot) = self.warm[index].graphics_layer.take() {
42            self.graphics_layers.remove(slot.index());
43        }
44        self.tree[index].flags = self.tree[index].flags.without(
45            RecordFlags::HAS_PROPS
46                | RecordFlags::HAS_GRAPHICS_LAYER
47                | RecordFlags::HAS_LAYER_BOUNDS,
48        );
49    }
50
51    // Sets the flags that tell whether the record at `index` has properties
52    // and layer bounds, from them.
53    fn sync_props_flags(&mut self, index: usize) {
54        let props = self.warm[index]
55            .props
56            .map(|entry| &self.props[entry.index()]);
57        let bounds = props.is_some_and(|props| props.layer_bounds.is_some());
58        let flags = self.tree[index]
59            .flags
60            .without(RecordFlags::HAS_PROPS | RecordFlags::HAS_LAYER_BOUNDS);
61        let flags = if props.is_some() {
62            flags.with(RecordFlags::HAS_PROPS)
63        } else {
64            flags
65        };
66        self.tree[index].flags = if bounds {
67            flags.with(RecordFlags::HAS_LAYER_BOUNDS)
68        } else {
69            flags
70        };
71    }
72
73    /// The graphics layer set on `id`, `None` when it reads as the default.
74    #[inline]
75    pub fn graphics_layer(&self, id: RecordId) -> Option<&GraphicsLayer> {
76        let index = id.index();
77        if !self.tree[index]
78            .flags
79            .contains(RecordFlags::HAS_GRAPHICS_LAYER)
80        {
81            return None;
82        }
83        self.warm[index]
84            .graphics_layer
85            .map(|slot| &self.graphics_layers[slot.index()])
86    }
87
88    /// Sets the graphics layer of `id`; `None` reads as the default. A record
89    /// that keeps one keeps its slot. A changed layer notes a property
90    /// change.
91    pub fn set_graphics_layer(&mut self, id: RecordId, layer: Option<&GraphicsLayer>) {
92        if self.graphics_layer(id) != layer {
93            self.write_graphics_layer(id, layer);
94            self.note_property_change(id);
95        }
96    }
97
98    // Writes the graphics layer of `id` for the scene builder, which notes
99    // the change itself; `false` when it stays.
100    #[inline]
101    pub(crate) fn write_graphics_layer(
102        &mut self,
103        id: RecordId,
104        layer: Option<&GraphicsLayer>,
105    ) -> bool {
106        // Most records keep no graphics layer.
107        (layer.is_some() || self.warm[id.index()].graphics_layer.is_some())
108            && self.replace_graphics_layer(id, layer)
109    }
110
111    #[inline(never)]
112    fn replace_graphics_layer(&mut self, id: RecordId, layer: Option<&GraphicsLayer>) -> bool {
113        let index = id.index();
114        let slot = self.warm[index].graphics_layer;
115        let space =
116            |layer: Option<&GraphicsLayer>| layer.is_some_and(|layer| layer.space.is_some());
117        if space(slot.map(|slot| &self.graphics_layers[slot.index()])) != space(layer) {
118            super::facts::forget_children(self, id.index());
119        }
120        match (slot, layer) {
121            (Some(slot), Some(layer)) => {
122                let current = &self.graphics_layers[slot.index()];
123                if current == layer {
124                    return false;
125                }
126                if super::facts::layer_inputs(current) != super::facts::layer_inputs(layer) {
127                    self.stale_with_parent(id.index());
128                }
129                self.graphics_layers[slot.index()].clone_from(layer);
130            }
131            (Some(slot), None) => {
132                self.graphics_layers.remove(slot.index());
133                self.warm[index].graphics_layer = None;
134                let flags = &mut self.tree[index].flags;
135                *flags = flags.without(RecordFlags::HAS_GRAPHICS_LAYER);
136                self.stale_with_parent(index);
137            }
138            (None, Some(layer)) => {
139                let slot = GraphicsLayerId::from_index(self.graphics_layers.insert(layer.clone()));
140                self.warm[index].graphics_layer = Some(slot);
141                let flags = &mut self.tree[index].flags;
142                *flags = flags.with(RecordFlags::HAS_GRAPHICS_LAYER);
143                self.stale_with_parent(index);
144            }
145            (None, None) => return false,
146        }
147        true
148    }
149
150    // Writes the layer bounds of `id` for the scene builder, which notes the
151    // change itself; `false` when they stay.
152    pub(crate) fn write_layer_bounds(&mut self, id: RecordId, bounds: Option<Rect>) -> bool {
153        let bits = |bounds: Option<Rect>| {
154            bounds.map(|bounds| [bounds.x, bounds.y, bounds.width, bounds.height].map(f32::to_bits))
155        };
156        let changed = bits(self.props(id).and_then(|props| props.layer_bounds)) != bits(bounds);
157        if changed {
158            self.edit_props(id, |props| props.layer_bounds = bounds);
159        }
160        changed
161    }
162
163    /// Sets the pointer handling of `id`: `None` when it has none. What the
164    /// record draws stays.
165    pub fn set_hit_test(&mut self, id: RecordId, hit_test: Option<HitTestNode>) {
166        if hit_test.is_some() || self.props(id).is_some_and(|props| props.hit_test.is_some()) {
167            self.edit_props(id, |props| props.hit_test = hit_test);
168        }
169    }
170
171    // Changes the properties of `id` with `edit`, starting from defaults
172    // when it has none, for the scene builder, which notes the change
173    // itself. Properties left at their defaults go.
174    pub(crate) fn edit_props(&mut self, id: RecordId, edit: impl FnOnce(&mut LayerProps)) {
175        let index = id.index();
176        let entry = self.warm[index].props;
177        let mut fresh = None;
178        let props = match entry {
179            Some(entry) => &mut self.props[entry.index()],
180            None => fresh.insert(LayerProps::default()),
181        };
182        let inputs =
183            |props: &LayerProps| (super::facts::props_inputs(props), props.hit_test.is_some());
184        let before = inputs(props);
185        edit(props);
186        let after = inputs(props);
187        match (entry, props.is_default(), fresh) {
188            (Some(entry), true, _) => {
189                self.props.remove(entry.index());
190                self.warm[index].props = None;
191            }
192            (None, false, Some(props)) => {
193                self.warm[index].props = Some(PropsId::from_index(self.props.insert(props)));
194            }
195            _ => {}
196        }
197        if self.warm[index].props.is_some() != entry.is_some() {
198            self.sync_props_flags(index);
199        }
200        // The other flags and the facts follow from the inputs alone.
201        if before == after {
202            return;
203        }
204        self.sync_props_flags(index);
205        if before.1 != after.1 {
206            super::facts::forget_children(self, index);
207        }
208        if before.0 != after.0 {
209            self.stale_with_parent(index);
210        }
211    }
212}