From 55ee2bb3bf1b7ee13a50a86683c517f3a9a0204c Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Fri, 22 Jan 2016 21:35:31 +0100 Subject: [PATCH] Player input properly separated from editor input and server --- .../Editor/EditorCameraInputController.h | 25 ++++++++++--- .../Engine/Input/FirstPersonInputController.h | 35 ++++++++++--------- src/Engine/Editor/EditorSystem.cpp | 12 +++++-- src/Game/Systems/PlayerMovementSystem.cpp | 7 ++-- 4 files changed, 54 insertions(+), 25 deletions(-) diff --git a/include/Engine/Editor/EditorCameraInputController.h b/include/Engine/Editor/EditorCameraInputController.h index c12113c7..82e8b337 100644 --- a/include/Engine/Editor/EditorCameraInputController.h +++ b/include/Engine/Editor/EditorCameraInputController.h @@ -23,13 +23,17 @@ public: m_SpeedMultiplier = m_Config->Get("Editor.CameraSpeed", 3.f); } - virtual const glm::vec3 Movement() const override - { - return m_Movement * m_SpeedMultiplier; - } + virtual const glm::vec3 Movement() const override { return m_Movement * m_SpeedMultiplier; } + + void Enable() { m_Enabled = true; } + void Disable() { m_Enabled = false; } virtual bool OnCommand(const Events::InputCommand& e) override { + if (!m_MouseLocked) { + return false; + } + ImGuiIO& io = ImGui::GetIO(); if (glm::abs(e.Value) > 0 && (io.WantCaptureKeyboard || io.WantCaptureMouse)) { return false; @@ -64,11 +68,16 @@ public: protected: ConfigFile* m_Config; + bool m_Enabled = false; float m_SpeedMultiplier = 1.f; EventRelay m_EMousePress; bool OnMousePress(const Events::MousePress& e) { + if (!m_Enabled) { + return false; + } + if (e.Button == GLFW_MOUSE_BUTTON_2) { ImGuiIO& io = ImGui::GetIO(); if (!io.WantCaptureMouse) { @@ -80,6 +89,10 @@ protected: EventRelay m_EMouseRelease; bool OnMouseRelease(const Events::MouseRelease& e) { + if (!m_Enabled) { + return false; + } + if (e.Button == GLFW_MOUSE_BUTTON_2) { UnlockMouse(); } @@ -88,6 +101,10 @@ protected: EventRelay m_EMouseScroll; bool OnMouseScroll(const Events::MouseScroll& e) { + if (!m_Enabled) { + return false; + } + m_SpeedMultiplier += e.DeltaY * (0.1f * m_SpeedMultiplier); m_Config->Set("Editor.CameraSpeed", m_SpeedMultiplier); m_Config->SaveToDisk(); diff --git a/include/Engine/Input/FirstPersonInputController.h b/include/Engine/Input/FirstPersonInputController.h index 91445534..cd80fb21 100644 --- a/include/Engine/Input/FirstPersonInputController.h +++ b/include/Engine/Input/FirstPersonInputController.h @@ -18,7 +18,7 @@ public: } virtual const glm::vec3 Movement() const { return m_Movement; } - virtual const glm::vec3 Orientation() const { return m_Orientation; } + virtual const glm::vec3 Rotation() const { return m_Rotation; } void LockMouse() { @@ -40,21 +40,19 @@ public: return false; } - if (m_MouseLocked) { - if (e.Command == "Pitch") { - float val = glm::radians(e.Value); - m_Orientation.x += -val; - m_Orientation.x = glm::clamp(m_Orientation.x, -glm::half_pi(), glm::half_pi()); - //m_Orientation = m_Orientation * glm::angleAxis(-val, glm::vec3(1.f, 0, 0)); - return true; - } + if (e.Command == "Pitch") { + float val = glm::radians(e.Value); + m_Rotation.x += -val; + m_Rotation.x = glm::clamp(m_Rotation.x, -glm::half_pi(), glm::half_pi()); + //m_Rotation = m_Rotation * glm::angleAxis(-val, glm::vec3(1.f, 0, 0)); + return true; + } - if (e.Command == "Yaw") { - float val = glm::radians(e.Value); - m_Orientation.y += -val; - //m_Orientation = glm::angleAxis(-val, glm::vec3(0, 1.f, 0)) * m_Orientation; - return true; - } + if (e.Command == "Yaw") { + float val = glm::radians(e.Value); + m_Rotation.y += -val; + //m_Rotation = glm::angleAxis(-val, glm::vec3(0, 1.f, 0)) * m_Rotation; + return true; } if (e.Command == "Forward" || e.Command == "Right") { @@ -75,11 +73,16 @@ public: return false; } + + virtual void Reset() + { + m_Rotation = glm::vec3(0.f, 0.f, 0.f); + } protected: const int m_PlayerID; bool m_MouseLocked = false; - glm::vec3 m_Orientation; + glm::vec3 m_Rotation; glm::vec3 m_Movement; EventRelay m_ELockMouse; diff --git a/src/Engine/Editor/EditorSystem.cpp b/src/Engine/Editor/EditorSystem.cpp index a357a542..9c0e2e47 100644 --- a/src/Engine/Editor/EditorSystem.cpp +++ b/src/Engine/Editor/EditorSystem.cpp @@ -72,8 +72,8 @@ void EditorSystem::Update(double dt) ComponentWrapper& cameraTransform = m_EditorCamera["Transform"]; glm::vec3& ori = cameraTransform["Orientation"]; - ori.x = m_EditorCameraInputController->Orientation().x; - ori.y = m_EditorCameraInputController->Orientation().y; + ori.x = m_EditorCameraInputController->Rotation().x; + ori.y = m_EditorCameraInputController->Rotation().y; glm::vec3& pos = cameraTransform["Position"]; pos += m_EditorCameraInputController->Movement() * glm::inverse(glm::quat(ori)) * (float)actualDelta; } @@ -81,6 +81,8 @@ void EditorSystem::Update(double dt) void EditorSystem::Enable() { + m_EditorCameraInputController->Enable(); + m_EventBroker->Publish(Events::UnlockMouse()); Events::SetCamera e; e.CameraEntity = m_EditorCamera; m_EventBroker->Publish(e); @@ -90,6 +92,8 @@ void EditorSystem::Enable() void EditorSystem::Disable() { + m_EditorCameraInputController->Disable(); + m_EventBroker->Publish(Events::LockMouse()); Events::SetCamera e; e.CameraEntity = m_ActualCamera; m_EventBroker->Publish(e); @@ -179,6 +183,10 @@ bool EditorSystem::OnWidgetDelta(const Events::WidgetDelta& e) bool EditorSystem::OnInputCommand(const Events::InputCommand& e) { + if (e.PlayerID != -1) { + return false; + } + if (e.Command == "ToggleEditor" && e.Value > 0) { if (m_Enabled) { Disable(); diff --git a/src/Game/Systems/PlayerMovementSystem.cpp b/src/Game/Systems/PlayerMovementSystem.cpp index 351acad5..843601e8 100644 --- a/src/Game/Systems/PlayerMovementSystem.cpp +++ b/src/Game/Systems/PlayerMovementSystem.cpp @@ -27,16 +27,17 @@ void PlayerMovementSystem::Update(double dt) EntityWrapper cameraEntity = player.FirstChildByName("Camera"); if (cameraEntity.Valid()) { glm::vec3& cameraOrientation = cameraEntity["Transform"]["Orientation"]; - cameraOrientation.x = controller->Orientation().x; + cameraOrientation.x += controller->Rotation().x; } ComponentWrapper& cTransform = player["Transform"]; glm::vec3& ori = cTransform["Orientation"]; - ori.y = controller->Orientation().y; + ori.y += controller->Rotation().y; glm::vec3& pos = cTransform["Position"]; pos += controller->Movement() * glm::inverse(glm::quat(ori)) * (float)player["Player"]["MovementSpeed"] * (float)dt; - + + controller->Reset(); } }