12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- class_name IntegerScalingControl extends Control
- @export
- var auto_scaling_rate:bool = true
- @export_range(0, 65535)
- var fixed_scaling_rate:int = 1
- 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 final_scale: Vector2i
-
- if auto_scaling_rate:
- var possible_scale := window_size / Vector2i(raw_size)
- final_scale = max(1, min(possible_scale.x, possible_scale.y)) * Vector2i.ONE
- else:
- final_scale = Vector2i.ONE * fixed_scaling_rate
- 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
|