Skip to content

Commit

Permalink
Clean up gizmo API design slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jul 18, 2024
1 parent 42491ff commit 8c108d7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 43 deletions.
16 changes: 3 additions & 13 deletions src/OpenSimCreator/UI/MeshImporter/MeshImporterTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1825,24 +1825,14 @@ class osc::mi::MeshImporterTab::Impl final : public IMeshImporterUILayerHost {

ui::same_line();

{
ui::GizmoOperation op = m_Gizmo.operation();
if (ui::draw_gizmo_op_selector(op)) {
m_Gizmo.set_operation(op);
}
}
ui::draw_gizmo_op_selector(m_Gizmo);

ui::push_style_var(ImGuiStyleVar_ItemSpacing, {0.0f, 0.0f});
ui::same_line();
ui::pop_style_var();

// local/global dropdown
{
ui::GizmoMode mode = m_Gizmo.mode();
if (ui::draw_gizmo_mode_selector(mode)) {
m_Gizmo.set_mode(mode);
}
}
ui::draw_gizmo_mode_selector(m_Gizmo);
ui::same_line();

// scale factor
Expand Down Expand Up @@ -2128,7 +2118,7 @@ class osc::mi::MeshImporterTab::Impl final : public IMeshImporterUILayerHost {
MIObject& el = m_Shared->updModelGraph().updByID(id);
switch (m_Gizmo.operation()) {
case ui::GizmoOperation::Rotate:
el.applyRotation(m_Shared->getModelGraph(), userManipulation->rotation, Vec3{m_GizmoModelMatrix[3]});
el.applyRotation(m_Shared->getModelGraph(), extract_eulers_xyz(userManipulation->rotation), Vec3{m_GizmoModelMatrix[3]});
break;
case ui::GizmoOperation::Translate: {
el.applyTranslation(m_Shared->getModelGraph(), userManipulation->position);
Expand Down
18 changes: 4 additions & 14 deletions src/OpenSimCreator/UI/Shared/ModelEditorViewerPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,17 @@ namespace
ui::same_line();

// draw translate/rotate/scale selector
{
ui::GizmoOperation op = m_Gizmo.getOperation();
if (ui::draw_gizmo_op_selector(op, true, true, false))
{
m_Gizmo.setOperation(op);
edited = true;
}
if (ui::draw_gizmo_op_selector(m_Gizmo, true, true, false)) {
edited = true;
}

ui::push_style_var(ImGuiStyleVar_ItemSpacing, {0.0f, 0.0f});
ui::same_line();
ui::pop_style_var();

// draw global/world selector
{
ui::GizmoMode mode = m_Gizmo.getMode();
if (ui::draw_gizmo_mode_selector(mode))
{
m_Gizmo.setMode(mode);
edited = true;
}
if (ui::draw_gizmo_mode_selector(m_Gizmo)) {
edited = true;
}

return edited;
Expand Down
2 changes: 2 additions & 0 deletions src/OpenSimCreator/UI/Shared/ModelSelectionGizmo.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ namespace osc
ui::GizmoMode getMode() { return m_Gizmo.mode(); }
void setMode(ui::GizmoMode mode) { m_Gizmo.set_mode(mode); }

operator ui::Gizmo& () { return m_Gizmo; }

private:
std::shared_ptr<UndoableModelStatePair> m_Model;
ui::Gizmo m_Gizmo;
Expand Down
33 changes: 28 additions & 5 deletions src/oscar/UI/oscimgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ namespace
}
}

bool osc::ui::draw_gizmo_mode_selector(Gizmo& gizmo)
{
GizmoMode mode = gizmo.mode();
if (draw_gizmo_mode_selector(mode)) {
gizmo.set_mode(mode);
return true;
}
return false;
}

bool osc::ui::draw_gizmo_mode_selector(GizmoMode& mode)
{
constexpr auto mode_labels = std::to_array({ "local", "global" });
Expand All @@ -68,7 +78,21 @@ bool osc::ui::draw_gizmo_mode_selector(GizmoMode& mode)
}

bool osc::ui::draw_gizmo_op_selector(
ui::GizmoOperation& op,
Gizmo& gizmo,
bool can_translate,
bool can_rotate,
bool can_scale)
{
GizmoOperation op = gizmo.operation();
if (draw_gizmo_op_selector(op, can_translate, can_rotate, can_scale)) {
gizmo.set_operation(op);
return true;
}
return false;
}

bool osc::ui::draw_gizmo_op_selector(
GizmoOperation& op,
bool can_translate,
bool can_rotate,
bool can_scale)
Expand Down Expand Up @@ -132,7 +156,7 @@ bool osc::ui::draw_gizmo_op_selector(
return rv;
}

std::optional<ui::GizmoTransform> osc::ui::Gizmo::draw(
std::optional<Transform> osc::ui::Gizmo::draw(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
Expand Down Expand Up @@ -197,12 +221,11 @@ std::optional<ui::GizmoTransform> osc::ui::Gizmo::draw(
value_ptr(world_scale)
);

const GizmoTransform rv = {
return Transform{
.scale = world_scale,
.rotation = EulerAnglesIn<Degrees>{world_rotation_in_degrees},
.rotation = to_worldspace_rotation_quat(EulerAnglesIn<Degrees>{world_rotation_in_degrees}),
.position = world_translation,
};
return rv;
}

bool osc::ui::Gizmo::is_using() const
Expand Down
24 changes: 13 additions & 11 deletions src/oscar/UI/oscimgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,16 +834,6 @@ namespace osc::ui
ImGui::ShowDemoWindow();
}

// a the "difference" added by a user-enacted manipulation with a `Gizmo`
//
// this can be left-multiplied by the original model matrix to apply the
// user's transformation
struct GizmoTransform final {
Vec3 scale = {1.0f, 1.0f, 1.0f};
EulerAngles rotation = {};
Vec3 position = {};
};

// an operation that a ui `Gizmo` shall perform
enum class GizmoOperation {
Translate,
Expand Down Expand Up @@ -873,7 +863,7 @@ namespace osc::ui
// you're thinking "oh I'm only handling rotation/scaling, so I'll
// ignore the translational part of the transform" then you're in
// for a nasty surprise: T(origin)*R*S*T(-origin)
std::optional<GizmoTransform> draw(
std::optional<Transform> draw(
Mat4& model_matrix, // edited in-place
const Mat4& view_matrix,
const Mat4& projection_matrix,
Expand All @@ -896,9 +886,21 @@ namespace osc::ui
bool was_using_last_frame_ = false;
};

bool draw_gizmo_mode_selector(
Gizmo&
);

bool draw_gizmo_mode_selector(
GizmoMode&
);

bool draw_gizmo_op_selector(
Gizmo&,
bool can_translate = true,
bool can_rotate = true,
bool can_scale = true
);

bool draw_gizmo_op_selector(
GizmoOperation&,
bool can_translate = true,
Expand Down

0 comments on commit 8c108d7

Please sign in to comment.