cranpose-render-common 0.26.0

Scene, renderer and hit-test contracts for Cranpose backends
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use std::rc::Rc;

use cranpose_core::NodeId;
use cranpose_ui_graphics::{Rect, layer_transform::layer_surface};
use smallvec::SmallVec;

use crate::{
    graph::{HitTestNode, ProjectiveTransform, quad_bounds},
    graph_scene::{
        HitClip, HitGeometry, HitTargetSpec, HitTargets, Scene, SurfaceGeometry, SurfaceTarget,
    },
    primitive_emit::resolve_clip,
    scene_pick::{ScenePick, SurfaceHolds},
    scene_store::LayerRef,
};

/// Receives the hit targets that `collect_hits_from_graph` finds in a scene.
pub trait HitGraphSink {
    /// Records an interactive node. `capture_path` lists the node first and then its pointer-input
    /// ancestors from the nearest outward, and `geometry` is in window space.
    fn push_hit(
        &mut self,
        node_id: NodeId,
        capture_path: &[NodeId],
        geometry: HitGeometry<'_>,
        hit: &HitTestNode,
    );

    /// Where the gestures on mesh surfaces keep the meshes they hold from
    /// one collection of the targets to the next; `None` for a sink that
    /// sends no gestures, where each scene's pick keeps its own.
    fn surface_holds(&self) -> Option<&SurfaceHolds> {
        None
    }
}

impl HitGraphSink for Scene {
    fn push_hit(
        &mut self,
        node_id: NodeId,
        capture_path: &[NodeId],
        geometry: HitGeometry<'_>,
        hit: &HitTestNode,
    ) {
        Self::push_hit(self, node_id, capture_path, geometry, hit_spec(hit));
    }

    fn surface_holds(&self) -> Option<&SurfaceHolds> {
        Some(&self.holds)
    }
}

impl HitGraphSink for HitTargets<'_> {
    fn push_hit(
        &mut self,
        node_id: NodeId,
        capture_path: &[NodeId],
        geometry: HitGeometry<'_>,
        hit: &HitTestNode,
    ) {
        self.push(node_id, capture_path, geometry, hit_spec(hit));
    }

    fn surface_holds(&self) -> Option<&SurfaceHolds> {
        Some(self.holds)
    }
}

fn hit_spec(hit: &HitTestNode) -> HitTargetSpec<'_> {
    HitTargetSpec {
        shape: None,
        handlers: &hit.handlers,
    }
}

/// Walks `layer` and the layers beneath it in draw order and pushes to `sink` a hit target for
/// each node that has a hit test, with its geometry in window space. `parent_transform` maps
/// the parent layer's space to the window, and `parent_hit_clip` is the clip bounds that the
/// layers above impose.
pub fn collect_hits_from_graph<S: HitGraphSink>(
    layer: LayerRef<'_>,
    parent_transform: ProjectiveTransform,
    sink: &mut S,
    parent_hit_clip: Option<Rect>,
) {
    if !layer.has_hit_targets() {
        return;
    }
    let holds = sink.surface_holds().cloned();
    let mut walk = HitWalk {
        sink,
        holds,
        hit_clips: Vec::new(),
        pointer_input_ancestors: Vec::new(),
        capture_path: SmallVec::new(),
        surface: None,
    };
    walk.visit(
        layer,
        parent_transform,
        parent_hit_clip,
        &ParentSpace::default(),
    );
}

struct HitWalk<'s, S> {
    sink: &'s mut S,
    holds: Option<SurfaceHolds>,
    hit_clips: Vec<HitClip>,
    pointer_input_ancestors: Vec<NodeId>,
    capture_path: SmallVec<[NodeId; 8]>,
    surface: Option<SurfaceWalk>,
}

struct SurfaceWalk {
    target: Rc<SurfaceTarget>,
    clips: Vec<HitClip>,
}

#[derive(Default)]
struct ParentSpace {
    scene: Option<Rc<ScenePick>>,
    scene_clips: (Option<Rect>, usize),
    content: Option<ProjectiveTransform>,
    content_clip: Option<Rect>,
    behind_doorway: bool,
    unseen: bool,
}

