Ninepatch Rect Drop Shadow Panel Script

Script that allows the panel to automatically resize and position based on the size and position of the parent node, allowing for a drop shadow to be easily added to a ninepatch rect in Godot 3
 avatar
HoppersEcho
plain_text
a year ago
1.1 kB
110
Indexable
class_name DropShadowPanel
extends MarginContainer

export var inset :int = 4
export var drop_amount :int = 8
export var top_buffer :int = 0
export var shadow_size :int = 2


func _ready() -> void:
	hide()
	_connect_signals()
	_test()
	yield(get_owner(), "ready")
	setup(get_parent())
#	show()


# Manually connect signals at ready
func _connect_signals() -> void:
	pass


func _test() -> void:
	pass


func setup(node :Control) -> void:
	hide()
	recalculate_size(node)
	var panel :StyleBox = $Panel.get_stylebox("panel", "")
	print(panel, " found")
	panel.set_shadow_size(shadow_size)
#	$Panel.set("custom_styles/panel:shadow_size", shadow_size)
	show()


func recalculate_size(node :Control) -> void:
	print("Setting up shadow panel in relation to %s | Node position: %s" % [node, node.rect_position])
	rect_min_size = node.rect_size
	rect_min_size.x -= inset * 2.0
	if top_buffer > 0:
		rect_min_size.y -= top_buffer
		drop_amount += top_buffer
	if is_instance_valid(self):
		yield(get_tree(), "idle_frame")
	rect_position = Vector2(inset, drop_amount)
	rect_size = Vector2.ZERO