Skip to content

Commit

Permalink
fix: mouse buttons
Browse files Browse the repository at this point in the history
As an example, MouseMidButton was the same as pressing MouseLeftButton+MouseRightButton at the same time.
Because MouseLeftButton (1) + MouseRightButton (2) = MouseMidButton (3). It means, if I check in my code if I'm pressing mid button `g_window.isPressed(MouseMidButton)`, it will work with mid button, but ALSO if I click left+right buttons.

It should not work like that.
Mouse buttons uses bitwise logic, so these values should be like 0, 1, 2, 4, 8, and so on, not like 0, 1, 2, 3, 4, 5, 6, 7, 8, and so on.
---------

Co-authored-by: Renato <mehahx@gmail.com>
  • Loading branch information
Riverlance and mehah authored Mar 31, 2023
1 parent ef94678 commit 1c631df
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/framework/platform/androidwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ void AndroidWindow::processFingerDownAndUp() {
m_inputEvent.type = (isTouchdown) ? Fw::MousePressInputEvent : Fw::MouseReleaseInputEvent;
m_inputEvent.mouseButton = mouseButton;
if(isTouchdown) {
m_mouseButtonStates |= mouseButton;
m_mouseButtonStates |= 1 << mouseButton;
} else {
g_dispatcher.addEvent([this, mouseButton] { m_mouseButtonStates &= ~mouseButton; });
g_dispatcher.addEvent([this, mouseButton] { m_mouseButtonStates &= ~(1 << mouseButton); });
}

handleInputEvent();
Expand Down
2 changes: 1 addition & 1 deletion src/framework/platform/platformwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class PlatformWindow

bool isKeyPressed(Fw::Key keyCode) { return m_keysState[keyCode]; }
bool isMouseButtonPressed(Fw::MouseButton mouseButton)
{ if (mouseButton == Fw::MouseNoButton) return m_mouseButtonStates != 0; return (m_mouseButtonStates & mouseButton) == mouseButton; }
{ if (mouseButton == Fw::MouseNoButton) return m_mouseButtonStates != 0; return (m_mouseButtonStates & (1 << mouseButton)) == (1 << mouseButton); }
bool isVisible() { return m_visible; }
bool isMaximized() { return m_maximized; }
bool isFullscreen() { return m_fullscreen; }
Expand Down
16 changes: 8 additions & 8 deletions src/framework/platform/win32window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(m_window);
m_inputEvent.reset(Fw::MousePressInputEvent);
m_inputEvent.mouseButton = Fw::MouseLeftButton;
m_mouseButtonStates |= Fw::MouseLeftButton;
m_mouseButtonStates |= 1 << Fw::MouseLeftButton;
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -657,7 +657,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(nullptr);
m_inputEvent.reset(Fw::MouseReleaseInputEvent);
m_inputEvent.mouseButton = Fw::MouseLeftButton;
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseLeftButton; });
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseLeftButton); });
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -667,7 +667,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(m_window);
m_inputEvent.reset(Fw::MousePressInputEvent);
m_inputEvent.mouseButton = Fw::MouseMidButton;
m_mouseButtonStates |= Fw::MouseMidButton;
m_mouseButtonStates |= 1 << Fw::MouseMidButton;
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -677,7 +677,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(nullptr);
m_inputEvent.reset(Fw::MouseReleaseInputEvent);
m_inputEvent.mouseButton = Fw::MouseMidButton;
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseMidButton; });
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseMidButton); });
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -687,7 +687,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(m_window);
m_inputEvent.reset(Fw::MousePressInputEvent);
m_inputEvent.mouseButton = Fw::MouseRightButton;
m_mouseButtonStates |= Fw::MouseRightButton;
m_mouseButtonStates |= 1 << Fw::MouseRightButton;
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -697,7 +697,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
SetCapture(nullptr);
m_inputEvent.reset(Fw::MouseReleaseInputEvent);
m_inputEvent.mouseButton = Fw::MouseRightButton;
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseRightButton; });
g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseRightButton); });
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);
break;
Expand All @@ -710,7 +710,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
const uint32_t mouseButton = (Fw::MouseXButton - 1) + GET_XBUTTON_WPARAM(wParam);
m_inputEvent.reset(Fw::MousePressInputEvent);
m_inputEvent.mouseButton = static_cast<Fw::MouseButton>(mouseButton);
m_mouseButtonStates |= mouseButton;
m_mouseButtonStates |= 1 << mouseButton;
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);

Expand All @@ -723,7 +723,7 @@ LRESULT WIN32Window::windowProc(HWND hWnd, uint32_t uMsg, WPARAM wParam, LPARAM
const uint32_t mouseButton = (Fw::MouseXButton - 1) + GET_XBUTTON_WPARAM(wParam);
m_inputEvent.reset(Fw::MouseReleaseInputEvent);
m_inputEvent.mouseButton = static_cast<Fw::MouseButton>(mouseButton);
g_dispatcher.addEvent([this, mouseButton] { m_mouseButtonStates &= ~mouseButton; });
g_dispatcher.addEvent([this, mouseButton] { m_mouseButtonStates &= ~(1 << mouseButton); });
if (m_onInputEvent)
m_onInputEvent(m_inputEvent);

Expand Down
12 changes: 6 additions & 6 deletions src/framework/platform/x11window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,18 +782,18 @@ void X11Window::poll()
switch (event.xbutton.button) {
case Button1:
m_inputEvent.mouseButton = Fw::MouseLeftButton;
if (event.type == ButtonPress) { m_mouseButtonStates |= Fw::MouseLeftButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseLeftButton; }); }
if (event.type == ButtonPress) { m_mouseButtonStates |= 1 << Fw::MouseLeftButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseLeftButton); }); }
break;
case Button3:
m_inputEvent.mouseButton = Fw::MouseRightButton;
if (event.type == ButtonPress) { m_mouseButtonStates |= Fw::MouseRightButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseRightButton; }); }
if (event.type == ButtonPress) { m_mouseButtonStates |= 1 << Fw::MouseRightButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseRightButton); }); }
break;
case Button2:
m_inputEvent.mouseButton = Fw::MouseMidButton;
if (event.type == ButtonPress) { m_mouseButtonStates |= Fw::MouseMidButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~Fw::MouseMidButton; }); }
if (event.type == ButtonPress) { m_mouseButtonStates |= 1 << Fw::MouseMidButton; }
else { g_dispatcher.addEvent([this] { m_mouseButtonStates &= ~(1 << Fw::MouseMidButton); }); }
break;
case Button4:
if (event.type == ButtonPress) {
Expand Down

0 comments on commit 1c631df

Please sign in to comment.