editor_settings_dialog.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /**************************************************************************/
  2. /* editor_settings_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "editor_settings_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input_map.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/debugger/editor_debugger_node.h"
  35. #include "editor/editor_file_system.h"
  36. #include "editor/editor_log.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_property_name_processor.h"
  39. #include "editor/editor_scale.h"
  40. #include "editor/editor_settings.h"
  41. #include "editor/editor_undo_redo_manager.h"
  42. #include "editor/event_listener_line_edit.h"
  43. #include "editor/input_event_configuration_dialog.h"
  44. #include "scene/gui/margin_container.h"
  45. void EditorSettingsDialog::ok_pressed() {
  46. if (!EditorSettings::get_singleton()) {
  47. return;
  48. }
  49. _settings_save();
  50. timer->stop();
  51. }
  52. void EditorSettingsDialog::_settings_changed() {
  53. timer->start();
  54. }
  55. void EditorSettingsDialog::_settings_property_edited(const String &p_name) {
  56. String full_name = inspector->get_full_item_path(p_name);
  57. if (full_name == "interface/theme/accent_color" || full_name == "interface/theme/base_color" || full_name == "interface/theme/contrast" || full_name == "interface/theme/draw_extra_borders") {
  58. EditorSettings::get_singleton()->set_manually("interface/theme/preset", "Custom"); // set preset to Custom
  59. } else if (full_name.begins_with("text_editor/theme/highlighting")) {
  60. EditorSettings::get_singleton()->set_manually("text_editor/theme/color_theme", "Custom");
  61. }
  62. }
  63. void EditorSettingsDialog::_settings_save() {
  64. EditorSettings::get_singleton()->notify_changes();
  65. EditorSettings::get_singleton()->save();
  66. }
  67. void EditorSettingsDialog::cancel_pressed() {
  68. if (!EditorSettings::get_singleton()) {
  69. return;
  70. }
  71. EditorSettings::get_singleton()->notify_changes();
  72. }
  73. void EditorSettingsDialog::popup_edit_settings() {
  74. if (!EditorSettings::get_singleton()) {
  75. return;
  76. }
  77. EditorSettings::get_singleton()->list_text_editor_themes(); // make sure we have an up to date list of themes
  78. inspector->edit(EditorSettings::get_singleton());
  79. inspector->get_inspector()->update_tree();
  80. search_box->select_all();
  81. search_box->grab_focus();
  82. _update_shortcuts();
  83. set_process_shortcut_input(true);
  84. // Restore valid window bounds or pop up at default size.
  85. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "editor_settings", Rect2());
  86. if (saved_size != Rect2()) {
  87. popup(saved_size);
  88. } else {
  89. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  90. }
  91. _focus_current_search_box();
  92. }
  93. void EditorSettingsDialog::_filter_shortcuts(const String &) {
  94. _update_shortcuts();
  95. }
  96. void EditorSettingsDialog::_filter_shortcuts_by_event(const Ref<InputEvent> &p_event) {
  97. if (p_event.is_null() || (p_event->is_pressed() && !p_event->is_echo())) {
  98. _update_shortcuts();
  99. }
  100. }
  101. void EditorSettingsDialog::_undo_redo_callback(void *p_self, const String &p_name) {
  102. EditorNode::get_log()->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  103. }
  104. void EditorSettingsDialog::_notification(int p_what) {
  105. switch (p_what) {
  106. case NOTIFICATION_VISIBILITY_CHANGED: {
  107. if (!is_visible()) {
  108. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "editor_settings", Rect2(get_position(), get_size()));
  109. set_process_shortcut_input(false);
  110. }
  111. } break;
  112. case NOTIFICATION_READY: {
  113. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  114. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_method_notify_callback(EditorDebuggerNode::_method_changeds, nullptr);
  115. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_property_notify_callback(EditorDebuggerNode::_property_changeds, nullptr);
  116. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_commit_notify_callback(_undo_redo_callback, this);
  117. } break;
  118. case NOTIFICATION_ENTER_TREE: {
  119. _update_icons();
  120. } break;
  121. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  122. _update_icons();
  123. bool update_shortcuts_tab =
  124. EditorSettings::get_singleton()->check_changed_settings_in_group("shortcuts") ||
  125. EditorSettings::get_singleton()->check_changed_settings_in_group("builtin_action_overrides");
  126. if (update_shortcuts_tab) {
  127. _update_shortcuts();
  128. }
  129. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  130. inspector->update_category_list();
  131. }
  132. } break;
  133. }
  134. }
  135. void EditorSettingsDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  136. ERR_FAIL_COND(p_event.is_null());
  137. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  138. const Ref<InputEventKey> k = p_event;
  139. if (k.is_valid() && k->is_pressed()) {
  140. bool handled = false;
  141. if (ED_IS_SHORTCUT("ui_undo", p_event)) {
  142. String action = undo_redo->get_current_action_name();
  143. if (!action.is_empty()) {
  144. EditorNode::get_log()->add_message(vformat(TTR("Undo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  145. }
  146. undo_redo->undo();
  147. handled = true;
  148. }
  149. if (ED_IS_SHORTCUT("ui_redo", p_event)) {
  150. undo_redo->redo();
  151. String action = undo_redo->get_current_action_name();
  152. if (!action.is_empty()) {
  153. EditorNode::get_log()->add_message(vformat(TTR("Redo: %s"), action), EditorLog::MSG_TYPE_EDITOR);
  154. }
  155. handled = true;
  156. }
  157. if (k->is_match(InputEventKey::create_reference(KeyModifierMask::CMD_OR_CTRL | Key::F))) {
  158. _focus_current_search_box();
  159. handled = true;
  160. }
  161. if (handled) {
  162. set_input_as_handled();
  163. }
  164. }
  165. }
  166. void EditorSettingsDialog::_update_icons() {
  167. search_box->set_right_icon(shortcuts->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  168. search_box->set_clear_button_enabled(true);
  169. shortcut_search_box->set_right_icon(shortcuts->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  170. shortcut_search_box->set_clear_button_enabled(true);
  171. restart_close_button->set_icon(shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  172. restart_container->add_theme_style_override("panel", shortcuts->get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
  173. restart_icon->set_texture(shortcuts->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  174. restart_label->add_theme_color_override("font_color", shortcuts->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  175. }
  176. void EditorSettingsDialog::_event_config_confirmed() {
  177. Ref<InputEventKey> k = shortcut_editor->get_event();
  178. if (k.is_null()) {
  179. return;
  180. }
  181. if (current_event_index == -1) {
  182. // Add new event
  183. current_events.push_back(k);
  184. } else {
  185. // Edit existing event
  186. current_events[current_event_index] = k;
  187. }
  188. if (is_editing_action) {
  189. _update_builtin_action(current_edited_identifier, current_events);
  190. } else {
  191. _update_shortcut_events(current_edited_identifier, current_events);
  192. }
  193. }
  194. void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Array &p_events) {
  195. Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(p_name);
  196. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  197. undo_redo->create_action(TTR("Edit Built-in Action") + " '" + p_name + "'");
  198. undo_redo->add_do_method(EditorSettings::get_singleton(), "mark_setting_changed", "builtin_action_overrides");
  199. undo_redo->add_undo_method(EditorSettings::get_singleton(), "mark_setting_changed", "builtin_action_overrides");
  200. undo_redo->add_do_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, p_events);
  201. undo_redo->add_undo_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, old_input_array);
  202. undo_redo->add_do_method(this, "_settings_changed");
  203. undo_redo->add_undo_method(this, "_settings_changed");
  204. undo_redo->commit_action();
  205. _update_shortcuts();
  206. }
  207. void EditorSettingsDialog::_update_shortcut_events(const String &p_path, const Array &p_events) {
  208. Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  209. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  210. undo_redo->create_action(TTR("Edit Shortcut") + " '" + p_path + "'");
  211. undo_redo->add_do_method(current_sc.ptr(), "set_events", p_events);
  212. undo_redo->add_undo_method(current_sc.ptr(), "set_events", current_sc->get_events());
  213. undo_redo->add_do_method(EditorSettings::get_singleton(), "mark_setting_changed", "shortcuts");
  214. undo_redo->add_undo_method(EditorSettings::get_singleton(), "mark_setting_changed", "shortcuts");
  215. undo_redo->add_do_method(this, "_update_shortcuts");
  216. undo_redo->add_undo_method(this, "_update_shortcuts");
  217. undo_redo->add_do_method(this, "_settings_changed");
  218. undo_redo->add_undo_method(this, "_settings_changed");
  219. undo_redo->commit_action();
  220. }
  221. Array EditorSettingsDialog::_event_list_to_array_helper(const List<Ref<InputEvent>> &p_events) {
  222. Array events;
  223. // Convert the list to an array, and only keep key events as this is for the editor.
  224. for (const List<Ref<InputEvent>>::Element *E = p_events.front(); E; E = E->next()) {
  225. Ref<InputEventKey> k = E->get();
  226. if (k.is_valid()) {
  227. events.append(E->get());
  228. }
  229. }
  230. return events;
  231. }
  232. void EditorSettingsDialog::_create_shortcut_treeitem(TreeItem *p_parent, const String &p_shortcut_identifier, const String &p_display, Array &p_events, bool p_allow_revert, bool p_is_action, bool p_is_collapsed) {
  233. TreeItem *shortcut_item = shortcuts->create_item(p_parent);
  234. shortcut_item->set_collapsed(p_is_collapsed);
  235. shortcut_item->set_text(0, p_display);
  236. Ref<InputEvent> primary = p_events.size() > 0 ? Ref<InputEvent>(p_events[0]) : Ref<InputEvent>();
  237. Ref<InputEvent> secondary = p_events.size() > 1 ? Ref<InputEvent>(p_events[1]) : Ref<InputEvent>();
  238. String sc_text = "None";
  239. if (primary.is_valid()) {
  240. sc_text = primary->as_text();
  241. if (secondary.is_valid()) {
  242. sc_text += ", " + secondary->as_text();
  243. if (p_events.size() > 2) {
  244. sc_text += " (+" + itos(p_events.size() - 2) + ")";
  245. }
  246. }
  247. }
  248. shortcut_item->set_text(1, sc_text);
  249. if (sc_text == "None") {
  250. // Fade out unassigned shortcut labels for easier visual grepping.
  251. shortcut_item->set_custom_color(1, shortcuts->get_theme_color(SNAME("font_color"), SNAME("Label")) * Color(1, 1, 1, 0.5));
  252. }
  253. if (p_allow_revert) {
  254. shortcut_item->add_button(1, shortcuts->get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")), SHORTCUT_REVERT);
  255. }
  256. shortcut_item->add_button(1, shortcuts->get_theme_icon(SNAME("Add"), SNAME("EditorIcons")), SHORTCUT_ADD);
  257. shortcut_item->add_button(1, shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")), SHORTCUT_ERASE);
  258. shortcut_item->set_meta("is_action", p_is_action);
  259. shortcut_item->set_meta("type", "shortcut");
  260. shortcut_item->set_meta("shortcut_identifier", p_shortcut_identifier);
  261. shortcut_item->set_meta("events", p_events);
  262. // Shortcut Input Events
  263. for (int i = 0; i < p_events.size(); i++) {
  264. Ref<InputEvent> ie = p_events[i];
  265. if (ie.is_null()) {
  266. continue;
  267. }
  268. TreeItem *event_item = shortcuts->create_item(shortcut_item);
  269. event_item->set_text(0, shortcut_item->get_child_count() == 1 ? "Primary" : "");
  270. event_item->set_text(1, ie->as_text());
  271. event_item->add_button(1, shortcuts->get_theme_icon(SNAME("Edit"), SNAME("EditorIcons")), SHORTCUT_EDIT);
  272. event_item->add_button(1, shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")), SHORTCUT_ERASE);
  273. event_item->set_custom_bg_color(0, shortcuts->get_theme_color(SNAME("dark_color_3"), SNAME("Editor")));
  274. event_item->set_custom_bg_color(1, shortcuts->get_theme_color(SNAME("dark_color_3"), SNAME("Editor")));
  275. event_item->set_meta("is_action", p_is_action);
  276. event_item->set_meta("type", "event");
  277. event_item->set_meta("event_index", i);
  278. }
  279. }
  280. bool EditorSettingsDialog::_should_display_shortcut(const String &p_name, const Array &p_events) const {
  281. const Ref<InputEvent> search_ev = shortcut_search_by_event->get_event();
  282. bool event_match = true;
  283. if (search_ev.is_valid()) {
  284. event_match = false;
  285. for (int i = 0; i < p_events.size(); ++i) {
  286. const Ref<InputEvent> ev = p_events[i];
  287. if (ev.is_valid() && ev->is_match(search_ev, true)) {
  288. event_match = true;
  289. }
  290. }
  291. }
  292. return event_match && shortcut_search_box->get_text().is_subsequence_ofn(p_name);
  293. }
  294. void EditorSettingsDialog::_update_shortcuts() {
  295. // Before clearing the tree, take note of which categories are collapsed so that this state can be maintained when the tree is repopulated.
  296. HashMap<String, bool> collapsed;
  297. if (shortcuts->get_root() && shortcuts->get_root()->get_first_child()) {
  298. TreeItem *ti = shortcuts->get_root()->get_first_child();
  299. while (ti) {
  300. // Not all items have valid or unique text in the first column - so if it has an identifier, use that, as it should be unique.
  301. if (ti->get_first_child() && ti->has_meta("shortcut_identifier")) {
  302. collapsed[ti->get_meta("shortcut_identifier")] = ti->is_collapsed();
  303. } else {
  304. collapsed[ti->get_text(0)] = ti->is_collapsed();
  305. }
  306. // Try go down tree
  307. TreeItem *ti_next = ti->get_first_child();
  308. // Try go to the next node via in-order traversal
  309. if (!ti_next) {
  310. ti_next = ti;
  311. while (ti_next && !ti_next->get_next()) {
  312. ti_next = ti_next->get_parent();
  313. }
  314. if (ti_next) {
  315. ti_next = ti_next->get_next();
  316. }
  317. }
  318. ti = ti_next;
  319. }
  320. }
  321. shortcuts->clear();
  322. TreeItem *root = shortcuts->create_item();
  323. HashMap<String, TreeItem *> sections;
  324. // Set up section for Common/Built-in actions
  325. TreeItem *common_section = shortcuts->create_item(root);
  326. sections["Common"] = common_section;
  327. common_section->set_text(0, TTR("Common"));
  328. common_section->set_selectable(0, false);
  329. common_section->set_selectable(1, false);
  330. if (collapsed.has("Common")) {
  331. common_section->set_collapsed(collapsed["Common"]);
  332. }
  333. common_section->set_custom_bg_color(0, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  334. common_section->set_custom_bg_color(1, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  335. // Get the action map for the editor, and add each item to the "Common" section.
  336. for (const KeyValue<StringName, InputMap::Action> &E : InputMap::get_singleton()->get_action_map()) {
  337. const String &action_name = E.key;
  338. const InputMap::Action &action = E.value;
  339. // Skip non-builtin actions.
  340. if (!InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().has(action_name)) {
  341. continue;
  342. }
  343. const List<Ref<InputEvent>> &all_default_events = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(action_name)->value;
  344. Array action_events = _event_list_to_array_helper(action.inputs);
  345. if (!_should_display_shortcut(action_name, action_events)) {
  346. continue;
  347. }
  348. Array default_events = _event_list_to_array_helper(all_default_events);
  349. bool same_as_defaults = Shortcut::is_event_array_equal(default_events, action_events);
  350. bool collapse = !collapsed.has(action_name) || (collapsed.has(action_name) && collapsed[action_name]);
  351. _create_shortcut_treeitem(common_section, action_name, action_name, action_events, !same_as_defaults, true, collapse);
  352. }
  353. // Editor Shortcuts
  354. List<String> slist;
  355. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  356. slist.sort(); // Sort alphabetically.
  357. const EditorPropertyNameProcessor::Style name_style = EditorPropertyNameProcessor::get_settings_style();
  358. const EditorPropertyNameProcessor::Style tooltip_style = EditorPropertyNameProcessor::get_tooltip_style(name_style);
  359. // Create all sections first.
  360. for (const String &E : slist) {
  361. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E);
  362. String section_name = E.get_slice("/", 0);
  363. if (sections.has(section_name)) {
  364. continue;
  365. }
  366. TreeItem *section = shortcuts->create_item(root);
  367. const String item_name = EditorPropertyNameProcessor::get_singleton()->process_name(section_name, name_style);
  368. const String tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(section_name, tooltip_style);
  369. section->set_text(0, item_name);
  370. section->set_tooltip_text(0, tooltip);
  371. section->set_selectable(0, false);
  372. section->set_selectable(1, false);
  373. section->set_custom_bg_color(0, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  374. section->set_custom_bg_color(1, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  375. if (collapsed.has(item_name)) {
  376. section->set_collapsed(collapsed[item_name]);
  377. }
  378. sections[section_name] = section;
  379. }
  380. // Add shortcuts to sections.
  381. for (const String &E : slist) {
  382. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E);
  383. if (!sc->has_meta("original")) {
  384. continue;
  385. }
  386. String section_name = E.get_slice("/", 0);
  387. TreeItem *section = sections[section_name];
  388. if (!_should_display_shortcut(sc->get_name(), sc->get_events())) {
  389. continue;
  390. }
  391. Array original = sc->get_meta("original");
  392. Array shortcuts_array = sc->get_events().duplicate(true);
  393. bool same_as_defaults = Shortcut::is_event_array_equal(original, shortcuts_array);
  394. bool collapse = !collapsed.has(E) || (collapsed.has(E) && collapsed[E]);
  395. _create_shortcut_treeitem(section, E, sc->get_name(), shortcuts_array, !same_as_defaults, false, collapse);
  396. }
  397. // remove sections with no shortcuts
  398. for (KeyValue<String, TreeItem *> &E : sections) {
  399. TreeItem *section = E.value;
  400. if (section->get_first_child() == nullptr) {
  401. root->remove_child(section);
  402. memdelete(section);
  403. }
  404. }
  405. }
  406. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx, MouseButton p_button) {
  407. if (p_button != MouseButton::LEFT) {
  408. return;
  409. }
  410. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  411. ERR_FAIL_COND_MSG(!ti, "Object passed is not a TreeItem");
  412. ShortcutButton button_idx = (ShortcutButton)p_idx;
  413. is_editing_action = ti->get_meta("is_action");
  414. String type = ti->get_meta("type");
  415. if (type == "event") {
  416. current_edited_identifier = ti->get_parent()->get_meta("shortcut_identifier");
  417. current_events = ti->get_parent()->get_meta("events");
  418. current_event_index = ti->get_meta("event_index");
  419. } else { // Type is "shortcut"
  420. current_edited_identifier = ti->get_meta("shortcut_identifier");
  421. current_events = ti->get_meta("events");
  422. current_event_index = -1;
  423. }
  424. switch (button_idx) {
  425. case EditorSettingsDialog::SHORTCUT_ADD: {
  426. // Only for "shortcut" types
  427. shortcut_editor->popup_and_configure();
  428. } break;
  429. case EditorSettingsDialog::SHORTCUT_EDIT: {
  430. // Only for "event" types
  431. shortcut_editor->popup_and_configure(current_events[current_event_index]);
  432. } break;
  433. case EditorSettingsDialog::SHORTCUT_ERASE: {
  434. if (type == "shortcut") {
  435. if (is_editing_action) {
  436. _update_builtin_action(current_edited_identifier, Array());
  437. } else {
  438. _update_shortcut_events(current_edited_identifier, Array());
  439. }
  440. } else if (type == "event") {
  441. current_events.remove_at(current_event_index);
  442. if (is_editing_action) {
  443. _update_builtin_action(current_edited_identifier, current_events);
  444. } else {
  445. _update_shortcut_events(current_edited_identifier, current_events);
  446. }
  447. }
  448. } break;
  449. case EditorSettingsDialog::SHORTCUT_REVERT: {
  450. // Only for "shortcut" types
  451. if (is_editing_action) {
  452. List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_edited_identifier];
  453. Array events = _event_list_to_array_helper(defaults);
  454. _update_builtin_action(current_edited_identifier, events);
  455. } else {
  456. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(current_edited_identifier);
  457. Array original = sc->get_meta("original");
  458. _update_shortcut_events(current_edited_identifier, original);
  459. }
  460. } break;
  461. default:
  462. break;
  463. }
  464. }
  465. void EditorSettingsDialog::_shortcut_cell_double_clicked() {
  466. // When a shortcut cell is double clicked:
  467. // If the cell has children and is in the bindings column, and if its first child is editable,
  468. // then uncollapse the cell, and if the first child is the only child, then edit that child.
  469. // If the cell is in the bindings column and can be edited, then edit it.
  470. // If the cell is in the name column, then toggle collapse.
  471. const ShortcutButton edit_btn_id = EditorSettingsDialog::SHORTCUT_EDIT;
  472. const int edit_btn_col = 1;
  473. TreeItem *ti = shortcuts->get_selected();
  474. if (ti == nullptr) {
  475. return;
  476. }
  477. String type = ti->get_meta("type");
  478. int col = shortcuts->get_selected_column();
  479. if (type == "shortcut" && col == 0) {
  480. if (ti->get_first_child()) {
  481. ti->set_collapsed(!ti->is_collapsed());
  482. }
  483. } else if (type == "shortcut" && col == 1) {
  484. if (ti->get_first_child()) {
  485. TreeItem *child_ti = ti->get_first_child();
  486. if (child_ti->get_button_by_id(edit_btn_col, edit_btn_id) != -1) {
  487. ti->set_collapsed(false);
  488. if (ti->get_child_count() == 1) {
  489. _shortcut_button_pressed(child_ti, edit_btn_col, edit_btn_id);
  490. }
  491. }
  492. }
  493. } else if (type == "event" && col == 1) {
  494. if (ti->get_button_by_id(edit_btn_col, edit_btn_id) != -1) {
  495. _shortcut_button_pressed(ti, edit_btn_col, edit_btn_id);
  496. }
  497. }
  498. }
  499. Variant EditorSettingsDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  500. TreeItem *selected = shortcuts->get_selected();
  501. // Only allow drag for events
  502. if (!selected || (String)selected->get_meta("type", "") != "event") {
  503. return Variant();
  504. }
  505. String label_text = "Event " + itos(selected->get_meta("event_index"));
  506. Label *label = memnew(Label(label_text));
  507. label->set_modulate(Color(1, 1, 1, 1.0f));
  508. shortcuts->set_drag_preview(label);
  509. shortcuts->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  510. return Dictionary(); // No data required
  511. }
  512. bool EditorSettingsDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  513. TreeItem *selected = shortcuts->get_selected();
  514. TreeItem *item = shortcuts->get_item_at_position(p_point);
  515. if (!selected || !item || item == selected || (String)item->get_meta("type", "") != "event") {
  516. return false;
  517. }
  518. // Don't allow moving an events in-between shortcuts.
  519. if (selected->get_parent()->get_meta("shortcut_identifier") != item->get_parent()->get_meta("shortcut_identifier")) {
  520. return false;
  521. }
  522. return true;
  523. }
  524. void EditorSettingsDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  525. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  526. return;
  527. }
  528. TreeItem *selected = shortcuts->get_selected();
  529. TreeItem *target = shortcuts->get_item_at_position(p_point);
  530. if (!target) {
  531. return;
  532. }
  533. int target_event_index = target->get_meta("event_index");
  534. int index_moving_from = selected->get_meta("event_index");
  535. Array events = selected->get_parent()->get_meta("events");
  536. Variant event_moved = events[index_moving_from];
  537. events.remove_at(index_moving_from);
  538. events.insert(target_event_index, event_moved);
  539. String ident = selected->get_parent()->get_meta("shortcut_identifier");
  540. if (selected->get_meta("is_action")) {
  541. _update_builtin_action(ident, events);
  542. } else {
  543. _update_shortcut_events(ident, events);
  544. }
  545. }
  546. void EditorSettingsDialog::_tabs_tab_changed(int p_tab) {
  547. _focus_current_search_box();
  548. }
  549. void EditorSettingsDialog::_focus_current_search_box() {
  550. Control *tab = tabs->get_current_tab_control();
  551. LineEdit *current_search_box = nullptr;
  552. if (tab == tab_general) {
  553. current_search_box = search_box;
  554. } else if (tab == tab_shortcuts) {
  555. current_search_box = shortcut_search_box;
  556. }
  557. if (current_search_box) {
  558. current_search_box->grab_focus();
  559. current_search_box->select_all();
  560. }
  561. }
  562. void EditorSettingsDialog::_editor_restart() {
  563. EditorNode::get_singleton()->save_all_scenes();
  564. EditorNode::get_singleton()->restart_editor();
  565. }
  566. void EditorSettingsDialog::_editor_restart_request() {
  567. restart_container->show();
  568. }
  569. void EditorSettingsDialog::_editor_restart_close() {
  570. restart_container->hide();
  571. }
  572. void EditorSettingsDialog::_bind_methods() {
  573. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  574. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  575. }
  576. EditorSettingsDialog::EditorSettingsDialog() {
  577. set_title(TTR("Editor Settings"));
  578. set_clamp_to_embedder(true);
  579. tabs = memnew(TabContainer);
  580. tabs->set_theme_type_variation("TabContainerOdd");
  581. tabs->connect("tab_changed", callable_mp(this, &EditorSettingsDialog::_tabs_tab_changed));
  582. add_child(tabs);
  583. // General Tab
  584. tab_general = memnew(VBoxContainer);
  585. tabs->add_child(tab_general);
  586. tab_general->set_name(TTR("General"));
  587. HBoxContainer *hbc = memnew(HBoxContainer);
  588. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  589. tab_general->add_child(hbc);
  590. search_box = memnew(LineEdit);
  591. search_box->set_placeholder(TTR("Filter Settings"));
  592. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  593. hbc->add_child(search_box);
  594. inspector = memnew(SectionedInspector);
  595. inspector->get_inspector()->set_use_filter(true);
  596. inspector->register_search_box(search_box);
  597. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  598. tab_general->add_child(inspector);
  599. inspector->get_inspector()->connect("property_edited", callable_mp(this, &EditorSettingsDialog::_settings_property_edited));
  600. inspector->get_inspector()->connect("restart_requested", callable_mp(this, &EditorSettingsDialog::_editor_restart_request));
  601. restart_container = memnew(PanelContainer);
  602. tab_general->add_child(restart_container);
  603. HBoxContainer *restart_hb = memnew(HBoxContainer);
  604. restart_container->add_child(restart_hb);
  605. restart_icon = memnew(TextureRect);
  606. restart_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  607. restart_hb->add_child(restart_icon);
  608. restart_label = memnew(Label);
  609. restart_label->set_text(TTR("The editor must be restarted for changes to take effect."));
  610. restart_hb->add_child(restart_label);
  611. restart_hb->add_spacer();
  612. Button *restart_button = memnew(Button);
  613. restart_button->connect("pressed", callable_mp(this, &EditorSettingsDialog::_editor_restart));
  614. restart_hb->add_child(restart_button);
  615. restart_button->set_text(TTR("Save & Restart"));
  616. restart_close_button = memnew(Button);
  617. restart_close_button->set_flat(true);
  618. restart_close_button->connect("pressed", callable_mp(this, &EditorSettingsDialog::_editor_restart_close));
  619. restart_hb->add_child(restart_close_button);
  620. restart_container->hide();
  621. // Shortcuts Tab
  622. tab_shortcuts = memnew(VBoxContainer);
  623. tabs->add_child(tab_shortcuts);
  624. tab_shortcuts->set_name(TTR("Shortcuts"));
  625. HBoxContainer *top_hbox = memnew(HBoxContainer);
  626. top_hbox->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  627. tab_shortcuts->add_child(top_hbox);
  628. shortcut_search_box = memnew(LineEdit);
  629. shortcut_search_box->set_placeholder(TTR("Filter by name..."));
  630. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  631. top_hbox->add_child(shortcut_search_box);
  632. shortcut_search_box->connect("text_changed", callable_mp(this, &EditorSettingsDialog::_filter_shortcuts));
  633. shortcut_search_by_event = memnew(EventListenerLineEdit);
  634. shortcut_search_by_event->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  635. shortcut_search_by_event->set_stretch_ratio(0.75);
  636. shortcut_search_by_event->set_allowed_input_types(INPUT_KEY);
  637. shortcut_search_by_event->connect("event_changed", callable_mp(this, &EditorSettingsDialog::_filter_shortcuts_by_event));
  638. top_hbox->add_child(shortcut_search_by_event);
  639. Button *clear_all_search = memnew(Button);
  640. clear_all_search->set_text(TTR("Clear All"));
  641. clear_all_search->connect("pressed", callable_mp(shortcut_search_box, &LineEdit::clear));
  642. clear_all_search->connect("pressed", callable_mp(shortcut_search_by_event, &EventListenerLineEdit::clear_event));
  643. top_hbox->add_child(clear_all_search);
  644. shortcuts = memnew(Tree);
  645. shortcuts->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  646. shortcuts->set_columns(2);
  647. shortcuts->set_hide_root(true);
  648. shortcuts->set_column_titles_visible(true);
  649. shortcuts->set_column_title(0, TTR("Name"));
  650. shortcuts->set_column_title(1, TTR("Binding"));
  651. shortcuts->connect("button_clicked", callable_mp(this, &EditorSettingsDialog::_shortcut_button_pressed));
  652. shortcuts->connect("item_activated", callable_mp(this, &EditorSettingsDialog::_shortcut_cell_double_clicked));
  653. tab_shortcuts->add_child(shortcuts);
  654. SET_DRAG_FORWARDING_GCD(shortcuts, EditorSettingsDialog);
  655. // Adding event dialog
  656. shortcut_editor = memnew(InputEventConfigurationDialog);
  657. shortcut_editor->connect("confirmed", callable_mp(this, &EditorSettingsDialog::_event_config_confirmed));
  658. shortcut_editor->set_allowed_input_types(INPUT_KEY);
  659. add_child(shortcut_editor);
  660. set_hide_on_ok(true);
  661. timer = memnew(Timer);
  662. timer->set_wait_time(1.5);
  663. timer->connect("timeout", callable_mp(this, &EditorSettingsDialog::_settings_save));
  664. timer->set_one_shot(true);
  665. add_child(timer);
  666. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorSettingsDialog::_settings_changed));
  667. set_ok_button_text(TTR("Close"));
  668. }
  669. EditorSettingsDialog::~EditorSettingsDialog() {
  670. }