impl<S: HitGraphSink> HitWalk<'_, S> {
    fn visit(
        &mut self,
        layer: LayerRef<'_>,
        parent_transform: ProjectiveTransform,
        parent_hit_clip: Option<Rect>,
        space: &ParentSpace,
    ) {
        if !layer.has_hit_targets() {
            return;
        }
        // Behind a doorway the eye does not see through, nothing takes input.
        let Some((doorway, unseen)) = Self::seen(layer, space) else {
            return;
        };
        let transform = layer.transform_to_parent().then(parent_transform);
        // The node's handlers sit on its own rect; its clip, at its layer's
        // bounds, cuts them as it cuts what the layer holds.
        let node_rect = layer.node_rect();
        let transformed_quad = hit_quad(&transform, node_rect);
        let transformed_rect = quad_bounds(transformed_quad);

        // Inside a scene the camera's pick decides where the surfaces'
        // content takes input, whatever their flat stand-ins show: an eye
        // behind a wall still sees what lies through its doorway.
        let in_scene = space.scene.is_some();
        if !in_scene && (transformed_rect.width <= 0.0 || transformed_rect.height <= 0.0) {
            return;
        }
        let world_to_local = match transform.inverse() {
            Some(inverse) => inverse,
            None if in_scene => ProjectiveTransform::identity(),
            None => return,
        };

        let (entered, content, content_clip) = self.enter_content(layer, (space, doorway));
        let behind_doorway = space.behind_doorway || doorway;
        let Some((hit_clip_bounds, pushed_clip)) =
            self.push_window_clip(layer, &transform, (parent_hit_clip, in_scene))
        else {
            self.leave(entered, content_clip.pushed);
            return;
        };

        if let (Some(node_id), Some(hit), false) = (
            layer.node_id(),
            layer.hit_test(),
            self.hides_targets(unseen, behind_doorway),
        ) {
            let geometry = PlacedNode {
                rect: transformed_rect,
                quad: transformed_quad,
                local_bounds: node_rect,
                world_to_local,
                hit_clip_bounds,
                content,
                content_clip: content_clip.bounds,
            };
            self.push_node(node_id, hit, geometry);
        }

        let pointer_input_ancestor = layer
            .hit_test()
            .filter(|hit| !hit.handlers.pointer_inputs().is_empty())
            .and_then(|_| layer.node_id());
        if let Some(node_id) = pointer_input_ancestor {
            self.pointer_input_ancestors.push(node_id);
        }

        let children_space = ParentSpace {
            content,
            content_clip: content_clip.bounds,
            behind_doorway,
            unseen,
            ..self.children_scene(layer, (transform, hit_clip_bounds), space)
        };
        for child_layer in layer.children() {
            self.visit(child_layer, transform, hit_clip_bounds, &children_space);
        }

        if pointer_input_ancestor.is_some() {
            let _ = self.pointer_input_ancestors.pop();
        }

        if pushed_clip {
            let _ = self.hit_clips.pop();
        }
        self.leave(entered, content_clip.pushed);
    }

    fn push_node(&mut self, node_id: NodeId, hit: &HitTestNode, node: PlacedNode) {
        self.capture_path.clear();
        self.capture_path.push(node_id);
        self.capture_path
            .extend(self.pointer_input_ancestors.iter().rev().copied());
        let content_to_local = node.content.and_then(ProjectiveTransform::inverse);
        let surface =
            self.surface
                .as_ref()
                .zip(content_to_local)
                .map(|(surface, content_to_local)| SurfaceGeometry {
                    target: &surface.target,
                    content_to_local,
                    clip_bounds: node.content_clip,
                    clips: &surface.clips,
                });
        self.sink.push_hit(
            node_id,
            &self.capture_path,
            HitGeometry {
                rect: node.rect,
                quad: node.quad,
                local_bounds: node.local_bounds,
                world_to_local: node.world_to_local,
                hit_clip_bounds: node.hit_clip_bounds,
                hit_clips: &self.hit_clips,
                surface,
            },
            hit,
        );
    }

    fn children_scene(
        &self,
        layer: LayerRef<'_>,
        (transform, hit_clip_bounds): (ProjectiveTransform, Option<Rect>),
        space: &ParentSpace,
    ) -> ParentSpace {
        let through_portal = space
            .scene
            .as_ref()
            .is_some_and(|scene| scene.holds_scene(layer));
        let shown = match (layer.wraps(), through_portal) {
            (None, false) => ScenePick::of(layer, transform, self.holds.as_ref()).map(Rc::new),
            _ => None,
        };
        let (scene, scene_clips) = match shown {
            Some(pick) => (Some(pick), (hit_clip_bounds, self.hit_clips.len())),
            None => (space.scene.clone(), space.scene_clips),
        };
        ParentSpace {
            scene,
            scene_clips,
            ..ParentSpace::default()
        }
    }

    fn enter_surface(
        &mut self,
        layer: LayerRef<'_>,
        space: &ParentSpace,
    ) -> Option<Option<SurfaceWalk>> {
        let pick = space.scene.as_ref()?;
        let index = pick.index_of(layer)?;
        let (outer_clip_bounds, outer_clip_count) = space.scene_clips;
        let target = Rc::new(SurfaceTarget {
            pick: Rc::clone(pick),
            index,
            outer_clip_bounds,
            outer_clips: self.hit_clips[..outer_clip_count.min(self.hit_clips.len())].to_vec(),
        });
        Some(self.surface.replace(SurfaceWalk {
            target,
            clips: Vec::new(),
        }))
    }

    fn doorway(layer: LayerRef<'_>, space: &ParentSpace) -> Option<bool> {
        let Some(pick) = space.scene.as_ref().filter(|pick| pick.holds_scene(layer)) else {
            return Some(false);
        };
        pick.sees_scene(layer).then_some(true)
    }

    fn seen(layer: LayerRef<'_>, space: &ParentSpace) -> Option<(bool, bool)> {
        let doorway = Self::doorway(layer, space)?;
        let unseen = !doorway && (space.unseen || Self::unseen_surface(layer, space));
        Some((doorway, unseen))
    }

    fn hides_targets(&self, unseen: bool, behind_doorway: bool) -> bool {
        unseen || (behind_doorway && self.surface.is_none())
    }

    fn unseen_surface(layer: LayerRef<'_>, space: &ParentSpace) -> bool {
        space.scene.as_ref().is_some_and(|pick| {
            layer_surface(layer.graphics_layer()).is_some() && pick.index_of(layer).is_none()
        })
    }

    fn enter_content(
        &mut self,
        layer: LayerRef<'_>,
        (space, doorway): (&ParentSpace, bool),
    ) -> (
        Option<Option<SurfaceWalk>>,
        Option<ProjectiveTransform>,
        ContentClip,
    ) {
        if doorway {
            let unclipped = ContentClip {
                bounds: None,
                pushed: false,
            };
            return (Some(self.surface.take()), None, unclipped);
        }
        let entered = self.enter_surface(layer, space);
        // A surface's content starts a space of its own, which the clips of
        // the content above do not cut.
        let (content, above) = match entered {
            Some(_) => (Some(ProjectiveTransform::identity()), None),
            None => (
                space
                    .content
                    .map(|parent| layer.transform_to_parent().then(parent)),
                space.content_clip,
            ),
        };
        let content_clip = self.push_content_clip(layer, content, above);
        (entered, content, content_clip)
    }

    fn push_window_clip(
        &mut self,
        layer: LayerRef<'_>,
        transform: &ProjectiveTransform,
        (parent_hit_clip, in_scene): (Option<Rect>, bool),
    ) -> Option<(Option<Rect>, bool)> {
        let Some(local_clip) = layer.clip_rect().filter(|_| !in_scene) else {
            return Some((parent_hit_clip, false));
        };
        let quad = hit_quad(transform, local_clip);
        let bounds = quad_bounds(quad);
        let resolved = resolve_clip(parent_hit_clip, Some(bounds));
        if resolved.is_some_and(|clip| clip.is_empty()) {
            return None;
        }
        self.hit_clips.push(HitClip { quad, bounds });
        Some((resolved, true))
    }

    fn push_content_clip(
        &mut self,
        layer: LayerRef<'_>,
        content: Option<ProjectiveTransform>,
        parent_clip: Option<Rect>,
    ) -> ContentClip {
        let unclipped = ContentClip {
            bounds: parent_clip,
            pushed: false,
        };
        let (Some(surface), Some(content)) = (self.surface.as_mut(), content) else {
            return unclipped;
        };
        let Some(local_clip) = layer.clip_rect() else {
            return unclipped;
        };
        let quad = hit_quad(&content, local_clip);
        let bounds = quad_bounds(quad);
        surface.clips.push(HitClip { quad, bounds });
        ContentClip {
            bounds: resolve_clip(parent_clip, Some(bounds)),
            pushed: true,
        }
    }

    fn leave(&mut self, entered: Option<Option<SurfaceWalk>>, pushed_content_clip: bool) {
        if pushed_content_clip && let Some(surface) = self.surface.as_mut() {
            let _ = surface.clips.pop();
        }
        if let Some(previous) = entered {
            self.surface = previous;
        }
    }
}

struct PlacedNode {
    rect: Rect,
    quad: [[f32; 2]; 4],
    local_bounds: Rect,
    world_to_local: ProjectiveTransform,
    hit_clip_bounds: Option<Rect>,
    content: Option<ProjectiveTransform>,
    content_clip: Option<Rect>,
}

struct ContentClip {
    bounds: Option<Rect>,
    pushed: bool,
}

fn hit_quad(transform: &ProjectiveTransform, rect: Rect) -> [[f32; 2]; 4] {
    if transform.rect_in_front(rect) {
        return transform.map_rect(rect);
    }
    let bounds = transform.bounds_for_rect(rect);
    let (right, bottom) = (bounds.x + bounds.width, bounds.y + bounds.height);
    [
        [bounds.x, bounds.y],
        [right, bounds.y],
        [bounds.x, bottom],
        [right, bottom],
    ]
}

#[cfg(test)]
#[path = "tests/hit_graph_tests.rs"]
mod tests;