Skip to content

Commit

Permalink
Remove ImGuizmo references into oscimgui.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Sep 16, 2024
1 parent c043e0f commit c57db56
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/oscar/Platform/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ namespace osc
public:
explicit MouseEvent(const SDL_Event&);

Vec2 relative_delta() const { return relative_delta_; }
Vec2 relative_delta() const { return relative_delta_; }
private:
Vec2 relative_delta_;
};
Expand Down
64 changes: 63 additions & 1 deletion src/oscar/UI/oscimgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,37 @@ bool osc::ui::draw_float_circular_slider(

// gizmo stuff

void osc::ui::gizmo_demo_draw_grid(
const Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
float grid_size,
const Rect& screenspace_rect)
{
ImGuizmo::SetRect(
screenspace_rect.p1.x,
screenspace_rect.p1.y,
dimensions_of(screenspace_rect).x,
dimensions_of(screenspace_rect).y
);
ImGuizmo::DrawGrid(value_ptr(view_matrix), value_ptr(projection_matrix), value_ptr(model_matrix), grid_size);
}

void osc::ui::gizmo_demo_draw_cube(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect)
{
ImGuizmo::SetRect(
screenspace_rect.p1.x,
screenspace_rect.p1.y,
dimensions_of(screenspace_rect).x,
dimensions_of(screenspace_rect).y
);
ImGuizmo::DrawCubes(value_ptr(view_matrix), value_ptr(projection_matrix), value_ptr(model_matrix), 1);
}

bool osc::ui::draw_gizmo_mode_selector(Gizmo& gizmo)
{
GizmoMode mode = gizmo.mode();
Expand Down Expand Up @@ -2574,6 +2605,37 @@ std::optional<Transform> osc::ui::Gizmo::draw(
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect)
{
return draw_to(
model_matrix,
view_matrix,
projection_matrix,
screenspace_rect,
ImGui::GetWindowDrawList()
);
}

std::optional<Transform> osc::ui::Gizmo::draw_to_foreground(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect)
{
return draw_to(
model_matrix,
view_matrix,
projection_matrix,
screenspace_rect,
ImGui::GetForegroundDrawList()
);
}

std::optional<Transform> osc::ui::Gizmo::draw_to(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect,
ImDrawList* drawlist)
{
if (operation_ == GizmoOperation::None) {
return std::nullopt; // disabled
Expand All @@ -2593,7 +2655,7 @@ std::optional<Transform> osc::ui::Gizmo::draw(
dimensions_of(screenspace_rect).x,
dimensions_of(screenspace_rect).y
);
ImGuizmo::SetDrawlist(ImGui::GetWindowDrawList());
ImGuizmo::SetDrawlist(drawlist);
ImGuizmo::AllowAxisFlip(false); // user's didn't like this feature in UX sessions

// use rotation from the parent, translation from station
Expand Down
33 changes: 33 additions & 0 deletions src/oscar/UI/oscimgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,16 @@ namespace osc::ui
const Mat4& projection_matrix,
const Rect& screenspace_rect
);

// same as `draw`, but draws to the foreground drawlist, rather than the
// drawlist of the currently active panel
std::optional<Transform> draw_to_foreground(
Mat4& model_matrix, // edited in-place
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect
);

bool is_using() const;
bool was_using() const { return was_using_last_frame_; }
bool is_over() const;
Expand All @@ -924,12 +934,35 @@ namespace osc::ui
// updates the gizmo based on keyboard inputs (e.g. pressing `G` enables grab mode)
bool handle_keyboard_inputs();
private:
std::optional<Transform> draw_to(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect,
ImDrawList* drawlist
);

UID id_;
GizmoOperation operation_ = GizmoOperation::Translate;
GizmoMode mode_ = GizmoMode::World;
bool was_using_last_frame_ = false;
};

void gizmo_demo_draw_grid(
const Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
float grid_size,
const Rect& screenspace_rect
);

void gizmo_demo_draw_cube(
Mat4& model_matrix,
const Mat4& view_matrix,
const Mat4& projection_matrix,
const Rect& screenspace_rect
);

bool draw_gizmo_mode_selector(
Gizmo&
);
Expand Down
2 changes: 1 addition & 1 deletion src/oscar/UI/ui_graphics_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

#include <ankerl/unordered_dense.h>
#define IMGUI_USER_CONFIG <oscar/UI/oscimgui_config.h> // NOLINT(bugprone-macro-parentheses)
#include <imgui/imgui.h>
#include <imgui.h>

#include <array>
#include <cstddef>
Expand Down
42 changes: 14 additions & 28 deletions src/oscar_demos/ImGuizmoDemoTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

#include <oscar/oscar.h>

#include <imgui.h>
#include <ImGuizmo.h>

#include <memory>

using namespace osc::literals;
Expand All @@ -23,31 +20,20 @@ class osc::ImGuizmoDemoTab::Impl final : public StandardTabImpl {
private:
void impl_on_draw() final
{
// ImGuizmo::BeginFrame(); already done by MainUIScreen

Mat4 view_matrix = scene_camera_.view_matrix();
Rect viewport_ui_rect = ui::get_main_viewport_workspace_uiscreenspace_rect();
Vec2 viewport_dimensions = dimensions_of(viewport_ui_rect);
Mat4 projection_matrix = scene_camera_.projection_matrix(aspect_ratio_of(viewport_dimensions));

ImGuizmo::SetRect(viewport_ui_rect.p1.x, viewport_ui_rect.p1.y, viewport_dimensions.x, viewport_dimensions.y);
Mat4 identity_matrix = identity<Mat4>();
ImGuizmo::DrawGrid(value_ptr(view_matrix), value_ptr(projection_matrix), value_ptr(identity_matrix), 100.f);
ImGuizmo::DrawCubes(value_ptr(view_matrix), value_ptr(projection_matrix), value_ptr(model_matrix_), 1);

ui::draw_checkbox("translate", &translate_mode_enabled_);

ImGuizmo::Manipulate(
value_ptr(view_matrix),
value_ptr(projection_matrix),
translate_mode_enabled_ ? ImGuizmo::TRANSLATE : ImGuizmo::ROTATE,
ImGuizmo::LOCAL,
value_ptr(model_matrix_),
nullptr,
nullptr, //&snap[0], // snap
nullptr, // bound sizing?
nullptr // bound sizing snap
const Mat4 view_matrix = scene_camera_.view_matrix();
const Rect viewport_ui_rect = ui::get_main_viewport_workspace_uiscreenspace_rect();
const Mat4 projection_matrix = scene_camera_.projection_matrix(aspect_ratio_of(dimensions_of(viewport_ui_rect)));

ui::gizmo_demo_draw_grid(identity<Mat4>(), view_matrix, projection_matrix, 100.0f, viewport_ui_rect);
ui::gizmo_demo_draw_cube(model_matrix_, view_matrix, projection_matrix, viewport_ui_rect);
gizmo_.draw_to_foreground(
model_matrix_,
view_matrix,
projection_matrix,
viewport_ui_rect
);
ui::draw_gizmo_mode_selector(gizmo_);
ui::draw_gizmo_op_selector(gizmo_);
}

PolarPerspectiveCamera scene_camera_ = []()
Expand All @@ -60,7 +46,7 @@ class osc::ImGuizmoDemoTab::Impl final : public StandardTabImpl {
return rv;
}();

bool translate_mode_enabled_ = false;
ui::Gizmo gizmo_;
Mat4 model_matrix_ = identity<Mat4>();
};

Expand Down

0 comments on commit c57db56

Please sign in to comment.