Skip to content

Commit

Permalink
Fix: Guizmo interaction
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Jan 19, 2025
1 parent 5edf760 commit a4ac94f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
19 changes: 10 additions & 9 deletions src/atta/ui/widgets/gizmo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gizmo::Gizmo() : _operation(TRANSLATE), _mode(WORLD), _snap(false) {}

void Gizmo::setOperation(Operation operation) { _operation = operation; }
void Gizmo::setMode(Mode mode) { _mode = mode; }
void Gizmo::setCamera(std::weak_ptr<gfx::Camera> camera) { _camera = camera; }
void Gizmo::setViewport(std::weak_ptr<Viewport> viewport) { _viewport = viewport; }
void Gizmo::setSnap(bool snap) { _snap = snap; }

ImGuizmo::OPERATION convert(Gizmo::Operation operation) {
Expand All @@ -37,29 +37,30 @@ ImGuizmo::OPERATION convert(Gizmo::Operation operation) {
ImGuizmo::MODE convert(Gizmo::Mode mode) { return mode == Gizmo::WORLD ? ImGuizmo::MODE::WORLD : ImGuizmo::MODE::LOCAL; }

bool Gizmo::manipulate(cmp::EntityId entity) {
std::shared_ptr<gfx::Camera> camera = _camera.lock();
std::shared_ptr<Viewport> viewport = _viewport.lock();
cmp::Transform* t = cmp::getComponent<cmp::Transform>(entity);
if (camera && t) {
if (viewport && t) {
mat4 transform = transpose(t->getWorldTransformMatrix(entity));

ImGuizmo::SetOrthographic(viewport->getCamera()->getName() == "OrthographicCamera");
ImGuizmo::SetDrawlist();
ImVec2 windowPos = ImGui::GetWindowPos();
ImVec2 windowSize = ImGui::GetWindowSize();
ImGuizmo::SetRect(windowPos.x, windowPos.y, windowSize.x - 10.0f, windowSize.y - 8.0f);
ImGuizmo::SetRect(windowPos.x, windowPos.y + 22.0f, viewport->getWidth(), viewport->getHeight());

ImGuizmo::OPERATION operation = convert(_operation);
ImGuizmo::MODE mode = convert(_mode);

ImGuizmo::SetOrthographic(camera->getName() == "OrthographicCamera");
mat4 view = transpose(camera->getView());
mat4 proj = transpose(camera->getProj());
mat4 view = transpose(viewport->getCamera()->getView());
mat4 proj = transpose(viewport->getCamera()->getProj());
proj.mat[1][1] *= -1;

float snapValue = 0.5f;
if (operation == ImGuizmo::OPERATION::ROTATE)
snapValue = 45.0f;
float snapValues[3] = {snapValue, snapValue, snapValue};

if (ImGuizmo::Manipulate(view.data, proj.data, operation, mode, transform.data, nullptr, _snap ? snapValues : nullptr)) {
ImGuizmo::Manipulate(view.data, proj.data, operation, mode, transform.data, nullptr, _snap ? snapValues : nullptr);
if (ImGuizmo::IsUsing()) {
transform.transpose();

// Get changed
Expand Down
6 changes: 3 additions & 3 deletions src/atta/ui/widgets/gizmo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define ATTA_UI_WIDGETS_GUIZMO_H

#include <atta/component/interface.h>
#include <atta/graphics/cameras/camera.h>
#include <atta/ui/windows/viewport/viewport.h>

namespace atta::ui {

Expand All @@ -30,15 +30,15 @@ class Gizmo {

void setOperation(Operation operation);
void setMode(Mode mode);
void setCamera(std::weak_ptr<gfx::Camera> camera);
void setViewport(std::weak_ptr<Viewport> viewport);
void setSnap(bool snap);

bool manipulate(component::EntityId entity);

private:
Operation _operation;
Mode _mode;
std::weak_ptr<gfx::Camera> _camera;
std::weak_ptr<Viewport> _viewport;
bool _snap;
};

Expand Down
16 changes: 8 additions & 8 deletions src/atta/ui/windows/viewport/viewportWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,6 @@ void ViewportWindows::renderUI() {
if (ImGui::IsMouseClicked(2) && ImGui::IsWindowHovered())
activeViewport = i;

// Update camera (wheel pressed)
if (activeViewport == i && ImGui::IsWindowHovered()) {
viewport->getCamera()->setViewportSize(viewport->getWidth(), viewport->getHeight());
viewport->getCamera()->move();
}

//----- Mouse click -----//
vec2i click = {-1, -1};
if (ImGui::IsMouseClicked(0) && ImGui::IsWindowHovered()) {
Expand All @@ -109,7 +103,7 @@ void ViewportWindows::renderUI() {
addBasicShapePopup();

//----- Operation selection -----//
if (ImGui::IsWindowHovered()) {
if (ImGui::IsWindowHovered() && !ImGui::IsMouseDown(ImGuiMouseButton_Middle)) {
ImGuiIO& io = ImGui::GetIO();

if (io.KeyCtrl)
Expand All @@ -131,11 +125,17 @@ void ViewportWindows::renderUI() {

//----- Gizmo manipulation -----//
bool gizmoUsingMouse = false;
_gizmo.setCamera(viewport->getCamera());
_gizmo.setViewport(viewport);
cmp::EntityId eid = component::getSelectedEntity();
if (eid >= 0 && _gizmo.manipulate(eid))
gizmoUsingMouse = true;

//----- Camera control -----//
if (activeViewport == i && ImGui::IsWindowHovered()) {
viewport->getCamera()->setViewportSize(viewport->getWidth(), viewport->getHeight());
viewport->getCamera()->move();
}

//----- Mouse click selection -----//
if (!gizmoUsingMouse) {
if (click.x >= 0 && click.y >= 0 && click.x < (int)viewport->getWidth() && click.y < (int)viewport->getHeight()) {
Expand Down

0 comments on commit a4ac94f

Please sign in to comment.