Skip to content

Commit

Permalink
Improve the UX of ViewportTexture in the editor
Browse files Browse the repository at this point in the history
The associated `ViewportTexture`s will update the `viewport_path`
in time when the `Viewport`'s nodepath is changed (caused by renaming
the node names or moving in the SceneTree dock).

If the target `Viewport` is changed by resetting the `viewport_path`,
the `ViewportTexture`s will be re-setup and emit `changed` signal in
time.

(cherry picked from commit af58f1e)
  • Loading branch information
Rindbee authored and akien-mga committed May 12, 2023
1 parent 1146172 commit b91b8fc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/classes/ViewportTexture.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<members>
<member name="viewport_path" type="NodePath" setter="set_viewport_path_in_scene" getter="get_viewport_path_in_scene" default="NodePath(&quot;&quot;)">
The path to the [Viewport] node to display. This is relative to the scene root, not to the node which uses the texture.
[b]Note:[/b] In the editor, it is automatically updated when the target viewport's node path changes due to renaming or moving the viewport or its ancestors. At runtime, it may not be able to automatically update due to the inability to determine the scene root.
</member>
</members>
</class>
1 change: 0 additions & 1 deletion editor/editor_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3998,7 +3998,6 @@ void EditorPropertyResource::_viewport_selected(const NodePath &p_path) {
Ref<ViewportTexture> vt;
vt.instantiate();
vt->set_viewport_path_in_scene(get_tree()->get_edited_scene_root()->get_path_to(to_node));
vt->setup_local_to_scene();

emit_changed(get_edited_property(), vt);
update_property();
Expand Down
46 changes: 43 additions & 3 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ void ViewportTexture::setup_local_to_scene() {

if (vp) {
vp->viewport_textures.erase(this);
vp = nullptr;
}

vp = nullptr;

if (loc_scene->is_ready()) {
_setup_local_to_scene(loc_scene);
} else {
Expand All @@ -91,8 +90,24 @@ void ViewportTexture::set_viewport_path_in_scene(const NodePath &p_path) {

path = p_path;

if (get_local_scene()) {
if (vp) {
vp->viewport_textures.erase(this);
vp = nullptr;
}

if (proxy_ph.is_valid()) {
RS::get_singleton()->free(proxy_ph);
}
if (proxy.is_valid()) {
RS::get_singleton()->free(proxy);
}
proxy_ph = RID();
proxy = RID();

if (get_local_scene() && !path.is_empty()) {
setup_local_to_scene();
} else {
emit_changed();
}
}

Expand Down Expand Up @@ -171,6 +186,8 @@ void ViewportTexture::_setup_local_to_scene(const Node *p_loc_scene) {
proxy = RS::get_singleton()->texture_proxy_create(vp->texture_rid);
}
vp_pending = false;

emit_changed();
}

void ViewportTexture::_bind_methods() {
Expand Down Expand Up @@ -408,9 +425,28 @@ int Viewport::_sub_window_find(Window *p_window) {
return -1;
}

void Viewport::_update_viewport_path() {
if (viewport_textures.is_empty()) {
return;
}

Node *scene_root = get_scene_file_path().is_empty() ? get_owner() : this;
if (!scene_root && is_inside_tree()) {
scene_root = get_tree()->get_edited_scene_root();
}
if (scene_root && (scene_root == this || scene_root->is_ancestor_of(this))) {
NodePath path_in_scene = scene_root->get_path_to(this);
for (ViewportTexture *E : viewport_textures) {
E->path = path_in_scene;
}
}
}

void Viewport::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_update_viewport_path();

if (get_parent()) {
parent = get_parent()->get_viewport();
RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, parent->get_viewport_rid());
Expand Down Expand Up @@ -503,6 +539,10 @@ void Viewport::_notification(int p_what) {
RenderingServer::get_singleton()->viewport_set_parent_viewport(viewport, RID());
} break;

case NOTIFICATION_PATH_RENAMED: {
_update_viewport_path();
} break;

case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (!get_tree()) {
return;
Expand Down
2 changes: 2 additions & 0 deletions scene/main/viewport.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ class Viewport : public Node {
Ref<ViewportTexture> default_texture;
HashSet<ViewportTexture *> viewport_textures;

void _update_viewport_path();

SDFOversize sdf_oversize = SDF_OVERSIZE_120_PERCENT;
SDFScale sdf_scale = SDF_SCALE_50_PERCENT;

Expand Down

0 comments on commit b91b8fc

Please sign in to comment.