editor_settings_dialog.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /**************************************************************************/
  2. /* editor_settings_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 "editor_settings_dialog.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input_map.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/debugger/editor_debugger_node.h"
  35. #include "editor/editor_log.h"
  36. #include "editor/editor_node.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/inspector/editor_property_name_processor.h"
  40. #include "editor/inspector/editor_sectioned_inspector.h"
  41. #include "editor/scene/3d/node_3d_editor_plugin.h"
  42. #include "editor/settings/editor_event_search_bar.h"
  43. #include "editor/settings/editor_settings.h"
  44. #include "editor/settings/event_listener_line_edit.h"
  45. #include "editor/settings/input_event_configuration_dialog.h"
  46. #include "editor/settings/project_settings_editor.h"
  47. #include "editor/themes/editor_scale.h"
  48. #include "editor/themes/editor_theme_manager.h"
  49. #include "scene/gui/check_button.h"
  50. #include "scene/gui/panel_container.h"
  51. #include "scene/gui/tab_container.h"
  52. #include "scene/gui/texture_rect.h"
  53. #include "scene/gui/tree.h"
  54. void EditorSettingsDialog::ok_pressed() {
  55. if (!EditorSettings::get_singleton()) {
  56. return;
  57. }
  58. _settings_save();
  59. }
  60. void EditorSettingsDialog::_settings_changed() {
  61. timer->start();
  62. }
  63. void EditorSettingsDialog::_settings_property_edited(const String &p_name) {
  64. String full_name = inspector->get_full_item_path(p_name);
  65. // Set theme presets to Custom when controlled settings change.
  66. if (full_name == "interface/theme/accent_color" || full_name == "interface/theme/base_color" || full_name == "interface/theme/contrast" || full_name == "interface/theme/draw_extra_borders" || full_name == "interface/theme/icon_saturation" || full_name == "interface/theme/draw_relationship_lines" || full_name == "interface/theme/corner_radius") {
  67. EditorSettings::get_singleton()->set_manually("interface/theme/color_preset", "Custom");
  68. } else if (full_name == "interface/theme/base_spacing" || full_name == "interface/theme/additional_spacing") {
  69. EditorSettings::get_singleton()->set_manually("interface/theme/spacing_preset", "Custom");
  70. } else if (full_name.begins_with("text_editor/theme/highlighting")) {
  71. EditorSettings::get_singleton()->set_manually("text_editor/theme/color_theme", "Custom");
  72. } else if (full_name.begins_with("editors/visual_editors/connection_colors") || full_name.begins_with("editors/visual_editors/category_colors")) {
  73. EditorSettings::get_singleton()->set_manually("editors/visual_editors/color_theme", "Custom");
  74. } else if (full_name == "editors/3d/navigation/orbit_mouse_button" || full_name == "editors/3d/navigation/pan_mouse_button" || full_name == "editors/3d/navigation/zoom_mouse_button" || full_name == "editors/3d/navigation/emulate_3_button_mouse") {
  75. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/navigation_scheme", (int)Node3DEditorViewport::NAVIGATION_CUSTOM);
  76. } else if (full_name == "editors/3d/navigation/navigation_scheme") {
  77. update_navigation_preset();
  78. }
  79. }
  80. void EditorSettingsDialog::update_navigation_preset() {
  81. Node3DEditorViewport::NavigationScheme nav_scheme = (Node3DEditorViewport::NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  82. Node3DEditorViewport::ViewportNavMouseButton set_orbit_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  83. Node3DEditorViewport::ViewportNavMouseButton set_pan_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  84. Node3DEditorViewport::ViewportNavMouseButton set_zoom_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  85. bool set_3_button_mouse = false;
  86. Ref<InputEventKey> orbit_mod_key_1;
  87. Ref<InputEventKey> orbit_mod_key_2;
  88. Ref<InputEventKey> pan_mod_key_1;
  89. Ref<InputEventKey> pan_mod_key_2;
  90. Ref<InputEventKey> zoom_mod_key_1;
  91. Ref<InputEventKey> zoom_mod_key_2;
  92. bool set_preset = false;
  93. if (nav_scheme == Node3DEditorViewport::NAVIGATION_GODOT) {
  94. set_preset = true;
  95. set_orbit_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  96. set_pan_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  97. set_zoom_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  98. set_3_button_mouse = false;
  99. orbit_mod_key_1 = InputEventKey::create_reference(Key::NONE);
  100. orbit_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  101. pan_mod_key_1 = InputEventKey::create_reference(Key::SHIFT);
  102. pan_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  103. zoom_mod_key_1 = InputEventKey::create_reference(Key::CTRL);
  104. zoom_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  105. } else if (nav_scheme == Node3DEditorViewport::NAVIGATION_MAYA) {
  106. set_preset = true;
  107. set_orbit_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  108. set_pan_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  109. set_zoom_mouse_button = Node3DEditorViewport::NAVIGATION_RIGHT_MOUSE;
  110. set_3_button_mouse = false;
  111. orbit_mod_key_1 = InputEventKey::create_reference(Key::ALT);
  112. orbit_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  113. pan_mod_key_1 = InputEventKey::create_reference(Key::NONE);
  114. pan_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  115. zoom_mod_key_1 = InputEventKey::create_reference(Key::ALT);
  116. zoom_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  117. } else if (nav_scheme == Node3DEditorViewport::NAVIGATION_MODO) {
  118. set_preset = true;
  119. set_orbit_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  120. set_pan_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  121. set_zoom_mouse_button = Node3DEditorViewport::NAVIGATION_LEFT_MOUSE;
  122. set_3_button_mouse = false;
  123. orbit_mod_key_1 = InputEventKey::create_reference(Key::ALT);
  124. orbit_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  125. pan_mod_key_1 = InputEventKey::create_reference(Key::SHIFT);
  126. pan_mod_key_2 = InputEventKey::create_reference(Key::ALT);
  127. zoom_mod_key_1 = InputEventKey::create_reference(Key::ALT);
  128. zoom_mod_key_2 = InputEventKey::create_reference(Key::CTRL);
  129. } else if (nav_scheme == Node3DEditorViewport::NAVIGATION_TABLET) {
  130. set_preset = true;
  131. set_orbit_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  132. set_pan_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  133. set_zoom_mouse_button = Node3DEditorViewport::NAVIGATION_MIDDLE_MOUSE;
  134. set_3_button_mouse = true;
  135. orbit_mod_key_1 = InputEventKey::create_reference(Key::ALT);
  136. orbit_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  137. pan_mod_key_1 = InputEventKey::create_reference(Key::SHIFT);
  138. pan_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  139. zoom_mod_key_1 = InputEventKey::create_reference(Key::CTRL);
  140. zoom_mod_key_2 = InputEventKey::create_reference(Key::NONE);
  141. }
  142. // Set settings to the desired preset values.
  143. if (set_preset) {
  144. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/orbit_mouse_button", (int)set_orbit_mouse_button);
  145. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/pan_mouse_button", (int)set_pan_mouse_button);
  146. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/zoom_mouse_button", (int)set_zoom_mouse_button);
  147. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/emulate_3_button_mouse", set_3_button_mouse);
  148. _set_shortcut_input("spatial_editor/viewport_orbit_modifier_1", orbit_mod_key_1);
  149. _set_shortcut_input("spatial_editor/viewport_orbit_modifier_2", orbit_mod_key_2);
  150. _set_shortcut_input("spatial_editor/viewport_pan_modifier_1", pan_mod_key_1);
  151. _set_shortcut_input("spatial_editor/viewport_pan_modifier_2", pan_mod_key_2);
  152. _set_shortcut_input("spatial_editor/viewport_zoom_modifier_1", zoom_mod_key_1);
  153. _set_shortcut_input("spatial_editor/viewport_zoom_modifier_2", zoom_mod_key_2);
  154. }
  155. }
  156. void EditorSettingsDialog::_set_shortcut_input(const String &p_name, Ref<InputEventKey> &p_event) {
  157. Array sc_events;
  158. if (p_event->get_keycode() != Key::NONE) {
  159. sc_events.push_back((Variant)p_event);
  160. }
  161. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(p_name);
  162. sc->set_events(sc_events);
  163. }
  164. void EditorSettingsDialog::_settings_save() {
  165. if (!timer->is_stopped()) {
  166. timer->stop();
  167. }
  168. EditorSettings::get_singleton()->notify_changes();
  169. EditorSettings::get_singleton()->save();
  170. }
  171. void EditorSettingsDialog::cancel_pressed() {
  172. if (!EditorSettings::get_singleton()) {
  173. return;
  174. }
  175. EditorSettings::get_singleton()->notify_changes();
  176. }
  177. void EditorSettingsDialog::popup_edit_settings() {
  178. if (!EditorSettings::get_singleton()) {
  179. return;
  180. }
  181. EditorSettings::get_singleton()->update_text_editor_themes_list(); // Make sure we have an up to date list of themes.
  182. _update_dynamic_property_hints();
  183. inspector->edit(EditorSettings::get_singleton());
  184. inspector->get_inspector()->update_tree();
  185. _update_shortcuts();
  186. set_process_shortcut_input(true);
  187. // Restore valid window bounds or pop up at default size.
  188. Rect2 saved_size;
  189. if (!_is_in_project_manager()) {
  190. saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "editor_settings", Rect2());
  191. }
  192. if (saved_size != Rect2()) {
  193. popup(saved_size);
  194. } else if (_is_in_project_manager()) {
  195. popup_centered_clamped(Size2(800, 600) * EDSCALE, 0.8); // Make it smaller that the default Project Manager size.
  196. } else {
  197. popup_centered_clamped(Size2(900, 700) * EDSCALE, 0.8);
  198. }
  199. _focus_current_search_box();
  200. }
  201. void EditorSettingsDialog::_undo_redo_callback(void *p_self, const String &p_name) {
  202. EditorNode::get_log()->add_message(p_name, EditorLog::MSG_TYPE_EDITOR);
  203. }
  204. void EditorSettingsDialog::_notification(int p_what) {
  205. switch (p_what) {
  206. case NOTIFICATION_VISIBILITY_CHANGED: {
  207. if (!is_visible() && !_is_in_project_manager()) {
  208. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "editor_settings", Rect2(get_position(), get_size()));
  209. set_process_shortcut_input(false);
  210. }
  211. } break;
  212. case NOTIFICATION_READY: {
  213. EditorSettingsPropertyWrapper::restart_request_callback = callable_mp(this, &EditorSettingsDialog::_editor_restart_request);
  214. if (_is_in_project_manager()) {
  215. return;
  216. }
  217. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  218. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_method_notify_callback(EditorDebuggerNode::_methods_changed, nullptr);
  219. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_property_notify_callback(EditorDebuggerNode::_properties_changed, nullptr);
  220. undo_redo->get_or_create_history(EditorUndoRedoManager::GLOBAL_HISTORY).undo_redo->set_commit_notify_callback(_undo_redo_callback, this);
  221. } break;
  222. case NOTIFICATION_ENTER_TREE: {
  223. _update_icons();
  224. } break;
  225. case NOTIFICATION_THEME_CHANGED: {
  226. _update_shortcuts();
  227. } break;
  228. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  229. if (EditorThemeManager::is_generated_theme_outdated()) {
  230. _update_icons();
  231. }
  232. bool update_shortcuts_tab =
  233. EditorSettings::get_singleton()->check_changed_settings_in_group("shortcuts") ||
  234. EditorSettings::get_singleton()->check_changed_settings_in_group("builtin_action_overrides");
  235. if (update_shortcuts_tab) {
  236. _update_shortcuts();
  237. }
  238. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/3d/navigation")) {
  239. // Shortcuts may have changed, so dynamic hint values must update.
  240. _update_dynamic_property_hints();
  241. inspector->get_inspector()->update_tree();
  242. }
  243. if (EditorSettings::get_singleton()->check_changed_settings_in_group("interface/editor/localize_settings")) {
  244. inspector->update_category_list();
  245. }
  246. } break;
  247. }
  248. }
  249. void EditorSettingsDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  250. const Ref<InputEventKey> k = p_event;
  251. if (k.is_valid() && k->is_pressed()) {
  252. bool handled = false;
  253. if (EditorNode::get_singleton()) {
  254. if (ED_IS_SHORTCUT("ui_undo", p_event)) {
  255. EditorNode::get_singleton()->undo();
  256. handled = true;
  257. }
  258. if (ED_IS_SHORTCUT("ui_redo", p_event)) {
  259. EditorNode::get_singleton()->redo();
  260. handled = true;
  261. }
  262. }
  263. if (k->is_match(InputEventKey::create_reference(KeyModifierMask::CMD_OR_CTRL | Key::F))) {
  264. _focus_current_search_box();
  265. handled = true;
  266. }
  267. if (handled) {
  268. set_input_as_handled();
  269. }
  270. }
  271. }
  272. void EditorSettingsDialog::_update_icons() {
  273. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  274. search_box->set_clear_button_enabled(true);
  275. restart_close_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  276. restart_container->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
  277. restart_icon->set_texture(get_editor_theme_icon(SNAME("StatusWarning")));
  278. restart_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  279. }
  280. void EditorSettingsDialog::_event_config_confirmed() {
  281. Ref<InputEventKey> k = shortcut_editor->get_event();
  282. if (k.is_null()) {
  283. return;
  284. }
  285. if (current_event_index == -1) {
  286. // Add new event
  287. current_events.push_back(k);
  288. } else {
  289. // Edit existing event
  290. current_events[current_event_index] = k;
  291. }
  292. if (is_editing_action) {
  293. _update_builtin_action(current_edited_identifier, current_events);
  294. } else {
  295. _update_shortcut_events(current_edited_identifier, current_events);
  296. }
  297. }
  298. bool EditorSettingsDialog::_is_in_project_manager() const {
  299. return !ProjectSettings::get_singleton()->is_project_loaded();
  300. }
  301. void EditorSettingsDialog::_update_builtin_action(const String &p_name, const Array &p_events) {
  302. Array old_input_array = EditorSettings::get_singleton()->get_builtin_action_overrides(p_name);
  303. if (old_input_array.is_empty()) {
  304. List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_edited_identifier];
  305. old_input_array = _event_list_to_array_helper(defaults);
  306. }
  307. if (_is_in_project_manager()) {
  308. // Project Manager doesn't have EditorUndoRedoManager, so apply changes directly.
  309. EditorSettings::get_singleton()->mark_setting_changed("builtin_action_overrides");
  310. EditorSettings::get_singleton()->set_builtin_action_override(p_name, p_events);
  311. _update_shortcuts();
  312. _settings_changed();
  313. } else {
  314. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  315. undo_redo->create_action(vformat(TTR("Edit Built-in Action: %s"), p_name));
  316. undo_redo->add_do_method(EditorSettings::get_singleton(), "mark_setting_changed", "builtin_action_overrides");
  317. undo_redo->add_undo_method(EditorSettings::get_singleton(), "mark_setting_changed", "builtin_action_overrides");
  318. undo_redo->add_do_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, p_events);
  319. undo_redo->add_undo_method(EditorSettings::get_singleton(), "set_builtin_action_override", p_name, old_input_array);
  320. undo_redo->add_do_method(this, "_update_shortcuts");
  321. undo_redo->add_undo_method(this, "_update_shortcuts");
  322. undo_redo->add_do_method(this, "_settings_changed");
  323. undo_redo->add_undo_method(this, "_settings_changed");
  324. undo_redo->commit_action();
  325. }
  326. }
  327. void EditorSettingsDialog::_update_shortcut_events(const String &p_path, const Array &p_events) {
  328. Ref<Shortcut> current_sc = EditorSettings::get_singleton()->get_shortcut(p_path);
  329. if (_is_in_project_manager()) {
  330. // Project Manager doesn't have EditorUndoRedoManager, so apply changes directly.
  331. current_sc->set_events(p_events);
  332. EditorSettings::get_singleton()->mark_setting_changed("shortcuts");
  333. _update_shortcuts();
  334. _settings_changed();
  335. } else {
  336. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  337. undo_redo->create_action(vformat(TTR("Edit Shortcut: %s"), p_path), UndoRedo::MERGE_DISABLE, EditorSettings::get_singleton());
  338. // History must be fixed based on the EditorSettings object because current_sc would
  339. // incorrectly make this action use the scene history.
  340. undo_redo->force_fixed_history();
  341. undo_redo->add_do_method(current_sc.ptr(), "set_events", p_events);
  342. undo_redo->add_undo_method(current_sc.ptr(), "set_events", current_sc->get_events());
  343. undo_redo->add_do_method(EditorSettings::get_singleton(), "mark_setting_changed", "shortcuts");
  344. undo_redo->add_undo_method(EditorSettings::get_singleton(), "mark_setting_changed", "shortcuts");
  345. undo_redo->add_do_method(this, "_update_shortcuts");
  346. undo_redo->add_undo_method(this, "_update_shortcuts");
  347. undo_redo->add_do_method(this, "_settings_changed");
  348. undo_redo->add_undo_method(this, "_settings_changed");
  349. undo_redo->commit_action();
  350. }
  351. bool path_is_orbit_mod = p_path == "spatial_editor/viewport_orbit_modifier_1" || p_path == "spatial_editor/viewport_orbit_modifier_2";
  352. bool path_is_pan_mod = p_path == "spatial_editor/viewport_pan_modifier_1" || p_path == "spatial_editor/viewport_pan_modifier_2";
  353. bool path_is_zoom_mod = p_path == "spatial_editor/viewport_zoom_modifier_1" || p_path == "spatial_editor/viewport_zoom_modifier_2";
  354. if (path_is_orbit_mod || path_is_pan_mod || path_is_zoom_mod) {
  355. EditorSettings::get_singleton()->set_manually("editors/3d/navigation/navigation_scheme", (int)Node3DEditorViewport::NAVIGATION_CUSTOM);
  356. }
  357. }
  358. Array EditorSettingsDialog::_event_list_to_array_helper(const List<Ref<InputEvent>> &p_events) {
  359. Array events;
  360. // Convert the list to an array, and only keep key events as this is for the editor.
  361. for (const List<Ref<InputEvent>>::Element *E = p_events.front(); E; E = E->next()) {
  362. Ref<InputEventKey> k = E->get();
  363. if (k.is_valid()) {
  364. events.append(E->get());
  365. }
  366. }
  367. return events;
  368. }
  369. TreeItem *EditorSettingsDialog::_create_shortcut_treeitem(TreeItem *p_parent, const String &p_shortcut_identifier, const String &p_display, Array &p_events, bool p_allow_revert, bool p_is_action, bool p_is_collapsed) {
  370. TreeItem *shortcut_item = shortcuts->create_item(p_parent);
  371. shortcut_item->set_collapsed(p_is_collapsed);
  372. shortcut_item->set_text(0, p_display);
  373. Ref<InputEvent> primary = p_events.size() > 0 ? Ref<InputEvent>(p_events[0]) : Ref<InputEvent>();
  374. Ref<InputEvent> secondary = p_events.size() > 1 ? Ref<InputEvent>(p_events[1]) : Ref<InputEvent>();
  375. String sc_text = TTRC("None");
  376. if (primary.is_valid()) {
  377. sc_text = primary->as_text();
  378. if (secondary.is_valid()) {
  379. sc_text += ", " + secondary->as_text();
  380. if (p_events.size() > 2) {
  381. sc_text += " (+" + itos(p_events.size() - 2) + ")";
  382. }
  383. }
  384. shortcut_item->set_auto_translate_mode(1, AUTO_TRANSLATE_MODE_DISABLED);
  385. }
  386. shortcut_item->set_text(1, sc_text);
  387. if (sc_text == "None") {
  388. // Fade out unassigned shortcut labels for easier visual grepping.
  389. shortcut_item->set_custom_color(1, get_theme_color(SceneStringName(font_color), SNAME("Label")) * Color(1, 1, 1, 0.5));
  390. }
  391. if (p_allow_revert) {
  392. shortcut_item->add_button(1, get_editor_theme_icon(SNAME("Reload")), SHORTCUT_REVERT);
  393. }
  394. shortcut_item->add_button(1, get_editor_theme_icon(SNAME("Add")), SHORTCUT_ADD);
  395. shortcut_item->add_button(1, get_editor_theme_icon(SNAME("Close")), SHORTCUT_ERASE, p_events.is_empty());
  396. shortcut_item->set_meta("is_action", p_is_action);
  397. shortcut_item->set_meta("type", "shortcut");
  398. shortcut_item->set_meta("shortcut_identifier", p_shortcut_identifier);
  399. shortcut_item->set_meta("events", p_events);
  400. // Shortcut Input Events
  401. for (int i = 0; i < p_events.size(); i++) {
  402. Ref<InputEvent> ie = p_events[i];
  403. if (ie.is_null()) {
  404. continue;
  405. }
  406. TreeItem *event_item = shortcuts->create_item(shortcut_item);
  407. // TRANSLATORS: This is the label for the main input event of a shortcut.
  408. event_item->set_text(0, shortcut_item->get_child_count() == 1 ? TTRC("Primary") : "");
  409. event_item->set_text(1, ie->as_text());
  410. event_item->set_auto_translate_mode(1, AUTO_TRANSLATE_MODE_DISABLED);
  411. event_item->add_button(1, get_editor_theme_icon(SNAME("Edit")), SHORTCUT_EDIT);
  412. event_item->add_button(1, get_editor_theme_icon(SNAME("Close")), SHORTCUT_ERASE);
  413. event_item->set_custom_bg_color(0, get_theme_color(SNAME("dark_color_3"), EditorStringName(Editor)));
  414. event_item->set_custom_bg_color(1, get_theme_color(SNAME("dark_color_3"), EditorStringName(Editor)));
  415. event_item->set_meta("is_action", p_is_action);
  416. event_item->set_meta("type", "event");
  417. event_item->set_meta("event_index", i);
  418. }
  419. return shortcut_item;
  420. }
  421. bool EditorSettingsDialog::_should_display_shortcut(const String &p_name, const Array &p_events, bool p_match_localized_name) const {
  422. const Ref<InputEvent> search_ev = shortcut_search_bar->get_event();
  423. if (search_ev.is_valid()) {
  424. bool event_match = false;
  425. for (int i = 0; i < p_events.size(); ++i) {
  426. const Ref<InputEvent> ev = p_events[i];
  427. if (ev.is_valid() && ev->is_match(search_ev, true)) {
  428. event_match = true;
  429. break;
  430. }
  431. }
  432. if (!event_match) {
  433. return false;
  434. }
  435. }
  436. const String &search_text = shortcut_search_bar->get_name();
  437. if (search_text.is_empty()) {
  438. return true;
  439. }
  440. if (search_text.is_subsequence_ofn(p_name)) {
  441. return true;
  442. }
  443. if (p_match_localized_name && search_text.is_subsequence_ofn(TTR(p_name))) {
  444. return true;
  445. }
  446. return false;
  447. }
  448. void EditorSettingsDialog::_update_shortcuts() {
  449. // Before clearing the tree, take note of which categories are collapsed so that this state can be maintained when the tree is repopulated.
  450. HashMap<String, bool> collapsed;
  451. if (shortcuts->get_root() && shortcuts->get_root()->get_first_child()) {
  452. TreeItem *ti = shortcuts->get_root()->get_first_child();
  453. while (ti) {
  454. // Not all items have valid or unique text in the first column - so if it has an identifier, use that, as it should be unique.
  455. if (ti->get_first_child() && ti->has_meta("shortcut_identifier")) {
  456. collapsed[ti->get_meta("shortcut_identifier")] = ti->is_collapsed();
  457. } else {
  458. collapsed[ti->get_text(0)] = ti->is_collapsed();
  459. }
  460. // Try go down tree
  461. TreeItem *ti_next = ti->get_first_child();
  462. // Try go to the next node via in-order traversal
  463. if (!ti_next) {
  464. ti_next = ti;
  465. while (ti_next && !ti_next->get_next()) {
  466. ti_next = ti_next->get_parent();
  467. }
  468. if (ti_next) {
  469. ti_next = ti_next->get_next();
  470. }
  471. }
  472. ti = ti_next;
  473. }
  474. }
  475. String prev_selected_shortcut;
  476. if (shortcuts->get_selected()) {
  477. prev_selected_shortcut = shortcuts->get_selected()->get_text(0);
  478. }
  479. shortcuts->clear();
  480. TreeItem *root = shortcuts->create_item();
  481. HashMap<String, TreeItem *> sections;
  482. // Set up section for Common/Built-in actions
  483. TreeItem *common_section = shortcuts->create_item(root);
  484. sections["Common"] = common_section;
  485. common_section->set_text(0, TTRC("Common"));
  486. common_section->set_selectable(0, false);
  487. common_section->set_selectable(1, false);
  488. if (collapsed.has("Common")) {
  489. common_section->set_collapsed(collapsed["Common"]);
  490. }
  491. common_section->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  492. common_section->set_custom_bg_color(1, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  493. // Get the action map for the editor, and add each item to the "Common" section.
  494. for (const KeyValue<StringName, InputMap::Action> &E : InputMap::get_singleton()->get_action_map()) {
  495. const String &action_name = E.key;
  496. const InputMap::Action &action = E.value;
  497. // Skip non-builtin actions.
  498. if (!InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().has(action_name)) {
  499. continue;
  500. }
  501. const List<Ref<InputEvent>> &all_default_events = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied().find(action_name)->value;
  502. Array action_events = _event_list_to_array_helper(action.inputs);
  503. if (!_should_display_shortcut(action_name, action_events, false)) {
  504. continue;
  505. }
  506. Array default_events = _event_list_to_array_helper(all_default_events);
  507. bool same_as_defaults = Shortcut::is_event_array_equal(default_events, action_events);
  508. bool collapse = !collapsed.has(action_name) || (collapsed.has(action_name) && collapsed[action_name]);
  509. TreeItem *item = _create_shortcut_treeitem(common_section, action_name, action_name, action_events, !same_as_defaults, true, collapse);
  510. item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); // `ui_*` input action names are untranslatable identifiers.
  511. if (!prev_selected_shortcut.is_empty() && action_name == prev_selected_shortcut) {
  512. item->select(0);
  513. }
  514. }
  515. // Editor Shortcuts
  516. List<String> slist;
  517. EditorSettings::get_singleton()->get_shortcut_list(&slist);
  518. slist.sort(); // Sort alphabetically.
  519. const EditorPropertyNameProcessor::Style name_style = EditorPropertyNameProcessor::get_settings_style();
  520. const EditorPropertyNameProcessor::Style tooltip_style = EditorPropertyNameProcessor::get_tooltip_style(name_style);
  521. // Create all sections first.
  522. for (const String &E : slist) {
  523. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E);
  524. String section_name = E.get_slicec('/', 0);
  525. if (sections.has(section_name)) {
  526. continue;
  527. }
  528. TreeItem *section = shortcuts->create_item(root);
  529. const String item_name = EditorPropertyNameProcessor::get_singleton()->process_name(section_name, name_style, E);
  530. const String tooltip = EditorPropertyNameProcessor::get_singleton()->process_name(section_name, tooltip_style, E);
  531. section->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_DISABLED); // Already translated manually.
  532. section->set_text(0, item_name);
  533. section->set_tooltip_text(0, tooltip);
  534. section->set_selectable(0, false);
  535. section->set_selectable(1, false);
  536. section->set_custom_bg_color(0, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  537. section->set_custom_bg_color(1, get_theme_color(SNAME("prop_subsection"), EditorStringName(Editor)));
  538. if (collapsed.has(item_name)) {
  539. section->set_collapsed(collapsed[item_name]);
  540. }
  541. sections[section_name] = section;
  542. }
  543. // Add shortcuts to sections.
  544. for (const String &E : slist) {
  545. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(E);
  546. if (!sc->has_meta("original")) {
  547. continue;
  548. }
  549. String section_name = E.get_slicec('/', 0);
  550. TreeItem *section = sections[section_name];
  551. if (!_should_display_shortcut(sc->get_name(), sc->get_events(), true)) {
  552. continue;
  553. }
  554. Array original = sc->get_meta("original");
  555. Array shortcuts_array = sc->get_events().duplicate(true);
  556. bool same_as_defaults = Shortcut::is_event_array_equal(original, shortcuts_array);
  557. bool collapse = !collapsed.has(E) || (collapsed.has(E) && collapsed[E]);
  558. TreeItem *shortcut_item = _create_shortcut_treeitem(section, E, sc->get_name(), shortcuts_array, !same_as_defaults, false, collapse);
  559. if (!prev_selected_shortcut.is_empty() && sc->get_name() == prev_selected_shortcut) {
  560. shortcut_item->select(0);
  561. }
  562. }
  563. if (!prev_selected_shortcut.is_empty()) {
  564. shortcuts->ensure_cursor_is_visible();
  565. }
  566. // remove sections with no shortcuts
  567. for (KeyValue<String, TreeItem *> &E : sections) {
  568. TreeItem *section = E.value;
  569. if (section->get_first_child() == nullptr) {
  570. root->remove_child(section);
  571. memdelete(section);
  572. }
  573. }
  574. }
  575. void EditorSettingsDialog::_shortcut_button_pressed(Object *p_item, int p_column, int p_idx, MouseButton p_button) {
  576. if (p_button != MouseButton::LEFT) {
  577. return;
  578. }
  579. TreeItem *ti = Object::cast_to<TreeItem>(p_item);
  580. ERR_FAIL_NULL_MSG(ti, "Object passed is not a TreeItem.");
  581. ShortcutButton button_idx = (ShortcutButton)p_idx;
  582. is_editing_action = ti->get_meta("is_action");
  583. String type = ti->get_meta("type");
  584. if (type == "event") {
  585. current_edited_identifier = ti->get_parent()->get_meta("shortcut_identifier");
  586. current_events = ti->get_parent()->get_meta("events");
  587. current_event_index = ti->get_meta("event_index");
  588. } else { // Type is "shortcut"
  589. current_edited_identifier = ti->get_meta("shortcut_identifier");
  590. current_events = ti->get_meta("events");
  591. current_event_index = -1;
  592. }
  593. switch (button_idx) {
  594. case EditorSettingsDialog::SHORTCUT_ADD: {
  595. // Only for "shortcut" types
  596. shortcut_editor->popup_and_configure();
  597. } break;
  598. case EditorSettingsDialog::SHORTCUT_EDIT: {
  599. // Only for "event" types
  600. shortcut_editor->popup_and_configure(current_events[current_event_index]);
  601. } break;
  602. case EditorSettingsDialog::SHORTCUT_ERASE: {
  603. if (type == "shortcut") {
  604. if (is_editing_action) {
  605. _update_builtin_action(current_edited_identifier, Array());
  606. } else {
  607. _update_shortcut_events(current_edited_identifier, Array());
  608. }
  609. } else if (type == "event") {
  610. current_events.remove_at(current_event_index);
  611. if (is_editing_action) {
  612. _update_builtin_action(current_edited_identifier, current_events);
  613. } else {
  614. _update_shortcut_events(current_edited_identifier, current_events);
  615. }
  616. }
  617. } break;
  618. case EditorSettingsDialog::SHORTCUT_REVERT: {
  619. // Only for "shortcut" types
  620. if (is_editing_action) {
  621. List<Ref<InputEvent>> defaults = InputMap::get_singleton()->get_builtins_with_feature_overrides_applied()[current_edited_identifier];
  622. Array events = _event_list_to_array_helper(defaults);
  623. _update_builtin_action(current_edited_identifier, events);
  624. } else {
  625. Ref<Shortcut> sc = EditorSettings::get_singleton()->get_shortcut(current_edited_identifier);
  626. Array original = sc->get_meta("original");
  627. _update_shortcut_events(current_edited_identifier, original);
  628. }
  629. } break;
  630. default:
  631. break;
  632. }
  633. }
  634. void EditorSettingsDialog::_shortcut_cell_double_clicked() {
  635. // When a shortcut cell is double clicked:
  636. // If the cell has children and is in the bindings column, and if its first child is editable,
  637. // then uncollapse the cell, and if the first child is the only child, then edit that child.
  638. // If the cell is in the bindings column and can be edited, then edit it.
  639. // If the cell is in the name column, then toggle collapse.
  640. const ShortcutButton edit_btn_id = EditorSettingsDialog::SHORTCUT_EDIT;
  641. const int edit_btn_col = 1;
  642. TreeItem *ti = shortcuts->get_selected();
  643. if (ti == nullptr) {
  644. return;
  645. }
  646. String type = ti->get_meta("type");
  647. int col = shortcuts->get_selected_column();
  648. if (type == "shortcut" && col == 0) {
  649. if (ti->get_first_child()) {
  650. ti->set_collapsed(!ti->is_collapsed());
  651. }
  652. } else if (type == "shortcut" && col == 1) {
  653. if (ti->get_first_child()) {
  654. TreeItem *child_ti = ti->get_first_child();
  655. if (child_ti->get_button_by_id(edit_btn_col, edit_btn_id) != -1) {
  656. ti->set_collapsed(false);
  657. if (ti->get_child_count() == 1) {
  658. _shortcut_button_pressed(child_ti, edit_btn_col, edit_btn_id);
  659. }
  660. }
  661. }
  662. } else if (type == "event" && col == 1) {
  663. if (ti->get_button_by_id(edit_btn_col, edit_btn_id) != -1) {
  664. _shortcut_button_pressed(ti, edit_btn_col, edit_btn_id);
  665. }
  666. }
  667. }
  668. Variant EditorSettingsDialog::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  669. TreeItem *selected = shortcuts->get_selected();
  670. // Only allow drag for events
  671. if (!selected || (String)selected->get_meta("type", "") != "event") {
  672. return Variant();
  673. }
  674. String label_text = vformat(TTR("Event %d"), selected->get_meta("event_index"));
  675. Label *label = memnew(Label(label_text));
  676. label->set_modulate(Color(1, 1, 1, 1.0f));
  677. shortcuts->set_drag_preview(label);
  678. shortcuts->set_drop_mode_flags(Tree::DROP_MODE_INBETWEEN);
  679. return Dictionary(); // No data required
  680. }
  681. bool EditorSettingsDialog::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  682. TreeItem *selected = shortcuts->get_selected();
  683. TreeItem *item = (p_point == Vector2(Math::INF, Math::INF)) ? shortcuts->get_selected() : shortcuts->get_item_at_position(p_point);
  684. if (!selected || !item || item == selected || (String)item->get_meta("type", "") != "event") {
  685. return false;
  686. }
  687. // Don't allow moving an events in-between shortcuts.
  688. if (selected->get_parent()->get_meta("shortcut_identifier") != item->get_parent()->get_meta("shortcut_identifier")) {
  689. return false;
  690. }
  691. return true;
  692. }
  693. void EditorSettingsDialog::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  694. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  695. return;
  696. }
  697. TreeItem *selected = shortcuts->get_selected();
  698. TreeItem *target = (p_point == Vector2(Math::INF, Math::INF)) ? shortcuts->get_selected() : shortcuts->get_item_at_position(p_point);
  699. if (!target) {
  700. return;
  701. }
  702. int target_event_index = target->get_meta("event_index");
  703. int index_moving_from = selected->get_meta("event_index");
  704. Array events = selected->get_parent()->get_meta("events");
  705. Variant event_moved = events[index_moving_from];
  706. events.remove_at(index_moving_from);
  707. events.insert(target_event_index, event_moved);
  708. String ident = selected->get_parent()->get_meta("shortcut_identifier");
  709. if (selected->get_meta("is_action")) {
  710. _update_builtin_action(ident, events);
  711. } else {
  712. _update_shortcut_events(ident, events);
  713. }
  714. }
  715. void EditorSettingsDialog::_tabs_tab_changed(int p_tab) {
  716. _focus_current_search_box();
  717. // When tab has switched, shortcuts may have changed.
  718. _update_dynamic_property_hints();
  719. inspector->get_inspector()->update_tree();
  720. }
  721. void EditorSettingsDialog::_update_dynamic_property_hints() {
  722. // Calling add_property_hint overrides the existing hint.
  723. EditorSettings *settings = EditorSettings::get_singleton();
  724. settings->add_property_hint(_create_mouse_shortcut_property_info("editors/3d/navigation/orbit_mouse_button", "spatial_editor/viewport_orbit_modifier_1", "spatial_editor/viewport_orbit_modifier_2"));
  725. settings->add_property_hint(_create_mouse_shortcut_property_info("editors/3d/navigation/pan_mouse_button", "spatial_editor/viewport_pan_modifier_1", "spatial_editor/viewport_pan_modifier_2"));
  726. settings->add_property_hint(_create_mouse_shortcut_property_info("editors/3d/navigation/zoom_mouse_button", "spatial_editor/viewport_zoom_modifier_1", "spatial_editor/viewport_zoom_modifier_2"));
  727. }
  728. PropertyInfo EditorSettingsDialog::_create_mouse_shortcut_property_info(const String &p_property_name, const String &p_shortcut_1_name, const String &p_shortcut_2_name) {
  729. String hint_string;
  730. hint_string += _get_shortcut_button_string(p_shortcut_1_name) + _get_shortcut_button_string(p_shortcut_2_name);
  731. hint_string += "Left Mouse,";
  732. hint_string += _get_shortcut_button_string(p_shortcut_1_name) + _get_shortcut_button_string(p_shortcut_2_name);
  733. hint_string += "Middle Mouse,";
  734. hint_string += _get_shortcut_button_string(p_shortcut_1_name) + _get_shortcut_button_string(p_shortcut_2_name);
  735. hint_string += "Right Mouse,";
  736. hint_string += _get_shortcut_button_string(p_shortcut_1_name) + _get_shortcut_button_string(p_shortcut_2_name);
  737. hint_string += "Mouse Button 4,";
  738. hint_string += _get_shortcut_button_string(p_shortcut_1_name) + _get_shortcut_button_string(p_shortcut_2_name);
  739. hint_string += "Mouse Button 5";
  740. return PropertyInfo(Variant::INT, p_property_name, PROPERTY_HINT_ENUM, hint_string);
  741. }
  742. String EditorSettingsDialog::_get_shortcut_button_string(const String &p_shortcut_name) {
  743. String button_string;
  744. Ref<Shortcut> shortcut_ref = EditorSettings::get_singleton()->get_shortcut(p_shortcut_name);
  745. if (shortcut_ref.is_null()) {
  746. return String();
  747. }
  748. Array events = shortcut_ref->get_events();
  749. for (Ref<InputEvent> input_event : events) {
  750. button_string += input_event->as_text() + " + ";
  751. }
  752. return button_string;
  753. }
  754. void EditorSettingsDialog::_focus_current_search_box() {
  755. Control *tab = tabs->get_current_tab_control();
  756. LineEdit *current_search_box = nullptr;
  757. if (tab == tab_general) {
  758. current_search_box = search_box;
  759. } else if (tab == tab_shortcuts) {
  760. current_search_box = shortcut_search_bar->get_name_search_box();
  761. }
  762. if (current_search_box) {
  763. current_search_box->grab_focus();
  764. current_search_box->select_all();
  765. }
  766. }
  767. void EditorSettingsDialog::_advanced_toggled(bool p_button_pressed) {
  768. EditorSettings::get_singleton()->set("_editor_settings_advanced_mode", p_button_pressed);
  769. }
  770. void EditorSettingsDialog::_editor_restart() {
  771. emit_signal("restart_requested");
  772. }
  773. void EditorSettingsDialog::_editor_restart_request() {
  774. restart_container->show();
  775. }
  776. void EditorSettingsDialog::_editor_restart_close() {
  777. restart_container->hide();
  778. }
  779. void EditorSettingsDialog::_bind_methods() {
  780. ClassDB::bind_method(D_METHOD("_update_shortcuts"), &EditorSettingsDialog::_update_shortcuts);
  781. ClassDB::bind_method(D_METHOD("_settings_changed"), &EditorSettingsDialog::_settings_changed);
  782. ADD_SIGNAL(MethodInfo("restart_requested"));
  783. }
  784. EditorSettingsDialog::EditorSettingsDialog() {
  785. set_title(TTRC("Editor Settings"));
  786. set_flag(FLAG_MAXIMIZE_DISABLED, false);
  787. set_clamp_to_embedder(true);
  788. tabs = memnew(TabContainer);
  789. tabs->set_theme_type_variation("TabContainerOdd");
  790. tabs->connect("tab_changed", callable_mp(this, &EditorSettingsDialog::_tabs_tab_changed));
  791. add_child(tabs);
  792. // General Tab
  793. tab_general = memnew(VBoxContainer);
  794. tabs->add_child(tab_general);
  795. tab_general->set_name(TTRC("General"));
  796. HBoxContainer *hbc = memnew(HBoxContainer);
  797. hbc->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  798. tab_general->add_child(hbc);
  799. search_box = memnew(LineEdit);
  800. search_box->set_placeholder(TTRC("Filter Settings"));
  801. search_box->set_accessibility_name(TTRC("Filter Settings"));
  802. search_box->set_virtual_keyboard_show_on_focus(false);
  803. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  804. hbc->add_child(search_box);
  805. advanced_switch = memnew(CheckButton(TTRC("Advanced Settings")));
  806. hbc->add_child(advanced_switch);
  807. bool use_advanced = EDITOR_DEF("_editor_settings_advanced_mode", false);
  808. advanced_switch->set_pressed(use_advanced);
  809. advanced_switch->connect(SceneStringName(toggled), callable_mp(this, &EditorSettingsDialog::_advanced_toggled));
  810. inspector = memnew(SectionedInspector);
  811. inspector->get_inspector()->set_use_filter(true);
  812. inspector->get_inspector()->set_mark_unsaved(false);
  813. inspector->register_search_box(search_box);
  814. inspector->register_advanced_toggle(advanced_switch);
  815. inspector->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  816. tab_general->add_child(inspector);
  817. inspector->get_inspector()->connect("property_edited", callable_mp(this, &EditorSettingsDialog::_settings_property_edited));
  818. inspector->get_inspector()->connect("restart_requested", callable_mp(this, &EditorSettingsDialog::_editor_restart_request));
  819. if (EDITOR_GET("interface/touchscreen/enable_touch_optimizations")) {
  820. inspector->set_touch_dragger_enabled(true);
  821. }
  822. restart_container = memnew(PanelContainer);
  823. tab_general->add_child(restart_container);
  824. HBoxContainer *restart_hb = memnew(HBoxContainer);
  825. restart_container->add_child(restart_hb);
  826. restart_icon = memnew(TextureRect);
  827. restart_icon->set_v_size_flags(Control::SIZE_SHRINK_CENTER);
  828. restart_hb->add_child(restart_icon);
  829. restart_label = memnew(Label);
  830. restart_label->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  831. if (_is_in_project_manager()) {
  832. restart_label->set_text(TTRC("The Project Manager must be restarted for changes to take effect."));
  833. } else {
  834. restart_label->set_text(TTRC("The editor must be restarted for changes to take effect."));
  835. }
  836. restart_hb->add_child(restart_label);
  837. restart_hb->add_spacer();
  838. Button *restart_button = memnew(Button);
  839. restart_button->connect(SceneStringName(pressed), callable_mp(this, &EditorSettingsDialog::_editor_restart));
  840. restart_hb->add_child(restart_button);
  841. restart_button->set_text(TTRC("Save & Restart"));
  842. restart_close_button = memnew(Button);
  843. restart_close_button->set_accessibility_name(TTRC("Close"));
  844. restart_close_button->set_flat(true);
  845. restart_close_button->connect(SceneStringName(pressed), callable_mp(this, &EditorSettingsDialog::_editor_restart_close));
  846. restart_hb->add_child(restart_close_button);
  847. restart_container->hide();
  848. // Shortcuts Tab
  849. tab_shortcuts = memnew(VBoxContainer);
  850. tabs->add_child(tab_shortcuts);
  851. tab_shortcuts->set_name(TTRC("Shortcuts"));
  852. shortcut_search_bar = memnew(EditorEventSearchBar);
  853. shortcut_search_bar->connect(SceneStringName(value_changed), callable_mp(this, &EditorSettingsDialog::_update_shortcuts));
  854. tab_shortcuts->add_child(shortcut_search_bar);
  855. MarginContainer *mc = memnew(MarginContainer);
  856. mc->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  857. mc->set_theme_type_variation("NoBorderHorizontalBottom");
  858. tab_shortcuts->add_child(mc);
  859. shortcuts = memnew(Tree);
  860. shortcuts->set_accessibility_name(TTRC("Shortcuts"));
  861. shortcuts->set_theme_type_variation("TreeTable");
  862. shortcuts->set_columns(2);
  863. shortcuts->set_hide_root(true);
  864. shortcuts->set_column_titles_visible(true);
  865. shortcuts->set_column_title(0, TTRC("Name"));
  866. shortcuts->set_column_title(1, TTRC("Binding"));
  867. shortcuts->connect("button_clicked", callable_mp(this, &EditorSettingsDialog::_shortcut_button_pressed));
  868. shortcuts->connect("item_activated", callable_mp(this, &EditorSettingsDialog::_shortcut_cell_double_clicked));
  869. mc->add_child(shortcuts);
  870. SET_DRAG_FORWARDING_GCD(shortcuts, EditorSettingsDialog);
  871. // Adding event dialog
  872. shortcut_editor = memnew(InputEventConfigurationDialog);
  873. shortcut_editor->connect(SceneStringName(confirmed), callable_mp(this, &EditorSettingsDialog::_event_config_confirmed));
  874. shortcut_editor->set_allowed_input_types(INPUT_KEY);
  875. add_child(shortcut_editor);
  876. set_hide_on_ok(true);
  877. timer = memnew(Timer);
  878. timer->set_wait_time(1.5);
  879. timer->connect("timeout", callable_mp(this, &EditorSettingsDialog::_settings_save));
  880. timer->set_one_shot(true);
  881. add_child(timer);
  882. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &EditorSettingsDialog::_settings_changed));
  883. set_ok_button_text(TTRC("Close"));
  884. Ref<EditorSettingsInspectorPlugin> plugin;
  885. plugin.instantiate();
  886. plugin->inspector = inspector;
  887. EditorInspector::add_inspector_plugin(plugin);
  888. }
  889. void EditorSettingsPropertyWrapper::_setup_override_info() {
  890. override_container = memnew(HBoxContainer);
  891. override_icon = memnew(TextureRect);
  892. override_icon->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  893. override_container->add_child(override_icon);
  894. Variant::Type type = ProjectSettings::get_singleton()->get_editor_setting_override(property).get_type();
  895. override_editor_property = get_parent_inspector()->instantiate_property_editor(ProjectSettings::get_singleton(), type, ProjectSettings::EDITOR_SETTING_OVERRIDE_PREFIX + property, hint, hint_text, usage);
  896. override_editor_property->set_object_and_property(ProjectSettings::get_singleton(), ProjectSettings::EDITOR_SETTING_OVERRIDE_PREFIX + property);
  897. override_editor_property->set_read_only(true);
  898. override_editor_property->set_label(TTR("Overridden in project"));
  899. override_editor_property->set_h_size_flags(SIZE_EXPAND_FILL);
  900. override_container->add_child(override_editor_property);
  901. goto_button = memnew(Button);
  902. goto_button->set_tooltip_text(TTRC("Go to the override in the Project Settings."));
  903. override_container->add_child(goto_button);
  904. if (EditorNode::get_singleton()) {
  905. goto_button->connect(SceneStringName(pressed), callable_mp(EditorNode::get_singleton(), &EditorNode::open_setting_override).bind(property), CONNECT_DEFERRED);
  906. }
  907. remove_button = memnew(Button);
  908. remove_button->set_tooltip_text(TTRC("Remove this override."));
  909. override_container->add_child(remove_button);
  910. remove_button->connect(SceneStringName(pressed), callable_mp(this, &EditorSettingsPropertyWrapper::_remove_override));
  911. add_child(override_container);
  912. if (is_ready()) {
  913. // Setup icons.
  914. _notification(NOTIFICATION_THEME_CHANGED);
  915. }
  916. }
  917. void EditorSettingsPropertyWrapper::_update_override() {
  918. // Don't allow overriding theme properties, because it causes problems. Overriding Project Manager settings makes no sense.
  919. // TODO: Find a better way to define exception prefixes (if the list happens to grow).
  920. if (property.begins_with("interface/theme") || property.begins_with("project_manager") || Engine::get_singleton()->is_project_manager_hint()) {
  921. can_override = false;
  922. return;
  923. }
  924. const bool has_override = ProjectSettings::get_singleton()->is_project_loaded() && ProjectSettings::get_singleton()->has_editor_setting_override(property);
  925. if (has_override) {
  926. if (!override_container) {
  927. _setup_override_info();
  928. }
  929. override_editor_property->update_property();
  930. set_bottom_editor(override_container);
  931. override_container->show();
  932. } else if (override_container) {
  933. override_container->hide();
  934. set_bottom_editor(nullptr);
  935. }
  936. can_override = !has_override;
  937. }
  938. void EditorSettingsPropertyWrapper::_create_override() {
  939. ProjectSettings::get_singleton()->set_editor_setting_override(property, EDITOR_GET(property));
  940. ProjectSettings::get_singleton()->save();
  941. _update_override();
  942. }
  943. void EditorSettingsPropertyWrapper::_remove_override() {
  944. ProjectSettings::get_singleton()->set_editor_setting_override(property, Variant());
  945. ProjectSettings::get_singleton()->save();
  946. EditorSettings::get_singleton()->mark_setting_changed(property);
  947. EditorNode::get_singleton()->notify_settings_overrides_changed();
  948. _update_override();
  949. if (usage & PROPERTY_USAGE_RESTART_IF_CHANGED) {
  950. restart_request_callback.call();
  951. }
  952. }
  953. void EditorSettingsPropertyWrapper::_notification(int p_what) {
  954. if (override_container && p_what == NOTIFICATION_THEME_CHANGED) {
  955. override_icon->set_texture(get_editor_theme_icon(SNAME("Hierarchy")));
  956. goto_button->set_button_icon(get_editor_theme_icon(SNAME("MethodOverride")));
  957. remove_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
  958. }
  959. }
  960. void EditorSettingsPropertyWrapper::update_property() {
  961. editor_property->update_property();
  962. }
  963. void EditorSettingsPropertyWrapper::setup(const String &p_property, EditorProperty *p_editor_property, PropertyHint p_hint, const String &p_hint_text, uint32_t p_usage) {
  964. hint = p_hint;
  965. hint_text = p_hint_text;
  966. usage = p_usage;
  967. property = p_property;
  968. editor_property = p_editor_property;
  969. add_child(editor_property);
  970. _update_override();
  971. connect(SNAME("property_overridden"), callable_mp(this, &EditorSettingsPropertyWrapper::_create_override));
  972. editor_property->connect("property_changed", callable_mp((EditorProperty *)this, &EditorProperty::emit_changed));
  973. }
  974. bool EditorSettingsInspectorPlugin::can_handle(Object *p_object) {
  975. return p_object && p_object->is_class("SectionedInspectorFilter") && p_object != current_object;
  976. }
  977. bool EditorSettingsInspectorPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  978. if (!p_object->is_class("SectionedInspectorFilter")) {
  979. return false;
  980. }
  981. const String property = inspector->get_full_item_path(p_path);
  982. if (!EditorSettings::get_singleton()->has_setting(property)) {
  983. return false;
  984. }
  985. current_object = p_object;
  986. EditorSettingsPropertyWrapper *editor = memnew(EditorSettingsPropertyWrapper);
  987. EditorProperty *real_property = inspector->get_inspector()->instantiate_property_editor(p_object, p_type, p_path, p_hint, p_hint_text, p_usage, p_wide);
  988. real_property->set_object_and_property(p_object, p_path);
  989. real_property->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  990. real_property->set_name_split_ratio(0.0);
  991. editor->setup(property, real_property, p_hint, p_hint_text, p_usage);
  992. add_property_editor(p_path, editor);
  993. current_object = nullptr;
  994. return true;
  995. }