From d8817477e43b1f7d35aca2435d2412bfb2a46218 Mon Sep 17 00:00:00 2001 From: Breno Cunha Queiroz Date: Wed, 12 Jun 2024 06:50:43 +0200 Subject: [PATCH] Fix: Viewport compilation --- .../project/projectSerializerDeserialize.cpp | 13 +- .../project/projectSerializerSerialize.cpp | 11 +- src/atta/graphics/interface.cpp | 16 +- src/atta/graphics/manager.cpp | 82 ++--- src/atta/pch.h | 1 + src/atta/ui/editor/topBar/topBar.cpp | 143 +++---- src/atta/ui/editor/topBar/topBar.h | 2 + .../ui/editor/windows/viewportWindows.cpp | 348 +++++++++--------- src/atta/ui/manager.cpp | 2 +- 9 files changed, 312 insertions(+), 306 deletions(-) diff --git a/src/atta/file/project/projectSerializerDeserialize.cpp b/src/atta/file/project/projectSerializerDeserialize.cpp index 4676a67f..1c792bd0 100644 --- a/src/atta/file/project/projectSerializerDeserialize.cpp +++ b/src/atta/file/project/projectSerializerDeserialize.cpp @@ -49,12 +49,13 @@ void ProjectSerializer::deserializeComponentModule(Section& section) { void ProjectSerializer::deserializeGraphicsModule(Section& section) { if (section.contains("viewports")) { std::vector viewports = std::vector(section["viewports"]); - graphics::clearViewports(); - for (auto& viewport : viewports) { - std::shared_ptr v = std::make_shared(); - *v = viewport; - graphics::addViewport(v); - } + LOG_WARN("file::ProjectSerializer", "Deserializing viewports was not implemented yet"); + // graphics::clearViewports(); + // for (auto& viewport : viewports) { + // std::shared_ptr v = std::make_shared(); + // *v = viewport; + // graphics::addViewport(v); + // } } if (section.contains("graphicsFPS")) graphics::setGraphicsFPS(float(section["graphicsFPS"])); diff --git a/src/atta/file/project/projectSerializerSerialize.cpp b/src/atta/file/project/projectSerializerSerialize.cpp index cedd8ba2..df3bdee8 100644 --- a/src/atta/file/project/projectSerializerSerialize.cpp +++ b/src/atta/file/project/projectSerializerSerialize.cpp @@ -59,11 +59,12 @@ void ProjectSerializer::serializeComponentModule(Section& section) { } void ProjectSerializer::serializeGraphicsModule(Section& section) { - std::vector> pviewports = graphics::getViewports(); - std::vector viewports; - for (auto pv : pviewports) - viewports.push_back(*pv); - section["viewports"] = viewports; + // std::vector> pviewports = graphics::getViewports(); + // std::vector viewports; + // for (auto pv : pviewports) + // viewports.push_back(*pv); + // section["viewports"] = viewports; + LOG_WARN("file::ProjectSerializer", "Serializing viewports was not implemented yet"); section["graphicsFPS"] = graphics::getGraphicsFPS(); section["viewportFPS"] = graphics::getViewportFPS(); section["viewportRendering"] = graphics::getViewportRendering(); diff --git a/src/atta/graphics/interface.cpp b/src/atta/graphics/interface.cpp index ad2bc19f..ec6cec22 100644 --- a/src/atta/graphics/interface.cpp +++ b/src/atta/graphics/interface.cpp @@ -34,14 +34,14 @@ void setUiShutDownFunc(std::function uiShutDownFunc) { Manager::getInsta void setUiStartUpFunc(std::function uiStartUpFunc) { Manager::getInstance()._uiStartUpFunc = uiStartUpFunc; } //----- Viewport -----// -std::vector> getViewports() { return Manager::getInstance().getViewportsImpl(); } -void clearViewports() { Manager::getInstance().clearViewportsImpl(); } -void addViewport(std::shared_ptr viewport) { Manager::getInstance().addViewportImpl(viewport); } -void removeViewport(std::shared_ptr viewport) { Manager::getInstance().removeViewportImpl(viewport); } -void createDefaultViewports() { return Manager::getInstance().createDefaultViewportsImpl(); } -component::EntityId viewportEntityClick(std::shared_ptr viewport, vec2i pos) { - return Manager::getInstance().viewportEntityClickImpl(viewport, pos); -} +// std::vector> getViewports() { return Manager::getInstance().getViewportsImpl(); } +// void clearViewports() { Manager::getInstance().clearViewportsImpl(); } +// void addViewport(std::shared_ptr viewport) { Manager::getInstance().addViewportImpl(viewport); } +// void removeViewport(std::shared_ptr viewport) { Manager::getInstance().removeViewportImpl(viewport); } +// void createDefaultViewports() { return Manager::getInstance().createDefaultViewportsImpl(); } +// component::EntityId viewportEntityClick(std::shared_ptr viewport, vec2i pos) { +// return Manager::getInstance().viewportEntityClickImpl(viewport, pos); +//} void* getImGuiImage(StringId sid) { return Manager::getInstance().getImGuiImageImpl(sid); } diff --git a/src/atta/graphics/manager.cpp b/src/atta/graphics/manager.cpp index ca45b4f1..462e3836 100644 --- a/src/atta/graphics/manager.cpp +++ b/src/atta/graphics/manager.cpp @@ -89,7 +89,7 @@ void Manager::startUpImpl() { _computeEntityClick = std::make_unique(); //----- Create viewports -----// - createDefaultViewportsImpl(); + // createDefaultViewportsImpl(); } void Manager::shutDownImpl() { @@ -164,46 +164,46 @@ void Manager::recreateGraphicsAPI() { _uiStartUpFunc(); } -std::vector>& Manager::getViewportsImpl() { return _viewports; } - -void Manager::clearViewportsImpl() { - _viewportsNext.clear(); - _swapViewports = true; -} - -void Manager::addViewportImpl(std::shared_ptr viewport) { - _viewportsNext.push_back(viewport); - _swapViewports = true; -} - -void Manager::removeViewportImpl(std::shared_ptr viewport) { - // TODO make it work with zero viewports - if (_viewportsNext.size() > 1) { - for (unsigned i = 0; i < _viewportsNext.size(); i++) - if (_viewportsNext[i] == viewport) { - _viewportsNext.erase(_viewportsNext.begin() + i); - break; - } - _swapViewports = true; - } else { - LOG_WARN("graphics::Manager", "It is not possible to have 0 viewports yet"); - } -} - -void Manager::createDefaultViewportsImpl() { - _viewportsNext.clear(); - - Viewport::CreateInfo viewportInfo; - viewportInfo.renderer = std::make_shared(); - viewportInfo.camera = std::make_shared(PerspectiveCamera::CreateInfo{}); - viewportInfo.sid = StringId("Main Viewport"); - _viewportsNext.push_back(std::make_shared(viewportInfo)); - _swapViewports = true; -} - -component::EntityId Manager::viewportEntityClickImpl(std::shared_ptr viewport, vec2i pos) { - return _computeEntityClick->click(viewport, pos); -} +// std::vector>& Manager::getViewportsImpl() { return _viewports; } + +// void Manager::clearViewportsImpl() { +// _viewportsNext.clear(); +// _swapViewports = true; +// } +// +// void Manager::addViewportImpl(std::shared_ptr viewport) { +// _viewportsNext.push_back(viewport); +// _swapViewports = true; +// } +// +// void Manager::removeViewportImpl(std::shared_ptr viewport) { +// // TODO make it work with zero viewports +// if (_viewportsNext.size() > 1) { +// for (unsigned i = 0; i < _viewportsNext.size(); i++) +// if (_viewportsNext[i] == viewport) { +// _viewportsNext.erase(_viewportsNext.begin() + i); +// break; +// } +// _swapViewports = true; +// } else { +// LOG_WARN("graphics::Manager", "It is not possible to have 0 viewports yet"); +// } +// } +// +// void Manager::createDefaultViewportsImpl() { +// _viewportsNext.clear(); +// +// Viewport::CreateInfo viewportInfo; +// viewportInfo.renderer = std::make_shared(); +// viewportInfo.camera = std::make_shared(PerspectiveCamera::CreateInfo{}); +// viewportInfo.sid = StringId("Main Viewport"); +// _viewportsNext.push_back(std::make_shared(viewportInfo)); +// _swapViewports = true; +// } +// +// component::EntityId Manager::viewportEntityClickImpl(std::shared_ptr viewport, vec2i pos) { +// return _computeEntityClick->click(viewport, pos); +// } void* Manager::getImGuiImageImpl(StringId sid) { return _images[sid]->getImGuiImage(); } diff --git a/src/atta/pch.h b/src/atta/pch.h index 14ad57d1..d14dc918 100644 --- a/src/atta/pch.h +++ b/src/atta/pch.h @@ -8,6 +8,7 @@ #define ATTA_PRE_COMPILED_HEADER_H #define _USE_MATH_DEFINES // Windows M_PI fix +#include #include #include #include diff --git a/src/atta/ui/editor/topBar/topBar.cpp b/src/atta/ui/editor/topBar/topBar.cpp index 70139fa7..10d020f8 100644 --- a/src/atta/ui/editor/topBar/topBar.cpp +++ b/src/atta/ui/editor/topBar/topBar.cpp @@ -93,45 +93,45 @@ void TopBar::render() { ImGui::EndMenu(); } - if (ImGui::BeginMenu("Viewports")) { - std::vector> viewports = graphics::getViewports(); - _viewportModals.resize(viewports.size()); - int i = 0; - for (auto viewport : viewports) { - if (ImGui::MenuItem(viewport->getName().c_str())) - _viewportModals[i] = true; - i++; - } - - ImGui::Separator(); - - if (ImGui::MenuItem("Create viewport")) { - // Choose viewport name - unsigned newViewportNumber = 0; - bool found = false; - while (!found) { - found = true; - for (auto viewport : viewports) - if (viewport->getSID() == StringId("Viewport " + std::to_string(newViewportNumber))) { - found = false; - break; - } - if (!found) - newViewportNumber++; - } + // if (ImGui::BeginMenu("Viewports")) { + // std::vector> viewports = graphics::getViewports(); + // _viewportModals.resize(viewports.size()); + // int i = 0; + // for (auto viewport : viewports) { + // if (ImGui::MenuItem(viewport->getName().c_str())) + // _viewportModals[i] = true; + // i++; + // } + + // ImGui::Separator(); + + // if (ImGui::MenuItem("Create viewport")) { + // // Choose viewport name + // unsigned newViewportNumber = 0; + // bool found = false; + // while (!found) { + // found = true; + // for (auto viewport : viewports) + // if (viewport->getSID() == StringId("Viewport " + std::to_string(newViewportNumber))) { + // found = false; + // break; + // } + // if (!found) + // newViewportNumber++; + // } + + // // Create viewport + // graphics::Viewport::CreateInfo viewportInfo; + // viewportInfo.renderer = std::make_shared(); + // viewportInfo.camera = std::static_pointer_cast( + // std::make_shared(graphics::PerspectiveCamera::CreateInfo{})); + // viewportInfo.sid = StringId("Viewport " + std::to_string(newViewportNumber)); + // std::shared_ptr viewport = std::make_shared(viewportInfo); + // graphics::addViewport(viewport); + // } - // Create viewport - graphics::Viewport::CreateInfo viewportInfo; - viewportInfo.renderer = std::make_shared(); - viewportInfo.camera = std::static_pointer_cast( - std::make_shared(graphics::PerspectiveCamera::CreateInfo{})); - viewportInfo.sid = StringId("Viewport " + std::to_string(newViewportNumber)); - std::shared_ptr viewport = std::make_shared(viewportInfo); - graphics::addViewport(viewport); - } - - ImGui::EndMenu(); - } + // ImGui::EndMenu(); + //} if (ImGui::BeginMenu("Modules")) { if (ImGui::MenuItem("Graphics")) @@ -459,7 +459,7 @@ void TopBar::saveProjectModal() { component::clear(); component::createDefault(); // Replace viewports with default - graphics::createDefaultViewports(); + // XXX graphics::createDefaultViewports(); if (_quitAfterSaveModal) { event::WindowClose e; @@ -474,38 +474,39 @@ void TopBar::saveProjectModal() { } void TopBar::viewportModals() { - std::vector> viewports = graphics::getViewports(); - static std::vector newViewportModals; // If first time creating the modal - _viewportModals.resize(viewports.size()); - - // Check if first time creating viewport - newViewportModals.resize(_viewportModals.size()); - for (unsigned i = 0; i < newViewportModals.size(); i++) - newViewportModals[i] = !newViewportModals[i] && _viewportModals[i]; - - for (uint32_t i = 0; i < _viewportModals.size(); i++) { - char nameBuf[196]; - sprintf(nameBuf, "%s###ViewportProps%s", viewports[i]->getName().c_str(), viewports[i]->getSID().getString().c_str()); - - bool open = _viewportModals[i]; - if (open) { - if (newViewportModals[i]) - ImGui::SetNextWindowSize(ImVec2(200.0f, 300.0f)); - if (ImGui::Begin(nameBuf, &open)) { - viewports[i]->renderUI(); - - ImGui::Separator(); - if (ImGui::Button("Delete Viewport")) { - graphics::removeViewport(viewports[i]); - ImGui::End(); - break; - } - } - ImGui::End(); - _viewportModals[i] = open; - } - } - newViewportModals = _viewportModals; + // XXX + // std::vector> viewports = graphics::getViewports(); + // static std::vector newViewportModals; // If first time creating the modal + //_viewportModals.resize(viewports.size()); + + //// Check if first time creating viewport + // newViewportModals.resize(_viewportModals.size()); + // for (unsigned i = 0; i < newViewportModals.size(); i++) + // newViewportModals[i] = !newViewportModals[i] && _viewportModals[i]; + + // for (uint32_t i = 0; i < _viewportModals.size(); i++) { + // char nameBuf[196]; + // sprintf(nameBuf, "%s###ViewportProps%s", viewports[i]->getName().c_str(), viewports[i]->getSID().getString().c_str()); + + // bool open = _viewportModals[i]; + // if (open) { + // if (newViewportModals[i]) + // ImGui::SetNextWindowSize(ImVec2(200.0f, 300.0f)); + // if (ImGui::Begin(nameBuf, &open)) { + // viewports[i]->renderUI(); + + // ImGui::Separator(); + // if (ImGui::Button("Delete Viewport")) { + // graphics::removeViewport(viewports[i]); + // ImGui::End(); + // break; + // } + // } + // ImGui::End(); + // _viewportModals[i] = open; + // } + //} + // newViewportModals = _viewportModals; } } // namespace atta::ui diff --git a/src/atta/ui/editor/topBar/topBar.h b/src/atta/ui/editor/topBar/topBar.h index ab0f5350..3595abd1 100644 --- a/src/atta/ui/editor/topBar/topBar.h +++ b/src/atta/ui/editor/topBar/topBar.h @@ -10,6 +10,7 @@ #include namespace atta::ui { + class TopBar { public: TopBar(); @@ -39,6 +40,7 @@ class TopBar { std::vector _viewportModals; ///< Which viewports should be rendered std::vector _repoWindows; ///< Which repository windows are open }; + } // namespace atta::ui #endif // ATTA_UI_EDITOR_TOP_BAR_TOP_BAR_H diff --git a/src/atta/ui/editor/windows/viewportWindows.cpp b/src/atta/ui/editor/windows/viewportWindows.cpp index 20715075..650a157b 100644 --- a/src/atta/ui/editor/windows/viewportWindows.cpp +++ b/src/atta/ui/editor/windows/viewportWindows.cpp @@ -23,7 +23,7 @@ namespace atta::ui { void ViewportWindows::render() { PROFILE(); - std::vector> viewports = graphics::getViewports(); + // std::vector> viewports = graphics::getViewports(); static int activeViewport = 0; // Viewports fps @@ -35,179 +35,179 @@ void ViewportWindows::render() { } int i = -1; - for (auto viewport : viewports) { - // ImGui::DockBuilderDockWindow((viewport->getName()+"###Viewport"+viewport->getSID().getString()).c_str(), _dockSpace.getViewportDockId()); - char nameBuf[128]; - sprintf(nameBuf, "%s###Viewport%s", viewport->getName().c_str(), viewport->getSID().getString().c_str()); - i++; - // Render and resize - ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(5.0f, 5.0f)); - bool open = true; - ImGui::Begin(nameBuf, &open); - if (graphics::getViewportRendering()) { - //----- Move camera -----// - // Check started camera movement - 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()) { - ImVec2 window = ImGui::GetWindowPos(); - ImVec2 cursor = ImGui::GetCursorPos(); - ImVec2 mouse = ImGui::GetMousePos(); - click = {int(mouse.x - window.x - cursor.x), int(mouse.y - window.y - cursor.y)}; - } - - if (ImGui::IsMouseClicked(1) && ImGui::IsWindowHovered()) - ImGui::OpenPopup("Editor_AddBasicShape"); - addBasicShapePopup(); - - //----- Keyboard click -----// - static ImGuizmo::OPERATION mouseOperation = ImGuizmo::OPERATION::TRANSLATE; - static ImGuizmo::MODE mouseMode = ImGuizmo::MODE::LOCAL; - static bool snap = false; - - if (ImGui::IsWindowHovered()) { - snap = false; - ImGuiIO& io = ImGui::GetIO(); - if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyCtrl) { - mouseOperation = ImGuizmo::OPERATION::TRANSLATE; - mouseMode = ImGuizmo::MODE::LOCAL; - } else if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyShift) { - mouseOperation = ImGuizmo::OPERATION::TRANSLATE; - mouseMode = ImGuizmo::MODE::WORLD; - } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyCtrl) { - mouseOperation = ImGuizmo::OPERATION::SCALE; - mouseMode = ImGuizmo::MODE::LOCAL; - } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyShift) { - mouseOperation = ImGuizmo::OPERATION::SCALE; - mouseMode = ImGuizmo::MODE::WORLD; - } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyCtrl) { - mouseOperation = ImGuizmo::OPERATION::ROTATE; - mouseMode = ImGuizmo::MODE::LOCAL; - } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyShift) { - mouseOperation = ImGuizmo::OPERATION::ROTATE; - mouseMode = ImGuizmo::MODE::WORLD; - } else if (io.KeyCtrl) - snap = true; - } - - //----- Render to texture -----// - ImVec2 size = ImVec2(viewport->getWidth(), viewport->getHeight()); - ImGui::Image(viewport->getImGuiTexture(), size, ImVec2(0, 0), ImVec2(1, 1)); - - //----- ImGuizmo -----// - bool imGuizmoUsingMouse = false; - component::EntityId entity = component::getSelectedEntity(); - if (entity >= 0) { - component::Transform* t = component::getComponent(entity); - - if (t) { - ImGuizmo::SetOrthographic(viewport->getCamera()->getName() == "OrthographicCamera"); - ImGuizmo::SetDrawlist(); - ImGuizmo::SetRect(ImGui::GetWindowPos().x + 5.0f, ImGui::GetWindowPos().y + 24.0f, viewport->getWidth(), viewport->getHeight()); - mat4 view = transpose(viewport->getCamera()->getView()); - mat4 proj = viewport->getCamera()->getProj(); - proj.mat[1][1] *= -1; - proj.transpose(); - - mat4 transform = transpose(t->getWorldTransformMatrix(entity)); - - float snapValue = 0.5f; - if (mouseOperation == ImGuizmo::OPERATION::ROTATE) - snapValue = 45.0f; - float snapValues[3] = {snapValue, snapValue, snapValue}; - - ImGuizmo::Manipulate(view.data, proj.data, mouseOperation, mouseMode, transform.data, nullptr, snap ? snapValues : nullptr); - - if (ImGuizmo::IsUsing()) { - imGuizmoUsingMouse = true; - 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(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(parentId); - r = component::getComponent(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(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 - // } - // } - } - } - } - - //----- Mouse click selection -----// - if (!imGuizmoUsingMouse) { - if (click.x >= 0 && click.y >= 0 && click.x < (int)viewport->getWidth() && click.y < (int)viewport->getHeight()) { - component::EntityId eid = graphics::viewportEntityClick(viewport, click); - component::setSelectedEntity(eid); - } - } - - //----- Resize -----// - ImVec2 windowSize = ImGui::GetWindowSize(); - if (windowSize.x != size.x || windowSize.y != size.y) - viewport->resize((uint32_t)windowSize.x - 10, (uint32_t)windowSize.y - 30); - } - ImGui::End(); - ImGui::PopStyleVar(1); - - if (!open) - graphics::removeViewport(viewport); - } + // for (auto viewport : viewports) { + // // ImGui::DockBuilderDockWindow((viewport->getName()+"###Viewport"+viewport->getSID().getString()).c_str(), + // _dockSpace.getViewportDockId()); char nameBuf[128]; sprintf(nameBuf, "%s###Viewport%s", viewport->getName().c_str(), + // viewport->getSID().getString().c_str()); i++; + // // Render and resize + // ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(5.0f, 5.0f)); + // bool open = true; + // ImGui::Begin(nameBuf, &open); + // // if (graphics::getViewportRendering()) { + // // //----- Move camera -----// + // // // Check started camera movement + // // 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()) { + // // ImVec2 window = ImGui::GetWindowPos(); + // // ImVec2 cursor = ImGui::GetCursorPos(); + // // ImVec2 mouse = ImGui::GetMousePos(); + // // click = {int(mouse.x - window.x - cursor.x), int(mouse.y - window.y - cursor.y)}; + // // } + + // // if (ImGui::IsMouseClicked(1) && ImGui::IsWindowHovered()) + // // ImGui::OpenPopup("Editor_AddBasicShape"); + // // addBasicShapePopup(); + + // // //----- Keyboard click -----// + // // static ImGuizmo::OPERATION mouseOperation = ImGuizmo::OPERATION::TRANSLATE; + // // static ImGuizmo::MODE mouseMode = ImGuizmo::MODE::LOCAL; + // // static bool snap = false; + + // // if (ImGui::IsWindowHovered()) { + // // snap = false; + // // ImGuiIO& io = ImGui::GetIO(); + // // if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyCtrl) { + // // mouseOperation = ImGuizmo::OPERATION::TRANSLATE; + // // mouseMode = ImGuizmo::MODE::LOCAL; + // // } else if (ImGui::IsKeyPressed(ImGuiKey_T) && io.KeyShift) { + // // mouseOperation = ImGuizmo::OPERATION::TRANSLATE; + // // mouseMode = ImGuizmo::MODE::WORLD; + // // } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyCtrl) { + // // mouseOperation = ImGuizmo::OPERATION::SCALE; + // // mouseMode = ImGuizmo::MODE::LOCAL; + // // } else if (ImGui::IsKeyPressed(ImGuiKey_S) && io.KeyShift) { + // // mouseOperation = ImGuizmo::OPERATION::SCALE; + // // mouseMode = ImGuizmo::MODE::WORLD; + // // } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyCtrl) { + // // mouseOperation = ImGuizmo::OPERATION::ROTATE; + // // mouseMode = ImGuizmo::MODE::LOCAL; + // // } else if (ImGui::IsKeyPressed(ImGuiKey_R) && io.KeyShift) { + // // mouseOperation = ImGuizmo::OPERATION::ROTATE; + // // mouseMode = ImGuizmo::MODE::WORLD; + // // } else if (io.KeyCtrl) + // // snap = true; + // // } + + // // //----- Render to texture -----// + // // ImVec2 size = ImVec2(viewport->getWidth(), viewport->getHeight()); + // // ImGui::Image(viewport->getImGuiTexture(), size, ImVec2(0, 0), ImVec2(1, 1)); + + // // //----- ImGuizmo -----// + // // bool imGuizmoUsingMouse = false; + // // component::EntityId entity = component::getSelectedEntity(); + // // if (entity >= 0) { + // // component::Transform* t = component::getComponent(entity); + + // // if (t) { + // // ImGuizmo::SetOrthographic(viewport->getCamera()->getName() == "OrthographicCamera"); + // // ImGuizmo::SetDrawlist(); + // // ImGuizmo::SetRect(ImGui::GetWindowPos().x + 5.0f, ImGui::GetWindowPos().y + 24.0f, viewport->getWidth(), + // viewport->getHeight()); + // // mat4 view = transpose(viewport->getCamera()->getView()); + // // mat4 proj = viewport->getCamera()->getProj(); + // // proj.mat[1][1] *= -1; + // // proj.transpose(); + + // // mat4 transform = transpose(t->getWorldTransformMatrix(entity)); + + // // float snapValue = 0.5f; + // // if (mouseOperation == ImGuizmo::OPERATION::ROTATE) + // // snapValue = 45.0f; + // // float snapValues[3] = {snapValue, snapValue, snapValue}; + + // // ImGuizmo::Manipulate(view.data, proj.data, mouseOperation, mouseMode, transform.data, nullptr, snap ? snapValues : nullptr); + + // // if (ImGuizmo::IsUsing()) { + // // imGuizmoUsingMouse = true; + // // 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(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(parentId); + // // r = component::getComponent(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(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 + // // // } + // // // } + // // } + // // } + // // } + + // // //----- Mouse click selection -----// + // // if (!imGuizmoUsingMouse) { + // // if (click.x >= 0 && click.y >= 0 && click.x < (int)viewport->getWidth() && click.y < (int)viewport->getHeight()) { + // // component::EntityId eid = graphics::viewportEntityClick(viewport, click); + // // component::setSelectedEntity(eid); + // // } + // // } + + // // //----- Resize -----// + // // ImVec2 windowSize = ImGui::GetWindowSize(); + // // if (windowSize.x != size.x || windowSize.y != size.y) + // // viewport->resize((uint32_t)windowSize.x - 10, (uint32_t)windowSize.y - 30); + // //} + // ImGui::End(); + // ImGui::PopStyleVar(1); + + // if (!open) + // graphics::removeViewport(viewport); + //} } void ViewportWindows::addBasicShapePopup() { diff --git a/src/atta/ui/manager.cpp b/src/atta/ui/manager.cpp index 779f985b..92e9e1af 100644 --- a/src/atta/ui/manager.cpp +++ b/src/atta/ui/manager.cpp @@ -64,7 +64,7 @@ void Manager::startUpImpl() { gfx::setUiShutDownFunc([&]() { shutDownImpl(); }); gfx::setUiStartUpFunc([&]() { startUpImpl(); }); - // Create editor (make sure windows are reorganized after start up) + // Create editor window _editor = {}; }