Make EditorInterface accessible as a singleton

- EditorPlugin.get_editor_interface() is removed as redundant.
This commit is contained in:
Yuri Sizov 2023-08-09 14:03:27 +02:00
parent f7bc653cbe
commit 951ea2415b
13 changed files with 37 additions and 25 deletions

View File

@ -1463,6 +1463,10 @@
<member name="DisplayServer" type="DisplayServer" setter="" getter=""> <member name="DisplayServer" type="DisplayServer" setter="" getter="">
The [DisplayServer] singleton. The [DisplayServer] singleton.
</member> </member>
<member name="EditorInterface" type="EditorInterface" setter="" getter="">
The [EditorInterface] singleton.
[b]Note:[/b] Only available in editor builds.
</member>
<member name="Engine" type="Engine" setter="" getter=""> <member name="Engine" type="Engine" setter="" getter="">
The [Engine] singleton. The [Engine] singleton.
</member> </member>

View File

@ -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. 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] [codeblocks]
[gdscript] [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. # external_command is a function that will be called with the command is executed.
var command_callable = Callable(self, "external_command").bind(arguments) var command_callable = Callable(self, "external_command").bind(arguments)
command_palette.add_command("command", "test/command",command_callable) command_palette.add_command("command", "test/command",command_callable)
[/gdscript] [/gdscript]
[csharp] [csharp]
EditorCommandPalette commandPalette = GetEditorInterface().GetCommandPalette(); EditorCommandPalette commandPalette = EditorInterface.Singleton.GetCommandPalette();
// ExternalCommand is a function that will be called with the command is executed. // ExternalCommand is a function that will be called with the command is executed.
Callable commandCallable = new Callable(this, MethodName.ExternalCommand); Callable commandCallable = new Callable(this, MethodName.ExternalCommand);
commandPalette.AddCommand("command", "test/command", commandCallable) commandPalette.AddCommand("command", "test/command", commandCallable)

View File

@ -5,7 +5,16 @@
</brief_description> </brief_description>
<description> <description>
[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. [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]
</description> </description>
<tutorials> <tutorials>
</tutorials> </tutorials>

View File

@ -245,7 +245,7 @@
# You can use a custom icon: # You can use a custom icon:
return preload("res://addons/my_plugin/my_plugin_icon.svg") return preload("res://addons/my_plugin/my_plugin_icon.svg")
# Or use a built-in icon: # 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] [/gdscript]
[csharp] [csharp]
public override Texture2D _GetPluginIcon() public override Texture2D _GetPluginIcon()
@ -253,7 +253,7 @@
// You can use a custom icon: // You can use a custom icon:
return ResourceLoader.Load&lt;Texture2D&gt;("res://addons/my_plugin/my_plugin_icon.svg"); return ResourceLoader.Load&lt;Texture2D&gt;("res://addons/my_plugin/my_plugin_icon.svg");
// Or use a built-in icon: // Or use a built-in icon:
return GetEditorInterface().GetBaseControl().GetThemeIcon("Node", "EditorIcons"); return EditorInterface.Singleton.GetBaseControl().GetThemeIcon("Node", "EditorIcons");
} }
[/csharp] [/csharp]
[/codeblocks] [/codeblocks]
@ -340,7 +340,7 @@
func _enter_tree(): func _enter_tree():
plugin_control = preload("my_plugin_control.tscn").instantiate() 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() plugin_control.hide()
func _has_main_screen(): func _has_main_screen():
@ -353,7 +353,7 @@
return "My Super Cool Plugin 3000" return "My Super Cool Plugin 3000"
func _get_plugin_icon(): 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] [/codeblock]
</description> </description>
</method> </method>
@ -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. 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.
</description> </description>
</method> </method>
<method name="get_editor_interface"> <method name="get_editor_interface" is_deprecated="true">
<return type="EditorInterface" /> <return type="EditorInterface" />
<description> <description>
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.
</description> </description>
</method> </method>
<method name="get_export_as_menu"> <method name="get_export_as_menu">

View File

@ -48,10 +48,11 @@
[b]Warning:[/b] The implementation of this method is currently disabled. [b]Warning:[/b] The implementation of this method is currently disabled.
</description> </description>
</method> </method>
<method name="get_editor_interface" qualifiers="const"> <method name="get_editor_interface" qualifiers="const" is_deprecated="true">
<return type="EditorInterface" /> <return type="EditorInterface" />
<description> <description>
Returns the [EditorInterface] singleton instance. Returns the [EditorInterface] singleton instance.
[i]Deprecated.[/i] [EditorInterface] is a global singleton and can be accessed directly by its name.
</description> </description>
</method> </method>
<method name="get_scene" qualifiers="const"> <method name="get_scene" qualifiers="const">

View File

