From d221242e053ea8ae968a3611e488aaffdf9b34bd Mon Sep 17 00:00:00 2001 From: Urho Laukkarinen Date: Thu, 1 Feb 2024 21:11:12 +0200 Subject: [PATCH] GizmoResult::value is now optional --- demo/src/main.rs | 23 ++++++++++++++--------- src/lib.rs | 2 +- src/subgizmo/arcball.rs | 14 ++++++-------- src/subgizmo/rotation.rs | 6 ++++-- src/subgizmo/scale.rs | 2 +- src/subgizmo/translation.rs | 2 +- 6 files changed, 27 insertions(+), 22 deletions(-) diff --git a/demo/src/main.rs b/demo/src/main.rs index c4a94db..780b98c 100644 --- a/demo/src/main.rs +++ b/demo/src/main.rs @@ -304,15 +304,20 @@ fn instructions_text(ui: &Ui) { } fn show_gizmo_status(ui: &Ui, response: GizmoResult) { - let length = Vec3::from(response.value).length(); - - let text = match response.mode { - GizmoMode::Rotate => format!("{:.1}°, {:.2} rad", length.to_degrees(), length), - - GizmoMode::Translate | GizmoMode::Scale => format!( - "dX: {:.2}, dY: {:.2}, dZ: {:.2}", - response.value[0], response.value[1], response.value[2] - ), + let text = if let Some(value) = response.value { + match response.mode { + GizmoMode::Rotate => { + let length = Vec3::from(value).length(); + format!("{:.1}°, {:.2} rad", length.to_degrees(), length) + } + + GizmoMode::Translate | GizmoMode::Scale => format!( + "dX: {:.2}, dY: {:.2}, dZ: {:.2}", + value[0], value[1], value[2] + ), + } + } else { + String::new() }; let rect = ui.clip_rect(); diff --git a/src/lib.rs b/src/lib.rs index d6f0ee8..75ded21 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -416,7 +416,7 @@ pub struct GizmoResult { /// Mode of the active subgizmo pub mode: GizmoMode, /// Total scale, rotation or translation of the current gizmo activation, depending on mode - pub value: [f32; 3], + pub value: Option<[f32; 3]>, } impl GizmoResult { diff --git a/src/subgizmo/arcball.rs b/src/subgizmo/arcball.rs index 7115d53..fb6871b 100644 --- a/src/subgizmo/arcball.rs +++ b/src/subgizmo/arcball.rs @@ -10,7 +10,7 @@ pub(crate) type ArcballSubGizmo = SubGizmoConfig; #[derive(Default, Debug, Copy, Clone)] pub(crate) struct ArcballState { - last: Pos2, + last_pos: Pos2, } impl WidgetData for ArcballState {} @@ -31,7 +31,7 @@ impl SubGizmo for ArcballSubGizmo { } self.update_state_with(ui, |state: &mut ArcballState| { - state.last = ray.screen_pos; + state.last_pos = ray.screen_pos; }); Some(pick_result.t) @@ -40,14 +40,12 @@ impl SubGizmo for ArcballSubGizmo { fn update(&mut self, ui: &Ui, ray: Ray) -> Option { let state = self.state(ui); - let dir = ray.screen_pos - state.last; - - // Not a typical ArcBall rotation, but instead always rotates the object in the direction of mouse movement + let dir = ray.screen_pos - state.last_pos; let quat = if dir.length_sq() > f32::EPSILON { let mat = self.config.view_projection.inverse(); let a = screen_to_world(self.config.viewport, mat, ray.screen_pos, 0.0); - let b = screen_to_world(self.config.viewport, mat, state.last, 0.0); + let b = screen_to_world(self.config.viewport, mat, state.last_pos, 0.0); let origin = self.config.view_forward(); let a = (a - origin).normalize(); let b = (b - origin).normalize(); @@ -58,7 +56,7 @@ impl SubGizmo for ArcballSubGizmo { }; self.update_state_with(ui, |state: &mut ArcballState| { - state.last = ray.screen_pos; + state.last_pos = ray.screen_pos; }); let new_rotation = quat * self.config.rotation; @@ -68,7 +66,7 @@ impl SubGizmo for ArcballSubGizmo { rotation: new_rotation.as_f32().into(), translation: self.config.translation.as_vec3().into(), mode: GizmoMode::Rotate, - value: [0.0; 3], // TODO + value: None, }) } diff --git a/src/subgizmo/rotation.rs b/src/subgizmo/rotation.rs index d5d016d..00d6e44 100644 --- a/src/subgizmo/rotation.rs +++ b/src/subgizmo/rotation.rs @@ -110,8 +110,10 @@ impl SubGizmo for RotationSubGizmo { rotation: new_rotation.as_f32().into(), translation: self.config.translation.as_vec3().into(), mode: GizmoMode::Rotate, - value: (gizmo_normal(&self.config, self.direction).as_vec3() * state.current_delta) - .to_array(), + value: Some( + (gizmo_normal(&self.config, self.direction).as_vec3() * state.current_delta) + .to_array(), + ), }) } diff --git a/src/subgizmo/scale.rs b/src/subgizmo/scale.rs index 0b2afd9..65a9bf3 100644 --- a/src/subgizmo/scale.rs +++ b/src/subgizmo/scale.rs @@ -89,7 +89,7 @@ impl SubGizmo for ScaleSubGizmo { rotation: self.config.rotation.as_f32().into(), translation: self.config.translation.as_vec3().into(), mode: GizmoMode::Scale, - value: offset.as_vec3().to_array(), + value: Some(offset.as_vec3().to_array()), }) } diff --git a/src/subgizmo/translation.rs b/src/subgizmo/translation.rs index ffa2ccf..4b5a195 100644 --- a/src/subgizmo/translation.rs +++ b/src/subgizmo/translation.rs @@ -95,7 +95,7 @@ impl SubGizmo for TranslationSubGizmo { rotation: self.config.rotation.as_f32().into(), translation: new_translation.as_vec3().into(), mode: GizmoMode::Translate, - value: state.current_delta.as_vec3().to_array(), + value: Some(state.current_delta.as_vec3().to_array()), }) }