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

Fix for Objects inside CanvasLayers are not visible in camera preview #14

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 38 additions & 2 deletions addons/anthonyec.camera_preview/preview.gd
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const min_panel_size: float = 250
@onready var viewport_margin_container: MarginContainer = %ViewportMarginContainer
@onready var overlay_margin_container: MarginContainer = %OverlayMarginContainer
@onready var overlay_container: Control = %OverlayContainer
@onready var ui_draw: CanvasLayer = $Panel/SubViewport/UIDraw

var camera_type: CameraType = CameraType.CAMERA_3D
var pinned_position: PinnedPosition = PinnedPosition.RIGHT
Expand Down Expand Up @@ -209,6 +210,7 @@ func _process(_delta: float) -> void:
preview_camera_3d.v_offset = selected_camera_3d.v_offset
preview_camera_3d.attributes = selected_camera_3d.attributes
preview_camera_3d.environment = selected_camera_3d.environment
redraw_ui(selected_camera_3d)

if camera_type == CameraType.CAMERA_2D and selected_camera_2d:
var project_window_size = get_project_window_size()
Expand All @@ -234,6 +236,7 @@ func _process(_delta: float) -> void:
preview_camera_2d.limit_right = selected_camera_2d.limit_right
preview_camera_2d.limit_top = selected_camera_2d.limit_top
preview_camera_2d.limit_bottom = selected_camera_2d.limit_bottom
redraw_ui(selected_camera_2d)

func link_with_camera_3d(camera_3d: Camera3D) -> void:
# TODO: Camera may not be ready since this method is called in `_enter_tree`
Expand All @@ -257,27 +260,43 @@ func link_with_camera_3d(camera_3d: Camera3D) -> void:

selected_camera_3d = camera_3d
camera_type = CameraType.CAMERA_3D
redraw_ui(selected_camera_3d)

func link_with_camera_2d(camera_2d: Camera2D) -> void:
if not preview_camera_2d:
return request_hide()

var is_different_camera = camera_2d != preview_camera_2d

# TODO: A bit messy.
if is_different_camera:

if preview_camera_2d.tree_exiting.is_connected(unlink_camera):
preview_camera_2d.tree_exiting.disconnect(unlink_camera)

if not camera_2d.tree_exiting.is_connected(unlink_camera):
camera_2d.tree_exiting.connect(unlink_camera)


sub_viewport.disable_3d = true
sub_viewport.world_2d = camera_2d.get_world_2d()

selected_camera_2d = camera_2d
camera_type = CameraType.CAMERA_2D
redraw_ui(selected_camera_2d)

func redraw_ui(cameraSelected) -> void:
var obj = get_main_scene(cameraSelected)
if obj:
if(ui_draw.get_children()):
for child in ui_draw.get_children():
ui_draw.remove_child(child)
child.queue_free()
for child in findByType(obj,CanvasLayer):
var newObj = child.duplicate()
ui_draw.add_child(newObj)
for child in findByType(obj,Control):
var newObj = child.duplicate()
ui_draw.add_child(newObj)

func unlink_camera() -> void:
if selected_camera_3d:
selected_camera_3d = null
Expand Down Expand Up @@ -402,3 +421,20 @@ func _on_drag_handle_button_up() -> void:

func _on_lock_button_pressed() -> void:
is_locked = !is_locked


var listOfAllNodesInTree = []
func findByType(parent, type):
listOfAllNodesInTree.clear()
for child in parent.get_children():
if is_instance_of(child, type):
listOfAllNodesInTree.append(child)
return listOfAllNodesInTree

func get_main_scene(obj):
if is_instance_of( obj ,SubViewport):
return null
if is_instance_of(obj.get_parent(),SubViewport):
return obj
else:
return get_main_scene(obj.get_parent())
48 changes: 26 additions & 22 deletions addons/anthonyec.camera_preview/preview.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[ext_resource type="Texture2D" uid="uid://btc01wc11tiid" path="res://addons/anthonyec.camera_preview/GuiResizerTopLeft.svg" id="2_t64ej"]
[ext_resource type="Texture2D" uid="uid://04l05jxuyt7k" path="res://addons/anthonyec.camera_preview/GuiResizerTopRight.svg" id="3_6yuab"]

[sub_resource type="ViewportTexture" id="ViewportTexture_hchdq"]
[sub_resource type="ViewportTexture" id="ViewportTexture_k8axx"]
viewport_path = NodePath("Panel/SubViewport")

