integer_scaling_control.gd 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. class_name IntegerScalingControl extends Control
  2. var _origin_size: Vector2
  3. func _ready() -> void:
  4. _origin_size = size
  5. layout_mode = 1
  6. get_viewport().size_changed.connect(_on_screen_resized, CONNECT_DEFERRED)
  7. _on_screen_resized()
  8. func _on_screen_resized() -> void:
  9. var window_size: Vector2i = get_viewport().size
  10. var raw_size := Vector2(ProjectSettings["display/window/size/viewport_width"], ProjectSettings["display/window/size/viewport_height"])
  11. var possible_scale := window_size / Vector2i(raw_size)
  12. var final_scale: Vector2i = max(1, min(possible_scale.x, possible_scale.y)) * Vector2i.ONE
  13. scale = final_scale
  14. var window_size_unscaled := Vector2(window_size.x / final_scale.x, window_size.y / final_scale.y)
  15. var diff_x :float = window_size_unscaled.x - _origin_size.x
  16. var diff_y :float = window_size_unscaled.y - _origin_size.y
  17. anchor_left = 0.5
  18. anchor_right = 0.5
  19. anchor_top = 0.5
  20. anchor_bottom = 0.5
  21. offset_left = -(_origin_size.x + diff_x) / 2
  22. offset_right = (_origin_size.x + diff_x) / 2
  23. offset_top = -(_origin_size.y + diff_y) / 2
  24. offset_bottom = (_origin_size.y + diff_y) / 2
  25. grow_horizontal = Control.GROW_DIRECTION_BOTH
  26. grow_vertical = Control.GROW_DIRECTION_BOTH
  27. pivot_offset = size / 2