Skip to main content

graphics_core/handles/
mod.rs

1//! Renderer-owned handles and controllers for smooth motion interpolation (kinetocore / kinetoxus).
2
3use glam::Vec3;
4use std::sync::{Arc, RwLock};
5
6/// Thread-safe animatable camera controller handle.
7#[derive(Clone)]
8pub struct CameraHandle {
9    inner: Arc<RwLock<CameraState>>,
10}
11
12#[derive(Clone, Copy, Debug)]
13pub struct CameraState {
14    pub position: Vec3,
15    pub target: Vec3,
16    pub zoom: f32,
17}
18
19impl Default for CameraHandle {
20    fn default() -> Self {
21        Self::new(Vec3::new(0.0, 5.0, 10.0), Vec3::ZERO, 1.0)
22    }
23}
24
25impl CameraHandle {
26    pub fn new(position: Vec3, target: Vec3, zoom: f32) -> Self {
27        Self {
28            inner: Arc::new(RwLock::new(CameraState {
29                position,
30                target,
31                zoom,
32            })),
33        }
34    }
35
36    pub fn get(&self) -> CameraState {
37        *self.inner.read().unwrap()
38    }
39
40    pub fn set_position(&self, pos: Vec3) {
41        self.inner.write().unwrap().position = pos;
42    }
43
44    pub fn set_target(&self, target: Vec3) {
45        self.inner.write().unwrap().target = target;
46    }
47
48    pub fn set_zoom(&self, zoom: f32) {
49        self.inner.write().unwrap().zoom = zoom;
50    }
51}
52
53/// Generic numeric array target for in-place tweening by motion engines.
54#[derive(Clone)]
55pub struct FloatBufferHandle {
56    data: Arc<RwLock<Vec<f32>>>,
57}
58
59impl FloatBufferHandle {
60    pub fn new(initial: Vec<f32>) -> Self {
61        Self {
62            data: Arc::new(RwLock::new(initial)),
63        }
64    }
65
66    pub fn read(&self) -> Vec<f32> {
67        self.data.read().unwrap().clone()
68    }
69
70    pub fn write_slice(&self, values: &[f32]) {
71        let mut lock = self.data.write().unwrap();
72        let len = values.len().min(lock.len());
73        lock[..len].copy_from_slice(&values[..len]);
74    }
75}