[sub_resource type="Gradient" id="Gradient_11p6r"]
Expand Down Expand Up @@ -54,10 +54,10 @@ anchor_left = 1.0
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = -520.0
offset_top = -908.889
offset_right = -20.0
offset_bottom = -20.0
offset_left = -260.0
offset_top = -150.0
offset_right = -10.0
offset_bottom = -10.0
grow_horizontal = 0
grow_vertical = 0
pivot_offset = Vector2(450, 300)
Expand All @@ -76,6 +76,8 @@ current = true
unique_name_in_owner = true
ignore_rotation = false

[node name="UIDraw" type="CanvasLayer" parent="Panel/SubViewport"]

[node name="ViewportMarginContainer" type="MarginContainer" parent="Panel"]
unique_name_in_owner = true
clip_contents = true
Expand All @@ -86,15 +88,15 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
theme_override_constants/margin_left = 4
theme_override_constants/margin_top = 4
theme_override_constants/margin_right = 4
theme_override_constants/margin_bottom = 4
theme_override_constants/margin_left = 2
theme_override_constants/margin_top = 2
theme_override_constants/margin_right = 2
theme_override_constants/margin_bottom = 2

[node name="TextureRect" type="TextureRect" parent="Panel/ViewportMarginContainer"]
unique_name_in_owner = true
layout_mode = 2
texture = SubResource("ViewportTexture_hchdq")
texture = SubResource("ViewportTexture_k8axx")
expand_mode = 1

[node name="Gradient" type="TextureRect" parent="Panel"]
Expand All @@ -118,10 +120,10 @@ anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
theme_override_constants/margin_left = 4
theme_override_constants/margin_top = 4
theme_override_constants/margin_right = 4
theme_override_constants/margin_bottom = 4
theme_override_constants/margin_left = 2
theme_override_constants/margin_top = 2
theme_override_constants/margin_right = 2
theme_override_constants/margin_bottom = 2

[node name="OverlayContainer" type="Control" parent="Panel/OverlayMarginContainer"]
unique_name_in_owner = true
Expand All @@ -143,8 +145,8 @@ flat = true
unique_name_in_owner = true
visible = false
layout_mode = 1
offset_right = 60.0
offset_bottom = 60.0
offset_right = 30.0
offset_bottom = 30.0
size_flags_horizontal = 0
size_flags_vertical = 0
mouse_default_cursor_shape = 12
Expand All @@ -160,9 +162,10 @@ layout_mode = 1
anchors_preset = 1
anchor_left = 1.0
anchor_right = 1.0
offset_left = -60.0
offset_bottom = 60.0
pivot_offset = Vector2(60, 60)
offset_left = -30.0
offset_bottom = 30.0
grow_horizontal = 0
pivot_offset = Vector2(30, 30)
size_flags_horizontal = 8
size_flags_vertical = 0
mouse_default_cursor_shape = 11
Expand All @@ -178,9 +181,10 @@ layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_top = -60.0
offset_right = 60.0
pivot_offset = Vector2(0, 60)
offset_top = -30.0
offset_right = 30.0
grow_vertical = 0
pivot_offset = Vector2(0, 30)
size_flags_horizontal = 0
size_flags_vertical = 8
tooltip_text = "Always Show Preview"
Expand Down
1 change: 0 additions & 1 deletion demo/addons/anthonyec.camera_preview

This file was deleted.

24 changes: 23 additions & 1 deletion demo/main_2d.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,29 @@ scale = Vector2(0.124943, 0.124943)
texture = ExtResource("2_fk71s")

[node name="Camera2D2" type="Camera2D" parent="."]
position = Vector2(162, 227)
position = Vector2(119, 166)
rotation = -0.268458
ignore_rotation = false
zoom = Vector2(2, 2)

[node name="test" type="CanvasLayer" parent="."]

[node name="Label" type="Label" parent="test"]
offset_left = 123.0
offset_top = 40.0
offset_right = 1220.0
offset_bottom = 206.0
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_font_sizes/font_size = 121
text = "Hello 2D"

[node name="Node2D" type="Node2D" parent="."]

[node name="Label" type="Label" parent="Node2D"]
offset_left = 438.0
offset_top = 378.0
offset_right = 3558.0
offset_bottom = 693.0
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_font_sizes/font_size = 231
text = "Hello Not to render"
11 changes: 11 additions & 0 deletions demo/main_3d.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ fov = 69.4097
[node name="Camera3D3" type="Camera3D" parent="."]
transform = Transform3D(0.716911, 0.00464808, 0.69715, 1.1914e-10, 0.999978, -0.00666711, -0.697165, 0.00477972, 0.716895, 8.21847, 1.01507, 7.42971)
fov = 19.3989

[node name="test" type="CanvasLayer" parent="."]

[node name="Label" type="Label" parent="test"]
offset_left = 123.0
offset_top = 40.0
offset_right = 1220.0
offset_bottom = 206.0
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_font_sizes/font_size = 121
text = "hello3D"