settings_config_dialog.cpp 18 KB

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