diff --git a/FlexEngine/include/FreeCamera.hpp b/FlexEngine/include/FreeCamera.hpp index fb5781192..073a7d7a8 100644 --- a/FlexEngine/include/FreeCamera.hpp +++ b/FlexEngine/include/FreeCamera.hpp @@ -64,6 +64,7 @@ namespace flex float m_ZFar; glm::vec3 m_Position; + glm::vec3 m_DragStartPosition; float m_Yaw; float m_Pitch; @@ -71,9 +72,10 @@ namespace flex glm::vec3 m_Up; glm::vec3 m_Right; - float m_MoveSpeed; - float m_ScrollDollySpeed; - float m_DragDollySpeed; + float m_MoveSpeed; // Keyboard + float m_PanSpeed; // MMB + float m_DragDollySpeed; // RMB + float m_ScrollDollySpeed; // Scroll wheel float m_MoveSpeedFastMultiplier; float m_MoveSpeedSlowMultiplier; float m_RotationSpeed; diff --git a/FlexEngine/include/InputManager.hpp b/FlexEngine/include/InputManager.hpp index 33715a2d0..7f31072ea 100644 --- a/FlexEngine/include/InputManager.hpp +++ b/FlexEngine/include/InputManager.hpp @@ -180,6 +180,12 @@ namespace flex int down; // A count of how many frames this key has been down for (0 means not down) }; + struct MouseDrag + { + glm::vec2 startLocation; + glm::vec2 endLocation; + }; + InputManager(); ~InputManager(); @@ -191,7 +197,7 @@ namespace flex bool GetKeyPressed(KeyCode keyCode) const; void CursorPosCallback(double x, double y); - void MouseButtonCallback(const GameContext& gameContext, MouseButton button, Action action, int mods); + void MouseButtonCallback(const GameContext& gameContext, MouseButton mouseButton, Action action, int mods); void ScrollCallback(double xOffset, double yOffset); void KeyCallback(KeyCode keycode, Action action, int mods); void CharCallback(unsigned int character); @@ -199,10 +205,12 @@ namespace flex void SetMousePosition(glm::vec2 mousePos, bool updatePreviousPos = true); glm::vec2 GetMousePosition() const; glm::vec2 GetMouseMovement() const; - int GetMouseButtonDown(MouseButton button) const; - bool GetMouseButtonClicked(MouseButton button) const; + int GetMouseButtonDown(MouseButton mouseButton) const; + bool GetMouseButtonClicked(MouseButton mouseButton) const; float GetVerticalScrollDistance() const; + glm::vec2 GetMouseDragDistance(MouseButton mouseButton); + void ClearAllInputs(const GameContext& gameContext); void ClearMouseInput(const GameContext& gameContext); void ClearKeyboadInput(const GameContext& gameContext); @@ -212,6 +220,7 @@ namespace flex static const int MOUSE_BUTTON_COUNT = (int)MouseButton::_NONE; Key m_MouseButtons[MOUSE_BUTTON_COUNT]; + MouseDrag m_MouseButtonDrags[MOUSE_BUTTON_COUNT]; glm::vec2 m_MousePosition; glm::vec2 m_PrevMousePosition; float m_ScrollXOffset; diff --git a/FlexEngine/src/FreeCamera.cpp b/FlexEngine/src/FreeCamera.cpp index 9d96db942..1ffdb05ea 100644 --- a/FlexEngine/src/FreeCamera.cpp +++ b/FlexEngine/src/FreeCamera.cpp @@ -15,10 +15,11 @@ namespace flex m_FOV(FOV), m_ZNear(zNear), m_ZFar(zFar), m_Position(glm::vec3(0.0f)), m_MoveSpeed(50.0f), + m_PanSpeed(10.0f), + m_DragDollySpeed(0.1f), + m_ScrollDollySpeed(2.0f), m_MoveSpeedFastMultiplier(3.5f), m_MoveSpeedSlowMultiplier(0.05f), - m_ScrollDollySpeed(2.0f), - m_DragDollySpeed(0.1f), m_RotationSpeed(0.0011f), m_View(glm::mat4(0.0f)), m_Proj(glm::mat4(0.0f)), @@ -90,6 +91,18 @@ namespace flex translation -= m_Up; } + if (gameContext.inputManager->GetMouseButtonClicked(InputManager::MouseButton::MIDDLE)) + { + m_DragStartPosition = m_Position; + } + else if (gameContext.inputManager->GetMouseButtonDown(InputManager::MouseButton::MIDDLE)) + { + glm::vec2 dragDist = gameContext.inputManager->GetMouseDragDistance(InputManager::MouseButton::MIDDLE); + glm::vec2 frameBufferSize = (glm::vec2)gameContext.window->GetFrameBufferSize(); + glm::vec2 normDragDist = dragDist / frameBufferSize; + m_Position = (m_DragStartPosition + (normDragDist.x * m_Right + normDragDist.y * m_Up) * m_PanSpeed); + } + float scrollDistance = gameContext.inputManager->GetVerticalScrollDistance(); if (scrollDistance != 0.0f) { @@ -112,7 +125,9 @@ namespace flex speedMultiplier = m_MoveSpeedSlowMultiplier; } - Translate(translation * m_MoveSpeed * speedMultiplier * gameContext.deltaTime); + glm::vec3 finalTranslation = translation * m_MoveSpeed * speedMultiplier * gameContext.deltaTime; + Translate(finalTranslation); + m_DragStartPosition += finalTranslation; RecalculateViewProjection(gameContext); } diff --git a/FlexEngine/src/InputManager.cpp b/FlexEngine/src/InputManager.cpp index cdb99d607..f8821d8a1 100644 --- a/FlexEngine/src/InputManager.cpp +++ b/FlexEngine/src/InputManager.cpp @@ -57,6 +57,7 @@ namespace flex if (m_MouseButtons[i].down > 0) { ++m_MouseButtons[i].down; + m_MouseButtonDrags[i].endLocation = m_MousePosition; } } } @@ -105,25 +106,28 @@ namespace flex io.MousePos = m_MousePosition; } - void InputManager::MouseButtonCallback(const GameContext& gameContext, MouseButton button, Action action, int mods) + void InputManager::MouseButtonCallback(const GameContext& gameContext, MouseButton mouseButton, Action action, int mods) { UNREFERENCED_PARAMETER(gameContext); UNREFERENCED_PARAMETER(mods); - assert((int)button < MOUSE_BUTTON_COUNT); + assert((int)mouseButton < MOUSE_BUTTON_COUNT); if (action == Action::PRESS) { - ++m_MouseButtons[(int)button].down; - + ++m_MouseButtons[(int)mouseButton].down; + m_MouseButtonDrags[(int)mouseButton].startLocation = m_MousePosition; + m_MouseButtonDrags[(int)mouseButton].endLocation = m_MousePosition; //if (button == MouseButton::LEFT) //{ // gameContext.window->SetCursorMode(Window::CursorMode::HIDDEN); //} } - else + else if (action == Action::RELEASE) { - m_MouseButtons[(int)button].down = 0; + m_MouseButtons[(int)mouseButton].down = 0; + m_MouseButtonDrags[(int)mouseButton].startLocation = m_MousePosition; + m_MouseButtonDrags[(int)mouseButton].endLocation = m_MousePosition; //if (button == MouseButton::LEFT) //{ @@ -132,8 +136,8 @@ namespace flex } ImGuiIO& io = ImGui::GetIO(); - io.MouseDown[(int)button] = m_MouseButtons[(int)button].down > 0; - io.MouseClicked[(int)button] = m_MouseButtons[(int)button].down == 1; + io.MouseDown[(int)mouseButton] = m_MouseButtons[(int)mouseButton].down > 0; + io.MouseClicked[(int)mouseButton] = m_MouseButtons[(int)mouseButton].down == 1; } void InputManager::ScrollCallback(double xOffset, double yOffset) @@ -197,18 +201,18 @@ namespace flex return m_MousePosition - m_PrevMousePosition; } - int InputManager::GetMouseButtonDown(MouseButton button) const + int InputManager::GetMouseButtonDown(MouseButton mouseButton) const { - assert((int)button >= 0 && (int)button <= MOUSE_BUTTON_COUNT - 1); + assert((int)mouseButton >= 0 && (int)mouseButton <= MOUSE_BUTTON_COUNT - 1); - return m_MouseButtons[(int)button].down; + return m_MouseButtons[(int)mouseButton].down; } - bool InputManager::GetMouseButtonClicked(MouseButton button) const + bool InputManager::GetMouseButtonClicked(MouseButton mouseButton) const { - assert((int)button >= 0 && (int)button <= MOUSE_BUTTON_COUNT - 1); + assert((int)mouseButton >= 0 && (int)mouseButton <= MOUSE_BUTTON_COUNT - 1); - return m_MouseButtons[(int)button].down == 1; + return (m_MouseButtons[(int)mouseButton].down == 1); } float InputManager::GetVerticalScrollDistance() const @@ -216,6 +220,13 @@ namespace flex return m_ScrollYOffset; } + glm::vec2 InputManager::GetMouseDragDistance(MouseButton mouseButton) + { + assert((int)mouseButton >= 0 && (int)mouseButton <= MOUSE_BUTTON_COUNT - 1); + + return (m_MouseButtonDrags[(int)mouseButton].endLocation - m_MouseButtonDrags[(int)mouseButton].startLocation); + } + void InputManager::ClearAllInputs(const GameContext& gameContext) { ClearMouseInput(gameContext);