diff --git a/include/SFML/Window/Mouse.hpp b/include/SFML/Window/Mouse.hpp index e5fe2e6b4..35ec379d7 100644 --- a/include/SFML/Window/Mouse.hpp +++ b/include/SFML/Window/Mouse.hpp @@ -46,32 +46,33 @@ namespace Mouse /// \brief Mouse buttons /// //////////////////////////////////////////////////////////// -enum Button +enum class Button { Left, //!< The left mouse button Right, //!< The right mouse button Middle, //!< The middle (wheel) mouse button XButton1, //!< The first extra mouse button - XButton2, //!< The second extra mouse button - - ButtonCount //!< Keep last -- the total number of mouse buttons + XButton2 //!< The second extra mouse button }; +// NOLINTNEXTLINE(readability-identifier-naming) +static constexpr unsigned int ButtonCount{5}; //!< The total number of mouse buttons + //////////////////////////////////////////////////////////// /// \brief Mouse wheels /// //////////////////////////////////////////////////////////// -enum Wheel +enum class Wheel { - VerticalWheel, //!< The vertical mouse wheel - HorizontalWheel //!< The horizontal mouse wheel + Vertical, //!< The vertical mouse wheel + Horizontal //!< The horizontal mouse wheel }; //////////////////////////////////////////////////////////// /// \brief Check if a mouse button is pressed /// -/// \warning Checking the state of buttons Mouse::XButton1 and -/// Mouse::XButton2 is not supported on Linux with X11. +/// \warning Checking the state of buttons Mouse::Button::XButton1 and +/// Mouse::Button::XButton2 is not supported on Linux with X11. /// /// \param button Button to check /// @@ -157,7 +158,7 @@ SFML_WINDOW_API void setPosition(const Vector2i& position, const WindowBase& rel /// /// Usage example: /// \code -/// if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) +/// if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) /// { /// // left click... /// } diff --git a/src/SFML/Window/Android/InputImpl.cpp b/src/SFML/Window/Android/InputImpl.cpp index 38f9f040e..dbe12deac 100644 --- a/src/SFML/Window/Android/InputImpl.cpp +++ b/src/SFML/Window/Android/InputImpl.cpp @@ -171,7 +171,7 @@ bool isMouseButtonPressed(Mouse::Button button) ActivityStates& states = getActivity(); const std::lock_guard lock(states.mutex); - return states.isButtonPressed[button]; + return states.isButtonPressed[static_cast(button)]; } diff --git a/src/SFML/Window/Android/WindowImplAndroid.cpp b/src/SFML/Window/Android/WindowImplAndroid.cpp index ee0f676e1..85ace1058 100644 --- a/src/SFML/Window/Android/WindowImplAndroid.cpp +++ b/src/SFML/Window/Android/WindowImplAndroid.cpp @@ -393,7 +393,7 @@ int WindowImplAndroid::processScrollEvent(AInputEvent* inputEvent, ActivityState // Create and send our mouse wheel event Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::VerticalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Vertical; event.mouseWheelScroll.delta = static_cast(delta); event.mouseWheelScroll.x = static_cast(AMotionEvent_getX(inputEvent, 0)); event.mouseWheelScroll.y = static_cast(AMotionEvent_getY(inputEvent, 0)); @@ -539,7 +539,7 @@ int WindowImplAndroid::processPointerEvent(bool isDown, AInputEvent* inputEvent, event.mouseButton.x = x; event.mouseButton.y = y; - if (id >= 0 && id < Mouse::ButtonCount) + if (id >= 0 && id < static_cast(Mouse::ButtonCount)) states.isButtonPressed[id] = true; } else if (static_cast(device) & AINPUT_SOURCE_TOUCHSCREEN) @@ -561,7 +561,7 @@ int WindowImplAndroid::processPointerEvent(bool isDown, AInputEvent* inputEvent, event.mouseButton.x = x; event.mouseButton.y = y; - if (id >= 0 && id < Mouse::ButtonCount) + if (id >= 0 && id < static_cast(Mouse::ButtonCount)) states.isButtonPressed[id] = false; } else if (static_cast(device) & AINPUT_SOURCE_TOUCHSCREEN) diff --git a/src/SFML/Window/DRM/InputImpl.cpp b/src/SFML/Window/DRM/InputImpl.cpp index b89c74d1f..8df033bb6 100644 --- a/src/SFML/Window/DRM/InputImpl.cpp +++ b/src/SFML/Window/DRM/InputImpl.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -165,23 +166,23 @@ void initFileDescriptors() std::atexit(uninitFileDescriptors); } -sf::Mouse::Button toMouseButton(int code) +std::optional toMouseButton(int code) { switch (code) { case BTN_LEFT: - return sf::Mouse::Left; + return sf::Mouse::Button::Left; case BTN_RIGHT: - return sf::Mouse::Right; + return sf::Mouse::Button::Right; case BTN_MIDDLE: - return sf::Mouse::Middle; + return sf::Mouse::Button::Middle; case BTN_SIDE: - return sf::Mouse::XButton1; + return sf::Mouse::Button::XButton1; case BTN_EXTRA: - return sf::Mouse::XButton2; + return sf::Mouse::Button::XButton2; default: - return sf::Mouse::ButtonCount; + return std::nullopt; } } @@ -384,15 +385,14 @@ bool eventProcess(sf::Event& event) { if (inputEvent.type == EV_KEY) { - const sf::Mouse::Button mb = toMouseButton(inputEvent.code); - if (mb != sf::Mouse::ButtonCount) + if (const std::optional mb = toMouseButton(inputEvent.code)) { event.type = inputEvent.value ? sf::Event::MouseButtonPressed : sf::Event::MouseButtonReleased; - event.mouseButton.button = mb; + event.mouseButton.button = *mb; event.mouseButton.x = mousePos.x; event.mouseButton.y = mousePos.y; - mouseMap[mb] = inputEvent.value; + mouseMap[static_cast(*mb)] = inputEvent.value; return true; } else @@ -628,11 +628,11 @@ void setVirtualKeyboardVisible(bool /*visible*/) bool isMouseButtonPressed(Mouse::Button button) { const std::lock_guard lock(inputMutex); - if ((button < 0) || (button >= static_cast(mouseMap.size()))) + if ((static_cast(button) < 0) || (static_cast(button) >= static_cast(mouseMap.size()))) return false; update(); - return mouseMap[button]; + return mouseMap[static_cast(button)]; } diff --git a/src/SFML/Window/Unix/InputImpl.cpp b/src/SFML/Window/Unix/InputImpl.cpp index 681e6a2d6..1cb74d03b 100644 --- a/src/SFML/Window/Unix/InputImpl.cpp +++ b/src/SFML/Window/Unix/InputImpl.cpp @@ -103,16 +103,16 @@ bool isMouseButtonPressed(Mouse::Button button) // Buttons 4 and 5 are the vertical wheel and 6 and 7 the horizontal wheel. // There is no mask for buttons 8 and 9, so checking the state of buttons - // Mouse::XButton1 and Mouse::XButton2 is not supported. + // Mouse::Button::XButton1 and Mouse::Button::XButton2 is not supported. // clang-format off switch (button) { - case Mouse::Left: return buttons & Button1Mask; - case Mouse::Right: return buttons & Button3Mask; - case Mouse::Middle: return buttons & Button2Mask; - case Mouse::XButton1: return false; // not supported by X - case Mouse::XButton2: return false; // not supported by X - default: return false; + case Mouse::Button::Left: return buttons & Button1Mask; + case Mouse::Button::Right: return buttons & Button3Mask; + case Mouse::Button::Middle: return buttons & Button2Mask; + case Mouse::Button::XButton1: return false; // not supported by X + case Mouse::Button::XButton2: return false; // not supported by X + default: return false; } // clang-format on } diff --git a/src/SFML/Window/Unix/WindowImplX11.cpp b/src/SFML/Window/Unix/WindowImplX11.cpp index 81a60cc79..6132e7d63 100644 --- a/src/SFML/Window/Unix/WindowImplX11.cpp +++ b/src/SFML/Window/Unix/WindowImplX11.cpp @@ -1937,11 +1937,11 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) // clang-format off switch(button) { - case Button1: event.mouseButton.button = Mouse::Left; break; - case Button2: event.mouseButton.button = Mouse::Middle; break; - case Button3: event.mouseButton.button = Mouse::Right; break; - case 8: event.mouseButton.button = Mouse::XButton1; break; - case 9: event.mouseButton.button = Mouse::XButton2; break; + case Button1: event.mouseButton.button = Mouse::Button::Left; break; + case Button2: event.mouseButton.button = Mouse::Button::Middle; break; + case Button3: event.mouseButton.button = Mouse::Button::Right; break; + case 8: event.mouseButton.button = Mouse::Button::XButton1; break; + case 9: event.mouseButton.button = Mouse::Button::XButton2; break; } // clang-format on @@ -1966,19 +1966,19 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) switch (button) { case Button1: - event.mouseButton.button = Mouse::Left; + event.mouseButton.button = Mouse::Button::Left; break; case Button2: - event.mouseButton.button = Mouse::Middle; + event.mouseButton.button = Mouse::Button::Middle; break; case Button3: - event.mouseButton.button = Mouse::Right; + event.mouseButton.button = Mouse::Button::Right; break; case 8: - event.mouseButton.button = Mouse::XButton1; + event.mouseButton.button = Mouse::Button::XButton1; break; case 9: - event.mouseButton.button = Mouse::XButton2; + event.mouseButton.button = Mouse::Button::XButton2; break; } pushEvent(event); @@ -1988,7 +1988,7 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::VerticalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Vertical; event.mouseWheelScroll.delta = (button == Button4) ? 1 : -1; event.mouseWheelScroll.x = windowEvent.xbutton.x; event.mouseWheelScroll.y = windowEvent.xbutton.y; @@ -1998,7 +1998,7 @@ bool WindowImplX11::processEvent(XEvent& windowEvent) { Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::HorizontalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Horizontal; event.mouseWheelScroll.delta = (button == 6) ? 1 : -1; event.mouseWheelScroll.x = windowEvent.xbutton.x; event.mouseWheelScroll.y = windowEvent.xbutton.y; diff --git a/src/SFML/Window/Win32/InputImpl.cpp b/src/SFML/Window/Win32/InputImpl.cpp index 9d4d35fb7..007c9c141 100644 --- a/src/SFML/Window/Win32/InputImpl.cpp +++ b/src/SFML/Window/Win32/InputImpl.cpp @@ -622,19 +622,19 @@ bool isMouseButtonPressed(Mouse::Button button) int virtualKey = 0; switch (button) { - case Mouse::Left: + case Mouse::Button::Left: virtualKey = GetSystemMetrics(SM_SWAPBUTTON) ? VK_RBUTTON : VK_LBUTTON; break; - case Mouse::Right: + case Mouse::Button::Right: virtualKey = GetSystemMetrics(SM_SWAPBUTTON) ? VK_LBUTTON : VK_RBUTTON; break; - case Mouse::Middle: + case Mouse::Button::Middle: virtualKey = VK_MBUTTON; break; - case Mouse::XButton1: + case Mouse::Button::XButton1: virtualKey = VK_XBUTTON1; break; - case Mouse::XButton2: + case Mouse::Button::XButton2: virtualKey = VK_XBUTTON2; break; default: diff --git a/src/SFML/Window/Win32/WindowImplWin32.cpp b/src/SFML/Window/Win32/WindowImplWin32.cpp index 57a0fe021..b3761d567 100644 --- a/src/SFML/Window/Win32/WindowImplWin32.cpp +++ b/src/SFML/Window/Win32/WindowImplWin32.cpp @@ -921,7 +921,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::VerticalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Vertical; event.mouseWheelScroll.delta = static_cast(delta) / 120.f; event.mouseWheelScroll.x = position.x; event.mouseWheelScroll.y = position.y; @@ -942,7 +942,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::HorizontalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Horizontal; event.mouseWheelScroll.delta = -static_cast(delta) / 120.f; event.mouseWheelScroll.x = position.x; event.mouseWheelScroll.y = position.y; @@ -955,7 +955,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonPressed; - event.mouseButton.button = Mouse::Left; + event.mouseButton.button = Mouse::Button::Left; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -967,7 +967,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonReleased; - event.mouseButton.button = Mouse::Left; + event.mouseButton.button = Mouse::Button::Left; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -979,7 +979,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonPressed; - event.mouseButton.button = Mouse::Right; + event.mouseButton.button = Mouse::Button::Right; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -991,7 +991,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonReleased; - event.mouseButton.button = Mouse::Right; + event.mouseButton.button = Mouse::Button::Right; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -1003,7 +1003,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonPressed; - event.mouseButton.button = Mouse::Middle; + event.mouseButton.button = Mouse::Button::Middle; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -1015,7 +1015,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonReleased; - event.mouseButton.button = Mouse::Middle; + event.mouseButton.button = Mouse::Button::Middle; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -1027,7 +1027,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonPressed; - event.mouseButton.button = HIWORD(wParam) == XBUTTON1 ? Mouse::XButton1 : Mouse::XButton2; + event.mouseButton.button = HIWORD(wParam) == XBUTTON1 ? Mouse::Button::XButton1 : Mouse::Button::XButton2; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); @@ -1039,7 +1039,7 @@ void WindowImplWin32::processEvent(UINT message, WPARAM wParam, LPARAM lParam) { Event event; event.type = Event::MouseButtonReleased; - event.mouseButton.button = HIWORD(wParam) == XBUTTON1 ? Mouse::XButton1 : Mouse::XButton2; + event.mouseButton.button = HIWORD(wParam) == XBUTTON1 ? Mouse::Button::XButton1 : Mouse::Button::XButton2; event.mouseButton.x = static_cast(LOWORD(lParam)); event.mouseButton.y = static_cast(HIWORD(lParam)); pushEvent(event); diff --git a/src/SFML/Window/macOS/InputImpl.mm b/src/SFML/Window/macOS/InputImpl.mm index 5df6fa581..a64f5475b 100644 --- a/src/SFML/Window/macOS/InputImpl.mm +++ b/src/SFML/Window/macOS/InputImpl.mm @@ -167,7 +167,7 @@ bool isMouseButtonPressed(Mouse::Button button) { const AutoreleasePool pool; const NSUInteger state = [NSEvent pressedMouseButtons]; - const NSUInteger flag = 1 << button; + const NSUInteger flag = 1 << static_cast(button); return (state & flag) != 0; } diff --git a/src/SFML/Window/macOS/SFOpenGLView+mouse.mm b/src/SFML/Window/macOS/SFOpenGLView+mouse.mm index e193f2f9d..87fc214cd 100644 --- a/src/SFML/Window/macOS/SFOpenGLView+mouse.mm +++ b/src/SFML/Window/macOS/SFOpenGLView+mouse.mm @@ -131,14 +131,14 @@ //////////////////////////////////////////////////////// - (void)handleMouseDown:(NSEvent*)theEvent { - sf::Mouse::Button button = [SFOpenGLView mouseButtonFromEvent:theEvent]; + const std::optional button = [SFOpenGLView mouseButtonFromEvent:theEvent]; if (m_requester != nil) { NSPoint loc = [self cursorPositionFromEvent:theEvent]; - if (button != sf::Mouse::ButtonCount) - m_requester->mouseDownAt(button, static_cast(loc.x), static_cast(loc.y)); + if (button) + m_requester->mouseDownAt(*button, static_cast(loc.x), static_cast(loc.y)); } } @@ -176,14 +176,14 @@ //////////////////////////////////////////////////////////// - (void)handleMouseUp:(NSEvent*)theEvent { - sf::Mouse::Button button = [SFOpenGLView mouseButtonFromEvent:theEvent]; + const std::optional button = [SFOpenGLView mouseButtonFromEvent:theEvent]; if (m_requester != nil) { NSPoint loc = [self cursorPositionFromEvent:theEvent]; - if (button != sf::Mouse::ButtonCount) - m_requester->mouseUpAt(button, static_cast(loc.x), static_cast(loc.y)); + if (button) + m_requester->mouseUpAt(*button, static_cast(loc.x), static_cast(loc.y)); } } @@ -407,22 +407,22 @@ //////////////////////////////////////////////////////// -+ (sf::Mouse::Button)mouseButtonFromEvent:(NSEvent*)event ++ (std::optional)mouseButtonFromEvent:(NSEvent*)event { switch ([event buttonNumber]) { case 0: - return sf::Mouse::Left; + return sf::Mouse::Button::Left; case 1: - return sf::Mouse::Right; + return sf::Mouse::Button::Right; case 2: - return sf::Mouse::Middle; + return sf::Mouse::Button::Middle; case 3: - return sf::Mouse::XButton1; + return sf::Mouse::Button::XButton1; case 4: - return sf::Mouse::XButton2; + return sf::Mouse::Button::XButton2; default: - return sf::Mouse::ButtonCount; // Never happens! (hopefully) + return std::nullopt; // Never happens! (hopefully) } } diff --git a/src/SFML/Window/macOS/SFOpenGLView+mouse_priv.h b/src/SFML/Window/macOS/SFOpenGLView+mouse_priv.h index b24f7345a..52ced2bb8 100644 --- a/src/SFML/Window/macOS/SFOpenGLView+mouse_priv.h +++ b/src/SFML/Window/macOS/SFOpenGLView+mouse_priv.h @@ -30,6 +30,7 @@ #import #import +#include //////////////////////////////////////////////////////////// @@ -98,13 +99,13 @@ - (CGDirectDisplayID)displayId; //////////////////////////////////////////////////////////// -/// \brief Convert the NSEvent mouse button type to SFML type +/// \brief Try to convert the NSEvent mouse button type to SFML type /// /// \param event a mouse button event /// -/// \return Left, Right, ..., or ButtonCount if the button is unknown +/// \return Left, Right, ..., or std::nullopt if the button is unknown /// //////////////////////////////////////////////////////////// -+ (sf::Mouse::Button)mouseButtonFromEvent:(NSEvent*)event; ++ (std::optional)mouseButtonFromEvent:(NSEvent*)event; @end diff --git a/src/SFML/Window/macOS/WindowImplCocoa.mm b/src/SFML/Window/macOS/WindowImplCocoa.mm index 36876b894..14e2368db 100644 --- a/src/SFML/Window/macOS/WindowImplCocoa.mm +++ b/src/SFML/Window/macOS/WindowImplCocoa.mm @@ -298,7 +298,7 @@ void WindowImplCocoa::mouseWheelScrolledAt(float deltaX, float deltaY, int x, in Event event; event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::VerticalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Vertical; event.mouseWheelScroll.delta = deltaY; event.mouseWheelScroll.x = x; event.mouseWheelScroll.y = y; @@ -306,7 +306,7 @@ void WindowImplCocoa::mouseWheelScrolledAt(float deltaX, float deltaY, int x, in pushEvent(event); event.type = Event::MouseWheelScrolled; - event.mouseWheelScroll.wheel = Mouse::HorizontalWheel; + event.mouseWheelScroll.wheel = Mouse::Wheel::Horizontal; event.mouseWheelScroll.delta = deltaX; event.mouseWheelScroll.x = x; event.mouseWheelScroll.y = y;