lotus_shared/
gizmos.rs

1use serde::{Deserialize, Serialize};
2
3use crate::{graphics::Color, math::Vec3};
4
5#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
6pub enum GizmoKind {
7    WireCube { center: Vec3, half_extents: Vec3 },
8    WireSphere { center: Vec3, radius: f32 },
9    Arrow { start: Vec3, end: Vec3 },
10}
11
12/// A gizmo to draw in the game.
13/// A gizmo is a visual indicator of something in the game.
14/// It is used to help with debugging and development.
15/// Gizmos are drawn relative to the center of the object the script is attached to.
16#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
17pub struct Gizmo {
18    pub kind: GizmoKind,
19    pub color: Color,
20}
21
22impl Gizmo {
23    /// Create a new gizmo.
24    pub fn new(kind: GizmoKind, color: Color) -> Self {
25        Self { kind, color }
26    }
27
28    /// Create a new wire cube gizmo.
29    pub fn wire_cube(center: impl Into<Vec3>, half_extents: impl Into<Vec3>, color: Color) -> Self {
30        Self::new(
31            GizmoKind::WireCube {
32                center: center.into(),
33                half_extents: half_extents.into(),
34            },
35            color,
36        )
37    }
38
39    /// Create a new wire sphere gizmo.
40    pub fn wire_sphere(center: impl Into<Vec3>, radius: impl Into<f32>, color: Color) -> Self {
41        Self::new(
42            GizmoKind::WireSphere {
43                center: center.into(),
44                radius: radius.into(),
45            },
46            color,
47        )
48    }
49
50    /// Create a new arrow gizmo.
51    pub fn arrow(start: impl Into<Vec3>, end: impl Into<Vec3>, color: Color) -> Self {
52        Self::new(
53            GizmoKind::Arrow {
54                start: start.into(),
55                end: end.into(),
56            },
57            color,
58        )
59    }
60
61    /// Draw the gizmo.
62    #[cfg(feature = "ffi")]
63    pub fn draw(&self) {
64        use lotus_script_sys::FfiObject;
65
66        let obj = FfiObject::new(self);
67
68        unsafe {
69            lotus_script_sys::gizmo::draw(obj.packed());
70        }
71    }
72}