Skip to content

Commit

Permalink
Add mouse drag support to input manager
Browse files Browse the repository at this point in the history
Camera can now be panned using MMB click & drag
  • Loading branch information
ajweeks committed Nov 8, 2017
1 parent b136280 commit f1904b8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 23 deletions.
8 changes: 5 additions & 3 deletions FlexEngine/include/FreeCamera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,18 @@ namespace flex
float m_ZFar;

glm::vec3 m_Position;
glm::vec3 m_DragStartPosition;

float m_Yaw;
float m_Pitch;
glm::vec3 m_Forward;
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;
Expand Down
15 changes: 12 additions & 3 deletions FlexEngine/include/InputManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -191,18 +197,20 @@ 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);

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);
Expand All @@ -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;
Expand Down
21 changes: 18 additions & 3 deletions FlexEngine/src/FreeCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down
39 changes: 25 additions & 14 deletions FlexEngine/src/InputManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace flex
if (m_MouseButtons[i].down > 0)
{
++m_MouseButtons[i].down;
m_MouseButtonDrags[i].endLocation = m_MousePosition;
}
}
}
Expand Down Expand Up @@ -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)
//{
Expand All @@ -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)
Expand Down Expand Up @@ -197,25 +201,32 @@ 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
{
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);
Expand Down

0 comments on commit f1904b8

Please sign in to comment.