From ae3bd31dc5e40b5ca0b70f461952de9f8d884ae2 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Tue, 27 Aug 2024 13:24:05 -0400 Subject: [PATCH] Allowing editing of angles through the inspector --- Editor/src/ComponentsPanel/TransformPanel.h | 41 +++++++++++++++---- .../src/ScriptingContext/ScriptingContext.cpp | 4 +- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/Editor/src/ComponentsPanel/TransformPanel.h b/Editor/src/ComponentsPanel/TransformPanel.h index 3bb910df..452b3018 100644 --- a/Editor/src/ComponentsPanel/TransformPanel.h +++ b/Editor/src/ComponentsPanel/TransformPanel.h @@ -45,16 +45,41 @@ public: CompononentPropertyName("Rotation"); ImGui::TableNextColumn(); - Vector3 eulerAngles = glm::eulerAngles(component.GetLocalRotation()); - Vector3 eulerDegrees = Vector3(glm::degrees(eulerAngles.x), glm::degrees(eulerAngles.y), glm::degrees(eulerAngles.z)); - ImGuiHelper::DrawVec3("Rotation", &eulerDegrees); - Vector3 eulerAnglesDelta = Vector3(glm::radians(eulerDegrees.x), glm::radians(eulerDegrees.y), glm::radians(eulerDegrees.z)); + // Get the current local rotation as a quaternion + Quat currentRotation = component.GetLocalRotation(); - float delta = glm::length(eulerAngles - eulerAnglesDelta); - if (delta > 0.f) + // Convert quaternion to Euler angles in degrees + Vector3 eulerDegreesOld = glm::degrees(glm::eulerAngles(currentRotation)); + + // Draw the ImGui widget for rotation + ImGuiHelper::DrawVec3("Rotation", &eulerDegreesOld); + + // Calculate the delta in Euler angles + Vector3 eulerDelta = eulerDegreesOld - glm::degrees(glm::eulerAngles(currentRotation)); + + // Apply a small threshold to ignore minor changes + if (fabs(eulerDelta.x) < 0.01) eulerDelta.x = 0.0f; + if (fabs(eulerDelta.y) < 0.01) eulerDelta.y = 0.0f; + if (fabs(eulerDelta.z) < 0.01) eulerDelta.z = 0.0f; + + // Check if there was a significant rotation change + if (glm::length(eulerDelta) > 0.001f) { - Quat rotation = QuatFromEuler(eulerAnglesDelta.x, eulerAnglesDelta.y, eulerAnglesDelta.z); - //component.SetLocalRotation(rotation); + // Convert delta back to radians + Vector3 eulerAnglesDelta = glm::radians(eulerDelta); + + // Calculate the new rotation quaternion by applying the delta to the current rotation + // Apply changes only to the axis that was modified + Quat deltaRotation = glm::quat(eulerAnglesDelta); + Quat deltaRotationX = glm::angleAxis(eulerAnglesDelta.x, Vector3(1.0f, 0.0f, 0.0f)); + Quat deltaRotationY = glm::angleAxis(eulerAnglesDelta.y, Vector3(0.0f, 1.0f, 0.0f)); + Quat deltaRotationZ = glm::angleAxis(eulerAnglesDelta.z, Vector3(0.0f, 0.0f, 1.0f)); + + // Combine the rotations: only the modified axis is affected + Quat newRotation = deltaRotationZ * deltaRotationY * deltaRotationX * currentRotation; + + // Set the new rotation + component.SetLocalRotation(newRotation); } ImGui::TableNextColumn(); diff --git a/Editor/src/ScriptingContext/ScriptingContext.cpp b/Editor/src/ScriptingContext/ScriptingContext.cpp index 2096347e..6736bb15 100644 --- a/Editor/src/ScriptingContext/ScriptingContext.cpp +++ b/Editor/src/ScriptingContext/ScriptingContext.cpp @@ -16,7 +16,7 @@ void ScriptingContext::Initialize() } // Load Nuake assembly DLL - auto m_LoadContext2 = Nuake::ScriptingEngineNet::Get().GetHostInstance()->CreateAssemblyLoadContext("NuakeEditorContext"); + /*auto m_LoadContext2 = Nuake::ScriptingEngineNet::Get().GetHostInstance()->CreateAssemblyLoadContext("NuakeEditorContext"); m_NuakeAssembly = Nuake::ScriptingEngineNet::Get().ReloadEngineAPI(m_LoadContext2); m_EditorAssembly = m_LoadContext2.LoadAssembly("EditorNet.dll"); @@ -30,7 +30,7 @@ void ScriptingContext::Initialize() } m_EditorAssembly.UploadInternalCalls(); - m_EditorAssembly.GetType("Nuake.Editor").InvokeStaticMethod("Initialize"); + m_EditorAssembly.GetType("Nuake.Editor").InvokeStaticMethod("Initialize");*/ } void ScriptingContext::PushCommand(const std::string & command, const std::string & arg)