editor_settings_dialog.cpp 45 KB

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