summaryrefslogtreecommitdiff
path: root/editor/action_map_editor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'editor/action_map_editor.cpp')
-rw-r--r--editor/action_map_editor.cpp348
1 files changed, 235 insertions, 113 deletions
diff --git a/editor/action_map_editor.cpp b/editor/action_map_editor.cpp
index 7aa63f899b..462f314471 100644
--- a/editor/action_map_editor.cpp
+++ b/editor/action_map_editor.cpp
@@ -5,8 +5,8 @@
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
-/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
-/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
+/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
@@ -29,15 +29,16 @@
/*************************************************************************/
#include "action_map_editor.h"
+
#include "core/input/input_map.h"
#include "core/os/keyboard.h"
#include "editor/editor_scale.h"
-#include "scene/gui/center_container.h"
+#include "scene/gui/separator.h"
/////////////////////////////////////////
-// Maps to 2*axis if value is neg, or + 1 if value is pos.
-static const char *_joy_axis_descriptions[JOY_AXIS_MAX * 2] = {
+// Maps to 2*axis if value is neg, or 2*axis+1 if value is pos.
+static const char *_joy_axis_descriptions[(size_t)JoyAxis::MAX * 2] = {
TTRC("Left Stick Left, Joystick 0 Left"),
TTRC("Left Stick Right, Joystick 0 Right"),
TTRC("Left Stick Up, Joystick 0 Up"),
@@ -60,29 +61,37 @@ static const char *_joy_axis_descriptions[JOY_AXIS_MAX * 2] = {
TTRC("Joystick 4 Down"),
};
-String InputEventConfigurationDialog::get_event_text(const Ref<InputEvent> &p_event) {
+String InputEventConfigurationDialog::get_event_text(const Ref<InputEvent> &p_event, bool p_include_device) const {
ERR_FAIL_COND_V_MSG(p_event.is_null(), String(), "Provided event is not a valid instance of InputEvent");
- // Joypad motion events will display slightly differently than what the event->as_text() provides. See #43660.
- Ref<InputEventJoypadMotion> jpmotion = p_event;
- if (jpmotion.is_valid()) {
+ String text = p_event->as_text();
+
+ Ref<InputEventMouse> mouse = p_event;
+ Ref<InputEventJoypadMotion> jp_motion = p_event;
+ Ref<InputEventJoypadButton> jp_button = p_event;
+ if (jp_motion.is_valid()) {
+ // Joypad motion events will display slightly differently than what the event->as_text() provides. See #43660.
String desc = TTR("Unknown Joypad Axis");
- if (jpmotion->get_axis() < JOY_AXIS_MAX) {
- desc = RTR(_joy_axis_descriptions[2 * jpmotion->get_axis() + (jpmotion->get_axis_value() < 0 ? 0 : 1)]);
+ if (jp_motion->get_axis() < JoyAxis::MAX) {
+ desc = RTR(_joy_axis_descriptions[2 * (size_t)jp_motion->get_axis() + (jp_motion->get_axis_value() < 0 ? 0 : 1)]);
}
- return vformat("Joypad Axis %s %s (%s)", itos(jpmotion->get_axis()), jpmotion->get_axis_value() < 0 ? "-" : "+", desc);
- } else {
- return p_event->as_text();
+ text = vformat("Joypad Axis %s %s (%s)", itos((int64_t)jp_motion->get_axis()), jp_motion->get_axis_value() < 0 ? "-" : "+", desc);
}
+ if (p_include_device && (mouse.is_valid() || jp_button.is_valid() || jp_motion.is_valid())) {
+ String device_string = _get_device_string(p_event->get_device());
+ text += vformat(" - %s", device_string);
+ }
+
+ return text;
}
-void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event) {
+void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event, bool p_update_input_list_selection) {
if (p_event.is_valid()) {
event = p_event;
// Update Label
- event_as_text->set_text(get_event_text(event));
+ event_as_text->set_text(get_event_text(event, true));
Ref<InputEventKey> k = p_event;
Ref<InputEventMouseButton> mb = p_event;
@@ -108,7 +117,7 @@ void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event) {
if (k.is_valid()) {
show_phys_key = true;
- physical_key_checkbox->set_pressed(k->get_physical_keycode() != 0 && k->get_keycode() == 0);
+ physical_key_checkbox->set_pressed(k->get_physical_keycode() != Key::NONE && k->get_keycode() == Key::NONE);
} else if (joyb.is_valid() || joym.is_valid() || mb.is_valid()) {
show_device = true;
@@ -120,33 +129,35 @@ void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event) {
physical_key_checkbox->set_visible(show_phys_key);
additional_options_container->show();
- // Update selected item in input list for keys, joybuttons and joyaxis only (since the mouse cannot be "listened" for).
- if (k.is_valid() || joyb.is_valid() || joym.is_valid()) {
+ // Update selected item in input list.
+ if (p_update_input_list_selection && (k.is_valid() || joyb.is_valid() || joym.is_valid() || mb.is_valid())) {
TreeItem *category = input_list_tree->get_root()->get_first_child();
while (category) {
TreeItem *input_item = category->get_first_child();
- // has_type this should be always true, unless the tree structure has been misconfigured.
- bool has_type = input_item->get_parent()->has_meta("__type");
- int input_type = input_item->get_parent()->get_meta("__type");
- if (!has_type) {
- return;
- }
+ if (input_item != nullptr) {
+ // input_type should always be > 0, unless the tree structure has been misconfigured.
+ int input_type = input_item->get_parent()->get_meta("__type", 0);
+ if (input_type == 0) {
+ return;
+ }
- // If event type matches input types of this category.
- if ((k.is_valid() && input_type == INPUT_KEY) || (joyb.is_valid() && input_type == INPUT_JOY_BUTTON) || (joym.is_valid() && input_type == INPUT_JOY_MOTION)) {
- // Loop through all items of this category until one matches.
- while (input_item) {
- bool key_match = k.is_valid() && (Variant(k->get_keycode()) == input_item->get_meta("__keycode") || Variant(k->get_physical_keycode()) == input_item->get_meta("__keycode"));
- bool joyb_match = joyb.is_valid() && Variant(joyb->get_button_index()) == input_item->get_meta("__index");
- bool joym_match = joym.is_valid() && Variant(joym->get_axis()) == input_item->get_meta("__axis") && joym->get_axis_value() == (float)input_item->get_meta("__value");
- if (key_match || joyb_match || joym_match) {
- category->set_collapsed(false);
- input_item->select(0);
- input_list_tree->ensure_cursor_is_visible();
- return;
+ // If event type matches input types of this category.
+ if ((k.is_valid() && input_type == INPUT_KEY) || (joyb.is_valid() && input_type == INPUT_JOY_BUTTON) || (joym.is_valid() && input_type == INPUT_JOY_MOTION) || (mb.is_valid() && input_type == INPUT_MOUSE_BUTTON)) {
+ // Loop through all items of this category until one matches.
+ while (input_item) {
+ bool key_match = k.is_valid() && (Variant(k->get_keycode()) == input_item->get_meta("__keycode") || Variant(k->get_physical_keycode()) == input_item->get_meta("__keycode"));
+ bool joyb_match = joyb.is_valid() && Variant(joyb->get_button_index()) == input_item->get_meta("__index");
+ bool joym_match = joym.is_valid() && Variant(joym->get_axis()) == input_item->get_meta("__axis") && joym->get_axis_value() == (float)input_item->get_meta("__value");
+ bool mb_match = mb.is_valid() && Variant(mb->get_button_index()) == input_item->get_meta("__index");
+ if (key_match || joyb_match || joym_match || mb_match) {
+ category->set_collapsed(false);
+ input_item->select(0);
+ input_list_tree->ensure_cursor_is_visible();
+ return;
+ }
+ input_item = input_item->get_next();
}
- input_item = input_item->get_next();
}
}
@@ -165,7 +176,6 @@ void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event) {
if (allowed_input_types & INPUT_KEY) {
strings.append(TTR("Key"));
}
- // We don't check for INPUT_MOUSE_BUTTON since it is ignored in the "Listen Window Input" method.
if (allowed_input_types & INPUT_JOY_BUTTON) {
strings.append(TTR("Joypad Button"));
@@ -173,7 +183,9 @@ void InputEventConfigurationDialog::_set_event(const Ref<InputEvent> &p_event) {
if (allowed_input_types & INPUT_JOY_MOTION) {
strings.append(TTR("Joypad Axis"));
}
-
+ if (allowed_input_types & INPUT_MOUSE_BUTTON) {
+ strings.append(TTR("Mouse Button in area below"));
+ }
if (strings.size() == 0) {
text = TTR("Input Event dialog has been misconfigured: No input types are allowed.");
event_as_text->set_text(text);
@@ -214,20 +226,39 @@ void InputEventConfigurationDialog::_listen_window_input(const Ref<InputEvent> &
return;
}
- // Ignore mouse
- Ref<InputEventMouse> m = p_event;
- if (m.is_valid()) {
+ // Ignore mouse motion
+ Ref<InputEventMouseMotion> mm = p_event;
+ if (mm.is_valid()) {
return;
}
+ // Ignore mouse button if not in the detection rect
+ Ref<InputEventMouseButton> mb = p_event;
+ if (mb.is_valid()) {
+ Rect2 r = mouse_detection_rect->get_rect();
+ if (!r.has_point(mouse_detection_rect->get_local_mouse_position() + r.get_position())) {
+ return;
+ }
+ }
+
+ // Create an editable reference
+ Ref<InputEvent> received_event = p_event;
+
// Check what the type is and if it is allowed.
- Ref<InputEventKey> k = p_event;
- Ref<InputEventJoypadButton> joyb = p_event;
- Ref<InputEventJoypadMotion> joym = p_event;
+ Ref<InputEventKey> k = received_event;
+ Ref<InputEventJoypadButton> joyb = received_event;
+ Ref<InputEventJoypadMotion> joym = received_event;
- int type = k.is_valid() ? INPUT_KEY : joyb.is_valid() ? INPUT_JOY_BUTTON :
- joym.is_valid() ? INPUT_JOY_MOTION :
- 0;
+ int type = 0;
+ if (k.is_valid()) {
+ type = INPUT_KEY;
+ } else if (joyb.is_valid()) {
+ type = INPUT_JOY_BUTTON;
+ } else if (joym.is_valid()) {
+ type = INPUT_JOY_MOTION;
+ } else if (mb.is_valid()) {
+ type = INPUT_MOUSE_BUTTON;
+ }
if (!(allowed_input_types & type)) {
return;
@@ -240,31 +271,31 @@ void InputEventConfigurationDialog::_listen_window_input(const Ref<InputEvent> &
return;
} else {
// Always make the value 1 or -1 for display consistency
- joym->set_axis_value(SGN(axis_value));
+ joym->set_axis_value(SIGN(axis_value));
}
}
if (k.is_valid()) {
- k->set_pressed(false); // to avoid serialisation of 'pressed' property - doesn't matter for actions anyway.
+ k->set_pressed(false); // To avoid serialisation of 'pressed' property - doesn't matter for actions anyway.
// Maintain physical keycode option state
if (physical_key_checkbox->is_pressed()) {
- k->set_physical_keycode(k->get_keycode());
- k->set_keycode(KEY_NONE);
+ k->set_keycode(Key::NONE);
} else {
- k->set_keycode((Key)k->get_physical_keycode());
- k->set_physical_keycode(KEY_NONE);
+ k->set_physical_keycode(Key::NONE);
}
}
- Ref<InputEventWithModifiers> mod = p_event;
+ Ref<InputEventWithModifiers> mod = received_event;
if (mod.is_valid()) {
// Maintain store command option state
mod->set_store_command(store_command_checkbox->is_pressed());
-
mod->set_window_id(0);
}
- _set_event(p_event);
+ // Maintain device selection.
+ received_event->set_device(_get_current_device());
+
+ _set_event(received_event);
set_input_as_handled();
}
@@ -307,12 +338,12 @@ void InputEventConfigurationDialog::_update_input_list() {
mouse_root->set_collapsed(collapse);
mouse_root->set_meta("__type", INPUT_MOUSE_BUTTON);
- MouseButton mouse_buttons[9] = { MOUSE_BUTTON_LEFT, MOUSE_BUTTON_RIGHT, MOUSE_BUTTON_MIDDLE, MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN, MOUSE_BUTTON_WHEEL_LEFT, MOUSE_BUTTON_WHEEL_RIGHT, MOUSE_BUTTON_XBUTTON1, MOUSE_BUTTON_XBUTTON2 };
+ MouseButton mouse_buttons[9] = { MouseButton::LEFT, MouseButton::RIGHT, MouseButton::MIDDLE, MouseButton::WHEEL_UP, MouseButton::WHEEL_DOWN, MouseButton::WHEEL_LEFT, MouseButton::WHEEL_RIGHT, MouseButton::MB_XBUTTON1, MouseButton::MB_XBUTTON2 };
for (int i = 0; i < 9; i++) {
Ref<InputEventMouseButton> mb;
mb.instantiate();
mb->set_button_index(mouse_buttons[i]);
- String desc = get_event_text(mb);
+ String desc = get_event_text(mb, false);
if (!search_term.is_empty() && desc.findn(search_term) == -1) {
continue;
@@ -331,11 +362,11 @@ void InputEventConfigurationDialog::_update_input_list() {
joyb_root->set_collapsed(collapse);
joyb_root->set_meta("__type", INPUT_JOY_BUTTON);
- for (int i = 0; i < JOY_BUTTON_MAX; i++) {
+ for (int i = 0; i < (int)JoyButton::MAX; i++) {
Ref<InputEventJoypadButton> joyb;
joyb.instantiate();
joyb->set_button_index((JoyButton)i);
- String desc = get_event_text(joyb);
+ String desc = get_event_text(joyb, false);
if (!search_term.is_empty() && desc.findn(search_term) == -1) {
continue;
@@ -354,14 +385,14 @@ void InputEventConfigurationDialog::_update_input_list() {
joya_root->set_collapsed(collapse);
joya_root->set_meta("__type", INPUT_JOY_MOTION);
- for (int i = 0; i < JOY_AXIS_MAX * 2; i++) {
+ for (int i = 0; i < (int)JoyAxis::MAX * 2; i++) {
int axis = i / 2;
int direction = (i & 1) ? 1 : -1;
Ref<InputEventJoypadMotion> joym;
joym.instantiate();
joym->set_axis((JoyAxis)axis);
joym->set_axis_value(direction);
- String desc = get_event_text(joym);
+ String desc = get_event_text(joym, false);
if (!search_term.is_empty() && desc.findn(search_term) == -1) {
continue;
@@ -435,10 +466,10 @@ void InputEventConfigurationDialog::_physical_keycode_toggled(bool p_checked) {
if (p_checked) {
k->set_physical_keycode(k->get_keycode());
- k->set_keycode(KEY_NONE);
+ k->set_keycode(Key::NONE);
} else {
k->set_keycode((Key)k->get_physical_keycode());
- k->set_physical_keycode(KEY_NONE);
+ k->set_physical_keycode(Key::NONE);
}
_set_event(k);
@@ -462,9 +493,9 @@ void InputEventConfigurationDialog::_input_list_item_selected() {
if (physical_key_checkbox->is_pressed()) {
k->set_physical_keycode(keycode);
- k->set_keycode(KEY_NONE);
+ k->set_keycode(Key::NONE);
} else {
- k->set_physical_keycode(KEY_NONE);
+ k->set_physical_keycode(Key::NONE);
k->set_keycode(keycode);
}
@@ -476,7 +507,7 @@ void InputEventConfigurationDialog::_input_list_item_selected() {
k->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
k->set_store_command(store_command_checkbox->is_pressed());
- _set_event(k);
+ _set_event(k, false);
} break;
case InputEventConfigurationDialog::INPUT_MOUSE_BUTTON: {
MouseButton idx = (MouseButton)(int)selected->get_meta("__index");
@@ -491,12 +522,19 @@ void InputEventConfigurationDialog::_input_list_item_selected() {
mb->set_meta_pressed(mod_checkboxes[MOD_META]->is_pressed());
mb->set_store_command(store_command_checkbox->is_pressed());
- _set_event(mb);
+ // Maintain selected device
+ mb->set_device(_get_current_device());
+
+ _set_event(mb, false);
} break;
case InputEventConfigurationDialog::INPUT_JOY_BUTTON: {
JoyButton idx = (JoyButton)(int)selected->get_meta("__index");
Ref<InputEventJoypadButton> jb = InputEventJoypadButton::create_reference(idx);
- _set_event(jb);
+
+ // Maintain selected device
+ jb->set_device(_get_current_device());
+
+ _set_event(jb, false);
} break;
case InputEventConfigurationDialog::INPUT_JOY_MOTION: {
JoyAxis axis = (JoyAxis)(int)selected->get_meta("__axis");
@@ -506,24 +544,35 @@ void InputEventConfigurationDialog::_input_list_item_selected() {
jm.instantiate();
jm->set_axis(axis);
jm->set_axis_value(value);
- _set_event(jm);
+
+ // Maintain selected device
+ jm->set_device(_get_current_device());
+
+ _set_event(jm, false);
} break;
}
}
-void InputEventConfigurationDialog::_set_current_device(int i_device) {
- device_id_option->select(i_device + 1);
+void InputEventConfigurationDialog::_device_selection_changed(int p_option_button_index) {
+ // Subtract 1 as option index 0 corresponds to "All Devices" (value of -1)
+ // and option index 1 corresponds to device 0, etc...
+ event->set_device(p_option_button_index - 1);
+ event_as_text->set_text(get_event_text(event, true));
+}
+
+void InputEventConfigurationDialog::_set_current_device(int p_device) {
+ device_id_option->select(p_device + 1);
}
int InputEventConfigurationDialog::_get_current_device() const {
return device_id_option->get_selected() - 1;
}
-String InputEventConfigurationDialog::_get_device_string(int i_device) const {
- if (i_device == InputMap::ALL_DEVICES) {
+String InputEventConfigurationDialog::_get_device_string(int p_device) const {
+ if (p_device == InputMap::ALL_DEVICES) {
return TTR("All Devices");
}
- return TTR("Device") + " " + itos(i_device);
+ return TTR("Device") + " " + itos(p_device);
}
void InputEventConfigurationDialog::_notification(int p_what) {
@@ -541,8 +590,6 @@ void InputEventConfigurationDialog::_notification(int p_what) {
_update_input_list();
} break;
- default:
- break;
}
}
@@ -557,15 +604,24 @@ void InputEventConfigurationDialog::popup_and_configure(const Ref<InputEvent> &p
for (int i = 0; i < MOD_MAX; i++) {
mod_checkboxes[i]->set_pressed(false);
}
- physical_key_checkbox->set_pressed(false);
+
+ // Enable the Physical Key checkbox by default to encourage its use.
+ // Physical Key should be used for most game inputs as it allows keys to work
+ // on non-QWERTY layouts out of the box.
+ // This is especially important for WASD movement layouts.
+ physical_key_checkbox->set_pressed(true);
+
store_command_checkbox->set_pressed(true);
_set_current_device(0);
// Switch to "Listen" tab
tab_container->set_current_tab(0);
+
+ // Select "All Devices" by default.
+ device_id_option->select(0);
}
- popup_centered();
+ popup_centered(Size2(0, 400) * EDSCALE);
}
Ref<InputEvent> InputEventConfigurationDialog::get_event() const {
@@ -577,7 +633,7 @@ void InputEventConfigurationDialog::set_allowed_input_types(int p_type_masks) {
}
InputEventConfigurationDialog::InputEventConfigurationDialog() {
- allowed_input_types = INPUT_KEY | INPUT_MOUSE_BUTTON | INPUT_JOY_BUTTON | INPUT_JOY_MOTION;
+ allowed_input_types = INPUT_KEY | INPUT_MOUSE_BUTTON | INPUT_JOY_BUTTON | INPUT_JOY_MOTION | INPUT_MOUSE_BUTTON;
set_title(TTR("Event Configuration"));
set_min_size(Size2i(550 * EDSCALE, 0)); // Min width
@@ -586,18 +642,23 @@ InputEventConfigurationDialog::InputEventConfigurationDialog() {
add_child(main_vbox);
tab_container = memnew(TabContainer);
- tab_container->set_tab_align(TabContainer::TabAlign::ALIGN_LEFT);
tab_container->set_use_hidden_tabs_for_min_size(true);
tab_container->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+ tab_container->set_theme_type_variation("TabContainerOdd");
tab_container->connect("tab_selected", callable_mp(this, &InputEventConfigurationDialog::_tab_selected));
main_vbox->add_child(tab_container);
- CenterContainer *cc = memnew(CenterContainer);
- cc->set_name(TTR("Listen for Input"));
+ // Listen to input tab
+ VBoxContainer *vb = memnew(VBoxContainer);
+ vb->set_name(TTR("Listen for Input"));
event_as_text = memnew(Label);
- event_as_text->set_align(Label::ALIGN_CENTER);
- cc->add_child(event_as_text);
- tab_container->add_child(cc);
+ event_as_text->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
+ vb->add_child(event_as_text);
+ // Mouse button detection rect (Mouse button event outside this rect will be ignored)
+ mouse_detection_rect = memnew(Panel);
+ mouse_detection_rect->set_v_size_flags(Control::SIZE_EXPAND_FILL);
+ vb->add_child(mouse_detection_rect);
+ tab_container->add_child(vb);
// List of all input options to manually select from.
@@ -644,12 +705,13 @@ InputEventConfigurationDialog::InputEventConfigurationDialog() {
device_id_option = memnew(OptionButton);
device_id_option->set_h_size_flags(Control::SIZE_EXPAND_FILL);
- device_container->add_child(device_id_option);
-
for (int i = -1; i < 8; i++) {
device_id_option->add_item(_get_device_string(i));
}
- _set_current_device(0);
+ device_id_option->connect("item_selected", callable_mp(this, &InputEventConfigurationDialog::_device_selection_changed));
+ _set_current_device(InputMap::ALL_DEVICES);
+ device_container->add_child(device_id_option);
+
device_container->hide();
additional_options_container->add_child(device_container);
@@ -658,7 +720,7 @@ InputEventConfigurationDialog::InputEventConfigurationDialog() {
for (int i = 0; i < MOD_MAX; i++) {
String name = mods[i];
mod_checkboxes[i] = memnew(CheckBox);
- mod_checkboxes[i]->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_mod_toggled), varray(i));
+ mod_checkboxes[i]->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_mod_toggled).bind(i));
mod_checkboxes[i]->set_text(name);
mod_container->add_child(mod_checkboxes[i]);
}
@@ -683,7 +745,7 @@ InputEventConfigurationDialog::InputEventConfigurationDialog() {
physical_key_checkbox = memnew(CheckBox);
physical_key_checkbox->set_text(TTR("Use Physical Keycode"));
- physical_key_checkbox->set_tooltip(TTR("Stores the physical position of the key on the keyboard rather than the keys value. Used for compatibility with non-latin layouts."));
+ physical_key_checkbox->set_tooltip(TTR("Stores the physical position of the key on the keyboard rather than the key's value. Used for compatibility with non-latin layouts.\nThis should generally be enabled for most game shortcuts, but not in non-game applications."));
physical_key_checkbox->connect("toggled", callable_mp(this, &InputEventConfigurationDialog::_physical_keycode_toggled));
physical_key_checkbox->hide();
additional_options_container->add_child(physical_key_checkbox);
@@ -711,7 +773,7 @@ void ActionMapEditor::_event_config_confirmed() {
Ref<InputEvent> ev = event_config_dialog->get_event();
Dictionary new_action = current_action.duplicate();
- Array events = new_action["events"];
+ Array events = new_action["events"].duplicate();
if (current_action_event_index == -1) {
// Add new event
@@ -729,9 +791,37 @@ void ActionMapEditor::_add_action_pressed() {
_add_action(add_edit->get_text());
}
+String ActionMapEditor::_check_new_action_name(const String &p_name) {
+ if (p_name.is_empty() || !_is_action_name_valid(p_name)) {
+ return TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'");
+ }
+
+ if (_has_action(p_name)) {
+ return vformat(TTR("An action with the name '%s' already exists."), p_name);
+ }
+
+ return "";
+}
+
+void ActionMapEditor::_add_edit_text_changed(const String &p_name) {
+ String error = _check_new_action_name(p_name);
+ add_button->set_tooltip(error);
+ add_button->set_disabled(!error.is_empty());
+}
+
+bool ActionMapEditor::_has_action(const String &p_name) const {
+ for (const ActionInfo &action_info : actions_cache) {
+ if (p_name == action_info.name) {
+ return true;
+ }
+ }
+ return false;
+}
+
void ActionMapEditor::_add_action(const String &p_name) {
- if (p_name == "" || !_is_action_name_valid(p_name)) {
- show_message(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'"));
+ String error = _check_new_action_name(p_name);
+ if (!error.is_empty()) {
+ show_message(error);
return;
}
@@ -754,12 +844,18 @@ void ActionMapEditor::_action_edited() {
return;
}
- if (new_name == "" || !_is_action_name_valid(new_name)) {
+ if (new_name.is_empty() || !_is_action_name_valid(new_name)) {
ti->set_text(0, old_name);
show_message(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'"));
return;
}
+ if (_has_action(new_name)) {
+ ti->set_text(0, old_name);
+ show_message(vformat(TTR("An action with the name '%s' already exists."), new_name));
+ return;
+ }
+
emit_signal(SNAME("action_renamed"), old_name, new_name);
} else if (action_tree->get_selected_column() == 1) {
// Deadzone Edited
@@ -773,7 +869,11 @@ void ActionMapEditor::_action_edited() {
}
}
-void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id) {
+void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
+ if (p_button != MouseButton::LEFT) {
+ return;
+ }
+
ItemButton option = (ItemButton)p_id;
TreeItem *item = Object::cast_to<TreeItem>(p_item);
@@ -788,7 +888,6 @@ void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_i
current_action_event_index = -1;
event_config_dialog->popup_and_configure();
-
} break;
case ActionMapEditor::BUTTON_EDIT_EVENT: {
// Action and Action name is located on the parent of the event.
@@ -801,7 +900,6 @@ void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_i
if (ie.is_valid()) {
event_config_dialog->popup_and_configure(ie);
}
-
} break;
case ActionMapEditor::BUTTON_REMOVE_ACTION: {
// Send removed action name
@@ -810,13 +908,13 @@ void ActionMapEditor::_tree_button_pressed(Object *p_item, int p_column, int p_i
} break;
case ActionMapEditor::BUTTON_REMOVE_EVENT: {
// Remove event and send updated action
- Dictionary action = item->get_parent()->get_meta("__action");
+ Dictionary action = item->get_parent()->get_meta("__action").duplicate();
String action_name = item->get_parent()->get_meta("__name");
int event_index = item->get_meta("__index");
- Array events = action["events"];
- events.remove(event_index);
+ Array events = action["events"].duplicate();
+ events.remove_at(event_index);
action["events"] = events;
emit_signal(SNAME("action_edited"), action_name, action);
@@ -833,7 +931,7 @@ void ActionMapEditor::_tree_item_activated() {
return;
}
- _tree_button_pressed(item, 2, BUTTON_EDIT_EVENT);
+ _tree_button_pressed(item, 2, BUTTON_EDIT_EVENT, MouseButton::LEFT);
}
void ActionMapEditor::set_show_builtin_actions(bool p_show) {
@@ -964,8 +1062,6 @@ void ActionMapEditor::_notification(int p_what) {
case NOTIFICATION_THEME_CHANGED: {
action_list_search->set_right_icon(get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
} break;
- default:
- break;
}
}
@@ -1051,10 +1147,35 @@ void ActionMapEditor::update_action_list(const Vector<ActionInfo> &p_action_info
TreeItem *event_item = action_tree->create_item(action_item);
// First Column - Text
- event_item->set_text(0, event_config_dialog->get_event_text(event)); // Need to us the special description for JoypadMotion here, so don't use as_text() directly.
+ event_item->set_text(0, event_config_dialog->get_event_text(event, true));
event_item->set_meta("__event", event);
event_item->set_meta("__index", evnt_idx);
+ // First Column - Icon
+ Ref<InputEventKey> k = event;
+ if (k.is_valid()) {
+ if (k->get_physical_keycode() == Key::NONE) {
+ event_item->set_icon(0, action_tree->get_theme_icon(SNAME("Keyboard"), SNAME("EditorIcons")));
+ } else {
+ event_item->set_icon(0, action_tree->get_theme_icon(SNAME("KeyboardPhysical"), SNAME("EditorIcons")));
+ }
+ }
+
+ Ref<InputEventMouseButton> mb = event;
+ if (mb.is_valid()) {
+ event_item->set_icon(0, action_tree->get_theme_icon(SNAME("Mouse"), SNAME("EditorIcons")));
+ }
+
+ Ref<InputEventJoypadButton> jb = event;
+ if (jb.is_valid()) {
+ event_item->set_icon(0, action_tree->get_theme_icon(SNAME("JoyButton"), SNAME("EditorIcons")));
+ }
+
+ Ref<InputEventJoypadMotion> jm = event;
+ if (jm.is_valid()) {
+ event_item->set_icon(0, action_tree->get_theme_icon(SNAME("JoyAxis"), SNAME("EditorIcons")));
+ }
+
// Third Column - Buttons
event_item->add_button(2, action_tree->get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), BUTTON_EDIT_EVENT, false, TTR("Edit Event"));
event_item->add_button(2, action_tree->get_theme_icon(SNAME("Remove"), SNAME("EditorIcons")), BUTTON_REMOVE_EVENT, false, TTR("Remove Event"));
@@ -1066,7 +1187,7 @@ void ActionMapEditor::update_action_list(const Vector<ActionInfo> &p_action_info
void ActionMapEditor::show_message(const String &p_message) {
message->set_text(p_message);
- message->popup_centered(Size2(300, 100) * EDSCALE);
+ message->popup_centered();
}
void ActionMapEditor::use_external_search_box(LineEdit *p_searchbox) {
@@ -1076,11 +1197,9 @@ void ActionMapEditor::use_external_search_box(LineEdit *p_searchbox) {
}
ActionMapEditor::ActionMapEditor() {
- show_builtin_actions = false;
-
// Main Vbox Container
VBoxContainer *main_vbox = memnew(VBoxContainer);
- main_vbox->set_anchors_and_offsets_preset(PRESET_WIDE);
+ main_vbox->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
add_child(main_vbox);
HBoxContainer *top_hbox = memnew(HBoxContainer);
@@ -1107,13 +1226,16 @@ ActionMapEditor::ActionMapEditor() {
add_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL);
add_edit->set_placeholder(TTR("Add New Action"));
add_edit->set_clear_button_enabled(true);
+ add_edit->connect("text_changed", callable_mp(this, &ActionMapEditor::_add_edit_text_changed));
add_edit->connect("text_submitted", callable_mp(this, &ActionMapEditor::_add_action));
add_hbox->add_child(add_edit);
- Button *add_button = memnew(Button);
+ add_button = memnew(Button);
add_button->set_text(TTR("Add"));
add_button->connect("pressed", callable_mp(this, &ActionMapEditor::_add_action_pressed));
add_hbox->add_child(add_button);
+ // Disable the button and set its tooltip.
+ _add_edit_text_changed(add_edit->get_text());
main_vbox->add_child(add_hbox);
@@ -1132,7 +1254,7 @@ ActionMapEditor::ActionMapEditor() {
action_tree->set_column_custom_minimum_width(2, 50 * EDSCALE);
action_tree->connect("item_edited", callable_mp(this, &ActionMapEditor::_action_edited));
action_tree->connect("item_activated", callable_mp(this, &ActionMapEditor::_tree_item_activated));
- action_tree->connect("button_pressed", callable_mp(this, &ActionMapEditor::_tree_button_pressed));
+ action_tree->connect("button_clicked", callable_mp(this, &ActionMapEditor::_tree_button_pressed));
main_vbox->add_child(action_tree);
action_tree->set_drag_forwarding(this);