From 3c0fdcc8acfadb124fbfa914532868948561c351 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Fri, 13 Aug 2021 16:31:57 -0500 Subject: Use "enum class" for input enums --- core/input/input_event.cpp | 156 ++++++++++++++++++++++----------------------- 1 file changed, 78 insertions(+), 78 deletions(-) (limited to 'core/input/input_event.cpp') diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index af3190bb17..ccde8838e1 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -203,19 +203,19 @@ void InputEventWithModifiers::set_modifiers_from_event(const InputEventWithModif set_meta_pressed(event->is_meta_pressed()); } -uint32_t InputEventWithModifiers::get_modifiers_mask() const { - uint32_t mask = 0; +Key InputEventWithModifiers::get_modifiers_mask() const { + Key mask = Key::NONE; if (is_ctrl_pressed()) { - mask |= KEY_MASK_CTRL; + mask |= KeyModifierMask::CTRL; } if (is_shift_pressed()) { - mask |= KEY_MASK_SHIFT; + mask |= KeyModifierMask::SHIFT; } if (is_alt_pressed()) { - mask |= KEY_MASK_ALT; + mask |= KeyModifierMask::ALT; } if (is_meta_pressed()) { - mask |= KEY_MASK_META; + mask |= KeyModifierMask::META; } return mask; } @@ -224,16 +224,16 @@ String InputEventWithModifiers::as_text() const { Vector mod_names; if (is_ctrl_pressed()) { - mod_names.push_back(find_keycode_name(KEY_CTRL)); + mod_names.push_back(find_keycode_name(Key::CTRL)); } if (is_shift_pressed()) { - mod_names.push_back(find_keycode_name(KEY_SHIFT)); + mod_names.push_back(find_keycode_name(Key::SHIFT)); } if (is_alt_pressed()) { - mod_names.push_back(find_keycode_name(KEY_ALT)); + mod_names.push_back(find_keycode_name(Key::ALT)); } if (is_meta_pressed()) { - mod_names.push_back(find_keycode_name(KEY_META)); + mod_names.push_back(find_keycode_name(Key::META)); } if (!mod_names.is_empty()) { @@ -325,12 +325,12 @@ Key InputEventKey::get_physical_keycode() const { return physical_keycode; } -void InputEventKey::set_unicode(uint32_t p_unicode) { +void InputEventKey::set_unicode(char32_t p_unicode) { unicode = p_unicode; emit_changed(); } -uint32_t InputEventKey::get_unicode() const { +char32_t InputEventKey::get_unicode() const { return unicode; } @@ -343,18 +343,18 @@ bool InputEventKey::is_echo() const { return echo; } -uint32_t InputEventKey::get_keycode_with_modifiers() const { +Key InputEventKey::get_keycode_with_modifiers() const { return keycode | get_modifiers_mask(); } -uint32_t InputEventKey::get_physical_keycode_with_modifiers() const { +Key InputEventKey::get_physical_keycode_with_modifiers() const { return physical_keycode | get_modifiers_mask(); } String InputEventKey::as_text() const { String kc; - if (keycode == 0) { + if (keycode == Key::NONE) { kc = keycode_get_string(physical_keycode) + " (" + RTR("Physical") + ")"; } else { kc = keycode_get_string(keycode); @@ -374,11 +374,11 @@ String InputEventKey::to_string() { String kc = ""; String physical = "false"; - if (keycode == 0) { - kc = itos(physical_keycode) + " (" + keycode_get_string(physical_keycode) + ")"; + if (keycode == Key::NONE) { + kc = itos((int64_t)physical_keycode) + " (" + keycode_get_string(physical_keycode) + ")"; physical = "true"; } else { - kc = itos(keycode) + " (" + keycode_get_string(keycode) + ")"; + kc = itos((int64_t)keycode) + " (" + keycode_get_string(keycode) + ")"; } String mods = InputEventWithModifiers::as_text(); @@ -390,22 +390,22 @@ String InputEventKey::to_string() { Ref InputEventKey::create_reference(Key p_keycode) { Ref ie; ie.instantiate(); - ie->set_keycode(p_keycode & KEY_CODE_MASK); - ie->set_unicode(p_keycode & KEY_CODE_MASK); + ie->set_keycode(p_keycode & KeyModifierMask::CODE_MASK); + ie->set_unicode(char32_t(p_keycode & KeyModifierMask::CODE_MASK)); - if (p_keycode & KEY_MASK_SHIFT) { + if ((p_keycode & KeyModifierMask::SHIFT) != Key::NONE) { ie->set_shift_pressed(true); } - if (p_keycode & KEY_MASK_ALT) { + if ((p_keycode & KeyModifierMask::ALT) != Key::NONE) { ie->set_alt_pressed(true); } - if (p_keycode & KEY_MASK_CTRL) { + if ((p_keycode & KeyModifierMask::CTRL) != Key::NONE) { ie->set_ctrl_pressed(true); } - if (p_keycode & KEY_MASK_CMD) { + if ((p_keycode & KeyModifierMask::CMD) != Key::NONE) { ie->set_command_pressed(true); } - if (p_keycode & KEY_MASK_META) { + if ((p_keycode & KeyModifierMask::META) != Key::NONE) { ie->set_meta_pressed(true); } @@ -419,14 +419,14 @@ bool InputEventKey::action_match(const Ref &p_event, bool *p_pressed } bool match = false; - if (get_keycode() == 0) { - uint32_t code = get_physical_keycode_with_modifiers(); - uint32_t event_code = key->get_physical_keycode_with_modifiers(); + if (get_keycode() == Key::NONE) { + Key code = get_physical_keycode_with_modifiers(); + Key event_code = key->get_physical_keycode_with_modifiers(); match = get_physical_keycode() == key->get_physical_keycode() && (!key->is_pressed() || (code & event_code) == code); } else { - uint32_t code = get_keycode_with_modifiers(); - uint32_t event_code = key->get_keycode_with_modifiers(); + Key code = get_keycode_with_modifiers(); + Key event_code = key->get_keycode_with_modifiers(); match = get_keycode() == key->get_keycode() && (!key->is_pressed() || (code & event_code) == code); } @@ -452,7 +452,7 @@ bool InputEventKey::is_match(const Ref &p_event, bool p_exact_match) return false; } - if (keycode == 0) { + if (keycode == Key::NONE) { return physical_keycode == key->physical_keycode && (!p_exact_match || get_modifiers_mask() == key->get_modifiers_mask()); } else { @@ -487,12 +487,12 @@ void InputEventKey::_bind_methods() { /////////////////////////////////// -void InputEventMouse::set_button_mask(int p_mask) { +void InputEventMouse::set_button_mask(MouseButton p_mask) { button_mask = p_mask; emit_changed(); } -int InputEventMouse::get_button_mask() const { +MouseButton InputEventMouse::get_button_mask() const { return button_mask; } @@ -637,21 +637,21 @@ String InputEventMouseButton::as_text() const { String full_string = mods_text == "" ? "" : mods_text + "+"; // Button - int idx = get_button_index(); + MouseButton idx = get_button_index(); switch (idx) { - case MOUSE_BUTTON_LEFT: - case MOUSE_BUTTON_RIGHT: - case MOUSE_BUTTON_MIDDLE: - case MOUSE_BUTTON_WHEEL_UP: - case MOUSE_BUTTON_WHEEL_DOWN: - case MOUSE_BUTTON_WHEEL_LEFT: - case MOUSE_BUTTON_WHEEL_RIGHT: - case MOUSE_BUTTON_XBUTTON1: - case MOUSE_BUTTON_XBUTTON2: - full_string += RTR(_mouse_button_descriptions[idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1 + case MouseButton::LEFT: + case MouseButton::RIGHT: + case MouseButton::MIDDLE: + case MouseButton::WHEEL_UP: + case MouseButton::WHEEL_DOWN: + case MouseButton::WHEEL_LEFT: + case MouseButton::WHEEL_RIGHT: + case MouseButton::MB_XBUTTON1: + case MouseButton::MB_XBUTTON2: + full_string += RTR(_mouse_button_descriptions[(size_t)idx - 1]); // button index starts from 1, array index starts from 0, so subtract 1 break; default: - full_string += RTR("Button") + " #" + itos(idx); + full_string += RTR("Button") + " #" + itos((int64_t)idx); break; } @@ -667,20 +667,20 @@ String InputEventMouseButton::to_string() { String p = is_pressed() ? "true" : "false"; String d = double_click ? "true" : "false"; - int idx = get_button_index(); - String button_string = itos(idx); + MouseButton idx = get_button_index(); + String button_string = itos((int64_t)idx); switch (idx) { - case MOUSE_BUTTON_LEFT: - case MOUSE_BUTTON_RIGHT: - case MOUSE_BUTTON_MIDDLE: - case MOUSE_BUTTON_WHEEL_UP: - case MOUSE_BUTTON_WHEEL_DOWN: - case MOUSE_BUTTON_WHEEL_LEFT: - case MOUSE_BUTTON_WHEEL_RIGHT: - case MOUSE_BUTTON_XBUTTON1: - case MOUSE_BUTTON_XBUTTON2: - button_string += " (" + RTR(_mouse_button_descriptions[idx - 1]) + ")"; // button index starts from 1, array index starts from 0, so subtract 1 + case MouseButton::LEFT: + case MouseButton::RIGHT: + case MouseButton::MIDDLE: + case MouseButton::WHEEL_UP: + case MouseButton::WHEEL_DOWN: + case MouseButton::WHEEL_LEFT: + case MouseButton::WHEEL_RIGHT: + case MouseButton::MB_XBUTTON1: + case MouseButton::MB_XBUTTON2: + button_string += " (" + RTR(_mouse_button_descriptions[(size_t)idx - 1]) + ")"; // button index starts from 1, array index starts from 0, so subtract 1 break; default: break; @@ -778,23 +778,23 @@ String InputEventMouseMotion::as_text() const { } String InputEventMouseMotion::to_string() { - int button_mask = get_button_mask(); - String button_mask_string = itos(button_mask); - switch (get_button_mask()) { - case MOUSE_BUTTON_MASK_LEFT: - button_mask_string += " (" + RTR(_mouse_button_descriptions[MOUSE_BUTTON_LEFT - 1]) + ")"; + MouseButton button_mask = get_button_mask(); + String button_mask_string = itos((int64_t)button_mask); + switch (button_mask) { + case MouseButton::MASK_LEFT: + button_mask_string += " (" + RTR(_mouse_button_descriptions[(size_t)MouseButton::LEFT - 1]) + ")"; break; - case MOUSE_BUTTON_MASK_MIDDLE: - button_mask_string += " (" + RTR(_mouse_button_descriptions[MOUSE_BUTTON_MIDDLE - 1]) + ")"; + case MouseButton::MASK_MIDDLE: + button_mask_string += " (" + RTR(_mouse_button_descriptions[(size_t)MouseButton::MIDDLE - 1]) + ")"; break; - case MOUSE_BUTTON_MASK_RIGHT: - button_mask_string += " (" + RTR(_mouse_button_descriptions[MOUSE_BUTTON_RIGHT - 1]) + ")"; + case MouseButton::MASK_RIGHT: + button_mask_string += " (" + RTR(_mouse_button_descriptions[(size_t)MouseButton::RIGHT - 1]) + ")"; break; - case MOUSE_BUTTON_MASK_XBUTTON1: - button_mask_string += " (" + RTR(_mouse_button_descriptions[MOUSE_BUTTON_XBUTTON1 - 1]) + ")"; + case MouseButton::MASK_XBUTTON1: + button_mask_string += " (" + RTR(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON1 - 1]) + ")"; break; - case MOUSE_BUTTON_MASK_XBUTTON2: - button_mask_string += " (" + RTR(_mouse_button_descriptions[MOUSE_BUTTON_XBUTTON2 - 1]) + ")"; + case MouseButton::MASK_XBUTTON2: + button_mask_string += " (" + RTR(_mouse_button_descriptions[(size_t)MouseButton::MB_XBUTTON2 - 1]) + ")"; break; default: break; @@ -869,7 +869,7 @@ void InputEventMouseMotion::_bind_methods() { /////////////////////////////////// void InputEventJoypadMotion::set_axis(JoyAxis p_axis) { - ERR_FAIL_INDEX(p_axis, JOY_AXIS_MAX); + ERR_FAIL_COND(p_axis < JoyAxis::LEFT_X || p_axis > JoyAxis::MAX); axis = p_axis; emit_changed(); @@ -938,7 +938,7 @@ bool InputEventJoypadMotion::is_match(const Ref &p_event, bool p_exa (!p_exact_match || ((axis_value < 0) == (jm->axis_value < 0))); } -static const char *_joy_axis_descriptions[JOY_AXIS_MAX] = { +static const char *_joy_axis_descriptions[(size_t)JoyAxis::MAX] = { TTRC("Left Stick X-Axis, Joystick 0 X-Axis"), TTRC("Left Stick Y-Axis, Joystick 0 Y-Axis"), TTRC("Right Stick X-Axis, Joystick 1 X-Axis"), @@ -952,7 +952,7 @@ static const char *_joy_axis_descriptions[JOY_AXIS_MAX] = { }; String InputEventJoypadMotion::as_text() const { - String desc = axis < JOY_AXIS_MAX ? RTR(_joy_axis_descriptions[axis]) : TTR("Unknown Joypad Axis"); + String desc = axis < JoyAxis::MAX ? RTR(_joy_axis_descriptions[(size_t)axis]) : TTR("Unknown Joypad Axis"); return vformat(TTR("Joypad Motion on Axis %d (%s) with Value %.2f"), axis, desc, axis_value); } @@ -1032,7 +1032,7 @@ bool InputEventJoypadButton::is_match(const Ref &p_event, bool p_exa return button_index == button->button_index; } -static const char *_joy_button_descriptions[JOY_BUTTON_SDL_MAX] = { +static const char *_joy_button_descriptions[(size_t)JoyButton::SDL_MAX] = { TTRC("Bottom Action, Sony Cross, Xbox A, Nintendo B"), TTRC("Right Action, Sony Circle, Xbox B, Nintendo A"), TTRC("Left Action, Sony Square, Xbox X, Nintendo Y"), @@ -1057,10 +1057,10 @@ static const char *_joy_button_descriptions[JOY_BUTTON_SDL_MAX] = { }; String InputEventJoypadButton::as_text() const { - String text = "Joypad Button " + itos(button_index); + String text = "Joypad Button " + itos((int64_t)button_index); - if (button_index >= 0 && button_index < JOY_BUTTON_SDL_MAX) { - text += vformat(" (%s)", _joy_button_descriptions[button_index]); + if (button_index > JoyButton::INVALID && button_index < JoyButton::SDL_MAX) { + text += vformat(" (%s)", _joy_button_descriptions[(size_t)button_index]); } if (pressure != 0) { @@ -1506,7 +1506,7 @@ int InputEventMIDI::get_controller_value() const { } String InputEventMIDI::as_text() const { - return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos(message)); + return vformat(RTR("MIDI Input on Channel=%s Message=%s"), itos(channel), itos((int64_t)message)); } String InputEventMIDI::to_string() { -- cgit v1.2.3