settings_config_dialog.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "globals.h"
  35. #include "os/keyboard.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/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. popup_centered_ratio(0.7);
  73. }
  74. void EditorSettingsDialog::_clear_search_box() {
  75. if (search_box->get_text() == "")
  76. return;
  77. search_box->clear();
  78. property_editor->get_property_editor()->update_tree();
  79. }
  80. void EditorSettingsDialog::_clear_shortcut_search_box() {
  81. if (shortcut_search_box->get_text() == "")
  82. return;
  83. shortcut_search_box->clear();
  84. }
  85. void EditorSettingsDialog::_filter_shortcuts(const String &p_filter) {
  86. shortcut_filter = p_filter;
  87. _update_shortcuts();
  88. }
  89. void EditorSettingsDialog::_notification(int p_what) {
  90. if (p_what == NOTIFICATION_ENTER_TREE) {
  91. clear_button->set_icon(get_icon("Close", "EditorIcons"));
  92. shortcut_clear_button->set_icon(get_icon("Close", "EditorIcons"));
  93. }
  94. }
  95. void EditorSettingsDialog::_update_shortcuts() {
  96. shortcuts->clear();
  97. List<String> slist;
  98. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  99. TreeItem *root = shortcuts->create_item();
  100. Map<String, TreeItem *> sections;
  101. for (List<String>::Element *E = slist.front(); E; E = E->next()) {
  102. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(E->get());
  103. if (!sc->has_meta("original"))
  104. continue;
  105. InputEvent original = sc->get_meta("original");
  106. String section_name = E->get().get_slice("/", 0);
  107. TreeItem *section;
  108. if (sections.has(section_name)) {
  109. section = sections[section_name];
  110. } else {
  111. section = shortcuts->create_item(root);
  112. section->set_text(0, section_name.capitalize());
  113. sections[section_name] = section;
  114. section->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  115. section->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  116. }
  117. if (shortcut_filter.is_subsequence_ofi(sc->get_name())) {
  118. TreeItem *item = shortcuts->create_item(section);
  119. item->set_text(0, sc->get_name());
  120. item->set_text(1, sc->get_as_text());
  121. if (!sc->is_shortcut(original) && !(sc->get_shortcut().type == InputEvent::NONE && original.type == InputEvent::NONE)) {
  122. item->add_button(1, get_icon("Reload", "EditorIcons"), 2);
  123. }
  124. item->add_button(1, get_icon("Edit", "EditorIcons"), 0);
  125. item->add_button(1, get_icon("Close", "EditorIcons"), 1);
  126. item->set_tooltip(0, E->get());
  127. item->set_metadata(0, E->get());
  128. }
  129. }
  130. // remove sections with no shortcuts
  131. for (Map<String, TreeItem *>::Element *E = sections.front(); E; E = E->next()) {
  132. TreeItem *section = E->get();
  133. if (section->get_children() == NULL) {
  134. root->remove_child(section);
  135. }
  136. }
  137. }
  138. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) {
  139. TreeItem *ti = p_item->cast_to<TreeItem>();
  140. ERR_FAIL_COND(!ti);
  141. String item = ti->get_metadata(0);
  142. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(item);
  143. if (p_idx == 0) {
  144. press_a_key_label->set_text(TTR("Press a Key.."));
  145. last_wait_for_key = InputEvent();
  146. press_a_key->popup_centered(Size2(250, 80) * EDSCALE);
  147. press_a_key->grab_focus();
  148. press_a_key->get_ok()->set_focus_mode(FOCUS_NONE);
  149. press_a_key->get_cancel()->set_focus_mode(FOCUS_NONE);
  150. shortcut_configured = item;
  151. } else if (p_idx == 1) { //erase
  152. if (!sc.is_valid())
  153. return; //pointless, there is nothing
  154. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  155. ur->create_action("Erase Shortcut");
  156. ur->add_do_method(sc.ptr(), "set_shortcut", InputEvent());
  157. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  158. ur->add_do_method(this, "_update_shortcuts");
  159. ur->add_undo_method(this, "_update_shortcuts");
  160. ur->add_do_method(this, "_settings_changed");
  161. ur->add_undo_method(this, "_settings_changed");
  162. ur->commit_action();
  163. } else if (p_idx == 2) { //revert to original
  164. if (!sc.is_valid())
  165. return; //pointless, there is nothing
  166. InputEvent original = sc->get_meta("original");
  167. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  168. ur->create_action("Restore Shortcut");
  169. ur->add_do_method(sc.ptr(), "set_shortcut", original);
  170. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  171. ur->add_do_method(this, "_update_shortcuts");
  172. ur->add_undo_method(this, "_update_shortcuts");
  173. ur->add_do_method(this, "_settings_changed");
  174. ur->add_undo_method(this, "_settings_changed");
  175. ur->commit_action();
  176. }
  177. }
  178. void EditorSettingsDialog::_wait_for_key(const InputEvent &p_event) {
  179. if (p_event.type == InputEvent::KEY && p_event.key.pressed && p_event.key.scancode != 0) {
  180. last_wait_for_key = p_event;
  181. String str = keycode_get_string(p_event.key.scancode).capitalize();
  182. if (p_event.key.mod.meta)
  183. str = TTR("Meta+") + str;
  184. if (p_event.key.mod.shift)
  185. str = TTR("Shift+") + str;
  186. if (p_event.key.mod.alt)
  187. str = TTR("Alt+") + str;
  188. if (p_event.key.mod.control)
  189. str = TTR("Control+") + str;
  190. press_a_key_label->set_text(str);
  191. press_a_key->accept_event();
  192. }
  193. }
  194. void EditorSettingsDialog::_press_a_key_confirm() {
  195. if (last_wait_for_key.type != InputEvent::KEY)
  196. return;
  197. InputEvent ie;
  198. ie.type = InputEvent::KEY;
  199. ie.key.scancode = last_wait_for_key.key.scancode;
  200. ie.key.mod = last_wait_for_key.key.mod;
  201. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(shortcut_configured);
  202. UndoRedo *ur = EditorNode::get_singleton()->get_undo_redo();
  203. ur->create_action("Change Shortcut '" + shortcut_configured + "'");
  204. ur->add_do_method(sc.ptr(), "set_shortcut", ie);
  205. ur->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  206. ur->add_do_method(this, "_update_shortcuts");
  207. ur->add_undo_method(this, "_update_shortcuts");
  208. ur->add_do_method(this, "_settings_changed");
  209. ur->add_undo_method(this, "_settings_changed");
  210. ur->commit_action();
  211. }
  212. void EditorSettingsDialog::_bind_methods() {
  213. ObjectTypeDB::bind_method(_MD("_settings_save"), &EditorSettingsDialog::_settings_save);
  214. ObjectTypeDB::bind_method(_MD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  215. ObjectTypeDB::bind_method(_MD("_settings_property_edited"), &EditorSettingsDialog::_settings_property_edited);
  216. ObjectTypeDB::bind_method(_MD("_clear_search_box"), &EditorSettingsDialog::_clear_search_box);
  217. ObjectTypeDB::bind_method(_MD("_clear_shortcut_search_box"), &EditorSettingsDialog::_clear_shortcut_search_box);
  218. ObjectTypeDB::bind_method(_MD("_shortcut_button_pressed"), &EditorSettingsDialog::_shortcut_button_pressed);
  219. ObjectTypeDB::bind_method(_MD("_filter_shortcuts"), &EditorSettingsDialog::_filter_shortcuts);
  220. ObjectTypeDB::bind_method(_MD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  221. ObjectTypeDB::bind_method(_MD("_press_a_key_confirm"), &EditorSettingsDialog::_press_a_key_confirm);
  222. ObjectTypeDB::bind_method(_MD("_wait_for_key"), &EditorSettingsDialog::_wait_for_key);
  223. }
  224. EditorSettingsDialog::EditorSettingsDialog() {
  225. set_title(TTR("Editor Settings"));
  226. tabs = memnew(TabContainer);
  227. add_child(tabs);
  228. set_child_rect(tabs);
  229. VBoxContainer *vbc = memnew(VBoxContainer);
  230. tabs->add_child(vbc);
  231. vbc->set_name(TTR("General"));
  232. HBoxContainer *hbc = memnew(HBoxContainer);
  233. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  234. vbc->add_child(hbc);
  235. Label *l = memnew(Label);
  236. l->set_text(TTR("Search:") + " ");
  237. hbc->add_child(l);
  238. search_box = memnew(LineEdit);
  239. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  240. hbc->add_child(search_box);
  241. clear_button = memnew(ToolButton);
  242. hbc->add_child(clear_button);
  243. clear_button->connect("pressed", this, "_clear_search_box");
  244. property_editor = memnew(SectionedPropertyEditor);
  245. //property_editor->hide_top_label();
  246. property_editor->get_property_editor()->set_use_filter(true);
  247. property_editor->get_property_editor()->register_text_enter(search_box);
  248. property_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  249. vbc->add_child(property_editor);
  250. property_editor->get_property_editor()->connect("property_edited", this, "_settings_property_edited");
  251. vbc = memnew(VBoxContainer);
  252. tabs->add_child(vbc);
  253. vbc->set_name(TTR("Shortcuts"));
  254. hbc = memnew(HBoxContainer);
  255. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  256. vbc->add_child(hbc);
  257. l = memnew(Label);
  258. l->set_text(TTR("Search:") + " ");
  259. hbc->add_child(l);
  260. shortcut_search_box = memnew(LineEdit);
  261. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  262. hbc->add_child(shortcut_search_box);
  263. shortcut_search_box->connect("text_changed", this, "_filter_shortcuts");
  264. shortcut_clear_button = memnew(ToolButton);
  265. hbc->add_child(shortcut_clear_button);
  266. shortcut_clear_button->connect("pressed", this, "_clear_shortcut_search_box");
  267. shortcuts = memnew(Tree);
  268. vbc->add_margin_child("Shortcut List:", shortcuts, true);
  269. shortcuts->set_columns(2);
  270. shortcuts->set_hide_root(true);
  271. //shortcuts->set_hide_folding(true);
  272. shortcuts->set_column_titles_visible(true);
  273. shortcuts->set_column_title(0, "Name");
  274. shortcuts->set_column_title(1, "Binding");
  275. shortcuts->connect("button_pressed", this, "_shortcut_button_pressed");
  276. press_a_key = memnew(ConfirmationDialog);
  277. press_a_key->set_focus_mode(FOCUS_ALL);
  278. add_child(press_a_key);
  279. l = memnew(Label);
  280. l->set_text(TTR("Press a Key.."));
  281. l->set_area_as_parent_rect();
  282. l->set_align(Label::ALIGN_CENTER);
  283. l->set_margin(MARGIN_TOP, 20);
  284. l->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 30);
  285. press_a_key_label = l;
  286. press_a_key->add_child(l);
  287. press_a_key->connect("input_event", this, "_wait_for_key");
  288. press_a_key->connect("confirmed", this, "_press_a_key_confirm");
  289. //get_ok()->set_text("Apply");
  290. set_hide_on_ok(true);
  291. //get_cancel()->set_text("Close");
  292. timer = memnew(Timer);
  293. timer->set_wait_time(1.5);
  294. timer->connect("timeout", this, "_settings_save");
  295. timer->set_one_shot(true);
  296. add_child(timer);
  297. EditorSettings::get_singleton()->connect("settings_changed", this, "_settings_changed");
  298. get_ok()->set_text(TTR("Close"));
  299. updating = false;
  300. }