settings_config_dialog.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor_file_system.h"
  32. #include "editor_node.h"
  33. #include "editor_settings.h"
  34. #include "os/keyboard.h"
  35. #include "project_settings.h"
  36. #include "scene/gui/margin_container.h"
  37. void EditorSettingsDialog::ok_pressed() {
  38. if (!EditorSettings::get_singleton())
  39. return;
  40. _settings_save();
  41. timer->stop();
  42. }
  43. void EditorSettingsDialog::_settings_changed() {
  44. timer->start();
  45. }
  46. void EditorSettingsDialog::_settings_property_edited(const String &p_name) {
  47. String full_name = property_editor->get_full_item_path(p_name);
  48. // Small usability workaround to update the text color settings when the
  49. // color theme is changed
  50. if (full_name == "text_editor/theme/color_theme") {
  51. property_editor->get_property_editor()->update_tree();
  52. }
  53. }
  54. void EditorSettingsDialog::_settings_save() {
  55. EditorSettings::get_singleton()->notify_changes();
  56. EditorSettings::get_singleton()->save();
  57. }
  58. void EditorSettingsDialog::cancel_pressed() {
  59. if (!EditorSettings::get_singleton())
  60. return;
  61. EditorSettings::get_singleton()->notify_changes();
  62. }
  63. void EditorSettingsDialog::popup_edit_settings() {
  64. if (!EditorSettings::get_singleton())
  65. return;
  66. EditorSettings::get_singleton()->list_text_editor_themes(); // make sure we have an up to date list of themes
  67. property_editor->edit(EditorSettings::get_singleton());
  68. property_editor->get_property_editor()->update_tree();
  69. search_box->select_all();
  70. search_box->grab_focus();
  71. _update_shortcuts();
  72. // Restore valid window bounds or pop up at default size.
  73. if (EditorSettings::get_singleton()->has("interface/dialogs/editor_settings_bounds")) {
  74. popup(EditorSettings::get_singleton()->get("interface/dialogs/editor_settings_bounds"));
  75. } else {
  76. popup_centered_ratio(0.7);
  77. }
  78. }
  79. void EditorSettingsDialog::_clear_search_box() {
  80. if (search_box->get_text() == "")
  81. return;
  82. search_box->clear();
  83. property_editor->get_property_editor()->update_tree();
  84. }
  85. void EditorSettingsDialog::_clear_shortcut_search_box() {
  86. if (shortcut_search_box->get_text() == "")
  87. return;
  88. shortcut_search_box->clear();
  89. }
  90. void EditorSettingsDialog::_filter_shortcuts(const String &p_filter) {
  91. shortcut_filter = p_filter;
  92. _update_shortcuts();
  93. }
  94. void EditorSettingsDialog::_notification(int p_what) {
  95. switch (p_what) {
  96. case NOTIFICATION_ENTER_TREE: {
  97. clear_button->set_icon(get_icon("Close", "EditorIcons"));
  98. shortcut_clear_button->set_icon(get_icon("Close", "EditorIcons"));
  99. } break;
  100. case NOTIFICATION_POPUP_HIDE: {
  101. EditorSettings::get_singleton()->set("interface/dialogs/editor_settings_bounds", get_rect());
  102. } break;
  103. }
  104. }
  105. void EditorSettingsDialog::_update_shortcuts() {
  106. shortcuts->clear();
  107. List<String> slist;
  108. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  109. TreeItem *root = shortcuts->create_item();
  110. Map<String, TreeItem *> sections;
  111. for (List<String>::Element *E = slist.front(); E; E = E->next()) {
  112. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(E->get());
  113. if (!sc->has_meta("original"))
  114. continue;
  115. Ref<InputEvent> original = sc->get_meta("original");
  116. String section_name = E->get().get_slice("/", 0);
  117. TreeItem *section;
  118. if (sections.has(section_name)) {
  119. section = sections[section_name];
  120. } else {
  121. section = shortcuts->create_item(root);
  122. section->set_text(0, section_name.capitalize());
  123. sections[section_name] = section;
  124. section->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  125. section->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  126. }
  127. if (shortcut_filter.is_subsequence_ofi(sc->get_name())) {
  128. TreeItem *item = shortcuts->create_item(section);
  129. item->set_text(0, sc->get_name());
  130. item->set_text(1, sc->get_as_text());
  131. if (!sc->is_shortcut(original) && !(sc->get_shortcut().is_null() && original.is_null())) {
  132. item->add_button(1, get_icon("Reload", "EditorIcons"), 2);
  133. }
  134. item->add_button(1, get_icon("Edit", "EditorIcons"), 0);
  135. item->add_button(1, get_icon("Close", "EditorIcons"), 1);
  136. item->set_tooltip(0, E->get());
  137. item->set_metadata(0, E->get());
  138. }
  139. }
  140. // remove sections with no shortcuts
  141. for (Map<String, TreeItem *>::Element *E = sections.front(); E; E = E->next()) {
  142. TreeItem *section = E->get();
  143. if (section->get_children() == NULL) {
  144. root->remove_child(section);
  145. }
  146. }
  147. }
  148. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) {
  149. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  150. ERR_FAIL_COND(!ti);
  151. String item = ti->get_metadata(0);
  152. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(item);
  153. if (p_idx == 0) {
  154. press_a_key_label->set_text(TTR("Press a Key.."));
  155. last_wait_for_key = Ref<InputEventKey>();
  156. press_a_key->popup_centered(Size2(250, 80) * EDSCALE);
  157. press_a_key->grab_focus();
  158. press_a_key->get_ok()->set_focus_mode(FOCUS_NONE);
  159. press_a_key->get_cancel()->set_focus_mode(FOCUS_NONE);
  160. shortcut_configured = item;
  161. } else if (p_idx == 1) { //erase
  162. if (!sc.is_valid())
  163. return; //pointless, there is nothing
  164. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  165. ur->create_action("Erase Shortcut");
  166. ur->add_do_method(sc.ptr(), "set_shortcut", Ref<InputEvent>());
  167. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  168. ur->add_do_method(this, "_update_shortcuts");
  169. ur->add_undo_method(this, "_update_shortcuts");
  170. ur->add_do_method(this, "_settings_changed");
  171. ur->add_undo_method(this, "_settings_changed");
  172. ur->commit_action();
  173. } else if (p_idx == 2) { //revert to original
  174. if (!sc.is_valid())
  175. return; //pointless, there is nothing
  176. Ref<InputEvent> original = sc->get_meta("original");
  177. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  178. ur->create_action("Restore Shortcut");
  179. ur->add_do_method(sc.ptr(), "set_shortcut", original);
  180. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  181. ur->add_do_method(this, "_update_shortcuts");
  182. ur->add_undo_method(this, "_update_shortcuts");
  183. ur->add_do_method(this, "_settings_changed");
  184. ur->add_undo_method(this, "_settings_changed");
  185. ur->commit_action();
  186. }
  187. }
  188. void EditorSettingsDialog::_wait_for_key(const Ref<InputEvent> &p_event) {
  189. Ref<InputEventKey> k = p_event;
  190. if (k.is_valid() && k->is_pressed() && k->get_scancode() != 0) {
  191. last_wait_for_key = k;
  192. String str = keycode_get_string(k->get_scancode()).capitalize();
  193. if (k->get_metakey())
  194. str = TTR("Meta+") + str;
  195. if (k->get_shift())
  196. str = TTR("Shift+") + str;
  197. if (k->get_alt())
  198. str = TTR("Alt+") + str;
  199. if (k->get_control())
  200. str = TTR("Control+") + str;
  201. press_a_key_label->set_text(str);
  202. press_a_key->accept_event();
  203. }
  204. }
  205. void EditorSettingsDialog::_press_a_key_confirm() {
  206. if (last_wait_for_key.is_null())
  207. return;
  208. Ref<InputEventKey> ie;
  209. ie.instance();
  210. ie->set_scancode(last_wait_for_key->get_scancode());
  211. ie->set_shift(last_wait_for_key->get_shift());
  212. ie->set_control(last_wait_for_key->get_control());
  213. ie->set_alt(last_wait_for_key->get_alt());
  214. ie->set_metakey(last_wait_for_key->get_metakey());
  215. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(shortcut_configured);
  216. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  217. ur->create_action("Change Shortcut '" + shortcut_configured + "'");
  218. ur->add_do_method(sc.ptr(), "set_shortcut", ie);
  219. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  220. ur->add_do_method(this, "_update_shortcuts");
  221. ur->add_undo_method(this, "_update_shortcuts");
  222. ur->add_do_method(this, "_settings_changed");
  223. ur->add_undo_method(this, "_settings_changed");
  224. ur->commit_action();
  225. }
  226. void EditorSettingsDialog::_bind_methods() {
  227. ClassDB::bind_method(D_METHOD("_settings_save"), &EditorSettingsDialog::_settings_save);
  228. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  229. ClassDB::bind_method(D_METHOD("_settings_property_edited"), &EditorSettingsDialog::_settings_property_edited);
  230. ClassDB::bind_method(D_METHOD("_clear_search_box"), &EditorSettingsDialog::_clear_search_box);
  231. ClassDB::bind_method(D_METHOD("_clear_shortcut_search_box"), &EditorSettingsDialog::_clear_shortcut_search_box);
  232. ClassDB::bind_method(D_METHOD("_shortcut_button_pressed"), &EditorSettingsDialog::_shortcut_button_pressed);
  233. ClassDB::bind_method(D_METHOD("_filter_shortcuts"), &EditorSettingsDialog::_filter_shortcuts);
  234. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  235. ClassDB::bind_method(D_METHOD("_press_a_key_confirm"), &EditorSettingsDialog::_press_a_key_confirm);
  236. ClassDB::bind_method(D_METHOD("_wait_for_key"), &EditorSettingsDialog::_wait_for_key);
  237. }
  238. EditorSettingsDialog::EditorSettingsDialog() {
  239. set_title(TTR("Editor Settings"));
  240. set_resizable(true);
  241. tabs = memnew(TabContainer);
  242. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  243. add_child(tabs);
  244. //set_child_rect(tabs);
  245. VBoxContainer *vbc = memnew(VBoxContainer);
  246. tabs->add_child(vbc);
  247. vbc->set_name(TTR("General"));
  248. HBoxContainer *hbc = memnew(HBoxContainer);
  249. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  250. vbc->add_child(hbc);
  251. Label *l = memnew(Label);
  252. l->set_text(TTR("Search:") + " ");
  253. hbc->add_child(l);
  254. search_box = memnew(LineEdit);
  255. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  256. hbc->add_child(search_box);
  257. clear_button = memnew(ToolButton);
  258. hbc->add_child(clear_button);
  259. clear_button->connect("pressed", this, "_clear_search_box");
  260. property_editor = memnew(SectionedPropertyEditor);
  261. //property_editor->hide_top_label();
  262. property_editor->get_property_editor()->set_use_filter(true);
  263. property_editor->register_search_box(search_box);
  264. property_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  265. vbc->add_child(property_editor);
  266. property_editor->get_property_editor()->connect("property_edited", this, "_settings_property_edited");
  267. vbc = memnew(VBoxContainer);
  268. tabs->add_child(vbc);
  269. vbc->set_name(TTR("Shortcuts"));
  270. hbc = memnew(HBoxContainer);
  271. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  272. vbc->add_child(hbc);
  273. l = memnew(Label);
  274. l->set_text(TTR("Search:") + " ");
  275. hbc->add_child(l);
  276. shortcut_search_box = memnew(LineEdit);
  277. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  278. hbc->add_child(shortcut_search_box);
  279. shortcut_search_box->connect("text_changed", this, "_filter_shortcuts");
  280. shortcut_clear_button = memnew(ToolButton);
  281. hbc->add_child(shortcut_clear_button);
  282. shortcut_clear_button->connect("pressed", this, "_clear_shortcut_search_box");
  283. shortcuts = memnew(Tree);
  284. vbc->add_margin_child("Shortcut List:", shortcuts, true);
  285. shortcuts->set_columns(2);
  286. shortcuts->set_hide_root(true);
  287. //shortcuts->set_hide_folding(true);
  288. shortcuts->set_column_titles_visible(true);
  289. shortcuts->set_column_title(0, "Name");
  290. shortcuts->set_column_title(1, "Binding");
  291. shortcuts->connect("button_pressed", this, "_shortcut_button_pressed");
  292. press_a_key = memnew(ConfirmationDialog);
  293. press_a_key->set_focus_mode(FOCUS_ALL);
  294. add_child(press_a_key);
  295. l = memnew(Label);
  296. l->set_text(TTR("Press a Key.."));
  297. l->set_area_as_parent_rect();
  298. l->set_align(Label::ALIGN_CENTER);
  299. l->set_margin(MARGIN_TOP, 20);
  300. l->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 30);
  301. press_a_key_label = l;
  302. press_a_key->add_child(l);
  303. press_a_key->connect("gui_input", this, "_wait_for_key");
  304. press_a_key->connect("confirmed", this, "_press_a_key_confirm");
  305. //get_ok()->set_text("Apply");
  306. set_hide_on_ok(true);
  307. //get_cancel()->set_text("Close");
  308. timer = memnew(Timer);
  309. timer->set_wait_time(1.5);
  310. timer->connect("timeout", this, "_settings_save");
  311. timer->set_one_shot(true);
  312. add_child(timer);
  313. EditorSettings::get_singleton()->connect("settings_changed", this, "_settings_changed");
  314. get_ok()->set_text(TTR("Close"));
  315. updating = false;
  316. }