Merge pull request #44181 from EricEzaM/PR/INP5-new-input-editor

New Input Map Editor and Editor Settings Shortcut Editor
This commit is contained in:
Rémi Verschelde 2021-02-19 11:25:58 +01:00 committed by GitHub
commit 61e26d4431
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 1862 additions and 1263 deletions

1167
editor/action_map_editor.cpp Normal file

File diff suppressed because it is too large Load diff

203
editor/action_map_editor.h Normal file
View file

@ -0,0 +1,203 @@
/*************************************************************************/
/* action_map_editor.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 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 */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef ACTION_MAP_EDITOR_H
#define ACTION_MAP_EDITOR_H
#include "editor/editor_data.h"
// Confirmation Dialog used when configuring an input event.
// Separate from ActionMapEditor for code cleanliness and separation of responsibilities.
class InputEventConfigurationDialog : public ConfirmationDialog {
GDCLASS(InputEventConfigurationDialog, ConfirmationDialog);
public:
enum InputType {
INPUT_KEY = 1,
INPUT_MOUSE_BUTTON = 2,
INPUT_JOY_BUTTON = 4,
INPUT_JOY_MOTION = 8
};
private:
struct IconCache {
Ref<Texture2D> keyboard;
Ref<Texture2D> mouse;
Ref<Texture2D> joypad_button;
Ref<Texture2D> joypad_axis;
} icon_cache;
Ref<InputEvent> event = Ref<InputEvent>();
TabContainer *tab_container;
// Listening for input
Label *event_as_text;
// List of All Key/Mouse/Joypad input options.
int allowed_input_types;
Tree *input_list_tree;
LineEdit *input_list_search;
// Additional Options, shown depending on event selected
VBoxContainer *additional_options_container;
HBoxContainer *device_container;
OptionButton *device_id_option;
HBoxContainer *mod_container; // Contains the subcontainer and the store command checkbox.
enum ModCheckbox {
MOD_ALT,
MOD_SHIFT,
MOD_COMMAND,
MOD_CONTROL,
MOD_META,
MOD_MAX
};
String mods[MOD_MAX] = { "Alt", "Shift", "Command", "Control", "Meta" };
CheckBox *mod_checkboxes[MOD_MAX];
CheckBox *store_command_checkbox;
CheckBox *physical_key_checkbox;
void _set_event(const Ref<InputEvent> &p_event);
void _tab_selected(int p_tab);
void _listen_window_input(const Ref<InputEvent> &p_event);
void _search_term_updated(const String &p_term);
void _update_input_list();
void _input_list_item_selected();
void _mod_toggled(bool p_checked, int p_index);
void _store_command_toggled(bool p_checked);
void _physical_keycode_toggled(bool p_checked);
void _set_current_device(int i_device);
int _get_current_device() const;
String _get_device_string(int i_device) const;
protected:
void _notification(int p_what);
public:
// Pass an existing event to configure it. Alternatively, pass no event to start with a blank configuration.
void popup_and_configure(const Ref<InputEvent> &p_event = Ref<InputEvent>());
Ref<InputEvent> get_event() const;
String get_event_text(const Ref<InputEvent> &p_event);
void set_allowed_input_types(int p_type_masks);
InputEventConfigurationDialog();
};
class ActionMapEditor : public Control {
GDCLASS(ActionMapEditor, Control);
public:
struct ActionInfo {
String name = String();
Dictionary action = Dictionary();
Ref<Texture2D> icon = Ref<Texture2D>();
bool editable = true;
};
private:
enum ItemButton {
BUTTON_ADD_EVENT,
BUTTON_EDIT_EVENT,
BUTTON_REMOVE_ACTION,
BUTTON_REMOVE_EVENT,
};
Vector<ActionInfo> actions_cache;
Tree *action_tree;
// Storing which action/event is currently being edited in the InputEventConfigurationDialog.
Dictionary current_action = Dictionary();
String current_action_name = String();
int current_action_event_index = -1;
// Popups
InputEventConfigurationDialog *event_config_dialog;
AcceptDialog *message;
// Filtering and Adding actions
bool show_uneditable;
CheckBox *show_uneditable_actions_checkbox;
LineEdit *action_list_search;
bool allow_editing_actions;
HBoxContainer *add_hbox;
LineEdit *add_edit;
void _event_config_confirmed();
void _add_action_pressed();
void _add_action(const String &p_name);
void _action_edited();
void _tree_button_pressed(Object *p_item, int p_column, int p_id);
void _tree_item_activated();
void _search_term_updated(const String &p_search_term);
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
protected:
void _notification(int p_what);
static void _bind_methods();
public:
LineEdit *get_search_box() const;
InputEventConfigurationDialog *get_configuration_dialog();
// Dictionary represents an Action with "events" (Array) and "deadzone" (float) items. Pass with no param to update list from cached action map.
void update_action_list(const Vector<ActionInfo> &p_action_infos = Vector<ActionInfo>());
void show_message(const String &p_message);
void set_show_uneditable(bool p_show);
void set_allow_editing_actions(bool p_allow);
void set_toggle_editable_label(const String &p_label);
void use_external_search_box(LineEdit *p_searchbox);
ActionMapEditor();
};
#endif

