settings_config_dialog.cpp 19 KB

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