Skip to main content

cranpose_ui/
eye.rs

1//! The eye a window's 3D scene is seen from, as a state an app holds: it
2//! turns as the app asks, and walks through the spaces behind the scene's
3//! doorways, stopped by solid surfaces where the renderer last placed them.
4
5use cranpose_core::{
6    CompositionLocal, MutableState, NodeId, compositionLocalOf, mutableStateOf, remember,
7};
8use cranpose_macros::composable;
9use cranpose_ui_graphics::space::{
10    EyePlace, EyePose, Light, Multisample, Scene3d, Vec3, WorldSink,
11};
12
13/// The eye of a window's 3D scene.
14///
15/// Read in a [`scene3d`](crate::Modifier::scene3d) closure through
16/// [`Eye::scene`], it moves the scene's camera without recomposition. The
17/// scene tells the renderer where the eye stands among the spaces behind its
18/// doorways, and the renderer leaves the eye what it can bump into there
19/// each time it places the scene, which [`Eye::walk`] reads.
20#[derive(Clone)]
21pub struct Eye {
22    pose: MutableState<EyePose>,
23    world: WorldSink,
24    radius: f32,
25}
26
27impl Eye {
28    /// An eye at `pose`: a ball of `radius`, in the units of the space it
29    /// stands in, that stops short of solid surfaces.
30    pub fn new(pose: EyePose, radius: f32) -> Self {
31        Self {
32            pose: mutableStateOf(pose),
33            world: WorldSink::default(),
34            radius,
35        }
36    }
37
38    /// Where the eye stands and which way it looks.
39    pub fn pose(&self) -> EyePose {
40        self.pose.get()
41    }
42
43    /// The scene seen through the eye: its camera, with `focal_length`, in
44    /// the space it stands in, and `light`, drawn with one sample in each
45    /// pixel.
46    pub fn scene(&self, focal_length: f32, light: Option<Light>) -> Scene3d {
47        let pose = self.pose.get();
48        Scene3d {
49            camera: pose.camera(focal_length),
50            light,
51            eye: Some(EyePlace {
52                doorways: pose.doorways,
53                world: self.world.clone(),
54            }),
55            multisample: Multisample::Off,
56        }
57    }
58
59    /// Walks the eye `step`, in the coordinates of the space it stands in:
60    /// through the doorways the step passes, and pushed back from the solid
61    /// surfaces of the space it walks into, along which it slides.
62    pub fn walk(&self, step: Vec3) {
63        let world = self.world.current();
64        let radius = self.radius;
65        self.pose
66            .update(|pose| *pose = world.walk(pose, step, radius));
67    }
68
69    /// Turns the eye `yaw` radians to the right and `pitch` up (see
70    /// [`EyePose::turn`]).
71    pub fn turn(&self, yaw: f32, pitch: f32) {
72        self.pose.update(|pose| pose.turn(yaw, pitch));
73    }
74
75    /// Puts the eye at `pose`, through no doorway and stopped by nothing.
76    pub fn place(&self, pose: EyePose) {
77        self.pose.set(pose);
78    }
79
80    /// Where the eye stands and which way it looks, in the coordinates of
81    /// the space of the scene node `scene`: the one it stands in or one a
82    /// doorway from it. `None` when no such space lies there.
83    pub fn pose_in(&self, scene: NodeId) -> Option<EyePose> {
84        self.world.current().pose_in(&self.pose.get(), scene.0)
85    }
86
87    /// Puts the eye at `pose`, in the coordinates of the space of the scene
88    /// node `scene`, as if it walked there: that space is the one the eye
89    /// stands in or one a doorway from it. False, and the eye stays where
90    /// it is, when no such space lies there.
91    pub fn enter(&self, scene: NodeId, pose: EyePose) -> bool {
92        let Some(doorways) = self.world.current().doorways_to(scene.0) else {
93            return false;
94        };
95        self.pose.set(EyePose { doorways, ..pose });
96        true
97    }
98}
99
100/// Two handles are equal when they hold the same eye.
101impl PartialEq for Eye {
102    fn eq(&self, other: &Self) -> bool {
103        self.world == other.world
104    }
105}
106
107/// An [`Eye`] kept across recompositions, made at `pose` the first time.
108#[allow(non_snake_case, reason = "names follow Compose's casing")]
109#[composable]
110pub fn rememberEye(pose: impl FnOnce() -> EyePose, radius: f32) -> Eye {
111    remember(move || Eye::new(pose(), radius)).with(Clone::clone)
112}
113
114/// CompositionLocal carrying the eye of the window scene the content lies
115/// in, or `None` outside one.
116///
117/// An app that shows a scene through an [`Eye`] provides the eye around the
118/// scene's content, so a scene composed behind one of its doorways knows the
119/// window's eye walks into it and leaves its own camera standing in the
120/// doorway.
121pub fn local_eye() -> CompositionLocal<Option<Eye>> {
122    crate::environment_locals::cached_local(|locals| &locals.eye, || compositionLocalOf(|| None))
123}