File diff suppressed because it is too large Load diff

View file

@ -1,109 +0,0 @@
/*************************************************************************/
/* input_map_editor.h */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 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 */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#ifndef INPUT_MAP_EDITOR_H
#define INPUT_MAP_EDITOR_H
#include "core/object/undo_redo.h"
#include "editor/editor_data.h"
class InputMapEditor : public Control {
GDCLASS(InputMapEditor, Control);
enum InputType {
INPUT_KEY,
INPUT_KEY_PHYSICAL,
INPUT_JOY_BUTTON,
INPUT_JOY_MOTION,
INPUT_MOUSE_BUTTON
};
Tree *input_editor;
LineEdit *action_name;
Button *action_add;
Label *action_add_error;
InputType add_type;
String add_at;
int edit_idx;
PopupMenu *popup_add;
ConfirmationDialog *press_a_key;
bool press_a_key_physical;
Label *press_a_key_label;
ConfirmationDialog *device_input;
OptionButton *device_id;
OptionButton *device_index;
Label *device_index_label;
MenuButton *popup_copy_to_feature;
Ref<InputEventKey> last_wait_for_key;
AcceptDialog *message;
UndoRedo *undo_redo;
String inputmap_changed;
bool setting = false;
void _update_actions();
void _add_item(int p_item, Ref<InputEvent> p_exiting_event = Ref<InputEvent>());
void _edit_item(Ref<InputEvent> p_exiting_event);
void _action_check(String p_action);
void _action_adds(String);
void _action_add();
void _device_input_add();
void _action_selected();
void _action_edited();
void _action_activated();
void _action_button_pressed(Object *p_obj, int p_column, int p_id);
void _wait_for_key(const Ref<InputEvent> &p_event);
void _press_a_key_confirm();
void _show_last_added(const Ref<InputEvent> &p_event, const String &p_name);
String _get_joypad_motion_event_text(const Ref<InputEventJoypadMotion> &p_event);
Variant get_drag_data_fw(const Point2 &p_point, Control *p_from);
bool can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const;
void drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from);
protected:
int _get_current_device();
void _set_current_device(int i_device);
String _get_device_string(int i_device);
void _notification(int p_what);
static void _bind_methods();
public:
InputMapEditor();
};
#endif // INPUT_MAP_EDITOR_H

View file

