Skip to content

Commit

Permalink
GizmoResult::value is now optional
Browse files Browse the repository at this point in the history
  • Loading branch information
urholaukkarinen committed Feb 1, 2024
1 parent 38e5fa0 commit d221242
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
23 changes: 14 additions & 9 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 6 additions & 8 deletions src/subgizmo/arcball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) type ArcballSubGizmo = SubGizmoConfig<Arcball>;

#[derive(Default, Debug, Copy, Clone)]
pub(crate) struct ArcballState {
last: Pos2,
last_pos: Pos2,
}

impl WidgetData for ArcballState {}
Expand All @@ -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)
Expand All @@ -40,14 +40,12 @@ impl SubGizmo for ArcballSubGizmo {
fn update(&mut self, ui: &Ui, ray: Ray) -> Option<GizmoResult> {
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();
Expand All @@ -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;
Expand All @@ -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,
})
}

Expand Down
6 changes: 4 additions & 2 deletions src/subgizmo/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/subgizmo/scale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/subgizmo/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
})
}

Expand Down

0 comments on commit d221242

Please sign in to comment.