From 65f5bc673fdfe8856eb58137272b90cf8c6b5d02 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 1 Feb 2016 15:47:56 +0100 Subject: [PATCH] Functional translation widget in global and local space --- assets | 2 +- include/Engine/Editor/EditorGUI.h | 19 ++++++ include/Engine/Editor/EditorSystem.h | 2 + resources/DefaultInput.ini | 4 -- src/Engine/Editor/EditorGUI.cpp | 87 +++++++++++++++++++++--- src/Engine/Editor/EditorSystem.cpp | 29 ++++++-- src/Engine/Editor/EditorWidgetSystem.cpp | 13 ++-- 7 files changed, 131 insertions(+), 25 deletions(-) diff --git a/assets b/assets index e4dc9529..091ad5c0 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e4dc9529f2178d373808ddf487165fa50a641a78 +Subproject commit 091ad5c01bf7b6ef5501fc907fc610576a4a45ea diff --git a/include/Engine/Editor/EditorGUI.h b/include/Engine/Editor/EditorGUI.h index 8bd9bbab..7a0289c6 100644 --- a/include/Engine/Editor/EditorGUI.h +++ b/include/Engine/Editor/EditorGUI.h @@ -19,6 +19,7 @@ #include "../Core/ResourceManager.h" #include "../Core/EPause.h" #include "../Core/EKeyDown.h" +#include "../Core/ELockMouse.h" #include "../Core/EFileDropped.h" #include "../Rendering/Texture.h" @@ -34,6 +35,12 @@ public: Scale }; + enum class WidgetSpace + { + Global, + Local + }; + void Draw(); void SelectEntity(EntityWrapper entity); @@ -75,6 +82,9 @@ public: // Called when the user selects a widget mode. typedef std::function OnWidgetMode_t; void SetWidgetModeCallback(OnWidgetMode_t f) { m_OnWidgetMode = f; } + // Called when the user selects a widget space. + typedef std::function OnWidgetSpace_t; + void SetWidgetSpaceCallback(OnWidgetSpace_t f) { m_OnWidgetSpace = f; } private: World* m_World; @@ -95,10 +105,12 @@ private: EntityWrapper m_CurrentlyDragging = EntityWrapper::Invalid; std::string m_LastErrorMessage; WidgetMode m_CurrentWidgetMode = WidgetMode::Translate; + WidgetSpace m_CurrentWidgetSpace = WidgetSpace::Global; std::set m_ModalsToOpen; std::map m_ModalData; std::string m_DroppedFile = ""; bool m_Paused = false; + bool m_MouseLocked = false; // Callbacks OnEntitySelectedCallback_t m_OnEntitySelected = nullptr; @@ -111,6 +123,7 @@ private: OnComponentAttach_t m_OnComponentAttach = nullptr; OnComponentDelete_t m_OnComponentDelete = nullptr; OnWidgetMode_t m_OnWidgetMode = nullptr; + OnWidgetSpace_t m_OnWidgetSpace = nullptr; // Events EventRelay m_EKeyDown; @@ -121,6 +134,10 @@ private: bool OnPause(const Events::Pause& e); EventRelay m_EResume; bool OnResume(const Events::Resume& e); + EventRelay m_ELockMouse; + bool OnLockMouse(const Events::LockMouse& e); + EventRelay m_EUnlockMouse; + bool OnUnlockMouse(const Events::UnlockMouse& e); // Utility functions boost::filesystem::path fileOpenDialog(); @@ -128,6 +145,8 @@ private: const std::string formatEntityName(EntityWrapper entity); GLuint tryLoadTexture(std::string filePath); void openModal(const std::string& modal); + void setWidgetMode(WidgetMode mode); + void toggleWidgetSpace(); static bool compareCharArray(const char* c1, const char* c2); // Entity file handling methods diff --git a/include/Engine/Editor/EditorSystem.h b/include/Engine/Editor/EditorSystem.h index 2db24ba3..accf66e4 100644 --- a/include/Engine/Editor/EditorSystem.h +++ b/include/Engine/Editor/EditorSystem.h @@ -41,6 +41,7 @@ private: double m_LastTime = 0.f; bool m_Enabled = true; EditorGUI::WidgetMode m_WidgetMode = EditorGUI::WidgetMode::Translate; + EditorGUI::WidgetSpace m_WidgetSpace = EditorGUI::WidgetSpace::Global; EntityWrapper m_Widget = EntityWrapper::Invalid; EntityWrapper m_CurrentSelection = EntityWrapper::Invalid; @@ -57,6 +58,7 @@ private: void OnEntityChangeName(EntityWrapper entity, const std::string& name); void OnComponentAttach(EntityWrapper entity, const std::string& componentType); void OnComponentDelete(EntityWrapper entity, const std::string& componentType); + void OnWidgetSpace(EditorGUI::WidgetSpace widgetSpace); // Events EventRelay m_EMousePress; diff --git a/resources/DefaultInput.ini b/resources/DefaultInput.ini index d07ed6a3..a3f7d166 100644 --- a/resources/DefaultInput.ini +++ b/resources/DefaultInput.ini @@ -15,10 +15,6 @@ Space=Jump LeftControl=Crouch LeftShift=Sprint F1=ToggleEditor -1=EditorToolMove -2=EditorToolRotate -3=EditorToolScale -X=EditorToggleTransformSpace C=ConnectToServer N=SwitchToServer M=SwitchToClient diff --git a/src/Engine/Editor/EditorGUI.cpp b/src/Engine/Editor/EditorGUI.cpp index 007e845d..06938064 100644 --- a/src/Engine/Editor/EditorGUI.cpp +++ b/src/Engine/Editor/EditorGUI.cpp @@ -9,6 +9,8 @@ EditorGUI::EditorGUI(World* world, EventBroker* eventBroker) EVENT_SUBSCRIBE_MEMBER(m_EFileDropped, &EditorGUI::OnFileDropped); EVENT_SUBSCRIBE_MEMBER(m_EPause, &EditorGUI::OnPause); EVENT_SUBSCRIBE_MEMBER(m_EResume, &EditorGUI::OnResume); + EVENT_SUBSCRIBE_MEMBER(m_ELockMouse, &EditorGUI::OnLockMouse); + EVENT_SUBSCRIBE_MEMBER(m_EUnlockMouse, &EditorGUI::OnUnlockMouse); } void EditorGUI::Draw() @@ -40,26 +42,49 @@ void EditorGUI::drawTools() return; } + // Widget modes createWidgetToolButton(WidgetMode::Translate); if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("Translate"); + ImGui::SetTooltip("Translate (W)"); } ImGui::SameLine(); createWidgetToolButton(WidgetMode::Rotate); if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("Rotate"); + ImGui::SetTooltip("Rotate (E)"); } ImGui::SameLine(); createWidgetToolButton(WidgetMode::Scale); if (ImGui::IsItemHovered()) { - ImGui::SetTooltip("Scale"); + ImGui::SetTooltip("Scale (R)"); } + + ImGui::SameLine(); + ImGui::ItemSize(ImVec2(5, 0)); + + // Widget space + ImGui::SameLine(); + GLuint spaceTexture = 0; + if (m_CurrentWidgetSpace == WidgetSpace::Global) { + spaceTexture = tryLoadTexture("Textures/Icons/Global.png"); + } else if (m_CurrentWidgetSpace == WidgetSpace::Local) { + spaceTexture = tryLoadTexture("Textures/Icons/Local.png"); + } + if (ImGui::ImageButton((void*)spaceTexture, ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), ImVec4(1, 1, 1, 1))) { + toggleWidgetSpace(); + } + if (ImGui::IsItemHovered()) { + if (m_CurrentWidgetSpace == WidgetSpace::Global) { + ImGui::SetTooltip("Widget space: Global (X)"); + } else if (m_CurrentWidgetSpace == WidgetSpace::Local) { + ImGui::SetTooltip("Widget space: Local (X)"); + } + } + ImGui::SameLine(); ImGui::ItemSize(ImVec2(5, 0)); // Play button ImGui::SameLine(); - static bool paused = false; if (ImGui::ImageButton((void*)tryLoadTexture("Textures/Icons/Play.png"), ImVec2(24, 24), ImVec2(0, 1), ImVec2(1, 0), -1, ImVec4(0, 0, 0, 0), (!m_Paused) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1))) { Events::Resume e; e.World = m_World; @@ -549,10 +574,7 @@ void EditorGUI::createWidgetToolButton(WidgetMode mode) (m_CurrentWidgetMode == mode) ? ImVec4(0, 1, 0, 1) : ImVec4(1, 1, 1, 1) ) ) { - if (m_OnWidgetMode != nullptr) { - m_OnWidgetMode(mode); - } - m_CurrentWidgetMode = mode; + setWidgetMode(mode); } } @@ -582,6 +604,22 @@ bool EditorGUI::OnKeyDown(const Events::KeyDown& e) } } + if (!m_MouseLocked) { + if (e.KeyCode == GLFW_KEY_W) { + setWidgetMode(WidgetMode::Translate); + } + if (e.KeyCode == GLFW_KEY_E) { + setWidgetMode(WidgetMode::Rotate); + } + if (e.KeyCode == GLFW_KEY_R) { + setWidgetMode(WidgetMode::Scale); + } + + if (e.KeyCode == GLFW_KEY_X) { + toggleWidgetSpace(); + } + } + return true; } @@ -612,6 +650,18 @@ bool EditorGUI::OnResume(const Events::Resume& e) return true; } +bool EditorGUI::OnLockMouse(const Events::LockMouse& e) +{ + m_MouseLocked = true; + return true; +} + +bool EditorGUI::OnUnlockMouse(const Events::UnlockMouse& e) +{ + m_MouseLocked = false; + return true; +} + boost::filesystem::path EditorGUI::fileOpenDialog() { namespace bfs = boost::filesystem; @@ -683,6 +733,27 @@ void EditorGUI::openModal(const std::string& modal) m_ModalsToOpen.insert(modal); } +void EditorGUI::setWidgetMode(WidgetMode mode) +{ + if (m_OnWidgetMode != nullptr) { + m_OnWidgetMode(mode); + } + m_CurrentWidgetMode = mode; +} + +void EditorGUI::toggleWidgetSpace() +{ + if (m_CurrentWidgetSpace == WidgetSpace::Global) { + m_CurrentWidgetSpace = WidgetSpace::Local; + } else if (m_CurrentWidgetSpace == WidgetSpace::Local) { + m_CurrentWidgetSpace = WidgetSpace::Global; + } + + if (m_OnWidgetSpace != nullptr) { + m_OnWidgetSpace(m_CurrentWidgetSpace); + } +} + bool EditorGUI::compareCharArray(const char* c1, const char* c2) { return strcmp(c1, c2) < 0; diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 83963531..ccbc1b48 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -31,6 +31,7 @@ EditorSystem::EditorSystem(World* world, EventBroker* eventBroker, IRenderer* re m_EditorGUI->SetComponentAttachCallback(std::bind(&EditorSystem::OnComponentAttach, this, std::placeholders::_1, std::placeholders::_2)); m_EditorGUI->SetComponentDeleteCallback(std::bind(&EditorSystem::OnComponentDelete, this, std::placeholders::_1, std::placeholders::_2)); m_EditorGUI->SetWidgetModeCallback(std::bind(&EditorSystem::setWidgetMode, this, std::placeholders::_1)); + m_EditorGUI->SetWidgetSpaceCallback(std::bind(&EditorSystem::OnWidgetSpace, this, std::placeholders::_1)); EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &EditorSystem::OnMousePress); EVENT_SUBSCRIBE_MEMBER(m_EWidgetDelta, &EditorSystem::OnWidgetDelta); @@ -65,7 +66,12 @@ void EditorSystem::Update(double dt) m_EditorStats->Draw(actualDelta); if (m_CurrentSelection.Valid() && m_Widget.Valid()) { - (glm::vec3&)m_Widget["Transform"]["Position"] = Transform::AbsolutePosition(m_CurrentSelection.World, m_CurrentSelection.ID); + (glm::vec3&)m_Widget["Transform"]["Position"] = Transform::AbsolutePosition(m_CurrentSelection); + if (m_WidgetSpace == EditorGUI::WidgetSpace::Local) { + (glm::vec3&)m_Widget["Transform"]["Orientation"] = Transform::AbsoluteOrientationEuler(m_CurrentSelection); + } else { + (glm::vec3&)m_Widget["Transform"]["Orientation"] = glm::vec3(0, 0, 0); + } } m_EditorWorldSystemPipeline->Update(actualDelta); @@ -165,6 +171,11 @@ void EditorSystem::OnComponentDelete(EntityWrapper entity, const std::string& co } } +void EditorSystem::OnWidgetSpace(EditorGUI::WidgetSpace widgetSpace) +{ + m_WidgetSpace = widgetSpace; +} + bool EditorSystem::OnMousePress(const Events::MousePress& e) { ImGuiIO& io = ImGui::GetIO(); @@ -181,12 +192,18 @@ bool EditorSystem::OnMousePress(const Events::MousePress& e) bool EditorSystem::OnWidgetDelta(const Events::WidgetDelta& e) { if (m_CurrentSelection.Valid()) { - glm::quat parentOrientation; - EntityWrapper parent = m_CurrentSelection.Parent(); - if (parent.Valid()) { - parentOrientation = glm::inverse(Transform::AbsoluteOrientation(parent.World, parent.ID)); + if (m_WidgetSpace == EditorGUI::WidgetSpace::Global) { + glm::quat parentOrientation; + EntityWrapper parent = m_CurrentSelection.Parent(); + if (parent.Valid()) { + parentOrientation = glm::inverse(Transform::AbsoluteOrientation(parent)); + } + (glm::vec3&)m_CurrentSelection["Transform"]["Position"] += parentOrientation * e.Translation; + } else if (m_WidgetSpace == EditorGUI::WidgetSpace::Local) { + glm::quat selectionOri = glm::quat((glm::vec3)m_CurrentSelection["Transform"]["Orientation"]); + glm::vec3 localTranslation = selectionOri * e.Translation; + (glm::vec3&)m_CurrentSelection["Transform"]["Position"] += localTranslation; } - (glm::vec3&)m_CurrentSelection["Transform"]["Position"] += parentOrientation * e.Translation; m_EditorGUI->SetDirty(m_CurrentSelection); } return true; diff --git a/src/Engine/Editor/EditorWidgetSystem.cpp b/src/Engine/Editor/EditorWidgetSystem.cpp index 44d3796a..7c20a7c7 100644 --- a/src/Engine/Editor/EditorWidgetSystem.cpp +++ b/src/Engine/Editor/EditorWidgetSystem.cpp @@ -24,16 +24,17 @@ void EditorWidgetSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper Events::WidgetDelta e; // Widget axes should have a common parent - EntityWrapper moveEntity = entity.Parent(); - if (!moveEntity.Valid()) { - moveEntity = entity; + EntityWrapper widgetBase = entity.Parent(); + if (!widgetBase.Valid()) { + widgetBase = entity; } - glm::vec3 moveEntityPos = moveEntity["Transform"]["Position"]; + glm::vec3 widgetBasePos = widgetBase["Transform"]["Position"]; + glm::quat widgetBaseOri = glm::quat((glm::vec3)widgetBase["Transform"]["Orientation"]); auto camera = m_PickData.Camera; glm::vec3 axis = (glm::vec3)cEditorWidget["Axis"]; - glm::vec2 axisScreen = camera->WorldToScreen(moveEntityPos + axis, m_Renderer->GetViewportSize()) - camera->WorldToScreen(moveEntityPos, m_Renderer->GetViewportSize()); - ImGui::Text("axisScreen: (%f, %f)", axisScreen.x, axisScreen.y); + glm::vec3 axisOriented = widgetBaseOri * axis; + glm::vec2 axisScreen = camera->WorldToScreen(widgetBasePos + axisOriented, m_Renderer->GetViewportSize()) - camera->WorldToScreen(widgetBasePos, m_Renderer->GetViewportSize()); float dot = glm::dot(m_MouseDelta, glm::normalize(axisScreen)) / glm::length(axisScreen); glm::vec3 worldMovement = dot * axis;