Make Mouse::Button and Mouse::Wheel scoped enumerations

This commit is contained in:
kimci86 2023-12-10 17:47:13 +01:00 committed by Chris Thrasher
parent ebf190b660
commit 3fb0ffce51
12 changed files with 82 additions and 80 deletions

View File

@ -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...
/// }

View File

@ -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<int>(button)];
}

View File

@ -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<float>(delta);
event.mouseWheelScroll.x = static_cast<int>(AMotionEvent_getX(inputEvent, 0));
event.mouseWheelScroll.y = static_cast<int>(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<int>(Mouse::ButtonCount))
states.isButtonPressed[id] = true;
}
else if (static_cast<unsigned int>(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<int>(Mouse::ButtonCount))
states.isButtonPressed[id] = false;
}
else if (static_cast<std::uint32_t>(device) & AINPUT_SOURCE_TOUCHSCREEN)

View File

@ -34,6 +34,7 @@
#include <fcntl.h>
#include <linux/input.h>
#include <mutex>
#include <optional>
#include <queue>
#include <sstream>
#include <sys/stat.h>
@ -165,23 +166,23 @@ void initFileDescriptors()
std::atexit(uninitFileDescriptors);
}
sf::Mouse::Button toMouseButton(int code)
std::optional<sf::Mouse::Button> 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<sf::Mouse::Button> 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<std::size_t>(*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<int>(mouseMap.size())))
if ((static_cast<int>(button) < 0) || (static_cast<int>(button) >= static_cast<int>(mouseMap.size())))
return false;
update();
return mouseMap[button];
return mouseMap[static_cast<std::size_t>(button)];
}

View File

@ -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
}

View File

@ -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;

View File

@ -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:

View File

@ -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<float>(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<float>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(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<std::int16_t>(LOWORD(lParam));
event.mouseButton.y = static_cast<std::int16_t>(HIWORD(lParam));
pushEvent(event);

View File

@ -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<int>(button);
return (state & flag) != 0;
}

View File

@ -131,14 +131,14 @@
////////////////////////////////////////////////////////
- (void)handleMouseDown:(NSEvent*)theEvent
{
sf::Mouse::Button button = [SFOpenGLView mouseButtonFromEvent:theEvent];
const std::optional<sf::Mouse::Button> button = [SFOpenGLView mouseButtonFromEvent:theEvent];
if (m_requester != nil)
{
NSPoint loc = [self cursorPositionFromEvent:theEvent];
if (button != sf::Mouse::ButtonCount)
m_requester->mouseDownAt(button, static_cast<int>(loc.x), static_cast<int>(loc.y));
if (button)
m_requester->mouseDownAt(*button, static_cast<int>(loc.x), static_cast<int>(loc.y));
}
}
@ -176,14 +176,14 @@
////////////////////////////////////////////////////////////
- (void)handleMouseUp:(NSEvent*)theEvent
{
sf::Mouse::Button button = [SFOpenGLView mouseButtonFromEvent:theEvent];
const std::optional<sf::Mouse::Button> button = [SFOpenGLView mouseButtonFromEvent:theEvent];
if (m_requester != nil)
{
NSPoint loc = [self cursorPositionFromEvent:theEvent];
if (button != sf::Mouse::ButtonCount)
m_requester->mouseUpAt(button, static_cast<int>(loc.x), static_cast<int>(loc.y));
if (button)
m_requester->mouseUpAt(*button, static_cast<int>(loc.x), static_cast<int>(loc.y));
}
}
@ -407,22 +407,22 @@
////////////////////////////////////////////////////////
+ (sf::Mouse::Button)mouseButtonFromEvent:(NSEvent*)event
+ (std::optional<sf::Mouse::Button>)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)
}
}

View File

@ -30,6 +30,7 @@
#import <SFML/Window/macOS/SFOpenGLView.h>
#import <AppKit/AppKit.h>
#include <optional>
////////////////////////////////////////////////////////////
@ -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<sf::Mouse::Button>)mouseButtonFromEvent:(NSEvent*)event;
@end

View File

@ -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;