Skip to content

Commit

Permalink
Merge pull request #75694 from YuriSizov/editor-hot-singleton-interfa…
Browse files Browse the repository at this point in the history
…ces-in-your-area

Make `EditorInterface` accessible as a singleton
  • Loading branch information
akien-mga committed Aug 10, 2023
2 parents 11cfb23 + 951ea24 commit b78d52b
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 25 deletions.
4 changes: 4 additions & 0 deletions doc/classes/@GlobalScope.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1463,6 +1463,10 @@
<member name="DisplayServer" type="DisplayServer" setter="" getter="">
The [DisplayServer] singleton.
</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="">
The [Engine] singleton.
</member>
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/EditorCommandPalette.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
</brief_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.
[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>
<tutorials>
</tutorials>
Expand Down
13 changes: 7 additions & 6 deletions doc/classes/EditorPlugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@
# 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()
{
// You can use a custom icon:
return ResourceLoader.Load&lt;Texture2D&gt;("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]
Expand Down Expand Up @@ -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():
Expand All @@ -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]
</description>
</method>
Expand Down Expand Up @@ -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.
</description>
</method>
<method name="get_editor_interface">
<method name="get_editor_interface" is_deprecated="true">
<return type="EditorInterface" />
<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>
</method>
<method name="get_export_as_menu">
Expand Down
3 changes: 2 additions & 1 deletion doc/classes/EditorScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@
[b]Warning:[/b] The implementation of this method is currently disabled.
</description>
</method>
<method name="get_editor_interface" qualifiers="const">
<method name="get_editor_interface" qualifiers="const" is_deprecated="true">
<return type="EditorInterface" />
<description>
Returns the [EditorInterface] singleton instance.
[i]Deprecated.[/i] [EditorInterface] is a global singleton and can be accessed directly by its name.
</description>
</method>
<method name="get_scene" qualifiers="const">
Expand Down
4 changes: 2 additions & 2 deletions doc/classes/EditorSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
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.
settings.get_setting("some/property")
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.
Expand Down
1 change: 1 addition & 0 deletions editor/register_editor_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ private static Process LaunchBuild(BuildInfo buildInfo, Action<string> stdOutHan
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);

Expand Down Expand Up @@ -94,7 +94,7 @@ private static Process LaunchPublish(BuildInfo buildInfo, Action<string> stdOutH
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);

Expand Down
5 changes: 2 additions & 3 deletions modules/mono/editor/GodotTools/GodotTools/GodotSharpEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,9 @@ public override void _EnablePlugin()
}
}

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private string GetExternalEditorIdentity(ExternalEditorId editorId)

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>();
string editorIdentity = GetExternalEditorIdentity(editorId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ static RiderPathManager()

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;
}

public static void Initialize()
{
var editorSettings = GodotSharpEditor.Instance.GetEditorInterface().GetEditorSettings();
var editorSettings = EditorInterface.Singleton.GetEditorSettings();
var editor = editorSettings.GetSetting(GodotSharpEditor.Settings.ExternalEditor).As<ExternalEditorId>();
if (editor == ExternalEditorId.Rider)
{
Expand Down Expand Up @@ -92,7 +92,7 @@ private static string CheckAndUpdatePath(string riderPath)
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;
Expand Down
2 changes: 1 addition & 1 deletion modules/mono/editor/bindings_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...) {
Expand Down
3 changes: 0 additions & 3 deletions tests/core/object/test_class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b78d52b

Please sign in to comment.