@ -269,6 +269,206 @@ void ProjectSettingsEditor::_editor_restart_close() {
restart_container->hide();
}
void ProjectSettingsEditor::_action_added(const String &p_name) {
String name = "input/" + p_name;
if (ProjectSettings::get_singleton()->has_setting(name)) {
action_map->show_message(vformat(TTR("An action with the name '%s' already exists."), name));
return;
}
Dictionary action;
action["events"] = Array();
action["deadzone"] = 0.5f;
undo_redo->create_action(TTR("Add Input Action"));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", name, action);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", name);
undo_redo->add_do_method(this, "_update_action_map_editor");
undo_redo->add_undo_method(this, "_update_action_map_editor");
undo_redo->add_do_method(this, "queue_save");
undo_redo->add_undo_method(this, "queue_save");
undo_redo->commit_action();
}
void ProjectSettingsEditor::_action_edited(const String &p_name, const Dictionary &p_action) {
const String property_name = "input/" + p_name;
Dictionary old_val = ProjectSettings::get_singleton()->get(property_name);
if (old_val["deadzone"] != p_action["deadzone"]) {
// Deadzone Changed
undo_redo->create_action(TTR("Change Action deadzone"));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", property_name, p_action);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", property_name, old_val);
} else {
// Events changed
int event_count = ((Array)p_action["events"]).size();
int old_event_count = ((Array)old_val["events"]).size();
if (event_count == old_event_count) {
undo_redo->create_action(TTR("Edit Input Action Event"));
} else if (event_count > old_event_count) {
undo_redo->create_action(TTR("Add Input Action Event"));
} else if (event_count < old_event_count) {
undo_redo->create_action(TTR("Remove Input Action Event"));
}
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", property_name, p_action);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", property_name, old_val);
}
undo_redo->add_do_method(this, "_update_action_map_editor");
undo_redo->add_undo_method(this, "_update_action_map_editor");
undo_redo->add_do_method(this, "queue_save");
undo_redo->add_undo_method(this, "queue_save");
undo_redo->commit_action();
}
void ProjectSettingsEditor::_action_removed(const String &p_name) {
const String property_name = "input/" + p_name;
Dictionary old_val = ProjectSettings::get_singleton()->get(property_name);
int order = ProjectSettings::get_singleton()->get_order(property_name);
undo_redo->create_action(TTR("Erase Input Action"));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", property_name);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", property_name, old_val);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", property_name, order);
undo_redo->add_do_method(this, "_update_action_map_editor");
undo_redo->add_undo_method(this, "_update_action_map_editor");
undo_redo->add_do_method(this, "queue_save");
undo_redo->add_undo_method(this, "queue_save");
undo_redo->commit_action();
}
void ProjectSettingsEditor::_action_renamed(const String &p_old_name, const String &p_new_name) {
const String old_property_name = "input/" + p_old_name;
const String new_property_name = "input/" + p_new_name;
if (ProjectSettings::get_singleton()->has_setting(new_property_name)) {
action_map->show_message(vformat(TTR("An action with the name '%s' already exists."), new_property_name));
return;
}
int order = ProjectSettings::get_singleton()->get_order(old_property_name);
Dictionary action = ProjectSettings::get_singleton()->get(old_property_name);
undo_redo->create_action(TTR("Rename Input Action Event"));
// Do: clear old, set new
undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", old_property_name);
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", new_property_name, action);
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set_order", new_property_name, order);
// Undo: clear new, set old
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", new_property_name);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", old_property_name, action);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set_order", old_property_name, order);
undo_redo->add_do_method(this, "_update_action_map_editor");
undo_redo->add_undo_method(this, "_update_action_map_editor");
undo_redo->add_do_method(this, "queue_save");
undo_redo->add_undo_method(this, "queue_save");
undo_redo->commit_action();
}
void ProjectSettingsEditor::_action_reordered(const String &p_action_name, const String &p_relative_to, bool p_before) {
const String action_name = "input/" + p_action_name;
const String target_name = "input/" + p_relative_to;
// It is much easier to rebuild the custom "input" properties rather than messing around with the "order" values of them.
Variant action_value = ps->get(action_name);
Variant target_value = ps->get(target_name);
List<PropertyInfo> props;
OrderedHashMap<String, Variant> action_values;
ProjectSettings::get_singleton()->get_property_list(&props);
undo_redo->create_action(TTR("Update Input Action Order"));
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
PropertyInfo prop = E->get();
// Skip builtins and non-inputs
if (ProjectSettings::get_singleton()->is_builtin_setting(prop.name) || !prop.name.begins_with("input/")) {
continue;
}
action_values.insert(prop.name, ps->get(prop.name));
undo_redo->add_do_method(ProjectSettings::get_singleton(), "clear", prop.name);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "clear", prop.name);
}
for (OrderedHashMap<String, Variant>::Element E = action_values.front(); E; E = E.next()) {
String name = E.key();
Variant value = E.get();
if (name == target_name) {
if (p_before) {
// Insert before target
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", action_name, action_value);
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", target_name, target_value);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", target_name, target_value);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", action_name, action_value);
} else {
// Insert after target
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", target_name, target_value);
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", action_name, action_value);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", action_name, action_value);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", target_name, target_value);
}
} else if (name != action_name) {
undo_redo->add_do_method(ProjectSettings::get_singleton(), "set", name, value);
undo_redo->add_undo_method(ProjectSettings::get_singleton(), "set", name, value);
}
}
undo_redo->add_do_method(this, "_update_action_map_editor");
undo_redo->add_undo_method(this, "_update_action_map_editor");
undo_redo->add_do_method(this, "queue_save");
undo_redo->add_undo_method(this, "queue_save");
undo_redo->commit_action();
}
void ProjectSettingsEditor::_update_action_map_editor() {
Vector<ActionMapEditor::ActionInfo> actions;
List<PropertyInfo> props;
ProjectSettings::get_singleton()->get_property_list(&props);
const Ref<Texture2D> builtin_icon = get_theme_icon("PinPressed", "EditorIcons");
for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
const String property_name = E->get().name;
if (!property_name.begins_with("input/")) {
continue;
}
// Strip the "input/" from the left.
String display_name = property_name.substr(String("input/").size() - 1);
Dictionary action = ProjectSettings::get_singleton()->get(property_name);
ActionMapEditor::ActionInfo action_info;
action_info.action = action;
action_info.editable = true;
action_info.name = display_name;
const bool is_builtin_input = ProjectSettings::get_singleton()->get_input_presets().find(property_name) != nullptr;
if (is_builtin_input) {
action_info.editable = false;
action_info.icon = builtin_icon;
}
actions.push_back(action_info);
}
action_map->update_action_list(actions);
}
void ProjectSettingsEditor::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_VISIBILITY_CHANGED: {
@ -289,6 +489,8 @@ void ProjectSettingsEditor::_notification(int p_what) {
restart_container->add_theme_style_override("panel", get_theme_stylebox("bg", "Tree"));
restart_icon->set_texture(get_theme_icon("StatusWarning", "EditorIcons"));
restart_label->add_theme_color_override("font_color", get_theme_color("warning_color", "Editor"));
_update_action_map_editor();
} break;
case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
search_box->set_right_icon(get_theme_icon("Search", "EditorIcons"));
@ -299,6 +501,8 @@ void ProjectSettingsEditor::_notification(int p_what) {
void ProjectSettingsEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("queue_save"), &ProjectSettingsEditor::queue_save);
ClassDB::bind_method(D_METHOD("_update_action_map_editor"), &ProjectSettingsEditor::_update_action_map_editor);
}
ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
@ -437,10 +641,16 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
restart_close_button->connect("pressed", callable_mp(this, &ProjectSettingsEditor::_editor_restart_close));
restart_hb->add_child(restart_close_button);
inputmap_editor = memnew(InputMapEditor);
inputmap_editor->set_name(TTR("Input Map"));
inputmap_editor->connect("inputmap_changed", callable_mp(this, &ProjectSettingsEditor::queue_save));
tab_container->add_child(inputmap_editor);
action_map = memnew(ActionMapEditor);
action_map->set_name(TTR("Input Map"));
action_map->connect("action_added", callable_mp(this, &ProjectSettingsEditor::_action_added));
action_map->connect("action_edited", callable_mp(this, &ProjectSettingsEditor::_action_edited));
action_map->connect("action_removed", callable_mp(this, &ProjectSettingsEditor::_action_removed));
action_map->connect("action_renamed", callable_mp(this, &ProjectSettingsEditor::_action_renamed));
action_map->connect("action_reordered", callable_mp(this, &ProjectSettingsEditor::_action_reordered));
action_map->set_toggle_editable_label(TTR("Show built-in Actions"));
action_map->set_show_uneditable(false);
tab_container->add_child(action_map);
localization_editor = memnew(LocalizationEditor);
localization_editor->set_name(TTR("Localization"));

