settings_config_dialog.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #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 = property_editor->get_full_item_path(p_name);
  49. // Small usability workaround to update the text color settings when the
  50. // color theme is changed
  51. if (full_name == "text_editor/theme/color_theme") {
  52. property_editor->get_property_editor()->update_tree();
  53. } else 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", 6); // set preset to 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. property_editor->edit(EditorSettings::get_singleton());
  71. property_editor->get_property_editor()->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. if (EditorSettings::get_singleton()->has_setting("interface/dialogs/editor_settings_bounds")) {
  78. popup(EditorSettings::get_singleton()->get("interface/dialogs/editor_settings_bounds"));
  79. } else {
  80. popup_centered_ratio(0.7);
  81. }
  82. }
  83. void EditorSettingsDialog::_clear_search_box() {
  84. if (search_box->get_text() == "")
  85. return;
  86. search_box->clear();
  87. property_editor->get_property_editor()->update_tree();
  88. }
  89. void EditorSettingsDialog::_clear_shortcut_search_box() {
  90. if (shortcut_search_box->get_text() == "")
  91. return;
  92. shortcut_search_box->clear();
  93. }
  94. void EditorSettingsDialog::_filter_shortcuts(const String &p_filter) {
  95. shortcut_filter = p_filter;
  96. _update_shortcuts();
  97. }
  98. void EditorSettingsDialog::_undo_redo_callback(void *p_self, const String &p_name) {
  99. EditorNode::get_log()->add_message(p_name);
  100. }
  101. void EditorSettingsDialog::_notification(int p_what) {
  102. switch (p_what) {
  103. case NOTIFICATION_READY: {
  104. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  105. undo_redo->set_method_notify_callback(sed->_method_changeds, sed);
  106. undo_redo->set_property_notify_callback(sed->_property_changeds, sed);
  107. undo_redo->set_commit_notify_callback(_undo_redo_callback, this);
  108. } break;
  109. case NOTIFICATION_ENTER_TREE: {
  110. clear_button->set_icon(get_icon("Close", "EditorIcons"));
  111. shortcut_clear_button->set_icon(get_icon("Close", "EditorIcons"));
  112. } break;
  113. case NOTIFICATION_POPUP_HIDE: {
  114. EditorSettings::get_singleton()->set("interface/dialogs/editor_settings_bounds", get_rect());
  115. set_process_unhandled_input(false);
  116. } break;
  117. }
  118. }
  119. void EditorSettingsDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  120. Ref<InputEventKey> k = p_event;
  121. if (k.is_valid() && is_window_modal_on_top()) {
  122. if (k->is_pressed()) {
  123. bool handled = false;
  124. if (ED_IS_SHORTCUT("editor/undo", p_event)) {
  125. String action = undo_redo->get_current_action_name();
  126. if (action != "")
  127. EditorNode::get_log()->add_message("UNDO: " + action);
  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);
  136. handled = true;
  137. }
  138. if (handled) {
  139. accept_event();
  140. }
  141. }
  142. }
  143. }
  144. void EditorSettingsDialog::_update_shortcuts() {
  145. shortcuts->clear();
  146. List<String> slist;
  147. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  148. TreeItem *root = shortcuts->create_item();
  149. Map<String, TreeItem *> sections;
  150. for (List<String>::Element *E = slist.front(); E; E = E->next()) {
  151. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(E->get());
  152. if (!sc->has_meta("original"))
  153. continue;
  154. Ref<InputEvent> original = sc->get_meta("original");
  155. String section_name = E->get().get_slice("/", 0);
  156. TreeItem *section;
  157. if (sections.has(section_name)) {
  158. section = sections[section_name];
  159. } else {
  160. section = shortcuts->create_item(root);
  161. section->set_text(0, section_name.capitalize());
  162. sections[section_name] = section;
  163. section->set_custom_bg_color(0, get_color("prop_subsection", "Editor"));
  164. section->set_custom_bg_color(1, get_color("prop_subsection", "Editor"));
  165. }
  166. if (shortcut_filter.is_subsequence_ofi(sc->get_name()) || shortcut_filter.is_subsequence_ofi(sc->get_as_text())) {
  167. TreeItem *item = shortcuts->create_item(section);
  168. item->set_text(0, sc->get_name());
  169. item->set_text(1, sc->get_as_text());
  170. if (!sc->is_shortcut(original) && !(sc->get_shortcut().is_null() && original.is_null())) {
  171. item->add_button(1, get_icon("Reload", "EditorIcons"), 2);
  172. }
  173. item->add_button(1, get_icon("Edit", "EditorIcons"), 0);
  174. item->add_button(1, get_icon("Close", "EditorIcons"), 1);
  175. item->set_tooltip(0, E->get());
  176. item->set_metadata(0, E->get());
  177. }
  178. }
  179. // remove sections with no shortcuts
  180. for (Map<String, TreeItem *>::Element *E = sections.front(); E; E = E->next()) {
  181. TreeItem *section = E->get();
  182. if (section->get_children() == NULL) {
  183. root->remove_child(section);
  184. }
  185. }
  186. }
  187. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx) {
  188. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  189. ERR_FAIL_COND(!ti);
  190. String item = ti->get_metadata(0);
  191. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(item);
  192. if (p_idx == 0) {
  193. press_a_key_label->set_text(TTR("Press a Key.."));
  194. last_wait_for_key = Ref<InputEventKey>();
  195. press_a_key->popup_centered(Size2(250, 80) * EDSCALE);
  196. press_a_key->grab_focus();
  197. press_a_key->get_ok()->set_focus_mode(FOCUS_NONE);
  198. press_a_key->get_cancel()->set_focus_mode(FOCUS_NONE);
  199. shortcut_configured = item;
  200. } else if (p_idx == 1) { //erase
  201. if (!sc.is_valid())
  202. return; //pointless, there is nothing
  203. undo_redo->create_action("Erase Shortcut");
  204. undo_redo->add_do_method(sc.ptr(), "set_shortcut", Ref<InputEvent>());
  205. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  206. undo_redo->add_do_method(this, "_update_shortcuts");
  207. undo_redo->add_undo_method(this, "_update_shortcuts");
  208. undo_redo->add_do_method(this, "_settings_changed");
  209. undo_redo->add_undo_method(this, "_settings_changed");
  210. undo_redo->commit_action();
  211. } else if (p_idx == 2) { //revert to original
  212. if (!sc.is_valid())
  213. return; //pointless, there is nothing
  214. Ref<InputEvent> original = sc->get_meta("original");
  215. undo_redo->create_action("Restore Shortcut");
  216. undo_redo->add_do_method(sc.ptr(), "set_shortcut", original);
  217. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  218. undo_redo->add_do_method(this, "_update_shortcuts");
  219. undo_redo->add_undo_method(this, "_update_shortcuts");
  220. undo_redo->add_do_method(this, "_settings_changed");
  221. undo_redo->add_undo_method(this, "_settings_changed");
  222. undo_redo->commit_action();
  223. }
  224. }
  225. void EditorSettingsDialog::_wait_for_key(const Ref<InputEvent> &p_event) {
  226. Ref<InputEventKey> k = p_event;
  227. if (k.is_valid() && k->is_pressed() && k->get_scancode() != 0) {
  228. last_wait_for_key = k;
  229. String str = keycode_get_string(k->get_scancode()).capitalize();
  230. if (k->get_metakey())
  231. str = TTR("Meta+") + str;
  232. if (k->get_shift())
  233. str = TTR("Shift+") + str;
  234. if (k->get_alt())
  235. str = TTR("Alt+") + str;
  236. if (k->get_control())
  237. str = TTR("Control+") + str;
  238. press_a_key_label->set_text(str);
  239. press_a_key->accept_event();
  240. }
  241. }
  242. void EditorSettingsDialog::_press_a_key_confirm() {
  243. if (last_wait_for_key.is_null())
  244. return;
  245. Ref<InputEventKey> ie;
  246. ie.instance();
  247. ie->set_scancode(last_wait_for_key->get_scancode());
  248. ie->set_shift(last_wait_for_key->get_shift());
  249. ie->set_control(last_wait_for_key->get_control());
  250. ie->set_alt(last_wait_for_key->get_alt());
  251. ie->set_metakey(last_wait_for_key->get_metakey());
  252. Ref<ShortCut> sc = EditorSettings::get_singleton()->get_shortcut(shortcut_configured);
  253. undo_redo->create_action("Change Shortcut '" + shortcut_configured + "'");
  254. undo_redo->add_do_method(sc.ptr(), "set_shortcut", ie);
  255. undo_redo->add_undo_method(sc.ptr(), "set_shortcut", sc->get_shortcut());
  256. undo_redo->add_do_method(this, "_update_shortcuts");
  257. undo_redo->add_undo_method(this, "_update_shortcuts");
  258. undo_redo->add_do_method(this, "_settings_changed");
  259. undo_redo->add_undo_method(this, "_settings_changed");
  260. undo_redo->commit_action();
  261. }
  262. void EditorSettingsDialog::_bind_methods() {
  263. ClassDB::bind_method(D_METHOD("_unhandled_input"), &EditorSettingsDialog::_unhandled_input);
  264. ClassDB::bind_method(D_METHOD("_settings_save"), &EditorSettingsDialog::_settings_save);
  265. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  266. ClassDB::bind_method(D_METHOD("_settings_property_edited"), &EditorSettingsDialog::_settings_property_edited);
  267. ClassDB::bind_method(D_METHOD("_clear_search_box"), &EditorSettingsDialog::_clear_search_box);
  268. ClassDB::bind_method(D_METHOD("_clear_shortcut_search_box"), &EditorSettingsDialog::_clear_shortcut_search_box);
  269. ClassDB::bind_method(D_METHOD("_shortcut_button_pressed"), &EditorSettingsDialog::_shortcut_button_pressed);
  270. ClassDB::bind_method(D_METHOD("_filter_shortcuts"), &EditorSettingsDialog::_filter_shortcuts);
  271. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  272. ClassDB::bind_method(D_METHOD("_press_a_key_confirm"), &EditorSettingsDialog::_press_a_key_confirm);
  273. ClassDB::bind_method(D_METHOD("_wait_for_key"), &EditorSettingsDialog::_wait_for_key);
  274. }
  275. EditorSettingsDialog::EditorSettingsDialog() {
  276. set_title(TTR("Editor Settings"));
  277. set_resizable(true);
  278. undo_redo = memnew(UndoRedo);
  279. tabs = memnew(TabContainer);
  280. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  281. add_child(tabs);
  282. //set_child_rect(tabs);
  283. VBoxContainer *vbc = memnew(VBoxContainer);
  284. tabs->add_child(vbc);
  285. vbc->set_name(TTR("General"));
  286. HBoxContainer *hbc = memnew(HBoxContainer);
  287. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  288. vbc->add_child(hbc);
  289. Label *l = memnew(Label);
  290. l->set_text(TTR("Search:") + " ");
  291. hbc->add_child(l);
  292. search_box = memnew(LineEdit);
  293. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  294. hbc->add_child(search_box);
  295. clear_button = memnew(ToolButton);
  296. hbc->add_child(clear_button);
  297. clear_button->connect("pressed", this, "_clear_search_box");
  298. property_editor = memnew(SectionedPropertyEditor);
  299. //property_editor->hide_top_label();
  300. property_editor->get_property_editor()->set_use_filter(true);
  301. property_editor->register_search_box(search_box);
  302. property_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  303. property_editor->get_property_editor()->set_undo_redo(undo_redo);
  304. vbc->add_child(property_editor);
  305. property_editor->get_property_editor()->connect("property_edited", this, "_settings_property_edited");
  306. vbc = memnew(VBoxContainer);
  307. tabs->add_child(vbc);
  308. vbc->set_name(TTR("Shortcuts"));
  309. hbc = memnew(HBoxContainer);
  310. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  311. vbc->add_child(hbc);
  312. l = memnew(Label);
  313. l->set_text(TTR("Search:") + " ");
  314. hbc->add_child(l);
  315. shortcut_search_box = memnew(LineEdit);
  316. shortcut_search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  317. hbc->add_child(shortcut_search_box);
  318. shortcut_search_box->connect("text_changed", this, "_filter_shortcuts");
  319. shortcut_clear_button = memnew(ToolButton);
  320. hbc->add_child(shortcut_clear_button);
  321. shortcut_clear_button->connect("pressed", this, "_clear_shortcut_search_box");
  322. shortcuts = memnew(Tree);
  323. vbc->add_margin_child("Shortcut List:", shortcuts, true);
  324. shortcuts->set_columns(2);
  325. shortcuts->set_hide_root(true);
  326. //shortcuts->set_hide_folding(true);
  327. shortcuts->set_column_titles_visible(true);
  328. shortcuts->set_column_title(0, "Name");
  329. shortcuts->set_column_title(1, "Binding");
  330. shortcuts->connect("button_pressed", this, "_shortcut_button_pressed");
  331. press_a_key = memnew(ConfirmationDialog);
  332. press_a_key->set_focus_mode(FOCUS_ALL);
  333. add_child(press_a_key);
  334. l = memnew(Label);
  335. l->set_text(TTR("Press a Key.."));
  336. l->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  337. l->set_align(Label::ALIGN_CENTER);
  338. l->set_margin(MARGIN_TOP, 20);
  339. l->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_BEGIN, 30);
  340. press_a_key_label = l;
  341. press_a_key->add_child(l);
  342. press_a_key->connect("gui_input", this, "_wait_for_key");
  343. press_a_key->connect("confirmed", this, "_press_a_key_confirm");
  344. //get_ok()->set_text("Apply");
  345. set_hide_on_ok(true);
  346. //get_cancel()->set_text("Close");
  347. timer = memnew(Timer);
  348. timer->set_wait_time(1.5);
  349. timer->connect("timeout", this, "_settings_save");
  350. timer->set_one_shot(true);
  351. add_child(timer);
  352. EditorSettings::get_singleton()->connect("settings_changed", this, "_settings_changed");
  353. get_ok()->set_text(TTR("Close"));
  354. updating = false;
  355. }
  356. EditorSettingsDialog::~EditorSettingsDialog() {
  357. memdelete(undo_redo);
  358. }