tochie-kit/nodes/FittingSprite.gd

28 lines
915 B
GDScript

tool
extends Sprite
class_name TK_FittingSprite
## A Sprite that is resized to absolute pixel size, no matter the texture.
const NO_SIZE_SENTINEL = Vector2(-1, -1)
export var target_size: Vector2 = NO_SIZE_SENTINEL setget _set_target_size
# todo: Ask context about preferred sizing if there's any.
func _enter_tree() -> void:
# It's okay to fail here for non-grid contexts.
if get_parent().get_parent().connect("cell_size_changed", self, "_update_cell_size"):
_set_target_size(get_parent().get_parent().cell_size)
elif target_size == NO_SIZE_SENTINEL:
_set_target_size(texture.get_size())
func _set_target_size(p_size: Vector2) -> void:
target_size = p_size
_update_scale()
func _update_scale() -> void:
scale = target_size / texture.get_size()
func _update_cell_size(_p_old_cell_size: Vector2, p_new_cell_size: Vector2) -> void:
_set_target_size(p_new_cell_size)