View file

@ -32,10 +32,10 @@
#define PROJECT_SETTINGS_EDITOR_H
#include "core/object/undo_redo.h"
#include "editor/action_map_editor.h"
#include "editor/editor_data.h"
#include "editor/editor_plugin_settings.h"
#include "editor/editor_sectioned_inspector.h"
#include "editor/input_map_editor.h"
#include "editor/localization_editor.h"
#include "editor/shader_globals_editor.h"
#include "editor_autoload_settings.h"
@ -44,26 +44,18 @@
class ProjectSettingsEditor : public AcceptDialog {
GDCLASS(ProjectSettingsEditor, AcceptDialog);
enum InputType {
INPUT_KEY,
INPUT_KEY_PHYSICAL,
INPUT_JOY_BUTTON,
INPUT_JOY_MOTION,
INPUT_MOUSE_BUTTON
};
static ProjectSettingsEditor *singleton;
ProjectSettings *ps;
Timer *timer;
TabContainer *tab_container;
SectionedInspector *inspector;
InputMapEditor *inputmap_editor;
LocalizationEditor *localization_editor;
EditorAutoloadSettings *autoload_settings;
ShaderGlobalsEditor *shaders_global_variables_editor;
EditorPluginSettings *plugin_settings;
ActionMapEditor *action_map;
HBoxContainer *search_bar;
LineEdit *search_box;
CheckButton *advanced;
@ -102,6 +94,14 @@ class ProjectSettingsEditor : public AcceptDialog {
void _editor_restart_close();
void _add_feature_overrides();
void _action_added(const String &p_name);
void _action_edited(const String &p_name, const Dictionary &p_action);
void _action_removed(const String &p_name);
void _action_renamed(const String &p_old_name, const String &p_new_name);
void _action_reordered(const String &p_action_name, const String &p_relative_to, bool p_before);
void _update_action_map_editor();
ProjectSettingsEditor();
protected:

View file

@ -31,6 +31,7 @@
#include "settings_config_dialog.h"
#include "core/config/project_settings.h"
#include "core/input/input_map.h"
#include "core/os/keyboard.h"
#include "editor/debugger/editor_debugger_node.h"
#include "editor_file_system.h"
@ -184,7 +185,52 @@ void EditorSettingsDialog::_update_icons() {
restart_label->add_theme_color_override("font_color", shortcuts->get_theme_color("warning_color", "Editor"));
}
void EditorSettingsDialog::_event_config_confirmed() {
Ref<InputEventKey> k = shortcut_editor->get_event();
if (k.is_null()) {
return;
}
if (editing_action) {
if (current_action_event_index == -1) {
// Add new event
current_action_events.push_back(k);
} else {
// Edit existing event
current_action_events[current_action_event_index] = k;
}
_update_builtin_action(current_action, current_action_events);
} else {
k = k->duplicate();
Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(shortcut_being_edited);
undo_redo->create_action(TTR("Change Shortcut") + " '" + shortcut_being_edited + "'");
undo_redo->add_do_method(current_sc.ptr(), "set_shortcut", k);
undo_redo->add_undo_method(current_sc.ptr(), "set_shortcut", current_sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
}
}
void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Array &p_events) {
Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(current_action);
undo_redo->create_action(TTR("Edit Built-in Action"));
undo_redo->add_do_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, p_events);
undo_redo->add_undo_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, old_input_array);
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
_update_shortcuts();
}
void EditorSettingsDialog::_update_shortcuts() {
// Before clearing the tree, take note of which categories are collapsed so that this state can be maintained when the tree is repopulated.
Map<String, bool> collapsed;
if (shortcuts->get_root() && shortcuts->get_root()->get_children()) {
@ -192,14 +238,92 @@ void EditorSettingsDialog::_update_shortcuts() {
collapsed[item->get_text(0)] = item->is_collapsed();
}
}
shortcuts->clear();
TreeItem *root = shortcuts->create_item();
Map<String, TreeItem *> sections;
// Set up section for Common/Built-in actions
TreeItem *common_section = shortcuts->create_item(root);
sections["Common"] = common_section;
common_section->set_text(0, TTR("Common"));
if (collapsed.has("Common")) {
common_section->set_collapsed(collapsed["Common"]);
}
common_section->set_custom_bg_color(0, shortcuts->get_theme_color("prop_subsection", "Editor"));
common_section->set_custom_bg_color(1, shortcuts->get_theme_color("prop_subsection", "Editor"));
// Get the action map for the editor, and add each item to the "Common" section.
OrderedHashMap<StringName, InputMap::Action> action_map = InputMap::get_singleton()->get_action_map();
for (OrderedHashMap<StringName, InputMap::Action>::Element E = action_map.front(); E; E = E.next()) {
String action_name = E.key();
if (!shortcut_filter.is_subsequence_ofi(action_name)) {
continue;
}
InputMap::Action action = E.get();
Array events; // Need to get the list of events into an array so it can be set as metadata on the item.
Vector<String> event_strings;
List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins().find(action_name).value();
// Remove all non-key events from the defaults.
for (List<Ref<InputEvent>>::Element *I = defaults.front(); I; I = I->next()) {
Ref<InputEventKey> k = I->get();
if (k.is_null()) {
I->erase();
}
}
bool same_as_defaults = defaults.size() == action.inputs.size(); // Initially this is set to just whether the arrays are equal. Later we check the events if needed.
int count = 0;
for (List<Ref<InputEvent>>::Element *I = action.inputs.front(); I; I = I->next()) {
// Add event and event text to respective arrays.
events.push_back(I->get());
event_strings.push_back(I->get()->as_text());
// Only check if the events have been the same so far - once one fails, we don't need to check any more.
if (same_as_defaults) {
Ref<InputEventKey> k = defaults[count];
// Only check keys, since we are in the editor.
if (k.is_valid() && !defaults[count]->shortcut_match(I->get())) {
same_as_defaults = false;
}
}
count++;
}
// Join the text of the events with a delimiter so they can all be displayed in one cell.
String events_display_string = event_strings.is_empty() ? "None" : String("; ").join(event_strings);
TreeItem *item = shortcuts->create_item(common_section);
item->set_text(0, action_name);
item->set_text(1, events_display_string);
if (!same_as_defaults) {
item->add_button(1, shortcuts->get_theme_icon("Reload", "EditorIcons"), 2);
}
if (events_display_string == "None") {
// Fade out unassigned shortcut labels for easier visual grepping.
item->set_custom_color(1, shortcuts->get_theme_color("font_color", "Label") * Color(1, 1, 1, 0.5));
}
item->add_button(1, shortcuts->get_theme_icon("Edit", "EditorIcons"), 0);
item->add_button(1, shortcuts->get_theme_icon("Close", "EditorIcons"), 1);
item->set_tooltip(0, action_name);
item->set_tooltip(1, events_display_string);
item->set_metadata(0, "Common");
item->set_metadata(1, events);
}
// Editor Shortcuts
List<String> slist;
EditorSettings::get_singleton()->get_shortcut_list(&slist);
TreeItem *root = shortcuts->create_item();
Map<String, TreeItem *> sections;
for (List<String>::Element *E = slist.front(); E; E = E->next()) {
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E->get());
@ -267,86 +391,121 @@ void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column
TreeItem *ti = Object::cast_to<TreeItem>(p_item);
ERR_FAIL_COND(!ti);
String item = ti->get_metadata(0);
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(item);
if (ti->get_metadata(0) == "Common") {
// Editing a Built-in action, which can have multiple bindings.
button_idx = p_idx;
editing_action = true;
current_action = ti->get_text(0);
if (p_idx == 0) {
press_a_key_label->set_text(TTR("Press a Key..."));
last_wait_for_key = Ref<InputEventKey>();
press_a_key->popup_centered(Size2(250, 80) * EDSCALE);
//press_a_key->grab_focus();
press_a_key->get_ok_button()->set_focus_mode(Control::FOCUS_NONE);
press_a_key->get_cancel_button()->set_focus_mode(Control::FOCUS_NONE);
shortcut_configured = item;
switch (button_idx) {
case SHORTCUT_REVERT: {
Array events;
List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins()[current_action];
} else if (p_idx == 1) { //erase
if (!sc.is_valid()) {
return; //pointless, there is nothing
// Convert the list to an array, and only keep key events as this is for the editor.
for (List<Ref<InputEvent>>::Element *E = defaults.front(); E; E = E->next()) {
Ref<InputEventKey> k = E->get();
if (k.is_valid()) {
events.append(E->get());
}
}
_update_builtin_action(current_action, events);
} break;
case SHORTCUT_EDIT:
case SHORTCUT_ERASE: {
// For Edit end Delete, we will show a popup which displays each event so the user can select which one to edit/delete.
current_action_events = ti->get_metadata(1);
action_popup->clear();
for (int i = 0; i < current_action_events.size(); i++) {
Ref<InputEvent> ie = current_action_events[i];
action_popup->add_item(ie->as_text());
action_popup->set_item_metadata(i, ie);
}
if (button_idx == SHORTCUT_EDIT) {
// If editing, add a button which can be used to add an additional event.
action_popup->add_icon_item(get_theme_icon("Add", "EditorIcons"), TTR("Add"));
}
action_popup->set_position(get_position() + get_mouse_position());
action_popup->take_mouse_focus();
action_popup->popup();
action_popup->set_as_minsize();
} break;
default:
break;
}
} else {
// Editing an Editor Shortcut, which can only have 1 binding.
String item = ti->get_metadata(0);
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(item);
editing_action = false;
undo_redo->create_action(TTR("Erase Shortcut"));
undo_redo->add_do_method(sc.ptr(), "set_shortcut", Ref<InputEvent>());
undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
} else if (p_idx == 2) { //revert to original
if (!sc.is_valid()) {
return; //pointless, there is nothing
switch (button_idx) {
case EditorSettingsDialog::SHORTCUT_EDIT:
shortcut_editor->popup_and_configure(sc->get_shortcut());
shortcut_being_edited = item;
break;
case EditorSettingsDialog::SHORTCUT_ERASE: {
if (!sc.is_valid()) {
return; //pointless, there is nothing
}
undo_redo->create_action(TTR("Erase Shortcut"));
undo_redo->add_do_method(sc.ptr(), "set_shortcut", Ref<InputEvent>());
undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
} break;
case EditorSettingsDialog::SHORTCUT_REVERT: {
if (!sc.is_valid()) {
return; //pointless, there is nothing
}
Ref<InputEvent> original = sc->get_meta("original");
undo_redo->create_action(TTR("Restore Shortcut"));
undo_redo->add_do_method(sc.ptr(), "set_shortcut", original);
undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
} break;
default:
break;
}
Ref<InputEvent> original = sc->get_meta("original");
undo_redo->create_action(TTR("Restore Shortcut"));
undo_redo->add_do_method(sc.ptr(), "set_shortcut", original);
undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
}
}
void EditorSettingsDialog::_wait_for_key(const Ref<InputEvent> &p_event) {
Ref<InputEventKey> k = p_event;
if (k.is_valid() && k->is_pressed() && k->get_keycode() != 0) {
last_wait_for_key = k;
const String str = keycode_get_string(k->get_keycode_with_modifiers());
press_a_key_label->set_text(str);
press_a_key->set_input_as_handled();
void EditorSettingsDialog::_builtin_action_popup_index_pressed(int p_index) {
switch (button_idx) {
case SHORTCUT_EDIT: {
if (p_index == action_popup->get_item_count() - 1) {
// Selected last item in list (Add button), therefore add new
current_action_event_index = -1;
shortcut_editor->popup_and_configure();
} else {
// Configure existing
current_action_event_index = p_index;
shortcut_editor->popup_and_configure(action_popup->get_item_metadata(p_index));
}
} break;
case SHORTCUT_ERASE: {
current_action_events.remove(p_index);
_update_builtin_action(current_action, current_action_events);
} break;
default:
break;
}
}
void EditorSettingsDialog::_press_a_key_confirm() {
if (last_wait_for_key.is_null()) {
return;
}
Ref<InputEventKey> ie;
ie.instance();
ie->set_keycode(last_wait_for_key->get_keycode());
ie->set_shift(last_wait_for_key->get_shift());
ie->set_control(last_wait_for_key->get_control());
ie->set_alt(last_wait_for_key->get_alt());
ie->set_metakey(last_wait_for_key->get_metakey());
Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(shortcut_configured);
undo_redo->create_action(TTR("Change Shortcut") + " '" + shortcut_configured + "'");
undo_redo->add_do_method(sc.ptr(), "set_shortcut", ie);
undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
undo_redo->add_do_method(this, "_update_shortcuts");
undo_redo->add_undo_method(this, "_update_shortcuts");
undo_redo->add_do_method(this, "_settings_changed");
undo_redo->add_undo_method(this, "_settings_changed");
undo_redo->commit_action();
}
void EditorSettingsDialog::_tabs_tab_changed(int p_tab) {
_focus_current_search_box();
}
@ -382,9 +541,14 @@ void EditorSettingsDialog::_editor_restart_close() {
void EditorSettingsDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_unhandled_input"), &EditorSettingsDialog::_unhandled_input);
ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
}
EditorSettingsDialog::EditorSettingsDialog() {
action_popup = memnew(PopupMenu);
action_popup->connect("index_pressed", callable_mp(this, &EditorSettingsDialog::_builtin_action_popup_index_pressed));
add_child(action_popup);
set_title(TTR("Editor Settings"));
undo_redo = memnew(UndoRedo);
@ -442,21 +606,17 @@ EditorSettingsDialog::EditorSettingsDialog() {
// Shortcuts Tab
tab_shortcuts = memnew(VBoxContainer);
tabs->add_child(tab_shortcuts);
tab_shortcuts->set_name(TTR("Shortcuts"));
hbc = memnew(HBoxContainer);
hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
tab_shortcuts->add_child(hbc);
shortcut_search_box = memnew(LineEdit);
shortcut_search_box->set_placeholder(TTR("Search"));
shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
hbc->add_child(shortcut_search_box);
tab_shortcuts->add_child(shortcut_search_box);
shortcut_search_box->connect("text_changed", callable_mp(this, &EditorSettingsDialog::_filter_shortcuts));
shortcuts = memnew(Tree);
tab_shortcuts->add_child(shortcuts, true);
shortcuts->set_v_size_flags(Control::SIZE_EXPAND_FILL);
shortcuts->set_columns(2);
shortcuts->set_hide_root(true);
@ -464,21 +624,13 @@ EditorSettingsDialog::EditorSettingsDialog() {
shortcuts->set_column_title(0, TTR("Name"));
shortcuts->set_column_title(1, TTR("Binding"));
shortcuts->connect("button_pressed", callable_mp(this, &EditorSettingsDialog::_shortcut_button_pressed));
tab_shortcuts->add_child(shortcuts);
press_a_key = memnew(ConfirmationDialog);
//press_a_key->set_focus_mode(Control::FOCUS_ALL);
add_child(press_a_key);
Label *l = memnew(Label);
l->set_text(TTR("Press a Key..."));
l->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
l->set_align(Label::ALIGN_CENTER);
l->set_offset(SIDE_TOP, 20);
l->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_BEGIN, 30);
press_a_key_label = l;
press_a_key->add_child(l);
press_a_key->connect("window_input", callable_mp(this, &EditorSettingsDialog::_wait_for_key));
press_a_key->connect("confirmed", callable_mp(this, &EditorSettingsDialog::_press_a_key_confirm));
// Adding event dialog
shortcut_editor = memnew(InputEventConfigurationDialog);
shortcut_editor->connect("confirmed", callable_mp(this, &EditorSettingsDialog::_event_config_confirmed));
shortcut_editor->set_allowed_input_types(InputEventConfigurationDialog::InputType::INPUT_KEY);
add_child(shortcut_editor);
set_hide_on_ok(true);

View file

@ -31,6 +31,7 @@
#ifndef SETTINGS_CONFIG_DIALOG_H
#define SETTINGS_CONFIG_DIALOG_H
#include "editor/action_map_editor.h"
#include "editor/editor_sectioned_inspector.h"
#include "editor_inspector.h"
#include "scene/gui/dialogs.h"
@ -52,16 +53,28 @@ class EditorSettingsDialog : public AcceptDialog {
LineEdit *shortcut_search_box;
SectionedInspector *inspector;
enum ShortcutButton {
SHORTCUT_EDIT,
SHORTCUT_ERASE,
SHORTCUT_REVERT
};
int button_idx;
int current_action_event_index = -1;
bool editing_action = false;
String current_action;
Array current_action_events;
PopupMenu *action_popup;
Timer *timer;
UndoRedo *undo_redo;
Tree *shortcuts;
ConfirmationDialog *press_a_key;
Label *press_a_key_label;
Ref<InputEventKey> last_wait_for_key;
String shortcut_configured;
// Shortcuts
String shortcut_filter;
Tree *shortcuts;
InputEventConfigurationDialog *shortcut_editor;
String shortcut_being_edited;
virtual void cancel_pressed() override;
virtual void ok_pressed() override;
@ -74,20 +87,20 @@ class EditorSettingsDialog : public AcceptDialog {
void _notification(int p_what);
void _update_icons();
void _press_a_key_confirm();
void _wait_for_key(const Ref<InputEvent> &p_event);
void _event_config_confirmed();
void _update_builtin_action(const String &p_name, const Array &p_events);
void _tabs_tab_changed(int p_tab);
void _focus_current_search_box();
void _clear_shortcut_search_box();
void _clear_search_box();
void _filter_shortcuts(const String &p_filter);
void _update_shortcuts();
void _shortcut_button_pressed(Object *p_item, int p_column, int p_idx);
void _builtin_action_popup_index_pressed(int p_index);
static void _undo_redo_callback(void *p_self, const String &p_name);
Label *restart_label;

View file

@ -881,10 +881,6 @@ bool Window::_can_consume_input_events() const {
}
void Window::_window_input(const Ref<InputEvent> &p_ev) {
if (Engine::get_singleton()->is_editor_hint() && (Object::cast_to<InputEventJoypadButton>(p_ev.ptr()) || Object::cast_to<InputEventJoypadMotion>(*p_ev))) {
return; //avoid joy input on editor
}
if (EngineDebugger::is_active()) {
//quit from game window using F8
Ref<InputEventKey> k = p_ev;