Define KeyCount and ScancodeCount out of enums

Those values are not valid Key or Scancode values, so it doesn't make
sense for them to have Key or Scancode type.
Moving them out of their enum makes it possible to write exhaustive
switch case statement without having to write a case for those values.
Making them unsigned int allows to use them as array size without having
to do a static_cast.
This commit is contained in:
kimci86 2023-12-17 18:38:54 +01:00 committed by Chris Thrasher
parent 329e40019a
commit faaceb5b5b
5 changed files with 28 additions and 14 deletions

View File

@ -153,9 +153,16 @@ enum Key
F15, //!< The F15 key
Pause, //!< The Pause key
KeyCount, //!< Keep last -- the total number of keyboard keys
Last = Pause, //!< Keep equal to last enumerator -- used to define KeyCount
};
////////////////////////////////////////////////////////////
/// \brief The total number of keyboard keys, ignoring Key::Unknown
///
////////////////////////////////////////////////////////////
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr unsigned int KeyCount{static_cast<unsigned int>(Key::Last) + 1};
////////////////////////////////////////////////////////////
/// \brief Scancodes
///
@ -320,11 +327,18 @@ enum class Scan
LaunchMail, //!< Keyboard Launch Mail key
LaunchMediaSelect, //!< Keyboard Launch Media Select key
ScancodeCount //!< Keep last -- the total number of scancodes
Last = LaunchMediaSelect, //!< Keep equal to last enumerator -- used to define ScancodeCount
};
using Scancode = Scan;
////////////////////////////////////////////////////////////
/// \brief The total number of scancodes, ignoring Scan::Unknown
///
////////////////////////////////////////////////////////////
// NOLINTNEXTLINE(readability-identifier-naming)
static constexpr unsigned int ScancodeCount{static_cast<unsigned int>(Scan::Last) + 1};
////////////////////////////////////////////////////////////
/// \brief Check if a key is pressed
///

View File

@ -48,8 +48,8 @@ namespace
const KeyCode nullKeyCode = 0;
const int maxKeyCode = 256;
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
KeyCode scancodeToKeycode[sf::Keyboard::ScancodeCount]; ///< Mapping of SFML scancode to X11 KeyCode
sf::Keyboard::Scancode keycodeToScancode[maxKeyCode]; ///< Mapping of X11 KeyCode to SFML scancode
////////////////////////////////////////////////////////////
bool isValidKeycode(KeyCode keycode)

View File

@ -33,8 +33,8 @@
namespace
{
sf::Keyboard::Scancode keyToScancodeMapping[sf::Keyboard::KeyCount]; ///< Mapping from Key to Scancode
sf::Keyboard::Key scancodeToKeyMapping[static_cast<std::size_t>(sf::Keyboard::Scan::ScancodeCount)]; ///< Mapping from Scancode to Key
sf::Keyboard::Scancode keyToScancodeMapping[sf::Keyboard::KeyCount]; ///< Mapping from Key to Scancode
sf::Keyboard::Key scancodeToKeyMapping[sf::Keyboard::ScancodeCount]; ///< Mapping from Scancode to Key
sf::Keyboard::Key virtualKeyToSfKey(UINT virtualKey)
{
@ -527,7 +527,7 @@ void ensureMappings()
key = sf::Keyboard::Unknown;
// Phase 2: Translate scancode to virtual code to key names
for (int i = 0; i < static_cast<int>(sf::Keyboard::Scan::ScancodeCount); ++i)
for (unsigned int i = 0; i < sf::Keyboard::ScancodeCount; ++i)
{
const auto scan = static_cast<sf::Keyboard::Scancode>(i);
const UINT virtualKey = sfScanToVirtualKey(scan);
@ -542,12 +542,12 @@ void ensureMappings()
bool isValidScancode(sf::Keyboard::Scancode code)
{
return code > sf::Keyboard::Scan::Unknown && code < sf::Keyboard::Scan::ScancodeCount;
return code > sf::Keyboard::Scan::Unknown && static_cast<unsigned int>(code) < sf::Keyboard::ScancodeCount;
}
bool isValidKey(sf::Keyboard::Key key)
{
return key > sf::Keyboard::Unknown && key < sf::Keyboard::KeyCount;
return key > sf::Keyboard::Unknown && static_cast<unsigned int>(key) < sf::Keyboard::KeyCount;
}
} // namespace

View File

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

View File

@ -824,7 +824,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 < static_cast<int>(Keyboard::Scan::ScancodeCount); ++i)
for (unsigned int i = 0; i < Keyboard::ScancodeCount; ++i)
{
const auto scan = static_cast<Keyboard::Scancode>(i);
const std::uint8_t virtualCode = scanToVirtualCode(scan);