settings_config_dialog.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/os/keyboard.h"
  32. #include "core/project_settings.h"
  33. #include "editor_file_system.h"
  34. #include "editor_log.h"
  35. #include "editor_node.h"
  36. #include "editor_scale.h"
  37. #include "editor_settings.h"
  38. #include "scene/gui/margin_container.h"
  39. #include "script_editor_debugger.h"
  40. void EditorSettingsDialog::ok_pressed() {
  41. if (!EditorSettings::get_singleton())
  42. return;
  43. _settings_save();
  44. timer->stop();
  45. }
  46. void EditorSettingsDialog::_settings_changed() {
  47. timer->start();
  48. }
  49. void EditorSettingsDialog::_settings_property_edited(const String &p_name) {
  50. String full_name = inspector->get_full_item_path(p_name);
  51. if (full_name == "interface/theme/accent_color" || full_name == "interface/theme/base_color" || full_name == "interface/theme/contrast") {
  52. EditorSettings::get_singleton()->set_manually("interface/theme/preset", "Custom"); // set preset to Custom
  53. } else if (full_name.begins_with("text_editor/highlighting")) {
  54. EditorSettings::get_singleton()->set_manually("text_editor/theme/color_theme", "Custom");
  55. }
  56. }
  57. void EditorSettingsDialog::_settings_save() {
  58. EditorSettings::get_singleton()->notify_changes();
  59. EditorSettings::get_singleton()->save();
  60. }
  61. void EditorSettingsDialog::cancel_pressed() {
  62. if (!EditorSettings::get_singleton())
  63. return;
  64. EditorSettings::get_singleton()->notify_changes();
  65. }
  66. void EditorSettingsDialog::popup_edit_settings() {
  67. if (!EditorSettings::get_singleton())
  68. return;
  69. EditorSettings::get_singleton()->list_text_editor_themes(); // make sure we have an up to date list of themes
  70. inspector->edit(EditorSettings::get_singleton());
  71. inspector->get_inspector()->update_tree();
  72. search_box->select_all();
  73. search_box->grab_focus();
  74. _update_shortcuts();
  75. set_process_unhandled_input(true);
  76. // Restore valid window bounds or pop up at default size.
  77. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "editor_settings", Rect2());
  78. if (saved_size != Rect2()) {
  79. popup(saved_size);
  80. } else {
  81. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  82. }
  83. _focus_current_search_box();
  84. }
  85. void EditorSettingsDialog::_filter_shortcuts(const String &p_filter) {
  86. shortcut_filter = p_filter;
  87. _update_shortcuts();
  88. }
  89. void EditorSettingsDialog::_undo_redo_callback(void *p_self, const String &p_name) {
  90. EditorNode::get_log()->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  91. }
  92. void EditorSettingsDialog::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_READY: {
  95. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  96. undo_redo->set_method_notify_callback(sed->_method_changeds, sed);
  97. undo_redo->set_property_notify_callback(sed->_property_changeds, sed);
  98. undo_redo->set_commit_notify_callback(_undo_redo_callback, this);
  99. } break;
  100. case NOTIFICATION_ENTER_TREE: {
  101. _update_icons();
  102. } break;
  103. case NOTIFICATION_POPUP_HIDE: {
  104. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "editor_settings", get_rect());
  105. set_process_unhandled_input(false);
  106. } break;
  107. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  108. _update_icons();
  109. // Update theme colors.
  110. inspector->update_category_list();
  111. _update_shortcuts();
  112. } break;
  113. }
  114. }
  115. void EditorSettingsDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  116. const Ref<InputEventKey> k = p_event;
  117. if (k.is_valid() && is_window_modal_on_top() && k->is_pressed()) {
  118. bool handled = false;
  119. if (ED_IS_SHORTCUT("editor/undo", p_event)) {
  120. String action = undo_redo->get_current_action_name();
  121. if (action != "")
  122. EditorNode::get_log()->add_message("Undo: " + action, EditorLog::MSG_TYPE_EDITOR);
  123. undo_redo->undo();
  124. handled = true;
  125. }
  126. if (ED_IS_SHORTCUT("editor/redo", p_event)) {
  127. undo_redo->redo();
  128. String action = undo_redo->get_current_action_name();
  129. if (action != "")
  130. EditorNode::get_log()->add_message("Redo: " + action, EditorLog::MSG_TYPE_EDITOR);
  131. handled = true;
  132. }
  133. if (k->get_scancode_with_modifiers() == (KEY_MASK_CMD | KEY_F)) {
  134. _focus_current_search_box();
  135. handled = true;
  136. }
  137. if (handled) {
  138. accept_event();
  139. }
  140. }
  141. }
  142. void EditorSettingsDialog::_update_icons() {
  143. search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  144. search_box->set_clear_button_enabled(true);
  145. shortcut_search_box->set_right_icon(get_icon("Search", "EditorIcons"));
  146. shortcut_search_box->set_clear_button_enabled(true);
  147. restart_close_button->set_icon(get_icon("Close", "EditorIcons"));
  148. restart_container->add_style_override("panel", get_stylebox("bg", "Tree"));
  149. restart_icon->set_texture(get_icon("StatusWarning", "EditorIcons"));
  150. restart_label->add_color_override("font_color", get_color("warning_color", "Editor"));
  151. }
  152. void EditorSettingsDialog::_update_shortcuts() {
  153. Map<String, bool> collapsed;
  154. if (shortcuts->get_root() && shortcuts->get_root()->get_children()) {
  155. for (TreeItem *item = shortcuts->get_root()->get_children(); item; item = item->get_next()) {
  156. collapsed[item->get_text(0)] = item->is_collapsed();
  157. }
  158. }
  159. shortcuts->clear();
  160. List<String> slist;
  161. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  162. TreeItem *root = shortcuts->create_item();
  163. Map<String, TreeItem *> sections;
  164. for (List<String>::Element *E = slist.front(); E; E = E->next()) {
  165. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(E->get());
  166. if (!sc->has_meta("original"))
  167. continue;
  168. Ref<InputEvent> original = sc->get_meta("original");
  169. String section_name = E->get().get_slice("/", 0);
  170. TreeItem *section;
  171. if (sections.has(section_name)) {
  172. section = sections[section_name];
  173. } else {
  174. section = shortcuts->create_item(root);
  175. String item_name = section_name.capitalize();
  176. section->set_text(0, item_name);
  177. if (collapsed.has(item_name)) {
  178. section->set_collapsed(collapsed[item_name]);
  179. }
  180. sections[section_name] = section;
  181. section->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  182. section->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  183. }
  184. // Don't match unassigned shortcuts when searching for assigned keys in search results.
  185. // This prevents all unassigned shortcuts from appearing when searching a string like "no".
  186. if (shortcut_filter.is_subsequence_ofi(sc->get_name()) || (sc->get_as_text() != "None" && shortcut_filter.is_subsequence_ofi(sc->get_as_text()))) {
  187. TreeItem *item = shortcuts->create_item(section);
  188. item->set_text(0, sc->get_name());
  189. item->set_text(1, sc->get_as_text());
  190. if (!sc->is_shortcut(original) && !(sc->get_shortcut().is_null() && original.is_null())) {
  191. item->add_button(1, get_icon("Reload", "EditorIcons"), 2);
  192. }
  193. if (sc->get_as_text() == "None") {
  194. // Fade out unassigned shortcut labels for easier visual grepping.
  195. item->set_custom_color(1, get_color("font_color", "Label") * Color(1, 1, 1, 0.5));
  196. }
  197. item->add_button(1, get_icon("Edit", "EditorIcons"), 0);
  198. item->add_button(1, get_icon("Close", "EditorIcons"), 1);
  199. item->set_tooltip(0, E->get());
  200. item->set_metadata(0, E->get());
  201. }
  202. }
  203. // remove sections with no shortcuts
  204. for (Map<String, TreeItem *>::Element *E = sections.front(); E; E = E->next()) {
  205. TreeItem *section = E->get();
  206. if (section->get_children() == NULL) {
  207. root->remove_child(section);
  208. }
  209. }
  210. }
  211. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) {
  212. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  213. ERR_FAIL_COND(!ti);
  214. String item = ti->get_metadata(0);
  215. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(item);
  216. if (p_idx == 0) {
  217. press_a_key_label->set_text(TTR("Press a Key..."));
  218. last_wait_for_key = Ref<InputEventKey>();
  219. press_a_key->popup_centered(Size2(250, 80) * EDSCALE);
  220. press_a_key->grab_focus();
  221. press_a_key->get_ok()->set_focus_mode(FOCUS_NONE);
  222. press_a_key->get_cancel()->set_focus_mode(FOCUS_NONE);
  223. shortcut_configured = item;
  224. } else if (p_idx == 1) { //erase
  225. if (!sc.is_valid())
  226. return; //pointless, there is nothing
  227. undo_redo->create_action(TTR("Erase Shortcut"));
  228. undo_redo->add_do_method(sc.ptr(), "set_shortcut", Ref<InputEvent>());
  229. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  230. undo_redo->add_do_method(this, "_update_shortcuts");
  231. undo_redo->add_undo_method(this, "_update_shortcuts");
  232. undo_redo->add_do_method(this, "_settings_changed");
  233. undo_redo->add_undo_method(this, "_settings_changed");
  234. undo_redo->commit_action();
  235. } else if (p_idx == 2) { //revert to original
  236. if (!sc.is_valid())
  237. return; //pointless, there is nothing
  238. Ref<InputEvent> original = sc->get_meta("original");
  239. undo_redo->create_action(TTR("Restore Shortcut"));
  240. undo_redo->add_do_method(sc.ptr(), "set_shortcut", original);
  241. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  242. undo_redo->add_do_method(this, "_update_shortcuts");
  243. undo_redo->add_undo_method(this, "_update_shortcuts");
  244. undo_redo->add_do_method(this, "_settings_changed");
  245. undo_redo->add_undo_method(this, "_settings_changed");
  246. undo_redo->commit_action();
  247. }
  248. }
  249. void EditorSettingsDialog::_wait_for_key(const Ref<InputEvent> &p_event) {
  250. Ref<InputEventKey> k = p_event;
  251. if (k.is_valid() && k->is_pressed() && k->get_scancode() != 0) {
  252. last_wait_for_key = k;
  253. const String str = keycode_get_string(k->get_scancode_with_modifiers());
  254. press_a_key_label->set_text(str);
  255. press_a_key->accept_event();
  256. }
  257. }
  258. void EditorSettingsDialog::_press_a_key_confirm() {
  259. if (last_wait_for_key.is_null())
  260. return;
  261. Ref<InputEventKey> ie;
  262. ie.instance();
  263. ie->set_scancode(last_wait_for_key->get_scancode());
  264. ie->set_shift(last_wait_for_key->get_shift());
  265. ie->set_control(last_wait_for_key->get_control());
  266. ie->set_alt(last_wait_for_key->get_alt());
  267. ie->set_metakey(last_wait_for_key->get_metakey());
  268. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(shortcut_configured);
  269. undo_redo->create_action(TTR("Change Shortcut") + " '" + shortcut_configured + "'");
  270. undo_redo->add_do_method(sc.ptr(), "set_shortcut", ie);
  271. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  272. undo_redo->add_do_method(this, "_update_shortcuts");
  273. undo_redo->add_undo_method(this, "_update_shortcuts");
  274. undo_redo->add_do_method(this, "_settings_changed");
  275. undo_redo->add_undo_method(this, "_settings_changed");
  276. undo_redo->commit_action();
  277. }
  278. void EditorSettingsDialog::_tabs_tab_changed(int p_tab) {
  279. _focus_current_search_box();
  280. }
  281. void EditorSettingsDialog::_focus_current_search_box() {
  282. Control *tab = tabs->get_current_tab_control();
  283. LineEdit *current_search_box = NULL;
  284. if (tab == tab_general)
  285. current_search_box = search_box;
  286. else if (tab == tab_shortcuts)
  287. current_search_box = shortcut_search_box;
  288. if (current_search_box) {
  289. current_search_box->grab_focus();
  290. current_search_box->select_all();
  291. }
  292. }
  293. void EditorSettingsDialog::_editor_restart() {
  294. EditorNode::get_singleton()->save_all_scenes();
  295. EditorNode::get_singleton()->restart_editor();
  296. }
  297. void EditorSettingsDialog::_editor_restart_request() {
  298. restart_container->show();
  299. }
  300. void EditorSettingsDialog::_editor_restart_close() {
  301. restart_container->hide();
  302. }
  303. void EditorSettingsDialog::_bind_methods() {
  304. ClassDB::bind_method(D_METHOD("_unhandled_input"), &EditorSettingsDialog::_unhandled_input);
  305. ClassDB::bind_method(D_METHOD("_settings_save"), &EditorSettingsDialog::_settings_save);
  306. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  307. ClassDB::bind_method(D_METHOD("_settings_property_edited"), &EditorSettingsDialog::_settings_property_edited);
  308. ClassDB::bind_method(D_METHOD("_shortcut_button_pressed"), &EditorSettingsDialog::_shortcut_button_pressed);
  309. ClassDB::bind_method(D_METHOD("_filter_shortcuts"), &EditorSettingsDialog::_filter_shortcuts);
  310. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  311. ClassDB::bind_method(D_METHOD("_press_a_key_confirm"), &EditorSettingsDialog::_press_a_key_confirm);
  312. ClassDB::bind_method(D_METHOD("_wait_for_key"), &EditorSettingsDialog::_wait_for_key);
  313. ClassDB::bind_method(D_METHOD("_tabs_tab_changed"), &EditorSettingsDialog::_tabs_tab_changed);
  314. ClassDB::bind_method(D_METHOD("_editor_restart_request"), &EditorSettingsDialog::_editor_restart_request);
  315. ClassDB::bind_method(D_METHOD("_editor_restart"), &EditorSettingsDialog::_editor_restart);
  316. ClassDB::bind_method(D_METHOD("_editor_restart_close"), &EditorSettingsDialog::_editor_restart_close);
  317. }
  318. EditorSettingsDialog::EditorSettingsDialog() {
  319. set_title(TTR("Editor Settings"));
  320. set_resizable(true);
  321. undo_redo = memnew(UndoRedo);
  322. tabs = memnew(TabContainer);
  323. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  324. tabs->connect("tab_changed", this, "_tabs_tab_changed");
  325. add_child(tabs);
  326. // General Tab
  327. tab_general = memnew(VBoxContainer);
  328. tabs->add_child(tab_general);
  329. tab_general->set_name(TTR("General"));
  330. HBoxContainer *hbc = memnew(HBoxContainer);
  331. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  332. tab_general->add_child(hbc);
  333. search_box = memnew(LineEdit);
  334. search_box->set_placeholder(TTR("Search"));
  335. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  336. hbc->add_child(search_box);
  337. inspector = memnew(SectionedInspector);
  338. inspector->get_inspector()->set_use_filter(true);
  339. inspector->register_search_box(search_box);
  340. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  341. inspector->get_inspector()->set_undo_redo(undo_redo);
  342. tab_general->add_child(inspector);
  343. inspector->get_inspector()->connect("property_edited", this, "_settings_property_edited");
  344. inspector->get_inspector()->connect("restart_requested", this, "_editor_restart_request");
  345. restart_container = memnew(PanelContainer);
  346. tab_general->add_child(restart_container);
  347. HBoxContainer *restart_hb = memnew(HBoxContainer);
  348. restart_container->add_child(restart_hb);
  349. restart_icon = memnew(TextureRect);
  350. restart_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
  351. restart_hb->add_child(restart_icon);
  352. restart_label = memnew(Label);
  353. restart_label->set_text(TTR("The editor must be restarted for changes to take effect."));
  354. restart_hb->add_child(restart_label);
  355. restart_hb->add_spacer();
  356. Button *restart_button = memnew(Button);
  357. restart_button->connect("pressed", this, "_editor_restart");
  358. restart_hb->add_child(restart_button);
  359. restart_button->set_text(TTR("Save & Restart"));
  360. restart_close_button = memnew(ToolButton);
  361. restart_close_button->connect("pressed", this, "_editor_restart_close");
  362. restart_hb->add_child(restart_close_button);
  363. restart_container->hide();
  364. // Shortcuts Tab
  365. tab_shortcuts = memnew(VBoxContainer);
  366. tabs->add_child(tab_shortcuts);
  367. tab_shortcuts->set_name(TTR("Shortcuts"));
  368. hbc = memnew(HBoxContainer);
  369. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  370. tab_shortcuts->add_child(hbc);
  371. shortcut_search_box = memnew(LineEdit);
  372. shortcut_search_box->set_placeholder(TTR("Search"));
  373. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  374. hbc->add_child(shortcut_search_box);
  375. shortcut_search_box->connect("text_changed", this, "_filter_shortcuts");
  376. shortcuts = memnew(Tree);
  377. tab_shortcuts->add_child(shortcuts, true);
  378. shortcuts->set_v_size_flags(SIZE_EXPAND_FILL);
  379. shortcuts->set_columns(2);
  380. shortcuts->set_hide_root(true);
  381. shortcuts->set_column_titles_visible(true);
  382. shortcuts->set_column_title(0, TTR("Name"));
  383. shortcuts->set_column_title(1, TTR("Binding"));
  384. shortcuts->connect("button_pressed", this, "_shortcut_button_pressed");
  385. press_a_key = memnew(ConfirmationDialog);
  386. press_a_key->set_focus_mode(FOCUS_ALL);
  387. add_child(press_a_key);
  388. Label *l = memnew(Label);
  389. l->set_text(TTR("Press a Key..."));
  390. l->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  391. l->set_align(Label::ALIGN_CENTER);
  392. l->set_margin(MARGIN_TOP, 20);
  393. l->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 30);
  394. press_a_key_label = l;
  395. press_a_key->add_child(l);
  396. press_a_key->connect("gui_input", this, "_wait_for_key");
  397. press_a_key->connect("confirmed", this, "_press_a_key_confirm");
  398. set_hide_on_ok(true);
  399. timer = memnew(Timer);
  400. timer->set_wait_time(1.5);
  401. timer->connect("timeout", this, "_settings_save");
  402. timer->set_one_shot(true);
  403. add_child(timer);
  404. EditorSettings::get_singleton()->connect("settings_changed", this, "_settings_changed");
  405. get_ok()->set_text(TTR("Close"));
  406. updating = false;
  407. }
  408. EditorSettingsDialog::~EditorSettingsDialog() {
  409. memdelete(undo_redo);
  410. }