From 951ea2415bde8c0c901c893ba95f9c7fa1f6a311 Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Wed, 9 Aug 2023 14:03:27 +0200 Subject: [PATCH] Make EditorInterface accessible as a singleton - EditorPlugin.get_editor_interface() is removed as redundant. --- doc/classes/@GlobalScope.xml | 4 ++++ doc/classes/EditorCommandPalette.xml | 4 ++-- doc/classes/EditorInterface.xml | 11 ++++++++++- doc/classes/EditorPlugin.xml | 13 +++++++------ doc/classes/EditorScript.xml | 3 ++- doc/classes/EditorSettings.xml | 4 ++-- editor/register_editor_types.cpp | 1 + .../GodotTools/GodotTools/Build/BuildSystem.cs | 4 ++-- .../GodotTools/GodotTools/GodotSharpEditor.cs | 5 ++--- .../GodotTools/GodotTools/Ides/GodotIdeManager.cs | 2 +- .../GodotTools/Ides/Rider/RiderPathManager.cs | 6 +++--- modules/mono/editor/bindings_generator.cpp | 2 +- tests/core/object/test_class_db.h | 3 --- 13 files changed, 37 insertions(+), 25 deletions(-) diff --git a/doc/classes/@GlobalScope.xml b/doc/classes/@GlobalScope.xml index 008452a92ec..e44fdd97762 100644 --- a/doc/classes/@GlobalScope.xml +++ b/doc/classes/@GlobalScope.xml @@ -1463,6 +1463,10 @@ The [DisplayServer] singleton. + + The [EditorInterface] singleton. + [b]Note:[/b] Only available in editor builds. + The [Engine] singleton. diff --git a/doc/classes/EditorCommandPalette.xml b/doc/classes/EditorCommandPalette.xml index df8bec8002a..e6af82b1c32 100644 --- a/doc/classes/EditorCommandPalette.xml +++ b/doc/classes/EditorCommandPalette.xml @@ -8,13 +8,13 @@ Command key names use slash delimiters to distinguish sections, for example: [code]"example/command1"[/code] then [code]example[/code] will be the section name. [codeblocks] [gdscript] - var command_palette = get_editor_interface().get_command_palette() + var command_palette = EditorInterface.get_command_palette() # external_command is a function that will be called with the command is executed. var command_callable = Callable(self, "external_command").bind(arguments) command_palette.add_command("command", "test/command",command_callable) [/gdscript] [csharp] - EditorCommandPalette commandPalette = GetEditorInterface().GetCommandPalette(); + EditorCommandPalette commandPalette = EditorInterface.Singleton.GetCommandPalette(); // ExternalCommand is a function that will be called with the command is executed. Callable commandCallable = new Callable(this, MethodName.ExternalCommand); commandPalette.AddCommand("command", "test/command", commandCallable) diff --git a/doc/classes/EditorInterface.xml b/doc/classes/EditorInterface.xml index cabd9c0da6a..3ae4b7f8121 100644 --- a/doc/classes/EditorInterface.xml +++ b/doc/classes/EditorInterface.xml @@ -5,7 +5,16 @@ [EditorInterface] gives you control over Godot editor's window. It allows customizing the window, saving and (re-)loading scenes, rendering mesh previews, inspecting and editing resources and objects, and provides access to [EditorSettings], [EditorFileSystem], [EditorResourcePreview], [ScriptEditor], the editor viewport, and information about scenes. - [b]Note:[/b] This class shouldn't be instantiated directly. Instead, access the singleton using [method EditorPlugin.get_editor_interface]. + [b]Note:[/b] This class shouldn't be instantiated directly. Instead, access the singleton directly by its name. + [codeblocks] + [gdscript] + var editor_settings = EditorInterface.get_editor_settings() + [/gdscript] + [csharp] + // In C# you can access it via the static Singleton property. + EditorSettings settings = EditorInterface.Singleton.GetEditorSettings(); + [/csharp] + [/codeblocks] diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index ffb4df25d35..8d280b8276d 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -245,7 +245,7 @@ # You can use a custom icon: return preload("res://addons/my_plugin/my_plugin_icon.svg") # Or use a built-in icon: - return get_editor_interface().get_base_control().get_theme_icon("Node", "EditorIcons") + return EditorInterface.get_base_control().get_theme_icon("Node", "EditorIcons") [/gdscript] [csharp] public override Texture2D _GetPluginIcon() @@ -253,7 +253,7 @@ // You can use a custom icon: return ResourceLoader.Load<Texture2D>("res://addons/my_plugin/my_plugin_icon.svg"); // Or use a built-in icon: - return GetEditorInterface().GetBaseControl().GetThemeIcon("Node", "EditorIcons"); + return EditorInterface.Singleton.GetBaseControl().GetThemeIcon("Node", "EditorIcons"); } [/csharp] [/codeblocks] @@ -340,7 +340,7 @@ func _enter_tree(): plugin_control = preload("my_plugin_control.tscn").instantiate() - get_editor_interface().get_editor_main_screen().add_child(plugin_control) + EditorInterface.get_editor_main_screen().add_child(plugin_control) plugin_control.hide() func _has_main_screen(): @@ -353,7 +353,7 @@ return "My Super Cool Plugin 3000" func _get_plugin_icon(): - return get_editor_interface().get_base_control().get_theme_icon("Node", "EditorIcons") + return EditorInterface.get_base_control().get_theme_icon("Node", "EditorIcons") [/codeblock] @@ -558,10 +558,11 @@ The callback should have 4 arguments: [Object] [code]undo_redo[/code], [Object] [code]modified_object[/code], [String] [code]property[/code] and [Variant] [code]new_value[/code]. They are, respectively, the [UndoRedo] object used by the inspector, the currently modified object, the name of the modified property and the new value the property is about to take. - + - Returns the [EditorInterface] singleton. It provides access to some parts of the editor GUI as well as various inner states and tools. + Returns the [EditorInterface] singleton instance. + [i]Deprecated.[/i] [EditorInterface] is a global singleton and can be accessed directly by its name. diff --git a/doc/classes/EditorScript.xml b/doc/classes/EditorScript.xml index 01e6b9a52e6..8033c189187 100644 --- a/doc/classes/EditorScript.xml +++ b/doc/classes/EditorScript.xml @@ -48,10 +48,11 @@ [b]Warning:[/b] The implementation of this method is currently disabled. - + Returns the [EditorInterface] singleton instance. + [i]Deprecated.[/i] [EditorInterface] is a global singleton and can be accessed directly by its name. diff --git a/doc/classes/EditorSettings.xml b/doc/classes/EditorSettings.xml index 85efe4362e2..5ca89dc03ed 100644 --- a/doc/classes/EditorSettings.xml +++ b/doc/classes/EditorSettings.xml @@ -9,7 +9,7 @@ Accessing the settings can be done using the following methods, such as: [codeblocks] [gdscript] - var settings = get_editor_interface().get_editor_settings() + var settings = EditorInterface.get_editor_settings() # `settings.set("some/property", 10)` also works as this class overrides `_set()` internally. settings.set_setting("some/property", 10) # `settings.get("some/property")` also works as this class overrides `_get()` internally. @@ -17,7 +17,7 @@ var list_of_settings = settings.get_property_list() [/gdscript] [csharp] - EditorSettings settings = GetEditorInterface().GetEditorSettings(); + EditorSettings settings = EditorInterface.Singleton.GetEditorSettings(); // `settings.set("some/property", value)` also works as this class overrides `_set()` internally. settings.SetSetting("some/property", Value); // `settings.get("some/property", value)` also works as this class overrides `_get()` internally. diff --git a/editor/register_editor_types.cpp b/editor/register_editor_types.cpp index fbb724906b2..849a6cc0974 100644 --- a/editor/register_editor_types.cpp +++ b/editor/register_editor_types.cpp @@ -275,6 +275,7 @@ void register_editor_types() { GLOBAL_DEF("editor/version_control/autoload_on_startup", false); EditorInterface::create(); + Engine::get_singleton()->add_singleton(Engine::Singleton("EditorInterface", EditorInterface::get_singleton())); OS::get_singleton()->benchmark_end_measure("register_editor_types"); } diff --git a/modules/mono/editor/GodotTools/GodotTools/Build/BuildSystem.cs b/modules/mono/editor/GodotTools/GodotTools/Build/BuildSystem.cs index d7d484d166a..8a292fd73a0 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Build/BuildSystem.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Build/BuildSystem.cs @@ -23,7 +23,7 @@ namespace GodotTools.Build if (dotnetPath == null) throw new FileNotFoundException("Cannot find the dotnet executable."); - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); var startInfo = new ProcessStartInfo(dotnetPath); @@ -94,7 +94,7 @@ namespace GodotTools.Build if (dotnetPath == null) throw new FileNotFoundException("Cannot find the dotnet executable."); - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); var startInfo = new ProcessStartInfo(dotnetPath); diff --git a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs index 618d255938b..ec28658557e 100644 --- a/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs +++ b/modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs @@ -473,10 +473,9 @@ namespace GodotTools } } - var editorInterface = GetEditorInterface(); - var editorBaseControl = editorInterface.GetBaseControl(); + var editorBaseControl = EditorInterface.Singleton.GetBaseControl(); - _editorSettings = editorInterface.GetEditorSettings(); + _editorSettings = EditorInterface.Singleton.GetEditorSettings(); _errorDialog = new AcceptDialog(); editorBaseControl.AddChild(_errorDialog); diff --git a/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeManager.cs b/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeManager.cs index 5d1a2277f96..65b77112aab 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeManager.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Ides/GodotIdeManager.cs @@ -79,7 +79,7 @@ namespace GodotTools.Ides public async Task LaunchIdeAsync(int millisecondsTimeout = 10000) { - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); var editorId = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As(); string editorIdentity = GetExternalEditorIdentity(editorId); diff --git a/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs b/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs index 0d77b8999a0..fbbd01dafd8 100644 --- a/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs +++ b/modules/mono/editor/GodotTools/GodotTools/Ides/Rider/RiderPathManager.cs @@ -23,7 +23,7 @@ namespace GodotTools.Ides.Rider private static string GetRiderPathFromSettings() { - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); if (editorSettings.HasSetting(EditorPathSettingName)) return (string)editorSettings.GetSetting(EditorPathSettingName); return null; @@ -31,7 +31,7 @@ namespace GodotTools.Ides.Rider public static void Initialize() { - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); var editor = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As(); if (editor == ExternalEditorId.Rider) { @@ -92,7 +92,7 @@ namespace GodotTools.Ides.Rider string newPath = riderInfos.Length > 0 ? riderInfos[riderInfos.Length - 1].Path : allInfos[allInfos.Length - 1].Path; - var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); + var editorSettings = EditorInterface.Singleton.GetEditorSettings(); editorSettings.SetSetting(EditorPathSettingName, newPath); Globals.EditorDef(EditorPathSettingName, newPath); return newPath; diff --git a/modules/mono/editor/bindings_generator.cpp b/modules/mono/editor/bindings_generator.cpp index 1554b89c337..342c94cbd90 100644 --- a/modules/mono/editor/bindings_generator.cpp +++ b/modules/mono/editor/bindings_generator.cpp @@ -4014,7 +4014,7 @@ void BindingsGenerator::_initialize_blacklisted_methods() { } void BindingsGenerator::_initialize_compat_singletons() { - // No compat singletons yet. + compat_singletons.insert("EditorInterface"); } void BindingsGenerator::_log(const char *p_format, ...) { diff --git a/tests/core/object/test_class_db.h b/tests/core/object/test_class_db.h index 3f091fd2fe6..5f7de11c717 100644 --- a/tests/core/object/test_class_db.h +++ b/tests/core/object/test_class_db.h @@ -409,9 +409,6 @@ void validate_method(const Context &p_context, const ExposedClass &p_class, cons if (p_method.return_type.name != StringName()) { const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); if (return_class) { - TEST_COND(return_class->is_singleton, - "Method return type is a singleton: '", p_class.name, ".", p_method.name, "'."); - if (p_class.api_type == ClassDB::API_CORE) { TEST_COND(return_class->api_type == ClassDB::API_EDITOR, "Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,