Эх сурвалжийг харах

feat: integer_scaling_control.gd

LanzaSchneider 1 жил өмнө
parent
commit
7286e90cd2

+ 33 - 0
components/ui/integer_scaling_control.gd

@@ -0,0 +1,33 @@
+class_name IntegerScalingControl extends Control
+
+var _origin_size: Vector2
+
+func _ready() -> void:
+	_origin_size = size
+	layout_mode = 1
+	get_viewport().size_changed.connect(_on_screen_resized, CONNECT_DEFERRED)
+	_on_screen_resized()
+
+func _on_screen_resized() -> void:
+	var window_size: Vector2i = get_viewport().size
+	var raw_size := Vector2(ProjectSettings["display/window/size/viewport_width"], ProjectSettings["display/window/size/viewport_height"])
+	var possible_scale := window_size / Vector2i(raw_size)
+	var final_scale: Vector2i = max(1, min(possible_scale.x, possible_scale.y)) * Vector2i.ONE
+	scale = final_scale
+	
+	var window_size_unscaled := Vector2(window_size.x / final_scale.x, window_size.y / final_scale.y)
+	var diff_x :float = window_size_unscaled.x - _origin_size.x
+	var diff_y :float = window_size_unscaled.y - _origin_size.y
+
+	anchor_left = 0.5
+	anchor_right = 0.5
+	anchor_top = 0.5
+	anchor_bottom = 0.5
+	offset_left = -(_origin_size.x + diff_x) / 2
+	offset_right = (_origin_size.x + diff_x) / 2
+	offset_top = -(_origin_size.y + diff_y) / 2
+	offset_bottom = (_origin_size.y + diff_y) / 2
+	grow_horizontal = Control.GROW_DIRECTION_BOTH
+	grow_vertical = Control.GROW_DIRECTION_BOTH
+	
+	pivot_offset = size / 2