2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-05-14 15:43:26 +03:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2011-05-15 13:06:46 +03:00
|
|
|
|
2012-06-17 04:00:31 +03:00
|
|
|
#include "irrlichttypes.h"
|
2024-04-20 18:52:52 +02:00
|
|
|
#include <Keycodes.h>
|
2012-06-17 04:00:31 +03:00
|
|
|
#include <IEventReceiver.h>
|
2011-06-01 19:44:07 +02:00
|
|
|
#include <string>
|
2025-03-16 20:35:34 +01:00
|
|
|
#include <variant>
|
2011-05-14 15:43:26 +03:00
|
|
|
|
2025-03-21 12:07:51 +01:00
|
|
|
/* A key press, consisting of a scancode or a keycode.
|
|
|
|
* This fits into 64 bits, so prefer passing this by value.
|
|
|
|
*/
|
2011-08-13 22:44:31 +02:00
|
|
|
class KeyPress
|
|
|
|
{
|
|
|
|
public:
|
2017-08-18 08:21:01 +02:00
|
|
|
KeyPress() = default;
|
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
KeyPress(const std::string &name);
|
|
|
|
|
|
|
|
KeyPress(const irr::SEvent::SKeyInput &in);
|
|
|
|
|
|
|
|
// Get a string representation that is suitable for use in minetest.conf
|
|
|
|
std::string sym() const;
|
|
|
|
|
|
|
|
// Get a human-readable string representation
|
|
|
|
std::string name() const;
|
2011-08-13 22:44:31 +02:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
// Get the corresponding keycode or KEY_UNKNOWN if one is not available
|
|
|
|
irr::EKEY_CODE getKeycode() const;
|
2011-08-13 22:44:31 +02:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
// Get the corresponding keychar or '\0' if one is not available
|
|
|
|
wchar_t getKeychar() const;
|
|
|
|
|
|
|
|
// Get the scancode or 0 is one is not available
|
|
|
|
u32 getScancode() const
|
2011-08-13 22:44:31 +02:00
|
|
|
{
|
2025-03-16 20:35:34 +01:00
|
|
|
if (auto pv = std::get_if<u32>(&scancode))
|
|
|
|
return *pv;
|
|
|
|
return 0;
|
2011-08-13 22:44:31 +02:00
|
|
|
}
|
|
|
|
|
2025-03-21 12:07:51 +01:00
|
|
|
bool operator==(KeyPress o) const {
|
2025-03-16 20:35:34 +01:00
|
|
|
return scancode == o.scancode;
|
|
|
|
}
|
2025-03-21 12:07:51 +01:00
|
|
|
bool operator!=(KeyPress o) const {
|
2025-03-16 20:35:34 +01:00
|
|
|
return !(*this == o);
|
|
|
|
}
|
2017-04-23 09:52:40 +02:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
// Check whether the keypress is valid
|
|
|
|
operator bool() const
|
2011-08-13 22:44:31 +02:00
|
|
|
{
|
2025-03-16 20:35:34 +01:00
|
|
|
return std::holds_alternative<irr::EKEY_CODE>(scancode) ?
|
|
|
|
Keycode::isValid(std::get<irr::EKEY_CODE>(scancode)) :
|
|
|
|
std::get<u32>(scancode) != 0;
|
2011-08-13 22:44:31 +02:00
|
|
|
}
|
|
|
|
|
2025-03-21 12:07:51 +01:00
|
|
|
static KeyPress getSpecialKey(const std::string &name);
|
2011-08-13 22:44:31 +02:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
private:
|
|
|
|
bool loadFromScancode(const std::string &name);
|
|
|
|
void loadFromKey(irr::EKEY_CODE keycode, wchar_t keychar);
|
|
|
|
std::string formatScancode() const;
|
2024-04-20 18:52:52 +02:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
std::variant<u32, irr::EKEY_CODE> scancode = irr::KEY_UNKNOWN;
|
|
|
|
};
|
2011-05-14 15:43:26 +03:00
|
|
|
|
2025-03-16 20:35:34 +01:00
|
|
|
// Global defines for convenience
|
|
|
|
// This implementation defers creation of the objects to make sure that the
|
|
|
|
// IrrlichtDevice is initialized.
|
|
|
|
#define EscapeKey KeyPress::getSpecialKey("KEY_ESCAPE")
|
|
|
|
#define LMBKey KeyPress::getSpecialKey("KEY_LBUTTON")
|
|
|
|
#define MMBKey KeyPress::getSpecialKey("KEY_MBUTTON") // Middle Mouse Button
|
|
|
|
#define RMBKey KeyPress::getSpecialKey("KEY_RBUTTON")
|
2024-04-20 18:52:52 +02:00
|
|
|
|
2011-05-14 15:43:26 +03:00
|
|
|
// Key configuration getter
|
2025-03-21 12:07:51 +01:00
|
|
|
KeyPress getKeySetting(const std::string &settingname);
|
2011-07-22 21:53:50 +02:00
|
|
|
|
|
|
|
// Clear fast lookup cache
|
|
|
|
void clearKeyCache();
|