settings_config_dialog.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*************************************************************************/
  2. /* settings_config_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "settings_config_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_file_system.h"
  36. #include "editor_log.h"
  37. #include "editor_node.h"
  38. #include "editor_scale.h"
  39. #include "editor_settings.h"
  40. #include "scene/gui/margin_container.h"
  41. void EditorSettingsDialog::ok_pressed() {
  42. if (!EditorSettings::get_singleton()) {
  43. return;
  44. }
  45. _settings_save();
  46. timer->stop();
  47. }
  48. void EditorSettingsDialog::_settings_changed() {
  49. timer->start();
  50. }
  51. void EditorSettingsDialog::_settings_property_edited(const String &p_name) {
  52. String full_name = inspector->get_full_item_path(p_name);
  53. if (full_name == "interface/theme/accent_color" || full_name == "interface/theme/base_color" || full_name == "interface/theme/contrast") {
  54. EditorSettings::get_singleton()->set_manually("interface/theme/preset", "Custom"); // set preset to Custom
  55. } else if (full_name.begins_with("text_editor/theme/highlighting")) {
  56. EditorSettings::get_singleton()->set_manually("text_editor/theme/color_theme", "Custom");
  57. }
  58. }
  59. void EditorSettingsDialog::_settings_save() {
  60. EditorSettings::get_singleton()->notify_changes();
  61. EditorSettings::get_singleton()->save();
  62. }
  63. void EditorSettingsDialog::cancel_pressed() {
  64. if (!EditorSettings::get_singleton()) {
  65. return;
  66. }
  67. EditorSettings::get_singleton()->notify_changes();
  68. }
  69. void EditorSettingsDialog::popup_edit_settings() {
  70. if (!EditorSettings::get_singleton()) {
  71. return;
  72. }
  73. EditorSettings::get_singleton()->list_text_editor_themes(); // make sure we have an up to date list of themes
  74. inspector->edit(EditorSettings::get_singleton());
  75. inspector->get_inspector()->update_tree();
  76. search_box->select_all();
  77. search_box->grab_focus();
  78. _update_shortcuts();
  79. set_process_unhandled_input(true);
  80. // Restore valid window bounds or pop up at default size.
  81. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "editor_settings", Rect2());
  82. if (saved_size != Rect2()) {
  83. popup(saved_size);
  84. } else {
  85. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  86. }
  87. _focus_current_search_box();
  88. }
  89. void EditorSettingsDialog::_filter_shortcuts(const String &p_filter) {
  90. shortcut_filter = p_filter;
  91. _update_shortcuts();
  92. }
  93. void EditorSettingsDialog::_undo_redo_callback(void *p_self, const String &p_name) {
  94. EditorNode::get_log()->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  95. }
  96. void EditorSettingsDialog::_notification(int p_what) {
  97. switch (p_what) {
  98. case NOTIFICATION_VISIBILITY_CHANGED: {
  99. if (!is_visible()) {
  100. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "editor_settings", Rect2(get_position(), get_size()));
  101. set_process_unhandled_input(false);
  102. }
  103. } break;
  104. case NOTIFICATION_READY: {
  105. undo_redo->set_method_notify_callback(EditorDebuggerNode::_method_changeds, nullptr);
  106. undo_redo->set_property_notify_callback(EditorDebuggerNode::_property_changeds, nullptr);
  107. undo_redo->set_commit_notify_callback(_undo_redo_callback, this);
  108. } break;
  109. case NOTIFICATION_ENTER_TREE: {
  110. _update_icons();
  111. } break;
  112. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  113. _update_icons();
  114. // Update theme colors.
  115. inspector->update_category_list();
  116. _update_shortcuts();
  117. } break;
  118. }
  119. }
  120. void EditorSettingsDialog::unhandled_input(const Ref<InputEvent> &p_event) {
  121. ERR_FAIL_COND(p_event.is_null());
  122. const Ref<InputEventKey> k = p_event;
  123. if (k.is_valid() && k->is_pressed()) {
  124. bool handled = false;
  125. if (ED_IS_SHORTCUT("ui_undo", p_event)) {
  126. String action = undo_redo->get_current_action_name();
  127. if (action != "") {
  128. EditorNode::get_log()->add_message("Undo: " + action, EditorLog::MSG_TYPE_EDITOR);
  129. }
  130. undo_redo->undo();
  131. handled = true;
  132. }
  133. if (ED_IS_SHORTCUT("ui_redo", p_event)) {
  134. undo_redo->redo();
  135. String action = undo_redo->get_current_action_name();
  136. if (action != "") {
  137. EditorNode::get_log()->add_message("Redo: " + action, EditorLog::MSG_TYPE_EDITOR);
  138. }
  139. handled = true;
  140. }
  141. if (k->get_keycode_with_modifiers() == (KEY_MASK_CMD | KEY_F)) {
  142. _focus_current_search_box();
  143. handled = true;
  144. }
  145. if (handled) {
  146. set_input_as_handled();
  147. }
  148. }
  149. }
  150. void EditorSettingsDialog::_update_icons() {
  151. search_box->set_right_icon(shortcuts->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  152. search_box->set_clear_button_enabled(true);
  153. shortcut_search_box->set_right_icon(shortcuts->get_theme_icon(SNAME("Search"), SNAME("EditorIcons")));
  154. shortcut_search_box->set_clear_button_enabled(true);
  155. restart_close_button->set_icon(shortcuts->get_theme_icon(SNAME("Close"), SNAME("EditorIcons")));
  156. restart_container->add_theme_style_override("panel", shortcuts->get_theme_stylebox(SNAME("bg"), SNAME("Tree")));
  157. restart_icon->set_texture(shortcuts->get_theme_icon(SNAME("StatusWarning"), SNAME("EditorIcons")));
  158. restart_label->add_theme_color_override("font_color", shortcuts->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  159. }
  160. void EditorSettingsDialog::_event_config_confirmed() {
  161. Ref<InputEventKey> k = shortcut_editor->get_event();
  162. if (k.is_null()) {
  163. return;
  164. }
  165. if (current_event_index == -1) {
  166. // Add new event
  167. current_events.push_back(k);
  168. } else {
  169. // Edit existing event
  170. current_events[current_event_index] = k;
  171. }
  172. if (is_editing_action) {
  173. _update_builtin_action(current_edited_identifier, current_events);
  174. } else {
  175. _update_shortcut_events(current_edited_identifier, current_events);
  176. }
  177. }
  178. void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Array &p_events) {
  179. Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(p_name);
  180. undo_redo->create_action(TTR("Edit Built-in Action") + " '" + p_name + "'");
  181. undo_redo->add_do_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, p_events);
  182. undo_redo->add_undo_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, old_input_array);
  183. undo_redo->add_do_method(this, "_settings_changed");
  184. undo_redo->add_undo_method(this, "_settings_changed");
  185. undo_redo->commit_action();
  186. _update_shortcuts();
  187. }
  188. void EditorSettingsDialog::_update_shortcut_events(const String &p_path, const Array &p_events) {
  189. Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  190. undo_redo->create_action(TTR("Edit Shortcut") + " '" + p_path + "'");
  191. undo_redo->add_do_method(current_sc.ptr(), "set_events", p_events);
  192. undo_redo->add_undo_method(current_sc.ptr(), "set_events", current_sc->get_events());
  193. undo_redo->add_do_method(this, "_update_shortcuts");
  194. undo_redo->add_undo_method(this, "_update_shortcuts");
  195. undo_redo->add_do_method(this, "_settings_changed");
  196. undo_redo->add_undo_method(this, "_settings_changed");
  197. undo_redo->commit_action();
  198. }
  199. Array EditorSettingsDialog::_event_list_to_array_helper(List<Ref<InputEvent>> &p_events) {
  200. Array events;
  201. // Convert the list to an array, and only keep key events as this is for the editor.
  202. for (List<Ref<InputEvent>>::Element *E = p_events.front(); E; E = E->next()) {
  203. Ref<InputEventKey> k = E->get();
  204. if (k.is_valid()) {
  205. events.append(E->get());
  206. }
  207. }
  208. return events;
  209. }
  210. 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) {
  211. TreeItem *shortcut_item = shortcuts->create_item(p_parent);
  212. shortcut_item->set_collapsed(p_is_collapsed);
  213. shortcut_item->set_text(0, p_display);
  214. Ref<InputEvent> primary = p_events.size() > 0 ? Ref<InputEvent>(p_events[0]) : Ref<InputEvent>();
  215. Ref<InputEvent> secondary = p_events.size() > 1 ? Ref<InputEvent>(p_events[1]) : Ref<InputEvent>();
  216. String sc_text = "None";
  217. if (primary.is_valid()) {
  218. sc_text = primary->as_text();
  219. if (secondary.is_valid()) {
  220. sc_text += ", " + secondary->as_text();
  221. if (p_events.size() > 2) {
  222. sc_text += " (+" + itos(p_events.size() - 2) + ")";
  223. }
  224. }
  225. }
  226. shortcut_item->set_text(1, sc_text);
  227. if (sc_text == "None") {
  228. // Fade out unassigned shortcut labels for easier visual grepping.
  229. shortcut_item->set_custom_color(1, shortcuts->get_theme_color("font_color", "Label") * Color(1, 1, 1, 0.5));
  230. }
  231. if (p_allow_revert) {
  232. shortcut_item->add_button(1, shortcuts->get_theme_icon("Reload", "EditorIcons"), SHORTCUT_REVERT);
  233. }
  234. shortcut_item->add_button(1, shortcuts->get_theme_icon("Add", "EditorIcons"), SHORTCUT_ADD);
  235. shortcut_item->add_button(1, shortcuts->get_theme_icon("Close", "EditorIcons"), SHORTCUT_ERASE);
  236. shortcut_item->set_meta("is_action", p_is_action);
  237. shortcut_item->set_meta("type", "shortcut");
  238. shortcut_item->set_meta("shortcut_identifier", p_shortcut_identifier);
  239. shortcut_item->set_meta("events", p_events);
  240. // Shortcut Input Events
  241. for (int i = 0; i < p_events.size(); i++) {
  242. Ref<InputEvent> ie = p_events[i];
  243. if (ie.is_null()) {
  244. continue;
  245. }
  246. TreeItem *event_item = shortcuts->create_item(shortcut_item);
  247. event_item->set_text(0, shortcut_item->get_child_count() == 1 ? "Primary" : "");
  248. event_item->set_text(1, ie->as_text());
  249. event_item->add_button(1, shortcuts->get_theme_icon("Edit", "EditorIcons"), SHORTCUT_EDIT);
  250. event_item->add_button(1, shortcuts->get_theme_icon("Close", "EditorIcons"), SHORTCUT_ERASE);
  251. event_item->set_custom_bg_color(0, shortcuts->get_theme_color("dark_color_3", "Editor"));
  252. event_item->set_custom_bg_color(1, shortcuts->get_theme_color("dark_color_3", "Editor"));
  253. event_item->set_meta("is_action", p_is_action);
  254. event_item->set_meta("type", "event");
  255. event_item->set_meta("event_index", i);
  256. }
  257. }
  258. void EditorSettingsDialog::_update_shortcuts() {
  259. // Before clearing the tree, take note of which categories are collapsed so that this state can be maintained when the tree is repopulated.
  260. Map<String, bool> collapsed;
  261. if (shortcuts->get_root() && shortcuts->get_root()->get_first_child()) {
  262. TreeItem *ti = shortcuts->get_root()->get_first_child();
  263. while (ti) {
  264. // 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.
  265. if (ti->get_first_child() && ti->has_meta("shortcut_identifier")) {
  266. collapsed[ti->get_meta("shortcut_identifier")] = ti->is_collapsed();
  267. } else {
  268. collapsed[ti->get_text(0)] = ti->is_collapsed();
  269. }
  270. // Try go down tree
  271. TreeItem *ti_next = ti->get_first_child();
  272. // Try go across tree
  273. if (!ti_next) {
  274. ti_next = ti->get_next();
  275. }
  276. // Try go up tree, to next node
  277. if (!ti_next) {
  278. ti_next = ti->get_parent()->get_next();
  279. }
  280. ti = ti_next;
  281. }
  282. }
  283. shortcuts->clear();
  284. TreeItem *root = shortcuts->create_item();
  285. Map<String, TreeItem *> sections;
  286. // Set up section for Common/Built-in actions
  287. TreeItem *common_section = shortcuts->create_item(root);
  288. sections["Common"] = common_section;
  289. common_section->set_text(0, TTR("Common"));
  290. common_section->set_selectable(0, false);
  291. common_section->set_selectable(1, false);
  292. if (collapsed.has("Common")) {
  293. common_section->set_collapsed(collapsed["Common"]);
  294. }
  295. common_section->set_custom_bg_color(0, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  296. common_section->set_custom_bg_color(1, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  297. // Get the action map for the editor, and add each item to the "Common" section.
  298. OrderedHashMap<StringName, InputMap::Action> action_map = InputMap::get_singleton()->get_action_map();
  299. for (OrderedHashMap<StringName, InputMap::Action>::Element E = action_map.front(); E; E = E.next()) {
  300. String action_name = E.key();
  301. InputMap::Action action = E.get();
  302. Array events; // Need to get the list of events into an array so it can be set as metadata on the item.
  303. Vector<String> event_strings;
  304. List<Ref<InputEvent>> all_default_events = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(action_name).value();
  305. List<Ref<InputEventKey>> key_default_events;
  306. // Remove all non-key events from the defaults. Only check keys, since we are in the editor.
  307. for (List<Ref<InputEvent>>::Element *I = all_default_events.front(); I; I = I->next()) {
  308. Ref<InputEventKey> k = I->get();
  309. if (k.is_valid()) {
  310. key_default_events.push_back(k);
  311. }
  312. }
  313. // Join the text of the events with a delimiter so they can all be displayed in one cell.
  314. String events_display_string = event_strings.is_empty() ? "None" : String("; ").join(event_strings);
  315. if (!shortcut_filter.is_subsequence_ofi(action_name) && (events_display_string == "None" || !shortcut_filter.is_subsequence_ofi(events_display_string))) {
  316. continue;
  317. }
  318. Array action_events = _event_list_to_array_helper(action.inputs);
  319. Array default_events = _event_list_to_array_helper(all_default_events);
  320. bool same_as_defaults = Shortcut::is_event_array_equal(default_events, action_events);
  321. bool collapse = !collapsed.has(action_name) || (collapsed.has(action_name) && collapsed[action_name]);
  322. _create_shortcut_treeitem(common_section, action_name, action_name, action_events, !same_as_defaults, true, collapse);
  323. }
  324. // Editor Shortcuts
  325. List<String> slist;
  326. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  327. for (const String &E : slist) {
  328. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E);
  329. if (!sc->has_meta("original")) {
  330. continue;
  331. }
  332. // Shortcut Section
  333. TreeItem *section;
  334. String section_name = E.get_slice("/", 0);
  335. if (sections.has(section_name)) {
  336. section = sections[section_name];
  337. } else {
  338. section = shortcuts->create_item(root);
  339. String item_name = section_name.capitalize();
  340. section->set_text(0, item_name);
  341. section->set_selectable(0, false);
  342. section->set_selectable(1, false);
  343. section->set_custom_bg_color(0, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  344. section->set_custom_bg_color(1, shortcuts->get_theme_color(SNAME("prop_subsection"), SNAME("Editor")));
  345. if (collapsed.has(item_name)) {
  346. section->set_collapsed(collapsed[item_name]);
  347. }
  348. sections[section_name] = section;
  349. }
  350. // Shortcut Item
  351. if (!shortcut_filter.is_subsequence_ofi(sc->get_name())) {
  352. continue;
  353. }
  354. Array original = sc->get_meta("original");
  355. Array shortcuts_array = sc->get_events().duplicate(true);
  356. bool same_as_defaults = Shortcut::is_event_array_equal(original, shortcuts_array);
  357. bool collapse = !collapsed.has(E) || (collapsed.has(E) && collapsed[E]);
  358. _create_shortcut_treeitem(section, E, sc->get_name(), shortcuts_array, !same_as_defaults, false, collapse);
  359. }
  360. // remove sections with no shortcuts
  361. for (KeyValue<String, TreeItem *> &E : sections) {
  362. TreeItem *section = E.value;
  363. if (section->get_first_child() == nullptr) {
  364. root->remove_child(section);
  365. }
  366. }
  367. }
  368. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) {
  369. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  370. ERR_FAIL_COND_MSG(!ti, "Object passed is not a TreeItem");
  371. ShortcutButton button_idx = (ShortcutButton)p_idx;
  372. is_editing_action = ti->get_meta("is_action");
  373. String type = ti->get_meta("type");
  374. if (type == "event") {
  375. current_edited_identifier = ti->get_parent()->get_meta("shortcut_identifier");
  376. current_events = ti->get_parent()->get_meta("events");
  377. current_event_index = ti->get_meta("event_index");
  378. } else { // Type is "shortcut"
  379. current_edited_identifier = ti->get_meta("shortcut_identifier");
  380. current_events = ti->get_meta("events");
  381. current_event_index = -1;
  382. }
  383. switch (button_idx) {
  384. case EditorSettingsDialog::SHORTCUT_ADD: {
  385. // Only for "shortcut" types
  386. shortcut_editor->popup_and_configure();
  387. } break;
  388. case EditorSettingsDialog::SHORTCUT_EDIT: {
  389. // Only for "event" types
  390. shortcut_editor->popup_and_configure(current_events[current_event_index]);
  391. } break;
  392. case EditorSettingsDialog::SHORTCUT_ERASE: {
  393. if (type == "shortcut") {
  394. if (is_editing_action) {
  395. _update_builtin_action(current_edited_identifier, Array());
  396. } else {
  397. _update_shortcut_events(current_edited_identifier, Array());
  398. }
  399. } else if (type == "event") {
  400. current_events.remove(current_event_index);
  401. if (is_editing_action) {
  402. _update_builtin_action(current_edited_identifier, current_events);
  403. } else {
  404. _update_shortcut_events(current_edited_identifier, current_events);
  405. }
  406. }
  407. } break;
  408. case EditorSettingsDialog::SHORTCUT_REVERT: {
  409. // Only for "shortcut" types
  410. if (is_editing_action) {
  411. List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_edited_identifier];
  412. Array events = _event_list_to_array_helper(defaults);
  413. _update_builtin_action(current_edited_identifier, events);
  414. } else {
  415. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(current_edited_identifier);
  416. Array original = sc->get_meta("original");
  417. _update_shortcut_events(current_edited_identifier, original);
  418. }
  419. } break;
  420. default:
  421. break;
  422. }
  423. }
  424. Variant EditorSettingsDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  425. TreeItem *selected = shortcuts->get_selected();
  426. // Only allow drag for events
  427. if (!selected || !selected->has_meta("type") || selected->get_meta("type") != "event") {
  428. return Variant();
  429. }
  430. String label_text = "Event " + itos(selected->get_meta("event_index"));
  431. Label *label = memnew(Label(label_text));
  432. label->set_modulate(Color(1, 1, 1, 1.0f));
  433. shortcuts->set_drag_preview(label);
  434. shortcuts->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  435. return Dictionary(); // No data required
  436. }
  437. bool EditorSettingsDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  438. TreeItem *selected = shortcuts->get_selected();
  439. TreeItem *item = shortcuts->get_item_at_position(p_point);
  440. if (!selected || !item || item == selected || !item->has_meta("type") || item->get_meta("type") != "event") {
  441. return false;
  442. }
  443. // Don't allow moving an events in-between shortcuts.
  444. if (selected->get_parent()->get_meta("shortcut_identifier") != item->get_parent()->get_meta("shortcut_identifier")) {
  445. return false;
  446. }
  447. return true;
  448. }
  449. void EditorSettingsDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  450. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  451. return;
  452. }
  453. TreeItem *selected = shortcuts->get_selected();
  454. TreeItem *target = shortcuts->get_item_at_position(p_point);
  455. if (!target) {
  456. return;
  457. }
  458. int target_event_index = target->get_meta("event_index");
  459. int index_moving_from = selected->get_meta("event_index");
  460. Array events = selected->get_parent()->get_meta("events");
  461. Variant event_moved = events[index_moving_from];
  462. events.remove(index_moving_from);
  463. events.insert(target_event_index, event_moved);
  464. String ident = selected->get_parent()->get_meta("shortcut_identifier");
  465. if (selected->get_meta("is_action")) {
  466. _update_builtin_action(ident, events);
  467. } else {
  468. _update_shortcut_events(ident, events);
  469. }
  470. }
  471. void EditorSettingsDialog::_tabs_tab_changed(int p_tab) {
  472. _focus_current_search_box();
  473. }
  474. void EditorSettingsDialog::_focus_current_search_box() {
  475. Control *tab = tabs->get_current_tab_control();
  476. LineEdit *current_search_box = nullptr;
  477. if (tab == tab_general) {
  478. current_search_box = search_box;
  479. } else if (tab == tab_shortcuts) {
  480. current_search_box = shortcut_search_box;
  481. }
  482. if (current_search_box) {
  483. current_search_box->grab_focus();
  484. current_search_box->select_all();
  485. }
  486. }
  487. void EditorSettingsDialog::_editor_restart() {
  488. EditorNode::get_singleton()->save_all_scenes();
  489. EditorNode::get_singleton()->restart_editor();
  490. }
  491. void EditorSettingsDialog::_editor_restart_request() {
  492. restart_container->show();
  493. }
  494. void EditorSettingsDialog::_editor_restart_close() {
  495. restart_container->hide();
  496. }
  497. void EditorSettingsDialog::_bind_methods() {
  498. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  499. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  500. ClassDB::bind_method(D_METHOD("_get_drag_data_fw"), &EditorSettingsDialog::get_drag_data_fw);
  501. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &EditorSettingsDialog::can_drop_data_fw);
  502. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &EditorSettingsDialog::drop_data_fw);
  503. }
  504. EditorSettingsDialog::EditorSettingsDialog() {
  505. set_title(TTR("Editor Settings"));
  506. undo_redo = memnew(UndoRedo);
  507. tabs = memnew(TabContainer);
  508. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  509. tabs->connect("tab_changed", callable_mp(this, &EditorSettingsDialog::_tabs_tab_changed));
  510. add_child(tabs);
  511. // General Tab
  512. tab_general = memnew(VBoxContainer);
  513. tabs->add_child(tab_general);
  514. tab_general->set_name(TTR("General"));
  515. HBoxContainer *hbc = memnew(HBoxContainer);
  516. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  517. tab_general->add_child(hbc);
  518. search_box = memnew(LineEdit);
  519. search_box->set_placeholder(TTR("Search"));
  520. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  521. hbc->add_child(search_box);
  522. inspector = memnew(SectionedInspector);
  523. inspector->get_inspector()->set_use_filter(true);
  524. inspector->register_search_box(search_box);
  525. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  526. inspector->get_inspector()->set_undo_redo(undo_redo);
  527. tab_general->add_child(inspector);
  528. inspector->get_inspector()->connect("property_edited", callable_mp(this, &EditorSettingsDialog::_settings_property_edited));
  529. inspector->get_inspector()->connect("restart_requested", callable_mp(this, &EditorSettingsDialog::_editor_restart_request));
  530. restart_container = memnew(PanelContainer);
  531. tab_general->add_child(restart_container);
  532. HBoxContainer *restart_hb = memnew(HBoxContainer);
  533. restart_container->add_child(restart_hb);
  534. restart_icon = memnew(TextureRect);
  535. restart_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  536. restart_hb->add_child(restart_icon);
  537. restart_label = memnew(Label);
  538. restart_label->set_text(TTR("The editor must be restarted for changes to take effect."));
  539. restart_hb->add_child(restart_label);
  540. restart_hb->add_spacer();
  541. Button *restart_button = memnew(Button);
  542. restart_button->connect("pressed", callable_mp(this, &EditorSettingsDialog::_editor_restart));
  543. restart_hb->add_child(restart_button);
  544. restart_button->set_text(TTR("Save & Restart"));
  545. restart_close_button = memnew(Button);
  546. restart_close_button->set_flat(true);
  547. restart_close_button->connect("pressed", callable_mp(this, &EditorSettingsDialog::_editor_restart_close));
  548. restart_hb->add_child(restart_close_button);
  549. restart_container->hide();
  550. // Shortcuts Tab
  551. tab_shortcuts = memnew(VBoxContainer);
  552. tabs->add_child(tab_shortcuts);
  553. tab_shortcuts->set_name(TTR("Shortcuts"));
  554. shortcut_search_box = memnew(LineEdit);
  555. shortcut_search_box->set_placeholder(TTR("Search"));
  556. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  557. tab_shortcuts->add_child(shortcut_search_box);
  558. shortcut_search_box->connect("text_changed", callable_mp(this, &EditorSettingsDialog::_filter_shortcuts));
  559. shortcuts = memnew(Tree);
  560. shortcuts->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  561. shortcuts->set_columns(2);
  562. shortcuts->set_hide_root(true);
  563. shortcuts->set_column_titles_visible(true);
  564. shortcuts->set_column_title(0, TTR("Name"));
  565. shortcuts->set_column_title(1, TTR("Binding"));
  566. shortcuts->connect("button_pressed", callable_mp(this, &EditorSettingsDialog::_shortcut_button_pressed));
  567. tab_shortcuts->add_child(shortcuts);
  568. shortcuts->set_drag_forwarding(this);
  569. // Adding event dialog
  570. shortcut_editor = memnew(InputEventConfigurationDialog);
  571. shortcut_editor->connect("confirmed", callable_mp(this, &EditorSettingsDialog::_event_config_confirmed));
  572. shortcut_editor->set_allowed_input_types(InputEventConfigurationDialog::InputType::INPUT_KEY);
  573. add_child(shortcut_editor);
  574. set_hide_on_ok(true);
  575. timer = memnew(Timer);
  576. timer->set_wait_time(1.5);
  577. timer->connect("timeout", callable_mp(this, &EditorSettingsDialog::_settings_save));
  578. timer->set_one_shot(true);
  579. add_child(timer);
  580. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorSettingsDialog::_settings_changed));
  581. get_ok_button()->set_text(TTR("Close"));
  582. updating = false;
  583. }
  584. EditorSettingsDialog::~EditorSettingsDialog() {
  585. memdelete(undo_redo);
  586. }