1use crate::interaction::clip_plane::ray_plane_intersection;
4use crate::renderer::{GlyphItem, GlyphType, PolylineItem};
5use crate::scene::aabb::Aabb;
6use parry3d::math::{Pose, Vector};
7use parry3d::query::{Ray, RayCast};
8
9use super::{WidgetContext, WidgetResult, any_perpendicular_pair, ctx_ray, handle_world_radius};
10
11#[derive(Clone, Copy, PartialEq, Eq, Debug)]
13enum BoxHandle {
14 Center,
16 Face(usize),
18 RotArc(usize),
20}
21
22pub struct BoxWidget {
42 pub center: glam::Vec3,
44 pub half_extents: glam::Vec3,
46 pub rotation: glam::Quat,
48 pub color: [f32; 4],
50 pub handle_color: [f32; 4],
52
53 hovered_handle: Option<BoxHandle>,
54 active_handle: Option<BoxHandle>,
55 drag_plane_normal: glam::Vec3,
56 drag_plane_d: f32,
57 drag_anchor_world: glam::Vec3,
58}
59
60impl BoxWidget {
61 pub fn new(center: glam::Vec3, half_extents: glam::Vec3) -> Self {
63 Self {
64 center,
65 half_extents: half_extents.max(glam::Vec3::splat(0.01)),
66 rotation: glam::Quat::IDENTITY,
67 color: [0.3, 0.8, 0.4, 1.0],
68 handle_color: [0.0; 4],
69 hovered_handle: None,
70 active_handle: None,
71 drag_plane_normal: glam::Vec3::Z,
72 drag_plane_d: 0.0,
73 drag_anchor_world: glam::Vec3::ZERO,
74 }
75 }
76
77 pub fn is_active(&self) -> bool {
79 self.active_handle.is_some()
80 }
81
82 pub fn obb(&self) -> (glam::Vec3, glam::Vec3, glam::Quat) {
84 (self.center, self.half_extents, self.rotation)
85 }
86
87 pub fn aabb(&self) -> Aabb {
92 let mut min = glam::Vec3::splat(f32::MAX);
93 let mut max = glam::Vec3::splat(f32::MIN);
94 let h = self.half_extents;
95 for sx in [-1.0_f32, 1.0] {
96 for sy in [-1.0_f32, 1.0] {
97 for sz in [-1.0_f32, 1.0] {
98 let p = self.center
99 + self.rotation * glam::Vec3::new(sx * h.x, sy * h.y, sz * h.z);
100 min = min.min(p);
101 max = max.max(p);
102 }
103 }
104 }
105 Aabb { min, max }
106 }
107
108 pub fn contains_point(&self, point: glam::Vec3) -> bool {
110 let local = self.rotation.inverse() * (point - self.center);
111 local.x.abs() <= self.half_extents.x
112 && local.y.abs() <= self.half_extents.y
113 && local.z.abs() <= self.half_extents.z
114 }
115
116 pub fn update(&mut self, ctx: &WidgetContext) -> WidgetResult {
118 let (ro, rd) = ctx_ray(ctx);
119 let mut updated = false;
120
121 if self.active_handle.is_none() {
122 let hit = self.hit_test(ro, rd, ctx);
123 if hit.is_some() || !ctx.drag_started {
127 self.hovered_handle = hit;
128 }
129 }
130
131 if ctx.drag_started {
132 if let Some(handle) = self.hovered_handle {
133 let anchor = self.handle_pos(handle);
134 let n = -glam::Vec3::from(ctx.camera.forward);
138 self.drag_plane_normal = n;
139 self.drag_plane_d = -n.dot(anchor);
140 self.drag_anchor_world = anchor;
141 self.active_handle = Some(handle);
142 }
143 }
144
145 if let Some(handle) = self.active_handle {
146 if ctx.released || (!ctx.dragging && !ctx.drag_started) {
147 self.active_handle = None;
148 self.hovered_handle = None;
149 } else {
150 match handle {
151 BoxHandle::Center => {
152 if let Some(hit) = ray_plane_intersection(
153 ro, rd, self.drag_plane_normal, self.drag_plane_d,
154 ) {
155 let delta = hit - self.drag_anchor_world;
156 if delta.length_squared() > 1e-10 {
157 self.center += delta;
158 self.drag_anchor_world = hit;
159 updated = true;
160 }
161 }
162 }
163 BoxHandle::Face(i) => {
164 if let Some(hit) = ray_plane_intersection(
165 ro, rd, self.drag_plane_normal, self.drag_plane_d,
166 ) {
167 let world_normal = self.rotation * Self::local_face_normal(i);
169 let proj = (hit - self.drag_anchor_world).dot(world_normal);
170 if proj.abs() > 1e-5 {
171 let axis = i / 2; let new_he = (self.half_extents[axis] + proj).max(0.01);
173 let he_delta = new_he - self.half_extents[axis];
174 self.half_extents[axis] = new_he;
175 self.center += world_normal * (he_delta * 0.5);
177 self.drag_anchor_world = hit;
178 updated = true;
179 }
180 }
181 }
182 BoxHandle::RotArc(axis_idx) => {
183 let axis = Self::world_rotation_axis(axis_idx);
187 let plane_d = -axis.dot(self.center);
188 if let Some(plane_hit) =
189 ray_plane_intersection(ro, rd, axis, plane_d)
190 {
191 let start_dir =
192 (self.drag_anchor_world - self.center).normalize_or_zero();
193 let new_dir = (plane_hit - self.center).normalize_or_zero();
194 if start_dir.length_squared() > 0.5
195 && new_dir.length_squared() > 0.5
196 {
197 let cos_a = start_dir.dot(new_dir).clamp(-1.0, 1.0);
198 let cross = start_dir.cross(new_dir);
199 let sign = cross.dot(axis).signum();
200 let angle = cos_a.acos() * sign;
201 if angle.abs() > 1e-5 {
202 let delta_rot =
203 glam::Quat::from_axis_angle(axis, angle);
204 self.rotation = (delta_rot * self.rotation).normalize();
205 self.drag_anchor_world =
207 self.center + new_dir * self.arc_radius();
208 updated = true;
209 }
210 }
211 }
212 }
213 }
214 }
215 }
216
217 if updated { WidgetResult::Updated } else { WidgetResult::None }
218 }
219
220 pub fn wireframe_item(&self, id: u64) -> PolylineItem {
224 let c = self.center;
225 let h = self.half_extents;
226 let r = self.rotation;
227
228 let p = |x: f32, y: f32, z: f32| -> [f32; 3] {
229 (c + r * glam::Vec3::new(x, y, z)).to_array()
230 };
231
232 PolylineItem {
233 positions: vec![
234 p(-h.x, -h.y, -h.z), p( h.x, -h.y, -h.z),
236 p( h.x, h.y, -h.z), p(-h.x, h.y, -h.z), p(-h.x, -h.y, -h.z),
237 p(-h.x, -h.y, h.z), p( h.x, -h.y, h.z),
239 p( h.x, h.y, h.z), p(-h.x, h.y, h.z), p(-h.x, -h.y, h.z),
240 p(-h.x, -h.y, -h.z), p(-h.x, -h.y, h.z),
242 p( h.x, -h.y, -h.z), p( h.x, -h.y, h.z),
243 p( h.x, h.y, -h.z), p( h.x, h.y, h.z),
244 p(-h.x, h.y, -h.z), p(-h.x, h.y, h.z),
245 ],
246 strip_lengths: vec![5, 5, 2, 2, 2, 2],
247 default_color: self.color,
248 id,
249 ..PolylineItem::default()
250 }
251 }
252
253 pub fn rotation_arcs_item(&self, id: u64) -> PolylineItem {
258 const STEPS: usize = 48;
259 let c = self.center;
260 let r = self.arc_radius();
261 let arc_colors = [
262 [0.9_f32, 0.2, 0.2, 0.7], [0.2, 0.9, 0.2, 0.7], [0.2, 0.4, 1.0, 0.7], ];
266
267 let mut positions: Vec<[f32; 3]> = Vec::with_capacity((STEPS + 1) * 3);
270 let mut strip_lengths: Vec<u32> = Vec::new();
271 let _ = arc_colors; for axis_idx in 0..3_usize {
274 let axis = Self::world_rotation_axis(axis_idx);
275 let (u, v) = any_perpendicular_pair(axis);
276 for i in 0..=STEPS {
277 let a = i as f32 * std::f32::consts::TAU / STEPS as f32;
278 let (s, co) = a.sin_cos();
279 positions.push((c + u * (co * r) + v * (s * r)).to_array());
280 }
281 strip_lengths.push((STEPS + 1) as u32);
282 }
283
284 PolylineItem {
285 positions,
286 strip_lengths,
287 default_color: self.color,
288 line_width: 1.2,
289 id,
290 ..PolylineItem::default()
291 }
292 }
293
294 pub fn handle_glyphs(&self, id_base: u64, ctx: &WidgetContext) -> GlyphItem {
299 let all_handles = [
300 BoxHandle::Center,
301 BoxHandle::Face(0),
302 BoxHandle::Face(1),
303 BoxHandle::Face(2),
304 BoxHandle::Face(3),
305 BoxHandle::Face(4),
306 BoxHandle::Face(5),
307 BoxHandle::RotArc(0),
308 BoxHandle::RotArc(1),
309 BoxHandle::RotArc(2),
310 ];
311
312 let mut positions = Vec::with_capacity(10);
313 let mut vectors = Vec::with_capacity(10);
314 let mut scalars = Vec::with_capacity(10);
315
316 for handle in all_handles {
317 let pos = self.handle_pos(handle);
318 let target_px = if matches!(handle, BoxHandle::RotArc(_)) { 7.0 } else { 9.0 };
319 let r = handle_world_radius(pos, &ctx.camera, ctx.viewport_size.y, target_px);
320 let s = if self.hovered_handle == Some(handle) || self.active_handle == Some(handle) {
321 1.0_f32
322 } else {
323 0.2
324 };
325 positions.push(pos.to_array());
326 vectors.push([r, 0.0, 0.0]);
327 scalars.push(s);
328 }
329
330 GlyphItem {
331 positions,
332 vectors,
333 scale: 1.0,
334 scale_by_magnitude: true,
335 scalars,
336 scalar_range: Some((0.0, 1.0)),
337 glyph_type: GlyphType::Sphere,
338 id: id_base,
339 default_color: self.handle_color,
340 use_default_color: self.handle_color[3] > 0.0,
341 ..GlyphItem::default()
342 }
343 }
344
345 fn handle_pos(&self, handle: BoxHandle) -> glam::Vec3 {
351 match handle {
352 BoxHandle::Center => self.center,
353 BoxHandle::Face(i) => {
354 self.center + self.rotation * (Self::local_face_normal(i) * self.half_extents[i / 2])
355 }
356 BoxHandle::RotArc(i) => self.center + self.arc_grip_offset(i),
357 }
358 }
359
360 fn local_face_normal(i: usize) -> glam::Vec3 {
362 match i {
363 0 => glam::Vec3::X,
364 1 => glam::Vec3::NEG_X,
365 2 => glam::Vec3::Y,
366 3 => glam::Vec3::NEG_Y,
367 4 => glam::Vec3::Z,
368 _ => glam::Vec3::NEG_Z,
369 }
370 }
371
372 fn world_rotation_axis(i: usize) -> glam::Vec3 {
374 match i {
375 0 => glam::Vec3::X,
376 1 => glam::Vec3::Y,
377 _ => glam::Vec3::Z,
378 }
379 }
380
381 fn arc_radius(&self) -> f32 {
383 self.half_extents.length() * 1.4 + 0.1
384 }
385
386 fn arc_grip_offset(&self, i: usize) -> glam::Vec3 {
388 let r = self.arc_radius();
389 match i {
392 0 => glam::Vec3::new(0.0, r, 0.0), 1 => glam::Vec3::new(0.0, 0.0, r), _ => glam::Vec3::new(r, 0.0, 0.0), }
396 }
397
398 fn hit_test(
399 &self,
400 ray_origin: glam::Vec3,
401 ray_dir: glam::Vec3,
402 ctx: &WidgetContext,
403 ) -> Option<BoxHandle> {
404 let ray = Ray::new(
405 Vector::new(ray_origin.x, ray_origin.y, ray_origin.z),
406 Vector::new(ray_dir.x, ray_dir.y, ray_dir.z),
407 );
408
409 let all_handles = [
410 BoxHandle::Center,
411 BoxHandle::Face(0),
412 BoxHandle::Face(1),
413 BoxHandle::Face(2),
414 BoxHandle::Face(3),
415 BoxHandle::Face(4),
416 BoxHandle::Face(5),
417 BoxHandle::RotArc(0),
418 BoxHandle::RotArc(1),
419 BoxHandle::RotArc(2),
420 ];
421
422 let mut best: Option<(f32, BoxHandle)> = None;
423
424 for handle in all_handles {
425 let pos = self.handle_pos(handle);
426 let target_px = if matches!(handle, BoxHandle::RotArc(_)) { 7.0 } else { 9.0 };
427 let r = handle_world_radius(pos, &ctx.camera, ctx.viewport_size.y, target_px);
428 let ball = parry3d::shape::Ball::new(r);
429 let pose = Pose::from_parts([pos.x, pos.y, pos.z].into(), glam::Quat::IDENTITY);
430 if let Some(t) = ball.cast_ray(&pose, &ray, f32::MAX, true) {
431 if best.is_none() || t < best.unwrap().0 {
432 best = Some((t, handle));
433 }
434 }
435 }
436
437 best.map(|(_, h)| h)
438 }
439}