Merge pull request #47378 from aaronfranke/use-input-enums

Use key enum instead of plain integers for input code
This commit is contained in:
Rémi Verschelde 2021-08-11 11:20:45 +02:00 committed by GitHub
commit c00303ff55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
43 changed files with 173 additions and 102 deletions

View file

@ -104,9 +104,6 @@ static Vector<_CoreConstant> _global_constants;
#endif
VARIANT_ENUM_CAST(Key);
VARIANT_ENUM_CAST(KeyModifierMask);
void register_global_constants() {
BIND_CORE_ENUM_CONSTANT(SIDE_LEFT);
BIND_CORE_ENUM_CONSTANT(SIDE_TOP);

View file

@ -221,7 +221,7 @@ Input::SpeedTrack::SpeedTrack() {
reset();
}
bool Input::is_key_pressed(int p_keycode) const {
bool Input::is_key_pressed(Key p_keycode) const {
_THREAD_SAFE_METHOD_
return keys_pressed.has(p_keycode);
}

View file

@ -33,6 +33,7 @@
#include "core/input/input_event.h"
#include "core/object/object.h"
#include "core/os/keyboard.h"
#include "core/os/thread_safe.h"
class Input : public Object {
@ -244,7 +245,7 @@ public:
static Input *get_singleton();
bool is_key_pressed(int p_keycode) const;
bool is_key_pressed(Key p_keycode) const;
bool is_mouse_button_pressed(MouseButton p_button) const;
bool is_joy_button_pressed(int p_device, JoyButton p_button) const;
bool is_action_pressed(const StringName &p_action, bool p_exact = false) const;

View file

@ -307,21 +307,21 @@ bool InputEventKey::is_pressed() const {
return pressed;
}
void InputEventKey::set_keycode(uint32_t p_keycode) {
void InputEventKey::set_keycode(Key p_keycode) {
keycode = p_keycode;
emit_changed();
}
uint32_t InputEventKey::get_keycode() const {
Key InputEventKey::get_keycode() const {
return keycode;
}
void InputEventKey::set_physical_keycode(uint32_t p_keycode) {
void InputEventKey::set_physical_keycode(Key p_keycode) {
physical_keycode = p_keycode;
emit_changed();
}
uint32_t InputEventKey::get_physical_keycode() const {
Key InputEventKey::get_physical_keycode() const {
return physical_keycode;
}
@ -387,7 +387,7 @@ String InputEventKey::to_string() {
return vformat("InputEventKey: keycode=%s, mods=%s, physical=%s, pressed=%s, echo=%s", kc, mods, physical, p, e);
}
Ref<InputEventKey> InputEventKey::create_reference(uint32_t p_keycode) {
Ref<InputEventKey> InputEventKey::create_reference(Key p_keycode) {
Ref<InputEventKey> ie;
ie.instantiate();
ie->set_keycode(p_keycode & KEY_CODE_MASK);

View file

@ -34,6 +34,7 @@
#include "core/input/input_enums.h"
#include "core/io/resource.h"
#include "core/math/transform_2d.h"
#include "core/os/keyboard.h"
#include "core/string/ustring.h"
#include "core/typedefs.h"
@ -163,8 +164,8 @@ class InputEventKey : public InputEventWithModifiers {
bool pressed = false; /// otherwise release
uint32_t keycode = 0; ///< check keyboard.h , KeyCode enum, without modifier masks
uint32_t physical_keycode = 0;
Key keycode = KEY_NONE; // Key enum, without modifier masks.
Key physical_keycode = KEY_NONE;
uint32_t unicode = 0; ///unicode
bool echo = false; /// true if this is an echo key
@ -176,11 +177,11 @@ public:
void set_pressed(bool p_pressed);
virtual bool is_pressed() const override;
void set_keycode(uint32_t p_keycode);
uint32_t get_keycode() const;
void set_keycode(Key p_keycode);
Key get_keycode() const;
void set_physical_keycode(uint32_t p_keycode);
uint32_t get_physical_keycode() const;
void set_physical_keycode(Key p_keycode);
Key get_physical_keycode() const;
void set_unicode(uint32_t p_unicode);
uint32_t get_unicode() const;
@ -199,7 +200,7 @@ public:
virtual String as_text() const override;
virtual String to_string() override;
static Ref<InputEventKey> create_reference(uint32_t p_keycode_with_modifier_masks);
static Ref<InputEventKey> create_reference(Key p_keycode_with_modifier_masks);
InputEventKey() {}
};

View file

@ -46,6 +46,7 @@ enum {
};
enum Key {
KEY_NONE = 0,
/* CURSOR/FUNCTION/BROWSER/MULTIMEDIA/MISC KEYS */
KEY_ESCAPE = SPKEY | 0x01,
KEY_TAB = SPKEY | 0x02,
@ -314,6 +315,52 @@ enum KeyModifierMask {
// bit 31 can't be used because variant uses regular 32 bits int as datatype
};
// To avoid having unnecessary operators, only define the ones that are needed.
inline Key operator-(uint32_t a, Key b) {
return (Key)(a - (uint32_t)b);
}
inline Key &operator-=(Key &a, int b) {
return (Key &)((int &)a -= b);
}
inline Key operator+(Key a, Key b) {
return (Key)((int)a - (int)b);
}
inline Key &operator|=(Key &a, Key b) {
return (Key &)((int &)a |= (int)b);
}
inline Key &operator|=(Key &a, KeyModifierMask b) {
return (Key &)((int &)a |= (int)b);
}
inline Key operator|(Key a, KeyModifierMask b) {
return (Key)((int)a | (int)b);
}
inline Key operator&(Key a, KeyModifierMask b) {
return (Key)((int)a & (int)b);
}
inline Key operator+(KeyModifierMask a, Key b) {
return (Key)((int)a + (int)b);
}
inline Key operator|(KeyModifierMask a, Key b) {
return (Key)((int)a | (int)b);
}
inline KeyModifierMask operator+(KeyModifierMask a, KeyModifierMask b) {
return (KeyModifierMask)((int)a + (int)b);
}
inline KeyModifierMask operator|(KeyModifierMask a, KeyModifierMask b) {
return (KeyModifierMask)((int)a | (int)b);
}
String keycode_get_string(uint32_t p_code);
bool keycode_has_unicode(uint32_t p_keycode);
int find_keycode(const String &p_code);

View file

@ -33,6 +33,7 @@
#include "core/input/input_enums.h"
#include "core/object/object.h"
#include "core/os/keyboard.h"
#include "core/templates/list.h"
#include "core/templates/simple_type.h"
#include "core/typedefs.h"
@ -96,6 +97,8 @@ VARIANT_ENUM_CAST(HatDir);
VARIANT_ENUM_CAST(HatMask);
VARIANT_ENUM_CAST(JoyAxis);
VARIANT_ENUM_CAST(JoyButton);
VARIANT_ENUM_CAST(Key);
VARIANT_ENUM_CAST(KeyModifierMask);
VARIANT_ENUM_CAST(MIDIMessage);
VARIANT_ENUM_CAST(MouseButton);
VARIANT_ENUM_CAST(Orientation);