|
| 1 | +# The root Control node ("Main") and AspectRatioContainer nodes are the most important |
| 2 | +# pieces of this demo. |
| 3 | +# Both nodes have their Layout set to Full Rect |
| 4 | +# (with their rect spread across the whole viewport, and Anchor set to Full Rect). |
| 5 | +extends Control |
| 6 | + |
| 7 | +var base_window_size = Vector2(ProjectSettings.get_setting("display/window/size/width"), ProjectSettings.get_setting("display/window/size/height")) |
| 8 | + |
| 9 | +# These defaults match this demo's project settings. Adjust as needed if adapting this |
| 10 | +# in your own project. |
| 11 | +var stretch_mode = SceneTree.STRETCH_MODE_2D |
| 12 | +var stretch_aspect = SceneTree.STRETCH_ASPECT_EXPAND |
| 13 | + |
| 14 | +var scale_factor = 1.0 |
| 15 | +var gui_aspect_ratio = -1.0 |
| 16 | +var gui_margin = 0.0 |
| 17 | + |
| 18 | +onready var panel = $Panel |
| 19 | +onready var arc = $Panel/AspectRatioContainer |
| 20 | + |
| 21 | + |
| 22 | +func _ready(): |
| 23 | + # The `resized` signal will be emitted when the window size changes, as the root Control node |
| 24 | + # is resized whenever the window size changes. This is because the root Control node |
| 25 | + # uses a Full Rect anchor, so its size will always be equal to the window size. |
| 26 | + # warning-ignore:return_value_discarded |
| 27 | + connect("resized", self, "_on_resized") |
| 28 | + update_container() |
| 29 | + |
| 30 | + |
| 31 | +func update_container(): |
| 32 | + # The code within this function needs to be run twice to work around an issue with containers |
| 33 | + # having a 1-frame delay with updates. |
| 34 | + # Otherwise, `panel.rect_size` returns a value of the previous frame, which results in incorrect |
| 35 | + # sizing of the inner AspectRatioContainer when using the Fit to Window setting. |
| 36 | + for _i in 2: |
| 37 | + if is_equal_approx(gui_aspect_ratio, -1.0): |
| 38 | + # Fit to Window. Tell the AspectRatioContainer to use the same aspect ratio as the window, |
| 39 | + # making the AspectRatioContainer not have any visible effect. |
| 40 | + arc.ratio = panel.rect_size.aspect() |
| 41 | + # Apply GUI margin on the AspectRatioContainer's parent (Panel). |
| 42 | + # This also makes the GUI margin apply on controls located outside the AspectRatioContainer |
| 43 | + # (such as the inner side label in this demo). |
| 44 | + panel.margin_top = gui_margin |
| 45 | + panel.margin_bottom = -gui_margin |
| 46 | + else: |
| 47 | + # Constrained aspect ratio. |
| 48 | + arc.ratio = min(panel.rect_size.aspect(), gui_aspect_ratio) |
| 49 | + # Adjust top and bottom margins relative to the aspect ratio when it's constrained. |
| 50 | + # This ensures that GUI margin settings behave exactly as if the window had the |
| 51 | + # original aspect ratio size. |
| 52 | + panel.margin_top = gui_margin / gui_aspect_ratio |
| 53 | + panel.margin_bottom = -gui_margin / gui_aspect_ratio |
| 54 | + |
| 55 | + panel.margin_left = gui_margin |
| 56 | + panel.margin_right = -gui_margin |
| 57 | + |
| 58 | + |
| 59 | +func _on_gui_aspect_ratio_item_selected(index): |
| 60 | + match index: |
| 61 | + 0: # Fit to Window |
| 62 | + gui_aspect_ratio = -1.0 |
| 63 | + 1: # 5:4 |
| 64 | + gui_aspect_ratio = 5.0 / 4.0 |
| 65 | + 2: # 4:3 |
| 66 | + gui_aspect_ratio = 4.0 / 3.0 |
| 67 | + 3: # 3:2 |
| 68 | + gui_aspect_ratio = 3.0 / 2.0 |
| 69 | + 4: # 16:10 |
| 70 | + gui_aspect_ratio = 16.0 / 10.0 |
| 71 | + 5: # 16:9 |
| 72 | + gui_aspect_ratio = 16.0 / 9.0 |
| 73 | + 6: # 21:9 |
| 74 | + gui_aspect_ratio = 21.0 / 9.0 |
| 75 | + |
| 76 | + update_container() |
| 77 | + |
| 78 | + |
| 79 | +func _on_resized(): |
| 80 | + update_container() |
| 81 | + |
| 82 | + |
| 83 | +func _on_gui_margin_drag_ended(_value_changed): |
| 84 | + gui_margin = $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/GUIMargin/HSlider".value |
| 85 | + $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/GUIMargin/Value".text = str(gui_margin) |
| 86 | + update_container() |
| 87 | + |
| 88 | + |
| 89 | +func _on_window_base_size_item_selected(index): |
| 90 | + match index: |
| 91 | + 0: # 648×648 (1:1) |
| 92 | + base_window_size = Vector2(648, 648) |
| 93 | + 1: # 640×480 (4:3) |
| 94 | + base_window_size = Vector2(640, 480) |
| 95 | + 2: # 720×480 (3:2) |
| 96 | + base_window_size = Vector2(720, 480) |
| 97 | + 3: # 800×600 (4:3) |
| 98 | + base_window_size = Vector2(800, 600) |
| 99 | + 4: # 1152×648 (16:9) |
| 100 | + base_window_size = Vector2(1152, 648) |
| 101 | + 5: # 1280×720 (16:9) |
| 102 | + base_window_size = Vector2(1280, 720) |
| 103 | + 6: # 1280×800 (16:10) |
| 104 | + base_window_size = Vector2(1280, 800) |
| 105 | + 7: # 1680×720 (21:9) |
| 106 | + base_window_size = Vector2(1680, 720) |
| 107 | + |
| 108 | + get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor) |
| 109 | + update_container() |
| 110 | + |
| 111 | + |
| 112 | +func _on_window_stretch_mode_item_selected(index): |
| 113 | + stretch_mode = index |
| 114 | + get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor) |
| 115 | + |
| 116 | + # Disable irrelevant options when the stretch mode is Disabled. |
| 117 | + $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowBaseSize/OptionButton".disabled = stretch_mode == SceneTree.STRETCH_MODE_DISABLED |
| 118 | + $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowStretchAspect/OptionButton".disabled = stretch_mode == SceneTree.STRETCH_MODE_DISABLED |
| 119 | + |
| 120 | + |
| 121 | +func _on_window_stretch_aspect_item_selected(index): |
| 122 | + stretch_aspect = index |
| 123 | + get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor) |
| 124 | + |
| 125 | + |
| 126 | +func _on_window_scale_factor_drag_ended(_value_changed): |
| 127 | + scale_factor = $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowScaleFactor/HSlider".value |
| 128 | + $"Panel/AspectRatioContainer/ColorRect/CenterContainer/Options/WindowScaleFactor/Value".text = "%d%%" % (scale_factor * 100) |
| 129 | + get_tree().set_screen_stretch(stretch_mode, stretch_aspect, base_window_size, scale_factor) |
0 commit comments