@ -9,7 +9,7 @@
Accessing the settings can be done using the following methods, such as: Accessing the settings can be done using the following methods, such as:
[codeblocks] [codeblocks]
[gdscript] [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("some/property", 10)` also works as this class overrides `_set()` internally.
settings.set_setting("some/property", 10) settings.set_setting("some/property", 10)
# `settings.get("some/property")` also works as this class overrides `_get()` internally. # `settings.get("some/property")` also works as this class overrides `_get()` internally.
@ -17,7 +17,7 @@
var list_of_settings = settings.get_property_list() var list_of_settings = settings.get_property_list()
[/gdscript] [/gdscript]
[csharp] [csharp]
EditorSettings settings = GetEditorInterface().GetEditorSettings(); EditorSettings settings = EditorInterface.Singleton.GetEditorSettings();
// `settings.set("some/property", value)` also works as this class overrides `_set()` internally. // `settings.set("some/property", value)` also works as this class overrides `_set()` internally.
settings.SetSetting("some/property", Value); settings.SetSetting("some/property", Value);
// `settings.get("some/property", value)` also works as this class overrides `_get()` internally. // `settings.get("some/property", value)` also works as this class overrides `_get()` internally.

View File

@ -275,6 +275,7 @@ void register_editor_types() {
GLOBAL_DEF("editor/version_control/autoload_on_startup", false); GLOBAL_DEF("editor/version_control/autoload_on_startup", false);
EditorInterface::create(); EditorInterface::create();
Engine::get_singleton()->add_singleton(Engine::Singleton("EditorInterface", EditorInterface::get_singleton()));
OS::get_singleton()->benchmark_end_measure("register_editor_types"); OS::get_singleton()->benchmark_end_measure("register_editor_types");
} }

View File

@ -23,7 +23,7 @@ namespace GodotTools.Build
if (dotnetPath == null) if (dotnetPath == null)
throw new FileNotFoundException("Cannot find the dotnet executable."); throw new FileNotFoundException("Cannot find the dotnet executable.");
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
var startInfo = new ProcessStartInfo(dotnetPath); var startInfo = new ProcessStartInfo(dotnetPath);
@ -94,7 +94,7 @@ namespace GodotTools.Build
if (dotnetPath == null) if (dotnetPath == null)
throw new FileNotFoundException("Cannot find the dotnet executable."); throw new FileNotFoundException("Cannot find the dotnet executable.");
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
var startInfo = new ProcessStartInfo(dotnetPath); var startInfo = new ProcessStartInfo(dotnetPath);

View File

@ -473,10 +473,9 @@ namespace GodotTools
} }
} }
var editorInterface = GetEditorInterface(); var editorBaseControl = EditorInterface.Singleton.GetBaseControl();
var editorBaseControl = editorInterface.GetBaseControl();
_editorSettings = editorInterface.GetEditorSettings(); _editorSettings = EditorInterface.Singleton.GetEditorSettings();
_errorDialog = new AcceptDialog(); _errorDialog = new AcceptDialog();
editorBaseControl.AddChild(_errorDialog); editorBaseControl.AddChild(_errorDialog);

View File

@ -79,7 +79,7 @@ namespace GodotTools.Ides
public async Task<EditorPick?> LaunchIdeAsync(int millisecondsTimeout = 10000) public async Task<EditorPick?> LaunchIdeAsync(int millisecondsTimeout = 10000)
{ {
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
var editorId = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As<ExternalEditorId>(); var editorId = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As<ExternalEditorId>();
string editorIdentity = GetExternalEditorIdentity(editorId); string editorIdentity = GetExternalEditorIdentity(editorId);

View File

@ -23,7 +23,7 @@ namespace GodotTools.Ides.Rider
private static string GetRiderPathFromSettings() private static string GetRiderPathFromSettings()
{ {
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
if (editorSettings.HasSetting(EditorPathSettingName)) if (editorSettings.HasSetting(EditorPathSettingName))
return (string)editorSettings.GetSetting(EditorPathSettingName); return (string)editorSettings.GetSetting(EditorPathSettingName);
return null; return null;
@ -31,7 +31,7 @@ namespace GodotTools.Ides.Rider
public static void Initialize() public static void Initialize()
{ {
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
var editor = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As<ExternalEditorId>(); var editor = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As<ExternalEditorId>();
if (editor == ExternalEditorId.Rider) if (editor == ExternalEditorId.Rider)
{ {
@ -92,7 +92,7 @@ namespace GodotTools.Ides.Rider
string newPath = riderInfos.Length > 0 string newPath = riderInfos.Length > 0
? riderInfos[riderInfos.Length - 1].Path ? riderInfos[riderInfos.Length - 1].Path
: allInfos[allInfos.Length - 1].Path; : allInfos[allInfos.Length - 1].Path;
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings(); var editorSettings = EditorInterface.Singleton.GetEditorSettings();
editorSettings.SetSetting(EditorPathSettingName, newPath); editorSettings.SetSetting(EditorPathSettingName, newPath);
Globals.EditorDef(EditorPathSettingName, newPath); Globals.EditorDef(EditorPathSettingName, newPath);
return newPath; return newPath;

View File

@ -4014,7 +4014,7 @@ void BindingsGenerator::_initialize_blacklisted_methods() {
} }
void BindingsGenerator::_initialize_compat_singletons() { void BindingsGenerator::_initialize_compat_singletons() {
// No compat singletons yet. compat_singletons.insert("EditorInterface");
} }
void BindingsGenerator::_log(const char *p_format, ...) { void BindingsGenerator::_log(const char *p_format, ...) {

View File

@ -409,9 +409,6 @@ void validate_method(const Context &p_context, const ExposedClass &p_class, cons
if (p_method.return_type.name != StringName()) { if (p_method.return_type.name != StringName()) {
const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type); const ExposedClass *return_class = p_context.find_exposed_class(p_method.return_type);
if (return_class) { 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) { if (p_class.api_type == ClassDB::API_CORE) {
TEST_COND(return_class->api_type == ClassDB::API_EDITOR, TEST_COND(return_class->api_type == ClassDB::API_EDITOR,
"Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name, "Method '", p_class.name, ".", p_method.name, "' has return type '", return_class->name,