Skip to main content

dioxus_three/
gizmos.rs

1//! Gizmos and transform manipulators for Dioxus Three
2//!
3//! Provides visual handles for translating, rotating, and scaling
4//! selected objects in the 3D scene.
5
6use crate::input::{EntityId, Vector3};
7
8/// Type of transformation gizmo
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum GizmoMode {
11    /// Translation handles
12    #[default]
13    Translate,
14    /// Rotation handles
15    Rotate,
16    /// Scale handles
17    Scale,
18}
19
20/// Coordinate space for gizmo operation
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
22pub enum GizmoSpace {
23    /// World coordinates
24    #[default]
25    World,
26    /// Local object coordinates
27    Local,
28}
29
30/// Configuration for a transformation gizmo
31#[derive(Debug, Clone, PartialEq)]
32pub struct Gizmo {
33    /// The entity this gizmo is attached to
34    pub target: EntityId,
35    /// Current transformation mode
36    pub mode: GizmoMode,
37    /// Coordinate space
38    pub space: GizmoSpace,
39    /// Visual size of the gizmo
40    pub size: f32,
41    /// Show X axis handle
42    pub show_x: bool,
43    /// Show Y axis handle
44    pub show_y: bool,
45    /// Show Z axis handle
46    pub show_z: bool,
47    /// Show uniform scale handle (scale mode only)
48    pub show_xyz: bool,
49    /// Show plane handles (translate mode only)
50    pub show_planes: bool,
51}
52
53impl Gizmo {
54    pub fn new(target: EntityId) -> Self {
55        Self {
56            target,
57            mode: GizmoMode::Translate,
58            space: GizmoSpace::World,
59            size: 1.0,
60            show_x: true,
61            show_y: true,
62            show_z: true,
63            show_xyz: true,
64            show_planes: true,
65        }
66    }
67
68    pub fn with_mode(mut self, mode: GizmoMode) -> Self {
69        self.mode = mode;
70        self
71    }
72
73    pub fn with_space(mut self, space: GizmoSpace) -> Self {
74        self.space = space;
75        self
76    }
77
78    pub fn with_size(mut self, size: f32) -> Self {
79        self.size = size;
80        self
81    }
82
83    pub fn hide_x(mut self) -> Self {
84        self.show_x = false;
85        self
86    }
87
88    pub fn hide_y(mut self) -> Self {
89        self.show_y = false;
90        self
91    }
92
93    pub fn hide_z(mut self) -> Self {
94        self.show_z = false;
95        self
96    }
97}
98
99/// Event fired during gizmo interaction
100#[derive(Debug, Clone)]
101pub struct GizmoEvent {
102    /// The entity being transformed
103    pub target: EntityId,
104    /// The gizmo mode
105    pub mode: GizmoMode,
106    /// The gizmo space
107    pub space: GizmoSpace,
108    /// Current transform values
109    pub transform: GizmoTransform,
110    /// Whether this is the final event (drag ended)
111    pub is_finished: bool,
112}
113
114/// Transform values from gizmo interaction
115#[derive(Debug, Clone, Copy, Default)]
116pub struct GizmoTransform {
117    pub position: Vector3,
118    pub rotation: Vector3,
119    pub scale: Vector3,
120}
121
122impl GizmoTransform {
123    pub fn new() -> Self {
124        Self {
125            position: Vector3::new(0.0, 0.0, 0.0),
126            rotation: Vector3::new(0.0, 0.0, 0.0),
127            scale: Vector3::new(1.0, 1.0, 1.0),
128        }
129    }
130}
131
132/// Type of gizmo handle being interacted with
133#[derive(Debug, Clone, Copy, PartialEq, Eq)]
134pub enum GizmoHandle {
135    /// X axis
136    X,
137    /// Y axis
138    Y,
139    /// Z axis
140    Z,
141    /// XY plane
142    XY,
143    /// YZ plane
144    YZ,
145    /// XZ plane
146    XZ,
147    /// All axes (uniform)
148    XYZ,
149}
150
151/// Builder for creating gizmos
152pub struct GizmoBuilder {
153    gizmo: Gizmo,
154}
155
156impl GizmoBuilder {
157    pub fn new(target: EntityId) -> Self {
158        Self {
159            gizmo: Gizmo::new(target),
160        }
161    }
162
163    pub fn translate(mut self) -> Self {
164        self.gizmo.mode = GizmoMode::Translate;
165        self
166    }
167
168    pub fn rotate(mut self) -> Self {
169        self.gizmo.mode = GizmoMode::Rotate;
170        self
171    }
172
173    pub fn scale(mut self) -> Self {
174        self.gizmo.mode = GizmoMode::Scale;
175        self
176    }
177
178    pub fn world_space(mut self) -> Self {
179        self.gizmo.space = GizmoSpace::World;
180        self
181    }
182
183    pub fn local_space(mut self) -> Self {
184        self.gizmo.space = GizmoSpace::Local;
185        self
186    }
187
188    pub fn size(mut self, size: f32) -> Self {
189        self.gizmo.size = size;
190        self
191    }
192
193    pub fn build(self) -> Gizmo {
194        self.gizmo
195    }
196}