diff --git a/addons/anthonyec.camera_preview/preview.gd b/addons/anthonyec.camera_preview/preview.gd index 3c07d04..d3cc6cd 100644 --- a/addons/anthonyec.camera_preview/preview.gd +++ b/addons/anthonyec.camera_preview/preview.gd @@ -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 @@ -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() @@ -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` @@ -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 @@ -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()) diff --git a/addons/anthonyec.camera_preview/preview.tscn b/addons/anthonyec.camera_preview/preview.tscn index 77b3ced..4c8754c 100644 --- a/addons/anthonyec.camera_preview/preview.tscn +++ b/addons/anthonyec.camera_preview/preview.tscn @@ -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"] @@ -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) @@ -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 @@ -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"] @@ -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 @@ -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 @@ -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 @@ -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" diff --git a/demo/addons/anthonyec.camera_preview b/demo/addons/anthonyec.camera_preview deleted file mode 120000 index a939673..0000000 --- a/demo/addons/anthonyec.camera_preview +++ /dev/null @@ -1 +0,0 @@ -/Users/anthony/workspace/misc/240101_godot_camera_preview/addons/anthonyec.camera_preview \ No newline at end of file diff --git a/demo/main_2d.tscn b/demo/main_2d.tscn index 96ff74a..1e723e1 100644 --- a/demo/main_2d.tscn +++ b/demo/main_2d.tscn @@ -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" diff --git a/demo/main_3d.tscn b/demo/main_3d.tscn index e8ed806..d6ae9d4 100644 --- a/demo/main_3d.tscn +++ b/demo/main_3d.tscn @@ -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"