use std::cell::Cell;
use cranpose_core::NodeId;
use cranpose_ui_graphics::{
Point, Rect, Size,
space::{Camera, Mat4, MeshSurface, Ray, SurfaceHit, Vec3},
};
use crate::{
graph::ProjectiveTransform,
scene_store::LayerRef,
space::{EyeView, SpaceTree},
};
const PAST_DOORWAY: f32 = 1.0e-4;
pub struct ScenePick {
scene: Option<NodeId>,
camera: Camera,
size: Size,
window_to_scene: ProjectiveTransform,
scene_to_window: ProjectiveTransform,
surfaces: Vec<PickSurface>,
seen: Vec<PickSpace>,
scenes: Vec<NodeId>,
seen_scenes: Vec<NodeId>,
last: Cell<Option<LastPick>>,
}
struct PickSpace {
through: Option<usize>,
to_door: Mat4,
}
impl PickSpace {
fn meet(&self, ray: Ray) -> Option<f32> {
let origin = self.to_door.transform_point(ray.origin);
let direction = self.to_door.transform_vector(ray.direction);
if direction.z.abs() < f32::EPSILON {
return None;
}
let distance = -origin.z / direction.z;
let at = origin + direction * distance;
let inside = (0.0..=1.0).contains(&at.x) && (0.0..=1.0).contains(&at.y);
(inside && distance > 0.0).then_some(distance)
}
}
type LastPick = (Point, Option<(usize, SurfaceHit)>);
struct PickSurface {
node_id: Option<NodeId>,
surface: MeshSurface,
content: Rect,
seen: usize,
held: Cell<Option<u32>>,
}
impl PickSurface {
fn in_layer(&self, point: Point) -> Point {
Point::new(point.x + self.content.x, point.y + self.content.y)
}
fn cast(&self, ray: Ray) -> Option<SurfaceHit> {
let mut hit = self.surface.cast(ray, self.content.size())?;
hit.point = self.in_layer(hit.point);
Some(hit)
}
}
impl ScenePick {
pub fn of(layer: LayerRef<'_>, to_window: ProjectiveTransform) -> Option<Self> {
let tree = SpaceTree::of(layer)?;
let bounds = layer.local_bounds();
let to_window = ProjectiveTransform::translation(bounds.x, bounds.y).then(to_window);
let window_to_scene = to_window.inverse()?;
let view = EyeView::of(&tree, bounds.size());
let seen_scenes = view
.seen
.iter()
.filter_map(|seen| tree.cells[seen.cell].layer.node_id())
.collect();
let seen = view
.seen
.iter()
.map(|space| PickSpace {
through: space.through,
to_door: space
.door
.and_then(|door| door.quad.inverse())
.unwrap_or(Mat4::IDENTITY),
})
.collect();
let surfaces = view
.surfaces
.into_iter()
.map(|seen| PickSurface {
node_id: seen.layer.node_id(),
surface: seen.surface,
content: seen.content,
seen: seen.seen,
held: Cell::new(None),
})
.collect();
Some(Self {
scene: layer.node_id(),
camera: view.camera,
size: view.size,
window_to_scene,
scene_to_window: to_window,
surfaces,
seen,
scenes: tree
.cells
.iter()
.filter_map(|cell| cell.layer.node_id())
.collect(),
seen_scenes,
last: Cell::new(None),
})
}
pub fn scene(&self) -> Option<NodeId> {
self.scene
}
pub fn holds_scene(&self, layer: LayerRef<'_>) -> bool {
layer
.node_id()
.is_some_and(|node_id| self.scenes.contains(&node_id))
}
pub fn sees_scene(&self, layer: LayerRef<'_>) -> bool {
layer
.node_id()
.is_some_and(|node_id| self.seen_scenes.contains(&node_id))
}
pub fn index_of(&self, layer: LayerRef<'_>) -> Option<usize> {
let node_id = layer.node_id()?;
self.surfaces
.iter()
.position(|surface| surface.node_id == Some(node_id))
}
pub fn content_point(&self, window: Point, index: usize) -> Option<Point> {
let (nearest, hit) = self.nearest(window)?;
(nearest == index).then_some(hit.point)
}
pub fn nearest_along(&self, ray: Ray) -> Option<(usize, SurfaceHit)> {
self.nearest_in(ray, 0, f32::NEG_INFINITY)
}
pub fn drag_point(&self, window: Point, index: usize) -> Option<Point> {
self.drag_point_along(self.ray(window)?, index)
}
pub fn drag_point_along(&self, ray: Ray, index: usize) -> Option<Point> {
let target = self.surfaces.get(index)?;
if let Some(hit) = target.cast(ray) {
target.held.set(Some(hit.triangle));
return Some(hit.point);
}
target
.surface
.cast_plane(ray, target.held.get()?, target.content.size())
.map(|point| target.in_layer(point))
}
pub fn window_point(&self, index: usize, content: Point) -> Option<Point> {
let target = self.surfaces.get(index)?;
let from_corner = Point::new(content.x - target.content.x, content.y - target.content.y);
let at = target
.surface
.point_at(from_corner, target.content.size())?;
let shown = self.camera.project(at, self.size)?;
Some(self.scene_to_window.map_point(shown))
}
pub fn window_point_of(&self, point: Vec3) -> Option<Point> {
let shown = self.camera.project(point, self.size)?;
Some(self.scene_to_window.map_point(shown))
}
fn ray(&self, window: Point) -> Option<Ray> {
let local = self.window_to_scene.map_point(window);
self.camera.ray(local, self.size)
}
fn nearest(&self, window: Point) -> Option<(usize, SurfaceHit)> {
if let Some((point, nearest)) = self.last.get()
&& point == window
{
return nearest;
}
let nearest = self.ray(window).and_then(|ray| self.nearest_along(ray));
if let Some((index, hit)) = nearest {
self.surfaces[index].held.set(Some(hit.triangle));
}
self.last.set(Some((window, nearest)));
nearest
}
fn nearest_in(&self, ray: Ray, seen: usize, beyond: f32) -> Option<(usize, SurfaceHit)> {
let surface = self
.surfaces
.iter()
.enumerate()
.filter(|(_, target)| target.seen == seen)
.filter_map(|(index, target)| {
target
.cast(ray)
.filter(|hit| hit.distance > beyond)
.map(|hit| (index, hit))
})
.min_by(|(_, a), (_, b)| a.distance.total_cmp(&b.distance));
let doorway = self
.seen
.iter()
.enumerate()
.filter(|(_, space)| space.through == Some(seen))
.filter_map(|(index, space)| {
space
.meet(ray)
.filter(|distance| *distance > beyond)
.map(|distance| (index, distance))
})
.min_by(|(_, a), (_, b)| a.total_cmp(b));
let Some((door, distance)) = doorway.filter(|(_, distance)| {
surface.is_none_or(|(_, hit)| *distance <= hit.distance * (1.0 + PAST_DOORWAY))
}) else {
return surface;
};
self.nearest_in(ray, door, distance * (1.0 + PAST_DOORWAY))
.or(surface)
}
}