From b9147344e199f4f88fbd846e81d555a76859215f Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 21 Jan 2016 14:14:22 +0100 Subject: [PATCH] Semi-working translation widget --- include/Engine/Editor/EditorSystem.h | 7 +++++ include/Engine/Editor/EditorWidgetSystem.h | 12 +++++++ .../Schema/Entities/EditorWidgetRotate.xml | 3 ++ .../Schema/Entities/EditorWidgetTranslate.xml | 3 ++ src/Engine/Core/Transform.cpp | 2 +- src/Engine/Editor/EditorSystem.cpp | 31 +++++++++++++++++-- src/Engine/Editor/EditorWidgetSystem.cpp | 23 ++++++++++---- 7 files changed, 72 insertions(+), 9 deletions(-) diff --git a/include/Engine/Editor/EditorSystem.h b/include/Engine/Editor/EditorSystem.h index 31d5064c..befa46b6 100644 --- a/include/Engine/Editor/EditorSystem.h +++ b/include/Engine/Editor/EditorSystem.h @@ -9,6 +9,7 @@ #include "../Core/EntityFilePreprocessor.h" #include "../Core/EntityFileParser.h" #include "../Core/EntityFileWriter.h" +#include "../Core/EMouseRelease.h" #include "EditorGUI.h" #include "EditorStats.h" @@ -49,4 +50,10 @@ 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); + + // Events + EventRelay m_EMouseRelease; + bool OnMouseRelease(const Events::MouseRelease& e); + EventRelay m_EWidgetDelta; + bool OnWidgetDelta(const Events::WidgetDelta& e); }; \ No newline at end of file diff --git a/include/Engine/Editor/EditorWidgetSystem.h b/include/Engine/Editor/EditorWidgetSystem.h index f3e4e4db..a2329342 100644 --- a/include/Engine/Editor/EditorWidgetSystem.h +++ b/include/Engine/Editor/EditorWidgetSystem.h @@ -10,6 +10,18 @@ #include "../Core/EMousePress.h" #include "../Core/EMouseRelease.h" +namespace Events +{ + +struct WidgetDelta : Event +{ + glm::vec3 Translation; + glm::vec3 Rotation; + glm::vec3 Scale; +}; + +} + class EditorWidgetSystem : public ImpureSystem, PureSystem { public: diff --git a/resources/Schema/Entities/EditorWidgetRotate.xml b/resources/Schema/Entities/EditorWidgetRotate.xml index 6a5f72ea..0a8f06b8 100644 --- a/resources/Schema/Entities/EditorWidgetRotate.xml +++ b/resources/Schema/Entities/EditorWidgetRotate.xml @@ -9,6 +9,7 @@ + @@ -24,6 +25,7 @@ + @@ -39,6 +41,7 @@ + diff --git a/resources/Schema/Entities/EditorWidgetTranslate.xml b/resources/Schema/Entities/EditorWidgetTranslate.xml index 75d96a51..70ca09a8 100644 --- a/resources/Schema/Entities/EditorWidgetTranslate.xml +++ b/resources/Schema/Entities/EditorWidgetTranslate.xml @@ -12,6 +12,7 @@ + @@ -24,6 +25,7 @@ + @@ -36,6 +38,7 @@ + diff --git a/src/Engine/Core/Transform.cpp b/src/Engine/Core/Transform.cpp index 8acac937..cbc405a3 100644 --- a/src/Engine/Core/Transform.cpp +++ b/src/Engine/Core/Transform.cpp @@ -7,7 +7,7 @@ glm::vec3 Transform::AbsolutePosition(World* world, EntityID entity) while (entity != EntityID_Invalid) { ComponentWrapper transform = world->GetComponent(entity, "Transform"); EntityID parent = world->GetParent(entity); - position += Transform::AbsoluteScale(world, parent) * Transform::AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; + position += Transform::AbsoluteOrientation(world, parent) * (glm::vec3)transform["Position"]; entity = parent; } diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index 613f0edb..13d74a65 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -31,6 +31,9 @@ EditorSystem::EditorSystem(World* world, EventBroker* eventBroker, IRenderer* re 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)); + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &EditorSystem::OnMouseRelease); + EVENT_SUBSCRIBE_MEMBER(m_EWidgetDelta, &EditorSystem::OnWidgetDelta); + m_EditorStats = new EditorStats(); Events::SetCamera e; @@ -49,11 +52,15 @@ EditorSystem::~EditorSystem() void EditorSystem::Update(double dt) { - m_EditorWorldSystemPipeline->Update(dt); - m_EditorGUI->Draw(); m_EditorStats->Draw(dt); + if (m_CurrentSelection.Valid() && m_Widget.Valid()) { + (glm::vec3&)m_Widget["Transform"]["Position"] = Transform::AbsolutePosition(m_CurrentSelection.World, m_CurrentSelection.ID); + } + + m_EditorWorldSystemPipeline->Update(dt); + m_DebugCameraInputController->Update(dt); m_Camera["Transform"]["Position"] = m_DebugCameraInputController->Position(); m_Camera["Transform"]["Orientation"] = glm::eulerAngles(m_DebugCameraInputController->Orientation()); @@ -103,6 +110,26 @@ void EditorSystem::OnComponentDelete(EntityWrapper entity, const std::string& co entity.World->DeleteComponent(entity.ID, componentType); } +bool EditorSystem::OnMouseRelease(const Events::MouseRelease& e) +{ + if (e.Button == GLFW_MOUSE_BUTTON_1) { + PickData pick = m_Renderer->Pick(glm::vec2(e.X, e.Y)); + if (pick.World == m_World) { + m_CurrentSelection = EntityWrapper(m_World, pick.Entity); + m_EditorGUI->SelectEntity(m_CurrentSelection); + } + } + return true; +} + +bool EditorSystem::OnWidgetDelta(const Events::WidgetDelta& e) +{ + if (m_CurrentSelection.Valid()) { + (glm::vec3&)m_CurrentSelection["Transform"]["Position"] += glm::inverse(Transform::AbsoluteOrientation(m_CurrentSelection.World, m_CurrentSelection.ID)) * e.Translation; + } + return true; +} + EntityWrapper EditorSystem::importEntity(EntityWrapper parent, boost::filesystem::path filePath) { if (parent.World == nullptr) { diff --git a/src/Engine/Editor/EditorWidgetSystem.cpp b/src/Engine/Editor/EditorWidgetSystem.cpp index 3a200e0f..1417f20e 100644 --- a/src/Engine/Editor/EditorWidgetSystem.cpp +++ b/src/Engine/Editor/EditorWidgetSystem.cpp @@ -21,19 +21,30 @@ void EditorWidgetSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper return; } + Events::WidgetDelta e; + EntityWrapper moveEntity = entity.Parent(); if (!moveEntity.Valid()) { moveEntity = entity; } + auto camera = m_PickData.Camera; + glm::vec3 axis = cEditorWidget["Axis"]; + glm::vec2 axisScreen = camera->WorldToScreen(axis, m_Renderer->Resolution()) - camera->WorldToScreen(glm::vec3(0, 0, 0), m_Renderer->Resolution()); + float dot = glm::dot(m_MouseDelta, glm::normalize(axisScreen)) / glm::length(axisScreen); + glm::vec3 worldMovement = dot * axis; + ComponentWrapper::SubscriptProxy& type = cEditorWidget["Type"]; if ((ComponentInfo::EnumType)type == type.Enum("Translate")) { - auto camera = m_PickData.Camera; - glm::vec3 axis = cEditorWidget["Axis"]; - glm::vec2 axisScreen = camera->WorldToScreen(axis, m_Renderer->Resolution()) - camera->WorldToScreen(glm::vec3(0, 0, 0), m_Renderer->Resolution()); - float dot = glm::dot(m_MouseDelta, glm::normalize(axisScreen)) / glm::length(axisScreen); - glm::vec3 worldMovement = dot * axis; - (glm::vec3&)moveEntity["Transform"]["Position"] += worldMovement; + e.Translation += worldMovement; + m_EventBroker->Publish(e); + } else if ((ComponentInfo::EnumType)type == type.Enum("Rotate")) { + //if (glm::length(worldMovement) > 0) { + // glm::vec3& orientation = moveEntity["Transform"]["Orientation"]; + // glm::quat q = glm::quat(orientation); + // q *= glm::quat(glm::vec3(worldMovement)); + // orientation = glm::eulerAngles(q); + //} } m_MouseDelta = glm::vec2(0);