Skip to content

Commit

Permalink
Refactor: Starting widget gizmo
Browse files Browse the repository at this point in the history
  • Loading branch information
brenocq committed Mar 3, 2024
1 parent 7c5a8de commit 85a820e
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 42 deletions.
12 changes: 6 additions & 6 deletions src/atta/graphics/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ void setUiShutDownFunc(std::function<void()> uiShutDownFunc);
void setUiStartUpFunc(std::function<void()> uiStartUpFunc);

//----- Viewport -----//
std::vector<std::shared_ptr<Viewport>> getViewports();
void clearViewports();
void addViewport(std::shared_ptr<Viewport> viewport);
void removeViewport(std::shared_ptr<Viewport> viewport);
void createDefaultViewports();
component::EntityId viewportEntityClick(std::shared_ptr<Viewport> viewport, vec2i pos);
// std::vector<std::shared_ptr<Viewport>> getViewports();
// void clearViewports();
// void addViewport(std::shared_ptr<Viewport> viewport);
// void removeViewport(std::shared_ptr<Viewport> viewport);
// void createDefaultViewports();
// component::EntityId viewportEntityClick(std::shared_ptr<Viewport> viewport, vec2i pos);
void* getImGuiImage(StringId sid);

} // namespace atta::graphics
Expand Down
22 changes: 11 additions & 11 deletions src/atta/graphics/manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ class Manager final {

friend std::shared_ptr<GraphicsAPI> getGraphicsAPI();
friend std::shared_ptr<Window> getWindow();
friend std::vector<std::shared_ptr<Viewport>> getViewports();
friend void clearViewports();
friend void addViewport(std::shared_ptr<Viewport> viewport);
friend void removeViewport(std::shared_ptr<Viewport> viewport);
friend void createDefaultViewports();
friend component::EntityId viewportEntityClick(std::shared_ptr<Viewport> viewport, vec2i pos);
// friend std::vector<std::shared_ptr<Viewport>> getViewports();
// friend void clearViewports();
// friend void addViewport(std::shared_ptr<Viewport> viewport);
// friend void removeViewport(std::shared_ptr<Viewport> viewport);
// friend void createDefaultViewports();
// friend component::EntityId viewportEntityClick(std::shared_ptr<Viewport> viewport, vec2i pos);
friend void* getImGuiImage(StringId sid);

const std::unordered_map<StringId, std::shared_ptr<Mesh>>& getMeshes() const;
Expand All @@ -75,11 +75,11 @@ class Manager final {
void recreateGraphicsAPI();

std::vector<std::shared_ptr<Viewport>>& getViewportsImpl();
void clearViewportsImpl();
void addViewportImpl(std::shared_ptr<Viewport> viewport);
void removeViewportImpl(std::shared_ptr<Viewport> viewport);
void createDefaultViewportsImpl();
component::EntityId viewportEntityClickImpl(std::shared_ptr<Viewport> viewport, vec2i pos);
// void clearViewportsImpl();
// void addViewportImpl(std::shared_ptr<Viewport> viewport);
// void removeViewportImpl(std::shared_ptr<Viewport> viewport);
// void createDefaultViewportsImpl();
// component::EntityId viewportEntityClickImpl(std::shared_ptr<Viewport> viewport, vec2i pos);
void* getImGuiImageImpl(StringId sid);

gfx::Image::Format convertFormat(res::Image::Format format) const;
Expand Down
120 changes: 120 additions & 0 deletions src/atta/ui/editor/widgets/gizmo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//--------------------------------------------------
// Atta UI Module
// guizmo.h
// Date: 2024-01-05
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/ui/editor/widgets/gizmo.h>

#include <atta/component/components/relationship.h>
#include <atta/component/components/transform.h>

#include <imgui.h>
#include <imgui_internal.h>

#include <ImGuizmo.h>

namespace atta::ui {

Gizmo::Gizmo() : _operation(TRANSLATE), _mode(WORLD), _isOrthographic(false), _snap(false) {}

void Gizmo::setOperation(Operation operation) { _operation = operation; }
void Gizmo::setMode(Mode mode) { _mode = mode; }
void Gizmo::setSnap(bool snap) { _snap = snap; }

void Gizmo::setCamera(std::weak_ptr<Camera> camera) { _camera = camera; }

ImGuizmo::OPERATION convert(Operation operation) {
ImGuizmo::OPERATION result{};
if (operation & TRANSLATE > 0)
result = result | ImGuizmo::OPERATION::TRANSLATE;
if (operation & ROTATE > 0)
result = result | ImGuizmo::OPERATION::ROTATE;
if (operation & SCALE > 0)
result = result | ImGuizmo::OPERATION::SCALE;
return result;
}

ImGuizmo::MODE convert(Mode mode) { return mode == WORLD ? ImGuizmo::MODE::WORLD : ImGuizmo::MODE::LOCAL; }

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

ImGuizmo::SetDrawlist();
ImGuizmo::SetRect(ImGui::GetWindowPos().x + 5.0f, ImGui::GetWindowPos().y + 24.0f, 500, 500);

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());
proj.mat[1][1] *= -1;

if (ImGuizmo::Manipulate(transpose(_view).data, _proj.data, operation, mode, transform.data, nullptr, snap ? snapValues : nullptr)) {
transform.transpose();

// Get changed
vec3 pos, scale;
quat newOri;
transform.getPosOriScale(pos, newOri, scale);
vec3 oriDelta = newOri.getEuler() - t->orientation.getEuler();
quat ori;
ori.setEuler(t->orientation.getEuler() + oriDelta);

// Delta world to local
component::Relationship* r = component::getComponent<component::Relationship>(entity);
if (r && r->getParent() != -1) {
// Get transform of the first entity that has transform when going up in the hierarchy
component::Transform* pt = nullptr;
component::EntityId parentId = -1;
while (pt == nullptr) {
parentId = r->getParent();
pt = component::getComponent<component::Transform>(parentId);
r = component::getComponent<component::Relationship>(parentId);
if (r->getParent() == -1)
break;
}

// If found some entity with transform component, convert result to be relative to it
if (pt) {
component::Transform pTransform = pt->getWorldTransform(parentId);
vec3 pPos = pTransform.position;
vec3 pScale = pTransform.scale;
quat pOri = pTransform.orientation;

// Calculate pos ori scale relative to parent
pos -= pPos;
scale /= pScale;
ori = ori * (-pOri); // Rotation from pOri to ori
}
}

// Update entity transform
if (mouseOperation == ImGuizmo::OPERATION::TRANSLATE)
t->position = pos;
else if (mouseOperation == ImGuizmo::OPERATION::ROTATE)
t->orientation = ori;
else if (mouseOperation == ImGuizmo::OPERATION::SCALE)
t->scale = scale;

// component::RigidBody2D* rb2d = component::getComponent<component::RigidBody2D>(entity);
// if (rb2d) {
// if (mouseOperation == ImGuizmo::OPERATION::TRANSLATE || mouseOperation == ImGuizmo::OPERATION::ROTATE) {
// vec2 pos = vec2(t->position);
// float angle = -t->orientation.getEuler().z;
// rb2d->setTransform(pos, angle);
// } else if (mouseOperation == ImGuizmo::OPERATION::SCALE) {
// // TODO Recreate box2d rigid body
// }
// }
return true;
}
}
return false;
}

} // namespace atta::ui
38 changes: 38 additions & 0 deletions src/atta/ui/editor/widgets/gizmo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//--------------------------------------------------
// Atta UI Module
// guizmo.h
// Date: 2024-01-05
// By Breno Cunha Queiroz
//--------------------------------------------------
#include <atta/component/interface.h>
#include <atta/graphics/cameras/camera.h>

