Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose editor viewports in EditorInterface #68696

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/classes/EditorInterface.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@
[b]Note:[/b] When creating custom editor UI, prefer accessing theme items directly from your GUI nodes using the [code]get_theme_*[/code] methods.
</description>
</method>
<method name="get_editor_viewport_2d" qualifiers="const">
<return type="SubViewport" />
<description>
Returns the 2D editor [SubViewport]. It does not have a camera. Instead, the view transforms are done directly and can be accessed with [member Viewport.global_canvas_transform].
</description>
</method>
<method name="get_editor_viewport_3d" qualifiers="const">
<return type="SubViewport" />
<param index="0" name="idx" type="int" default="0" />
<description>
Returns the specified 3D editor [SubViewport], from [code]0[/code] to [code]3[/code]. The viewport can be used to access the active editor cameras with [method Viewport.get_camera_3d].
</description>
</method>
<method name="get_file_system_dock" qualifiers="const">
<return type="FileSystemDock" />
<description>
Expand Down
12 changes: 12 additions & 0 deletions editor/editor_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "editor/filesystem_dock.h"
#include "editor/gui/editor_run_bar.h"
#include "editor/inspector_dock.h"
#include "editor/plugins/node_3d_editor_plugin.h"
#include "main/main.h"
#include "scene/gui/box_container.h"
#include "scene/gui/control.h"
Expand Down Expand Up @@ -213,6 +214,15 @@ ScriptEditor *EditorInterface::get_script_editor() const {
return ScriptEditor::get_singleton();
}

SubViewport *EditorInterface::get_editor_viewport_2d() const {
return EditorNode::get_singleton()->get_scene_root();
}

SubViewport *EditorInterface::get_editor_viewport_3d(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, static_cast<int>(Node3DEditor::VIEWPORTS_COUNT), nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ERR_FAIL_INDEX_V(p_idx, static_cast<int>(Node3DEditor::VIEWPORTS_COUNT), nullptr);
ERR_FAIL_INDEX_V(p_idx, int(Node3DEditor::VIEWPORTS_COUNT), nullptr);

Assuming the casting is required, this should work too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this line directly from Node3DEditorViewport::get_editor_viewport(). VIEWPORTS_COUNT is an unsigned int.

Isn't static_cast<int> preferred for C++ rather than C cast style?
https://stackoverflow.com/questions/1609163/what-is-the-difference-between-static-cast-and-c-style-casting

If you confirm C style cast is desired, I'll make the change.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen C cast used more often.

Copy link
Member

@akien-mga akien-mga Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nitpicky, but indeed we prefer C style casts throughout the codebase, unless there's a real ambiguity that a dedicated C++ casting expression would resolve.

We'd actually style it like this usually, which we find the most readable.

Suggested change
ERR_FAIL_INDEX_V(p_idx, static_cast<int>(Node3DEditor::VIEWPORTS_COUNT), nullptr);
ERR_FAIL_INDEX_V(p_idx, (int)Node3DEditor::VIEWPORTS_COUNT, nullptr);

Edit: I see this was likely taken from:

	Node3DEditorViewport *get_editor_viewport(int p_idx) {
		ERR_FAIL_INDEX_V(p_idx, static_cast<int>(VIEWPORTS_COUNT), nullptr);
		return viewports[p_idx];
	}

so I guess there was a precedent for this cast :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I copied the line from the downstream function. I wasn't partial and was looking for firm direction. Now I know C style is preferred. Thanks for addressing the PR.

return Node3DEditor::get_singleton()->get_editor_viewport(p_idx)->get_viewport_node();
TokisanGames marked this conversation as resolved.
Show resolved Hide resolved
}

void EditorInterface::set_main_screen_editor(const String &p_name) {
EditorNode::get_singleton()->select_editor_by_name(p_name);
}
Expand Down Expand Up @@ -414,6 +424,8 @@ void EditorInterface::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_base_control"), &EditorInterface::get_base_control);
ClassDB::bind_method(D_METHOD("get_editor_main_screen"), &EditorInterface::get_editor_main_screen);
ClassDB::bind_method(D_METHOD("get_script_editor"), &EditorInterface::get_script_editor);
ClassDB::bind_method(D_METHOD("get_editor_viewport_2d"), &EditorInterface::get_editor_viewport_2d);
ClassDB::bind_method(D_METHOD("get_editor_viewport_3d", "idx"), &EditorInterface::get_editor_viewport_3d, DEFVAL(0));

ClassDB::bind_method(D_METHOD("set_main_screen_editor", "name"), &EditorInterface::set_main_screen_editor);
ClassDB::bind_method(D_METHOD("set_distraction_free_mode", "enter"), &EditorInterface::set_distraction_free_mode);
Expand Down
3 changes: 3 additions & 0 deletions editor/editor_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class FileSystemDock;
class Mesh;
class Node;
class ScriptEditor;
class SubViewport;
class Texture2D;
class Theme;
class VBoxContainer;
Expand Down Expand Up @@ -92,6 +93,8 @@ class EditorInterface : public Object {
Control *get_base_control() const;
VBoxContainer *get_editor_main_screen() const;
ScriptEditor *get_script_editor() const;
SubViewport *get_editor_viewport_2d() const;
SubViewport *get_editor_viewport_3d(int p_idx = 0) const;

void set_main_screen_editor(const String &p_name);
void set_distraction_free_mode(bool p_enter);
Expand Down
Loading