diff --git a/src/atta/ui/widgets/gizmo.cpp b/src/atta/ui/widgets/gizmo.cpp index 2833704d..901abec6 100644 --- a/src/atta/ui/widgets/gizmo.cpp +++ b/src/atta/ui/widgets/gizmo.cpp @@ -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 camera) { _camera = camera; } +void Gizmo::setViewport(std::weak_ptr viewport) { _viewport = viewport; } void Gizmo::setSnap(bool snap) { _snap = snap; } ImGuizmo::OPERATION convert(Gizmo::Operation operation) { @@ -37,21 +37,21 @@ 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 camera = _camera.lock(); + std::shared_ptr viewport = _viewport.lock(); cmp::Transform* t = cmp::getComponent(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; @@ -59,7 +59,8 @@ bool Gizmo::manipulate(cmp::EntityId entity) { 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 diff --git a/src/atta/ui/widgets/gizmo.h b/src/atta/ui/widgets/gizmo.h index c36d1fa3..abb2979f 100644 --- a/src/atta/ui/widgets/gizmo.h +++ b/src/atta/ui/widgets/gizmo.h @@ -8,7 +8,7 @@ #define ATTA_UI_WIDGETS_GUIZMO_H #include -#include +#include namespace atta::ui { @@ -30,7 +30,7 @@ class Gizmo { void setOperation(Operation operation); void setMode(Mode mode); - void setCamera(std::weak_ptr camera); + void setViewport(std::weak_ptr viewport); void setSnap(bool snap); bool manipulate(component::EntityId entity); @@ -38,7 +38,7 @@ class Gizmo { private: Operation _operation; Mode _mode; - std::weak_ptr _camera; + std::weak_ptr _viewport; bool _snap; }; diff --git a/src/atta/ui/windows/viewport/viewportWindows.cpp b/src/atta/ui/windows/viewport/viewportWindows.cpp index 9c8f908a..79e888f3 100644 --- a/src/atta/ui/windows/viewport/viewportWindows.cpp +++ b/src/atta/ui/windows/viewport/viewportWindows.cpp @@ -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()) { @@ -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) @@ -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()) {