namespace atta::ui {

class Gizmo {
public:
enum Operation {
TRANSLATE = 1 << 0,
ROTATE = 1 << 1,
SCALE = 1 << 2,
};
inline Operation operator|(OPERATION l, OPERATION r) { return Operation(int(l) | int(r)); }
enum Mode { WORLD = 0, LOCAL };

Gizmo();

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

bool manipulate(component::EntityId entity);

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

} // namespace atta::ui
25 changes: 0 additions & 25 deletions src/atta/ui/editor/windows/viewportWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
#include <imgui.h>
#include <imgui_internal.h>

#include <ImGuizmo.h>

namespace atta::ui {

void ViewportWindows::render() {
Expand Down Expand Up @@ -199,29 +197,6 @@ void ViewportWindows::render() {
}
}

//----- Overlay -----//
//{
// ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_AlwaysAutoResize |
// ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav |
// ImGuiWindowFlags_NoMove;
// ImVec2 window = ImGui::GetWindowPos();
// ImGui::SetNextWindowPos(ImVec2(window.x + 10, window.y + 30));
// ImGui::SetNextWindowBgAlpha(0.35f);
// bool open = true;
// if (ImGui::Begin((viewport->getSID().getString() + "Overlay").c_str(), &open, window_flags)) {
// ImGui::Text("To move the camera");
// ImGui::BulletText("Holding mouse middle button");
// ImGui::BulletText("Rotate with mouse");
// ImGui::BulletText("Move with ASWD QE");
// ImGui::Text("To move objects");
// ImGui::BulletText("Select some object");
// ImGui::BulletText("Translate: SHIFT+t");
// ImGui::BulletText("Scale: SHIFT+s");
// ImGui::BulletText("Rotate: SHIFT+r");
// }
// ImGui::End();
//}

//----- Resize -----//
ImVec2 windowSize = ImGui::GetWindowSize();
if (windowSize.x != size.x || windowSize.y != size.y)
Expand Down

0 comments on commit 85a820e

Please sign in to comment.