Convert scancodes into a scoped enumeration

This commit is contained in:
Chris Thrasher 2023-01-22 17:01:28 -07:00
parent 17a8e9e2c2
commit 9f08d67dcc
6 changed files with 184 additions and 198 deletions

View File

@ -164,20 +164,8 @@ enum Key
/// keyboard can be used as reference for the physical position of the keys.
///
////////////////////////////////////////////////////////////
struct Scan
enum class Scan
{
// TODO: replace with enum class in SFML 3.
// Clang warns us rightfully that Scancode names shadow Key names.
// A safer solution would be to use a C++11 scoped enumeration (enum class),
// but it is not possible in SFML 2 which uses C++03.
// For now, we just ignore those warnings.
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wshadow"
#endif
enum Scancode
{
Unknown = -1, //!< Represents any scancode not present in this enum
A = 0, //!< Keyboard a and A key
B, //!< Keyboard b and B key
@ -333,14 +321,9 @@ struct Scan
LaunchMediaSelect, //!< Keyboard Launch Media Select key
ScancodeCount //!< Keep last -- the total number of scancodes
};
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
};
using Scancode = Scan::Scancode;
using Scancode = Scan;
////////////////////////////////////////////////////////////
/// \brief Check if a key is pressed

View File

@ -286,9 +286,9 @@ private:
// Member data
////////////////////////////////////////////////////////////
IOHIDManagerRef m_manager{}; ///< Underlying HID Manager
IOHIDElements m_keys[Keyboard::Scan::ScancodeCount]; ///< All the keys on any connected keyboard
IOHIDElements m_keys[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< All the keys on any connected keyboard
Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
Keyboard::Key m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
////////////////////////////////////////////////////////////
/// m_keys' index corresponds to sf::Keyboard::Scancode enum.

View File

@ -561,7 +561,7 @@ bool HIDInputManager::isKeyPressed(Keyboard::Key key)
////////////////////////////////////////////////////////////
bool HIDInputManager::isKeyPressed(Keyboard::Scancode code)
{
return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[code]);
return (code != Keyboard::Scan::Unknown) && isPressed(m_keys[static_cast<std::size_t>(code)]);
}
@ -571,7 +571,7 @@ Keyboard::Key HIDInputManager::localize(Keyboard::Scancode code)
if (code == Keyboard::Scan::Unknown)
return Keyboard::Unknown;
return m_scancodeToKeyMapping[code];
return m_scancodeToKeyMapping[static_cast<std::size_t>(code)];
}
@ -794,7 +794,7 @@ void HIDInputManager::loadKey(IOHIDElementRef key)
if (code != Keyboard::Scan::Unknown)
{
CFRetain(key);
m_keys[code].push_back(key);
m_keys[static_cast<std::size_t>(code)].push_back(key);
}
}
@ -823,7 +823,7 @@ void HIDInputManager::buildMappings()
// For each scancode having a IOHIDElement, we translate the corresponding
// virtual code to a localized Key.
for (int i = 0; i < Keyboard::Scan::ScancodeCount; ++i)
for (int i = 0; i < static_cast<int>(Keyboard::Scan::ScancodeCount); ++i)
{
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
std::uint8_t virtualCode = scanToVirtualCode(scan);
@ -898,7 +898,7 @@ void HIDInputManager::buildMappings()
// Register the bi-mapping
if (m_keyToScancodeMapping[code] == Keyboard::Scan::Unknown)
m_keyToScancodeMapping[code] = scan;
m_scancodeToKeyMapping[scan] = code;
m_scancodeToKeyMapping[static_cast<std::size_t>(scan)] = code;
}
CFRelease(tis);

View File

