integer_scaling_control.gd 1.4 KB

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