diff --git a/doc/classes/EditorInterface.xml b/doc/classes/EditorInterface.xml index 48939db1f787..74825493a1af 100644 --- a/doc/classes/EditorInterface.xml +++ b/doc/classes/EditorInterface.xml @@ -1,5 +1,5 @@ - + Godot editor's interface. @@ -34,7 +34,7 @@ Edits the given [Script]. The line and column on which to open the script can also be specified. The script will be open with the user-configured editor for the script's language which may be an external editor. - + Returns the main container of Godot editor's window. For example, you can use it to retrieve the size of the container and place your controls accordingly. @@ -60,20 +60,20 @@ Returns the current path being viewed in the [FileSystemDock]. - + Returns the edited (current) scene's root [Node]. - + Returns the editor control responsible for main screen plugins and tools. Use it with plugins that implement [method EditorPlugin._has_main_screen]. [b]Warning:[/b] Removing and freeing this node will render a part of the editor useless and may cause a crash. - + Returns the [EditorPaths] singleton. @@ -86,13 +86,13 @@ [b]Note:[/b] This value is set via the [code]interface/editor/display_scale[/code] and [code]interface/editor/custom_display_scale[/code] editor settings. Editor must be restarted for changes to be properly applied. - + Returns the editor's [EditorSettings] instance. - + Returns the editor's [FileSystemDock] instance. @@ -118,19 +118,19 @@ Returns the name of the scene that is being played. If no scene is currently being played, returns an empty string. - + Returns the editor's [EditorFileSystem] instance. - + Returns the editor's [EditorResourcePreview] instance. - + Returns the editor's [ScriptEditor] instance. @@ -143,7 +143,7 @@ Returns an array containing the paths of the currently selected files (and directories) in the [FileSystemDock]. - + Returns the editor's [EditorSelection] instance. @@ -158,12 +158,6 @@ Shows the given property on the given [param object] in the editor's Inspector dock. If [param inspector_only] is [code]true[/code], plugins will not attempt to edit [param object]. - - - - Returns [code]true[/code] if Movie Maker mode is enabled in the editor. See also [method set_movie_maker_enabled]. See [MovieWriter] for more information. - - @@ -253,13 +247,6 @@ Sets the editor's current main screen to the one specified in [param name]. [param name] must match the text of the tab in question exactly ([code]2D[/code], [code]3D[/code], [code]Script[/code], [code]AssetLib[/code]). - - - - - Sets whether Movie Maker mode is enabled in the editor. See also [method is_movie_maker_enabled]. See [MovieWriter] for more information. - - @@ -279,5 +266,8 @@ If [code]true[/code], enables distraction-free mode which hides side docks to increase the space available for the main view. + + If [code]true[/code], the Movie Maker mode is enabled in the editor. See [MovieWriter] for more information. + diff --git a/doc/classes/EditorPlugin.xml b/doc/classes/EditorPlugin.xml index a8ee5cdc19c0..1f3f028bbe8e 100644 --- a/doc/classes/EditorPlugin.xml +++ b/doc/classes/EditorPlugin.xml @@ -532,7 +532,7 @@ - Returns the [EditorInterface] object that gives you control over Godot editor's window and its functionalities. + Returns the [EditorInterface] singleton. It provides access to some parts of the editor GUI as well as various inner states and tools. diff --git a/doc/classes/EditorSettings.xml b/doc/classes/EditorSettings.xml index 996af71c3b02..85d5f7dd55e4 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 = EditorInterface.get_editor_settings() + var settings = get_editor_interface().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. diff --git a/editor/editor_interface.cpp b/editor/editor_interface.cpp new file mode 100644 index 000000000000..99803fd82dab --- /dev/null +++ b/editor/editor_interface.cpp @@ -0,0 +1,437 @@ +/**************************************************************************/ +/* editor_interface.cpp */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#include "editor_interface.h" + +#include "editor/editor_command_palette.h" +#include "editor/editor_node.h" +#include "editor/editor_paths.h" +#include "editor/editor_resource_preview.h" +#include "editor/editor_scale.h" +#include "editor/editor_settings.h" +#include "editor/filesystem_dock.h" +#include "editor/inspector_dock.h" +#include "main/main.h" +#include "scene/gui/box_container.h" +#include "scene/gui/control.h" + +EditorInterface *EditorInterface::singleton = nullptr; + +void EditorInterface::restart_editor(bool p_save) { + if (p_save) { + EditorNode::get_singleton()->save_all_scenes(); + } + EditorNode::get_singleton()->restart_editor(); +} + +// Editor tools. + +EditorCommandPalette *EditorInterface::get_command_palette() const { + return EditorCommandPalette::get_singleton(); +} + +EditorFileSystem *EditorInterface::get_resource_file_system() const { + return EditorFileSystem::get_singleton(); +} + +EditorPaths *EditorInterface::get_editor_paths() const { + return EditorPaths::get_singleton(); +} + +EditorResourcePreview *EditorInterface::get_resource_previewer() const { + return EditorResourcePreview::get_singleton(); +} + +EditorSelection *EditorInterface::get_selection() const { + return EditorNode::get_singleton()->get_editor_selection(); +} + +Ref EditorInterface::get_editor_settings() const { + return EditorSettings::get_singleton(); +} + +TypedArray EditorInterface::_make_mesh_previews(const TypedArray &p_meshes, int p_preview_size) { + Vector> meshes; + + for (int i = 0; i < p_meshes.size(); i++) { + meshes.push_back(p_meshes[i]); + } + + Vector> textures = make_mesh_previews(meshes, nullptr, p_preview_size); + TypedArray ret; + for (int i = 0; i < textures.size(); i++) { + ret.push_back(textures[i]); + } + + return ret; +} + +Vector> EditorInterface::make_mesh_previews(const Vector> &p_meshes, Vector *p_transforms, int p_preview_size) { + int size = p_preview_size; + + RID scenario = RS::get_singleton()->scenario_create(); + + RID viewport = RS::get_singleton()->viewport_create(); + RS::get_singleton()->viewport_set_update_mode(viewport, RS::VIEWPORT_UPDATE_ALWAYS); + RS::get_singleton()->viewport_set_scenario(viewport, scenario); + RS::get_singleton()->viewport_set_size(viewport, size, size); + RS::get_singleton()->viewport_set_transparent_background(viewport, true); + RS::get_singleton()->viewport_set_active(viewport, true); + RID viewport_texture = RS::get_singleton()->viewport_get_texture(viewport); + + RID camera = RS::get_singleton()->camera_create(); + RS::get_singleton()->viewport_attach_camera(viewport, camera); + + RID light = RS::get_singleton()->directional_light_create(); + RID light_instance = RS::get_singleton()->instance_create2(light, scenario); + + RID light2 = RS::get_singleton()->directional_light_create(); + RS::get_singleton()->light_set_color(light2, Color(0.7, 0.7, 0.7)); + RID light_instance2 = RS::get_singleton()->instance_create2(light2, scenario); + + EditorProgress ep("mlib", TTR("Creating Mesh Previews"), p_meshes.size()); + + Vector> textures; + + for (int i = 0; i < p_meshes.size(); i++) { + Ref mesh = p_meshes[i]; + if (!mesh.is_valid()) { + textures.push_back(Ref()); + continue; + } + + Transform3D mesh_xform; + if (p_transforms != nullptr) { + mesh_xform = (*p_transforms)[i]; + } + + RID inst = RS::get_singleton()->instance_create2(mesh->get_rid(), scenario); + RS::get_singleton()->instance_set_transform(inst, mesh_xform); + + AABB aabb = mesh->get_aabb(); + Vector3 ofs = aabb.get_center(); + aabb.position -= ofs; + Transform3D xform; + xform.basis = Basis().rotated(Vector3(0, 1, 0), -Math_PI / 6); + xform.basis = Basis().rotated(Vector3(1, 0, 0), Math_PI / 6) * xform.basis; + AABB rot_aabb = xform.xform(aabb); + float m = MAX(rot_aabb.size.x, rot_aabb.size.y) * 0.5; + if (m == 0) { + textures.push_back(Ref()); + continue; + } + xform.origin = -xform.basis.xform(ofs); //-ofs*m; + xform.origin.z -= rot_aabb.size.z * 2; + xform.invert(); + xform = mesh_xform * xform; + + RS::get_singleton()->camera_set_transform(camera, xform * Transform3D(Basis(), Vector3(0, 0, 3))); + RS::get_singleton()->camera_set_orthogonal(camera, m * 2, 0.01, 1000.0); + + RS::get_singleton()->instance_set_transform(light_instance, xform * Transform3D().looking_at(Vector3(-2, -1, -1), Vector3(0, 1, 0))); + RS::get_singleton()->instance_set_transform(light_instance2, xform * Transform3D().looking_at(Vector3(+1, -1, -2), Vector3(0, 1, 0))); + + ep.step(TTR("Thumbnail..."), i); + DisplayServer::get_singleton()->process_events(); + Main::iteration(); + Main::iteration(); + Ref img = RS::get_singleton()->texture_2d_get(viewport_texture); + ERR_CONTINUE(!img.is_valid() || img->is_empty()); + Ref it = ImageTexture::create_from_image(img); + + RS::get_singleton()->free(inst); + + textures.push_back(it); + } + + RS::get_singleton()->free(viewport); + RS::get_singleton()->free(light); + RS::get_singleton()->free(light_instance); + RS::get_singleton()->free(light2); + RS::get_singleton()->free(light_instance2); + RS::get_singleton()->free(camera); + RS::get_singleton()->free(scenario); + + return textures; +} + +void EditorInterface::set_plugin_enabled(const String &p_plugin, bool p_enabled) { + EditorNode::get_singleton()->set_addon_plugin_enabled(p_plugin, p_enabled, true); +} + +bool EditorInterface::is_plugin_enabled(const String &p_plugin) const { + return EditorNode::get_singleton()->is_addon_plugin_enabled(p_plugin); +} + +// Editor GUI. + +Control *EditorInterface::get_base_control() const { + return EditorNode::get_singleton()->get_gui_base(); +} + +VBoxContainer *EditorInterface::get_editor_main_screen() const { + return EditorNode::get_singleton()->get_main_screen_control(); +} + +ScriptEditor *EditorInterface::get_script_editor() const { + return ScriptEditor::get_singleton(); +} + +void EditorInterface::set_main_screen_editor(const String &p_name) { + EditorNode::get_singleton()->select_editor_by_name(p_name); +} + +void EditorInterface::set_distraction_free_mode(bool p_enter) { + EditorNode::get_singleton()->set_distraction_free_mode(p_enter); +} + +bool EditorInterface::is_distraction_free_mode_enabled() const { + return EditorNode::get_singleton()->is_distraction_free_mode_enabled(); +} + +float EditorInterface::get_editor_scale() const { + return EDSCALE; +} + +// Editor docks. + +FileSystemDock *EditorInterface::get_file_system_dock() const { + return FileSystemDock::get_singleton(); +} + +void EditorInterface::select_file(const String &p_file) { + FileSystemDock::get_singleton()->select_file(p_file); +} + +Vector EditorInterface::get_selected_paths() const { + return FileSystemDock::get_singleton()->get_selected_paths(); +} + +String EditorInterface::get_current_path() const { + return FileSystemDock::get_singleton()->get_current_path(); +} + +String EditorInterface::get_current_directory() const { + return FileSystemDock::get_singleton()->get_current_directory(); +} + +EditorInspector *EditorInterface::get_inspector() const { + return InspectorDock::get_inspector_singleton(); +} + +// Object/Resource/Node editing. + +void EditorInterface::inspect_object(Object *p_obj, const String &p_for_property, bool p_inspector_only) { + EditorNode::get_singleton()->push_item(p_obj, p_for_property, p_inspector_only); +} + +void EditorInterface::edit_resource(const Ref &p_resource) { + EditorNode::get_singleton()->edit_resource(p_resource); +} + +void EditorInterface::edit_node(Node *p_node) { + EditorNode::get_singleton()->edit_node(p_node); +} + +void EditorInterface::edit_script(const Ref