@ -46,7 +46,7 @@ namespace
const KeyCode nullKeyCode = 0;
const int maxKeyCode = 256;
KeyCode scancodeToKeycode[sf::Keyboard::Scan::ScancodeCount]; ///< Mapping of SFML scancode to X11 KeyCode
KeyCode scancodeToKeycode[static_cast<std::size_t>(sf::Keyboard::Scan::ScancodeCount)]; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode
////////////////////////////////////////////////////////////
@ -491,7 +491,7 @@ void ensureMapping()
scancode = mappedScancode->second;
if (scancode != sf::Keyboard::Scan::Unknown)
scancodeToKeycode[scancode] = static_cast<KeyCode>(keycode);
scancodeToKeycode[static_cast<std::size_t>(scancode)] = static_cast<KeyCode>(keycode);
keycodeToScancode[keycode] = scancode;
}
@ -506,8 +506,9 @@ void ensureMapping()
{
scancode = translateKeyCode(display, static_cast<KeyCode>(keycode));
if (scancode != sf::Keyboard::Scan::Unknown && scancodeToKeycode[scancode] == nullKeyCode)
scancodeToKeycode[scancode] = static_cast<KeyCode>(keycode);
if (scancode != sf::Keyboard::Scan::Unknown &&
scancodeToKeycode[static_cast<std::size_t>(scancode)] == nullKeyCode)
scancodeToKeycode[static_cast<std::size_t>(scancode)] = static_cast<KeyCode>(keycode);
keycodeToScancode[keycode] = scancode;
}
@ -525,7 +526,7 @@ KeyCode scancodeToKeyCode(sf::Keyboard::Scancode code)
ensureMapping();
if (code != sf::Keyboard::Scan::Unknown)
return scancodeToKeycode[code];
return scancodeToKeycode[static_cast<std::size_t>(code)];
return nullKeyCode;
}
@ -560,7 +561,7 @@ KeyCode keyToKeyCode(sf::Keyboard::Key key)
// Fallback for when XKeysymToKeycode cannot tell the KeyCode for XK_Alt_R
if (key == sf::Keyboard::RAlt)
return scancodeToKeycode[sf::Keyboard::Scan::RAlt];
return scancodeToKeycode[static_cast<std::size_t>(sf::Keyboard::Scan::RAlt)];
return nullKeyCode;
}

View File

@ -34,7 +34,7 @@ namespace sf::priv
{
////////////////////////////////////////////////////////////
Keyboard::Scancode InputImpl::m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
Keyboard::Key InputImpl::m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
Keyboard::Key InputImpl::m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
////////////////////////////////////////////////////////////
Keyboard::Key virtualKeyToSfKey(UINT virtualKey)
@ -541,14 +541,14 @@ void InputImpl::ensureMappings()
key = Keyboard::Unknown;
// Phase 2: Translate scancode to virtual code to key names
for (int i = 0; i < Keyboard::Scan::ScancodeCount; ++i)
for (int i = 0; i < static_cast<int>(Keyboard::Scan::ScancodeCount); ++i)
{
Keyboard::Scancode scan = static_cast<Keyboard::Scancode>(i);
UINT virtualKey = sfScanToVirtualKey(scan);
Keyboard::Key key = virtualKeyToSfKey(virtualKey);
if (key != Keyboard::Unknown && m_keyToScancodeMapping[key] == Keyboard::Scan::Unknown)
m_keyToScancodeMapping[key] = scan;
m_scancodeToKeyMapping[scan] = key;
m_scancodeToKeyMapping[static_cast<std::size_t>(scan)] = key;
}
isMappingInitialized = true;
@ -576,7 +576,7 @@ Keyboard::Key InputImpl::localize(Keyboard::Scancode code)
ensureMappings();
return m_scancodeToKeyMapping[code];
return m_scancodeToKeyMapping[static_cast<std::size_t>(code)];
}
////////////////////////////////////////////////////////////

View File

@ -30,6 +30,8 @@
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <cstddef>
namespace sf::priv
{
@ -185,7 +187,7 @@ private:
////////////////////////////////////////////////////////////
// NOLINTBEGIN(readability-identifier-naming)
static Keyboard::Scancode m_keyToScancodeMapping[Keyboard::KeyCount]; ///< Mapping from Key to Scancode
static Keyboard::Key m_scancodeToKeyMapping[Keyboard::Scan::ScancodeCount]; ///< Mapping from Scancode to Key
static Keyboard::Key m_scancodeToKeyMapping[static_cast<std::size_t>(Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
// NOLINTEND(readability-identifier-naming)
};