trackball/first.rs
1use core::f32::consts::PI;
2use glam::{Vec2, Vec3};
3
4/// First person view induced by displacement on screen.
5///
6/// Implements [`Default`] and can be created with `First::default()`.
7///
8/// All methods except [`Self::enabled()`] must be invoked on matching events fired by your 3D
9/// graphics library of choice.
10#[derive(Debug, Clone, Default)]
11pub struct First {
12 /// Caches captured yaw axis.
13 ray: Option<Vec3>,
14}
15
16impl First {
17 /// Captures current yaw axis when entering first person view.
18 pub const fn capture(&mut self, yaw_axis: Vec3) {
19 self.ray = Some(yaw_axis);
20 }
21 /// Computes pitch and yaw from cursor/finger displacement vector in screen space.
22 ///
23 /// Carries cursor/finger displacements to arcs of the same length on great circles of an
24 /// eye-centered trackball with the radius of the target-centered trackball of
25 /// [`crate::Orbit`], that is maximum of the screen's width and height divided by π.
26 #[must_use]
27 pub fn compute(&self, vec: Vec2, max: Vec2) -> Option<(f32, f32, Vec3)> {
28 self.ray.map(|ray| {
29 let rad = max.x.max(max.y) / PI;
30 (vec.y / rad, vec.x / rad, ray)
31 })
32 }
33 /// Discards captured yaw axis when leaving first person view.
34 pub const fn discard(&mut self) {
35 self.ray = None;
36 }
37 /// Whether a yaw axis has been captured.
38 #[must_use]
39 pub const fn enabled(&self) -> bool {
40 self.ray.is_some()
41 }
42 /// Captured yaw axis.
43 #[must_use]
44 pub const fn yaw_axis(&self) -> Option<Vec3> {
45 self.ray
46 }
47}