SummerDay/addons/summer_day/tool/canvas/canvas_state.gd

55 lines
1.6 KiB
GDScript

extends Resource
class_name SummerDayCanvasState
signal fill_transform_changed(fill_transform)
signal canvas_transform_changed(canvas_transform)
signal view_transform_changed(view_transform)
var rect_size: Vector2
var fill_transform := Transform2D.IDENTITY
var canvas_transform := Transform2D.IDENTITY setget _set_canvas_transform
var view_transform := Transform2D.IDENTITY # fill_transform * canvas_transform
func _init():
connect("fill_transform_changed", self, "_update_view_transformation")
connect("canvas_transform_changed", self, "_update_view_transformation")
func update_fill_transform(scene):
if scene == null:
fill_transform = Transform2D.IDENTITY
else:
var aspect_ratio = scene.aspect_ratio.x / scene.aspect_ratio.y
var size_ratio = rect_size.x / rect_size.y
var scale
var translation = Vector2.ZERO
if aspect_ratio <= size_ratio:
scale = rect_size.y
translation.x = rect_size.x * 0.5 - aspect_ratio * scale * 0.5
else:
scale = rect_size.x / aspect_ratio
translation.y = rect_size.y * 0.5 - scale * 0.5
fill_transform = Transform2D.IDENTITY.scaled(Vector2(scale, scale))
fill_transform = fill_transform.translated(translation)
fill_transform = Transform2D(
Vector2(scale, 0.0),
Vector2(0.0, scale),
translation
)
emit_signal("fill_transform_changed", fill_transform)
func _update_view_transformation(transform):
view_transform = canvas_transform * fill_transform
emit_signal("view_transform_changed", view_transform)
func _set_canvas_transform(new_canvas_transform):
canvas_transform = new_canvas_transform
emit_signal("canvas_transform_changed", canvas_transform)