Add built-in action toggle in Input Map settings

This commit is contained in:
Haoyu Qiu 2022-11-29 16:22:23 +08:00 committed by Relintai
parent 91c53c41c2
commit 2d7145e936
2 changed files with 39 additions and 39 deletions

View File

@ -149,7 +149,6 @@ void ProjectSettingsEditor::_notification(int p_what) {
search_box->set_clear_button_enabled(true); search_box->set_clear_button_enabled(true);
action_add_error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor")); action_add_error->add_theme_color_override("font_color", get_theme_color("error_color", "Editor"));
translation_list->connect("button_pressed", this, "_translation_delete"); translation_list->connect("button_pressed", this, "_translation_delete");
_update_actions(); _update_actions();
@ -235,6 +234,16 @@ void ProjectSettingsEditor::_action_selected() {
edit_idx = -1; edit_idx = -1;
} }
String _check_new_action_name(const String &p_name) {
if (p_name.empty() || !_validate_action_name(p_name)) {
return TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'.");
}
if (ProjectSettings::get_singleton()->has_setting("input/" + p_name)) {
return vformat(TTR("An action with the name '%s' already exists."), p_name);
}
return String();
}
void ProjectSettingsEditor::_action_edited() { void ProjectSettingsEditor::_action_edited() {
TreeItem *ti = input_editor->get_selected(); TreeItem *ti = input_editor->get_selected();
if (!ti) { if (!ti) {
@ -249,26 +258,18 @@ void ProjectSettingsEditor::_action_edited() {
return; return;
} }
if (new_name == "" || !_validate_action_name(new_name)) { const String error = _check_new_action_name(new_name);
if (!error.empty()) {
ti->set_text(0, old_name); ti->set_text(0, old_name);
add_at = "input/" + old_name; add_at = "input/" + old_name;
message->set_text(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'")); message->set_text(error);
message->popup_centered(Size2(300, 100) * EDSCALE); message->popup_centered(Size2(300, 100) * EDSCALE);
return; return;
} }
String action_prop = "input/" + new_name; String action_prop = "input/" + new_name;
if (ProjectSettings::get_singleton()->has_setting(action_prop)) {
ti->set_text(0, old_name);
add_at = "input/" + old_name;
message->set_text(vformat(TTR("An action with the name '%s' already exists."), new_name));
message->popup_centered(Size2(300, 100) * EDSCALE);
return;
}
int order = ProjectSettings::get_singleton()->get_order(add_at); int order = ProjectSettings::get_singleton()->get_order(add_at);
Dictionary action = ProjectSettings::get_singleton()->get(add_at); Dictionary action = ProjectSettings::get_singleton()->get(add_at);
@ -781,6 +782,11 @@ void ProjectSettingsEditor::_update_actions() {
continue; continue;
} }
const bool is_builtin = ProjectSettings::get_singleton()->get_input_presets().find(pi.name) != nullptr;
if (is_builtin && !show_builtin_actions) {
continue;
}
Dictionary action = ProjectSettings::get_singleton()->get(pi.name); Dictionary action = ProjectSettings::get_singleton()->get(pi.name);
Array events = action["events"]; Array events = action["events"];
@ -798,7 +804,7 @@ void ProjectSettingsEditor::_update_actions() {
item->set_custom_bg_color(1, get_theme_color("prop_subsection", "Editor")); item->set_custom_bg_color(1, get_theme_color("prop_subsection", "Editor"));
item->add_button(2, get_theme_icon("Add", "EditorIcons"), 1, false, TTR("Add Event")); item->add_button(2, get_theme_icon("Add", "EditorIcons"), 1, false, TTR("Add Event"));
if (!ProjectSettings::get_singleton()->get_input_presets().find(pi.name)) { if (!is_builtin) {
item->add_button(2, get_theme_icon("Remove", "EditorIcons"), 2, false, TTR("Remove")); item->add_button(2, get_theme_icon("Remove", "EditorIcons"), 2, false, TTR("Remove"));
item->set_editable(0, true); item->set_editable(0, true);
} }
@ -995,26 +1001,9 @@ void ProjectSettingsEditor::_item_del() {
} }
void ProjectSettingsEditor::_action_check(String p_action) { void ProjectSettingsEditor::_action_check(String p_action) {
if (p_action == "") { String error = _check_new_action_name(p_action);
action_add->set_disabled(true); action_add->set_tooltip(error);
} else { action_add->set_disabled(!error.empty());
if (!_validate_action_name(p_action)) {
action_add_error->set_text(TTR("Invalid action name. It cannot be empty nor contain '/', ':', '=', '\\' or '\"'."));
action_add_error->show();
action_add->set_disabled(true);
return;
}
if (ProjectSettings::get_singleton()->has_setting("input/" + p_action)) {
action_add_error->set_text(vformat(TTR("An action with the name '%s' already exists."), p_action));
action_add_error->show();
action_add->set_disabled(true);
return;
}
action_add->set_disabled(false);
}
action_add_error->hide();
} }
void ProjectSettingsEditor::_action_adds(String) { void ProjectSettingsEditor::_action_adds(String) {
@ -1052,10 +1041,14 @@ void ProjectSettingsEditor::_action_add() {
r->select(0); r->select(0);
input_editor->ensure_cursor_is_visible(); input_editor->ensure_cursor_is_visible();
action_add_error->hide();
action_name->clear(); action_name->clear();
} }
void ProjectSettingsEditor::_set_show_builtin_actions(bool p_show) {
show_builtin_actions = p_show;
_update_actions();
}
void ProjectSettingsEditor::_item_checked(const String &p_item, bool p_check) { void ProjectSettingsEditor::_item_checked(const String &p_item, bool p_check) {
} }
@ -1803,6 +1796,7 @@ void ProjectSettingsEditor::_bind_methods() {
ClassDB::bind_method(D_METHOD("_action_edited"), &ProjectSettingsEditor::_action_edited); ClassDB::bind_method(D_METHOD("_action_edited"), &ProjectSettingsEditor::_action_edited);
ClassDB::bind_method(D_METHOD("_action_activated"), &ProjectSettingsEditor::_action_activated); ClassDB::bind_method(D_METHOD("_action_activated"), &ProjectSettingsEditor::_action_activated);
ClassDB::bind_method(D_METHOD("_action_button_pressed"), &ProjectSettingsEditor::_action_button_pressed); ClassDB::bind_method(D_METHOD("_action_button_pressed"), &ProjectSettingsEditor::_action_button_pressed);
ClassDB::bind_method(D_METHOD("_set_show_builtin_actions"), &ProjectSettingsEditor::_set_show_builtin_actions);
ClassDB::bind_method(D_METHOD("_update_actions"), &ProjectSettingsEditor::_update_actions); ClassDB::bind_method(D_METHOD("_update_actions"), &ProjectSettingsEditor::_update_actions);
ClassDB::bind_method(D_METHOD("_wait_for_key"), &ProjectSettingsEditor::_wait_for_key); ClassDB::bind_method(D_METHOD("_wait_for_key"), &ProjectSettingsEditor::_wait_for_key);
ClassDB::bind_method(D_METHOD("_add_item"), &ProjectSettingsEditor::_add_item, DEFVAL(Variant())); ClassDB::bind_method(D_METHOD("_add_item"), &ProjectSettingsEditor::_add_item, DEFVAL(Variant()));
@ -1979,15 +1973,12 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
l->set_text(TTR("Action:")); l->set_text(TTR("Action:"));
action_name = memnew(LineEdit); action_name = memnew(LineEdit);
action_name->set_clear_button_enabled(true);
action_name->set_h_size_flags(SIZE_EXPAND_FILL); action_name->set_h_size_flags(SIZE_EXPAND_FILL);
hbc->add_child(action_name); hbc->add_child(action_name);
action_name->connect("text_entered", this, "_action_adds"); action_name->connect("text_entered", this, "_action_adds");
action_name->connect("text_changed", this, "_action_check"); action_name->connect("text_changed", this, "_action_check");
action_add_error = memnew(Label);
hbc->add_child(action_add_error);
action_add_error->hide();
add = memnew(Button); add = memnew(Button);
hbc->add_child(add); hbc->add_child(add);
add->set_text(TTR("Add")); add->set_text(TTR("Add"));
@ -1995,6 +1986,12 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
add->connect("pressed", this, "_action_add"); add->connect("pressed", this, "_action_add");
action_add = add; action_add = add;
show_builtin_actions_checkbutton = memnew(CheckButton);
hbc->add_child(show_builtin_actions_checkbutton);
show_builtin_actions_checkbutton->set_text(TTR("Show Built-in Actions"));
show_builtin_actions_checkbutton->set_pressed(false);
show_builtin_actions_checkbutton->connect("toggled", this, "_set_show_builtin_actions");
input_editor = memnew(Tree); input_editor = memnew(Tree);
vbc->add_child(input_editor); vbc->add_child(input_editor);
input_editor->set_v_size_flags(SIZE_EXPAND_FILL); input_editor->set_v_size_flags(SIZE_EXPAND_FILL);
@ -2204,4 +2201,5 @@ ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
add_child(timer); add_child(timer);
updating_translations = false; updating_translations = false;
show_builtin_actions = false;
} }

View File

@ -109,10 +109,11 @@ class ProjectSettingsEditor : public AcceptDialog {
LineEdit *action_name; LineEdit *action_name;
Button *action_add; Button *action_add;
Label *action_add_error; CheckButton *show_builtin_actions_checkbutton;
Tree *input_editor; Tree *input_editor;
bool setting; bool setting;
bool updating_translations; bool updating_translations;
bool show_builtin_actions;
Ref<InputEventKey> last_wait_for_key; Ref<InputEventKey> last_wait_for_key;
@ -147,6 +148,7 @@ class ProjectSettingsEditor : public AcceptDialog {
void _action_adds(String); void _action_adds(String);
void _action_add(); void _action_add();
void _device_input_add(); void _device_input_add();
void _set_show_builtin_actions(bool p_show);
void _item_checked(const String &p_item, bool p_check); void _item_checked(const String &p_item, bool p_check);
void _action_selected(); void _action_selected();