replace std::variant::index

This commit is contained in:
y5nw 2024-09-03 23:04:32 +02:00
parent 6ccf6cebd1
commit d798bbf037
3 changed files with 10 additions and 5 deletions

View File

@ -349,7 +349,11 @@ public:
//! Get the scancode of the corresponding keycode.
virtual u32 getScancodeFromKey(const KeyCode &key) const
{
return key.index() == 0 ? std::get<EKEY_CODE>(key) : KEY_KEY_CODES_COUNT + std::get<wchar_t>(key);
if (const auto *keycode = std::get_if<EKEY_CODE>(&key))
// treat KEY_UNKNOWN and KEY_KEY_CODES_COUNT as the same and return 0.
return KeyCode::isValid(*keycode) ? *keycode : 0;
const auto keychar = std::get<wchar_t>(key);
return keychar == 0 ? 0 : KEY_KEY_CODES_COUNT + keychar;
}
//! Get the keycode of the corresponding scancode.

View File

@ -223,10 +223,9 @@ int CIrrDeviceSDL::findCharToPassToIrrlicht(uint32_t sdlKey, EKEY_CODE irrlichtK
u32 CIrrDeviceSDL::getScancodeFromKey(const KeyCode &key) const
{
u32 keynum = 0;
if (key.index() == 0) {
auto keycode = std::get<EKEY_CODE>(key);
if (const auto *keycode = std::get_if<EKEY_CODE>(&key)) {
for (const auto &entry: KeyMap) {
if (entry.second == keycode) {
if (entry.second == *keycode) {
keynum = entry.first;
break;
}

View File

@ -275,7 +275,9 @@ static const table_key &lookup_keykey(irr::EKEY_CODE key)
static const table_key &lookup_scancode(const u32 scancode)
{
auto key = RenderingEngine::get_raw_device()->getKeyFromScancode(scancode);
return key.index() == 0 ? lookup_keykey(std::get<irr::EKEY_CODE>(key)) : lookup_keychar(std::get<wchar_t>(key));
return std::holds_alternative<EKEY_CODE>(key) ?
lookup_keykey(std::get<irr::EKEY_CODE>(key)) :
lookup_keychar(std::get<wchar_t>(key));
}
KeyPress::KeyPress(std::string_view name)