newlon/addons/pvzadventure/scripts/packed_scene_preview_generator.gd
2025-07-17 20:28:47 +05:00

42 lines
1.1 KiB
GDScript

@tool
extends TextureRect
@onready var subview = $Viewport
@onready var camera = $Viewport/Camera2D
var scene : Node
func set_path(path):
if scene:
scene.queue_free()
if ResourceLoader.exists(path) == false:
return
var packed_scene : PackedScene = load(path)
render_scene(path, packed_scene)
func render_scene(path: String,packed : PackedScene):
scene = packed.instantiate()
subview.add_child(scene)
var bb = get_bounding_box(scene)
subview.size = Vector2(max(bb.size.x,bb.size.y),max(bb.size.x,bb.size.y))
camera.position = bb.position * Vector2(0.5,0.5)
var vtexture : ViewportTexture = subview.get_texture()
texture = vtexture
func get_bounding_box(root: Node2D) -> Rect2:
var rect := Rect2();
var children := root.get_children();
while children:
var child = children.pop_back();
children.append_array(child.get_children());
if child.has_method('get_rect') and child.has_method('to_global'):
var child_rect := child.get_rect() as Rect2;
child_rect.position = child.to_global(child_rect.position);
rect = rect.merge(child_rect);
return rect;