Skip to content

Commit

Permalink
core: Small improvements to the InputHandler class
Browse files Browse the repository at this point in the history
  • Loading branch information
pierremoreau committed Sep 1, 2021
1 parent 3d8dfa6 commit f3e1c99
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
9 changes: 4 additions & 5 deletions src/core/InputHandler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "InputHandler.h"
#include "Log.h"

/*----------------------------------------------------------------------------*/

Expand All @@ -16,7 +15,7 @@ void InputHandler::Advance()
mTick++;
}

void InputHandler::DownEvent(std::unordered_map<size_t, IState> &map, size_t loc)
void InputHandler::DownEvent(InputStateMap& map, size_t loc)
{
// If the key already exists, insert does not modify the associated value
// and just returns an iterator to it.
Expand All @@ -26,7 +25,7 @@ void InputHandler::DownEvent(std::unordered_map<size_t, IState> &map, size_t loc
state.mDownTick = mTick;
}

void InputHandler::UpEvent(std::unordered_map<size_t, IState> &map, size_t loc)
void InputHandler::UpEvent(InputStateMap& map, size_t loc)
{
// If the key already exists, insert does not modify the associated value
// and just returns an iterator to it.
Expand Down Expand Up @@ -55,7 +54,7 @@ void InputHandler::FeedKeyboard(int key, int scancode, int action)

void InputHandler::FeedMouseMotion(glm::vec2 const& position)
{
mMousePosition = position;
mMousePosition = position;
}

void InputHandler::FeedMouseButtons(int button, int action)
Expand All @@ -74,7 +73,7 @@ void InputHandler::FeedMouseButtons(int button, int action)
mMousePositionSwitched[button] = mMousePosition;
}

std::uint32_t InputHandler::GetState(std::unordered_map<size_t, IState> &map, size_t loc)
std::uint32_t InputHandler::GetState(InputStateMap const& map, size_t loc)
{
auto const sc = map.find(loc);
if (sc == map.end())
Expand Down
19 changes: 10 additions & 9 deletions src/core/InputHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class InputHandler
{
public:
struct IState {
std::uint64_t mDownTick{std::numeric_limits<std::uint64_t>::max()};
std::uint64_t mUpTick{std::numeric_limits<std::uint64_t>::max()};
bool mIsDown{false};
std::uint64_t mDownTick{ std::numeric_limits<std::uint64_t>::max() };
std::uint64_t mUpTick{ std::numeric_limits<std::uint64_t>::max() };
bool mIsDown{ false };
};

public:
Expand All @@ -42,14 +42,15 @@ class InputHandler
void SetUICapture(bool mouseCapture, bool keyboardCapture);

private:
void DownEvent(std::unordered_map<size_t, IState> &map, size_t loc);
void UpEvent(std::unordered_map<size_t, IState> &map, size_t loc);
using InputStateMap = std::unordered_map<size_t, IState>;

std::uint32_t GetState(std::unordered_map<size_t, IState> &map, size_t loc);
void DownEvent(InputStateMap& map, size_t loc);
void UpEvent(InputStateMap& map, size_t loc);
std::uint32_t GetState(InputStateMap const& map, size_t loc);

std::unordered_map<size_t, IState> mScancodeMap;
std::unordered_map<size_t, IState> mKeycodeMap;
std::unordered_map<size_t, IState> mMouseMap;
InputStateMap mScancodeMap;
InputStateMap mKeycodeMap;
InputStateMap mMouseMap;

glm::vec2 mMousePosition{ -1.0f };
std::array<glm::vec2, GLFW_MOUSE_BUTTON_LAST> mMousePositionSwitched;
Expand Down

0 comments on commit f3e1c99

Please sign in to comment.