Skip to content

Commit

Permalink
Merge pull request #103 from Dragonfly911117/update-input
Browse files Browse the repository at this point in the history
Enhance: Make Util::Input detect if mouse button up.
  • Loading branch information
Dragonfly911117 authored Feb 15, 2024
2 parents a133e2f + 7419dcd commit 26ca91a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
20 changes: 10 additions & 10 deletions include/Util/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Util {
* @brief The Input class provides access to keyboard and mouse input.
* @note This class is a singleton and constructable. Use is as follows: \n
`Util::Input::IsKeyPressed(Keycode::A)`,
`Util::Input::IsLButtonPressed()`, etc.
`Util::Input::IsLButtonDown()`, etc.
*/
class Input {
public:
Expand Down Expand Up @@ -67,27 +67,27 @@ class Input {
static bool IsKeyPressed(const Keycode &key);

/**
* \brief Checks if the left mouse button is currently pressed.
* \brief Checks if the left mouse button is currently down.
*
* \return true if the left mouse button is currently pressed, false
* \return true if the left mouse button is currently down, false
* otherwise.r
*
*/
static bool IsLButtonPressed();
static bool IsLButtonDown();

/**
* @brief Checks if the right mouse button is currently pressed.
* @return true if the right mouse button is currently pressed, false
* @brief Checks if the right mouse button is currently down.
* @return true if the right mouse button is currently down, false
* otherwise.
*/
static bool IsRButtonPressed();
static bool IsRButtonDown();

/**
* @brief Checks if the middle mouse button is currently pressed.
* @return true if the middle mouse button is currently pressed, false
* @brief Checks if the middle mouse button is currently down.
* @return true if the middle mouse button is currently down, false
* otherwise.
*/
static bool IsMButtonPressed();
static bool IsMButtonDown();

/**
* @brief Checks if the mouse wheel is currently being scrolled.
Expand Down
12 changes: 6 additions & 6 deletions src/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ void App::Start() {
}

void App::Update() {
if (Util::Input::IsLButtonPressed()) {
LOG_DEBUG("Left button pressed");
if (Util::Input::IsLButtonDown()) {
LOG_DEBUG("Left button down");
}
if (Util::Input::IsRButtonPressed()) {
LOG_DEBUG("Right button pressed");
if (Util::Input::IsRButtonDown()) {
LOG_DEBUG("Right button down");
}
if (Util::Input::IsMButtonPressed()) {
LOG_DEBUG("Middle button pressed");
if (Util::Input::IsMButtonDown()) {
LOG_DEBUG("Middle button down");
}
if (Util::Input::IfScroll()) {
auto delta = Util::Input::GetScrollDistance();
Expand Down
47 changes: 33 additions & 14 deletions src/Util/Input.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Util/Input.hpp"

#include <SDL_events.h> // for SDL_Event
#include "config.hpp"
#include <SDL_events.h> // for SDL_Event

namespace Util {

Expand All @@ -22,13 +22,13 @@ bool Input::IsKeyPressed(const Keycode &key) {
return s_KeyState[temp] != 0;
}

bool Input::IsLButtonPressed() {
bool Input::IsLButtonDown() {
return s_LBPressed;
}
bool Input::IsRButtonPressed() {
bool Input::IsRButtonDown() {
return s_RBPressed;
}
bool Input::IsMButtonPressed() {
bool Input::IsMButtonDown() {
return s_MBPressed;
}
bool Input::IsMouseMoving() {
Expand Down Expand Up @@ -57,19 +57,38 @@ void Input::Update() {
s_CursorPosition.y =
-(s_CursorPosition.y - static_cast<float>(WINDOW_HEIGHT) / 2);

s_LBPressed = s_RBPressed = s_MBPressed = s_Scroll = s_MouseMoving = false;
s_Scroll = s_MouseMoving = false;

while (SDL_PollEvent(&s_Event) != 0) {
s_LBPressed = (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_LEFT) ||
s_LBPressed;
s_RBPressed = (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_RIGHT) ||
s_RBPressed;
s_MBPressed = (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_MIDDLE) ||
s_MBPressed;
if (s_Event.type == SDL_MOUSEBUTTONUP &&
s_Event.button.button == SDL_BUTTON_LEFT) {
s_LBPressed = false;
}
if (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_LEFT) {
s_LBPressed = true;
}

if (s_Event.type == SDL_MOUSEBUTTONUP &&
s_Event.button.button == SDL_BUTTON_RIGHT) {
s_RBPressed = false;
}
if (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_RIGHT) {
s_RBPressed = true;
}

if (s_Event.type == SDL_MOUSEBUTTONUP &&
s_Event.button.button == SDL_BUTTON_MIDDLE) {
s_MBPressed = false;
}
if (s_Event.type == SDL_MOUSEBUTTONDOWN &&
s_Event.button.button == SDL_BUTTON_MIDDLE) {
s_MBPressed = true;
}

s_Scroll = s_Event.type == SDL_MOUSEWHEEL || s_Scroll;

if (s_Scroll) {
s_ScrollDistance.x = static_cast<float>(s_Event.wheel.x);
s_ScrollDistance.y = static_cast<float>(s_Event.wheel.y);
Expand Down

0 comments on commit 26ca91a

Please sign in to comment.