Skip to content

Commit 5ed98b5

Browse files
committed
fix: redraw ArrayView when (un)maximizing window
To accomplish this, ArrayView now uses manually positioned Polygon2Ds instead of ColorRects because Controls turned out to be extremely finnicky to work with. Also changed default window size to 1280x720 for convenience on smaller screens and refactored scenes/levels.tscn.
1 parent a74cb50 commit 5ed98b5

File tree

7 files changed

+75
-80
lines changed

7 files changed

+75
-80
lines changed

assets/theme.theme

-5 Bytes
Binary file not shown.

project.godot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ GlobalTheme="*res://scripts/theme.gd"
7575

7676
[display]
7777

78-
window/size/width=1920
79-
window/size/height=1080
78+
window/size/width=1280
79+
window/size/height=720
8080
window/size/always_on_top=true
8181
window/dpi/allow_hidpi=true
8282
window/stretch/mode="2d"

scenes/levels.tscn

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
[ext_resource path="res://scripts/border.gd" type="Script" id=3]
66

77
[node name="Viewport" type="MarginContainer"]
8-
anchor_top = 0.00114754
98
anchor_right = 1.0
10-
anchor_bottom = 1.00115
9+
anchor_bottom = 1.0
1110
theme = ExtResource( 2 )
1211
custom_constants/margin_right = 30
1312
custom_constants/margin_top = 30
@@ -20,61 +19,63 @@ __meta__ = {
2019
[node name="LevelSelect" type="HBoxContainer" parent="."]
2120
margin_left = 30.0
2221
margin_top = 30.0
23-
margin_right = 1890.0
24-
margin_bottom = 1050.0
22+
margin_right = 1250.0
23+
margin_bottom = 690.0
2524
script = ExtResource( 1 )
2625

2726
[node name="LevelsBorder" type="MarginContainer" parent="LevelSelect"]
28-
margin_right = 480.0
29-
margin_bottom = 1020.0
30-
rect_min_size = Vector2( 480, 0 )
27+
margin_right = 303.0
28+
margin_bottom = 660.0
29+
size_flags_horizontal = 3
3130
script = ExtResource( 3 )
3231

3332
[node name="Levels" type="VBoxContainer" parent="LevelSelect/LevelsBorder"]
3433
margin_left = 20.0
3534
margin_top = 20.0
36-
margin_right = 460.0
37-
margin_bottom = 1000.0
35+
margin_right = 283.0
36+
margin_bottom = 640.0
3837

3938
[node name="Preview" type="VBoxContainer" parent="LevelSelect"]
40-
margin_left = 488.0
41-
margin_right = 1860.0
42-
margin_bottom = 1020.0
39+
margin_left = 311.0
40+
margin_right = 1220.0
41+
margin_bottom = 660.0
4342
size_flags_horizontal = 3
43+
size_flags_stretch_ratio = 3.0
4444

4545
[node name="Display" type="MarginContainer" parent="LevelSelect/Preview"]
46-
margin_right = 1372.0
47-
margin_bottom = 640.0
48-
rect_min_size = Vector2( 0, 640 )
46+
margin_right = 909.0
47+
margin_bottom = 434.0
48+
size_flags_vertical = 3
49+
size_flags_stretch_ratio = 2.0
4950
script = ExtResource( 3 )
5051

5152
[node name="InfoBorder" type="MarginContainer" parent="LevelSelect/Preview"]
52-
margin_top = 648.0
53-
margin_right = 1372.0
54-
margin_bottom = 1020.0
53+
margin_top = 442.0
54+
margin_right = 909.0
55+
margin_bottom = 660.0
5556
size_flags_vertical = 3
5657
script = ExtResource( 3 )
5758

5859
[node name="Info" type="HBoxContainer" parent="LevelSelect/Preview/InfoBorder"]
5960
margin_left = 20.0
6061
margin_top = 20.0
61-
margin_right = 1352.0
62-
margin_bottom = 352.0
62+
margin_right = 889.0
63+
margin_bottom = 198.0
6364
custom_constants/separation = 50
6465

6566
[node name="About" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
66-
margin_right = 810.0
67-
margin_bottom = 332.0
68-
rect_min_size = Vector2( 810, 0 )
67+
margin_right = 546.0
68+
margin_bottom = 178.0
69+
size_flags_horizontal = 3
6970
size_flags_vertical = 3
71+
size_flags_stretch_ratio = 2.0
7072
text = "This is a short description of the algorithm. It should tell how it works in a simple yet complete way and explain its relevance in computer science. It should be accessible to the layman while not being oversimplifying."
7173
autowrap = true
7274

7375
[node name="Controls" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
74-
margin_left = 860.0
75-
margin_right = 1332.0
76-
margin_bottom = 332.0
77-
rect_min_size = Vector2( 450, 0 )
76+
margin_left = 596.0
77+
margin_right = 869.0
78+
margin_bottom = 178.0
7879
size_flags_horizontal = 3
7980
size_flags_vertical = 3
8081
text = "These are the controls for the level. They should be tailored to each level for maximum efficiency and simplicity."

scenes/menu.tscn

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,50 @@ __meta__ = {
2121
[node name="MainMenu" type="VBoxContainer" parent="."]
2222
margin_left = 40.0
2323
margin_top = 30.0
24-
margin_right = 1879.0
25-
margin_bottom = 1049.0
24+
margin_right = 1239.0
25+
margin_bottom = 689.0
2626
script = ExtResource( 1 )
2727

2828
[node name="Title" type="Label" parent="MainMenu"]
29-
margin_right = 1839.0
30-
margin_bottom = 38.0
29+
margin_right = 1199.0
30+
margin_bottom = 19.0
3131
text = "Human Computer Simulator"
3232
uppercase = true
3333

3434
[node name="Display" type="MarginContainer" parent="MainMenu"]
35-
margin_top = 46.0
36-
margin_right = 1839.0
37-
margin_bottom = 965.0
35+
margin_top = 27.0
36+
margin_right = 1199.0
37+
margin_bottom = 624.0
3838
size_flags_vertical = 3
3939
script = ExtResource( 3 )
4040
__meta__ = {
4141
"_edit_use_anchors_": false
4242
}
4343

4444
[node name="Spacing" type="Control" parent="MainMenu"]
45-
margin_top = 973.0
46-
margin_right = 1839.0
47-
margin_bottom = 973.0
45+
margin_top = 632.0
46+
margin_right = 1199.0
47+
margin_bottom = 632.0
4848

4949
[node name="Buttons" type="HBoxContainer" parent="MainMenu"]
50-
margin_left = 555.0
51-
margin_top = 981.0
52-
margin_right = 1283.0
53-
margin_bottom = 1019.0
50+
margin_left = 289.0
51+
margin_top = 640.0
52+
margin_right = 909.0
53+
margin_bottom = 659.0
5454
size_flags_horizontal = 4
5555
custom_constants/separation = 500
5656

5757
[node name="Start" type="Button" parent="MainMenu/Buttons"]
58-
margin_right = 95.0
59-
margin_bottom = 38.0
58+
margin_right = 50.0
59+
margin_bottom = 19.0
6060
size_flags_horizontal = 4
6161
text = "start"
6262
flat = true
6363

6464
[node name="Credits" type="Button" parent="MainMenu/Buttons"]
65-
margin_left = 595.0
66-
margin_right = 728.0
67-
margin_bottom = 38.0
65+
margin_left = 550.0
66+
margin_right = 620.0
67+
margin_bottom = 19.0
6868
text = "credits"
6969

7070
[node name="Timer" type="Timer" parent="."]

scripts/border.gd

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ var _timer = Timer.new()
1313
var _color_changes = 0
1414

1515
func _ready():
16-
"""Time last return to green with reenabling of controls."""
16+
# Time last return to green with reenabling of controls
1717
_timer.wait_time = ComparisonSort.DISABLE_TIME / COLOR_CHANGES
1818
_timer.connect("timeout", self, "_on_Timer_timeout")
1919
add_child(_timer)
2020

2121
func flash():
22-
"""Immediately flash red and then start timer."""
22+
# Immediately flash red and then start timer
2323
_on_Timer_timeout()
2424
_timer.start()
2525

2626
func _on_Timer_timeout():
27-
"""Switch between green and red."""
27+
# Switch between green and red
2828
if _color_changes == COLOR_CHANGES:
2929
_timer.stop()
3030
_color_changes = 0
@@ -35,5 +35,4 @@ func _on_Timer_timeout():
3535
update()
3636

3737
func _draw():
38-
"""Draw the border."""
3938
draw_rect(Rect2(Vector2(), rect_size), _color, false, WIDTH)

scripts/levels.gd

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const LEVELS = [
99
var _level: ComparisonSort
1010

1111
func _ready():
12-
"""Dynamically load level data."""
1312
for level in LEVELS:
1413
var button = Button.new()
1514
button.text = level.NAME
@@ -30,8 +29,7 @@ func _ready():
3029
top_button.grab_focus()
3130

3231
func _on_Button_focus_changed():
33-
"""Initialize the preview section."""
34-
_level = get_level(get_focus_owner().text).new(ArrayModel.new())
32+
_level = _get_level(get_focus_owner().text).new(ArrayModel.new())
3533
_level.active = false
3634
$Preview/InfoBorder/Info/About.text = _cleanup(_level.ABOUT)
3735
$Preview/InfoBorder/Info/Controls.text = _cleanup(_level.CONTROLS)
@@ -43,9 +41,9 @@ func _on_Button_focus_changed():
4341
$Preview/Display.add_child(ArrayView.new(_level))
4442

4543
func _on_Button_pressed(name):
46-
GlobalScene.change_scene("res://scenes/play.tscn", {"level": get_level(name)})
44+
GlobalScene.change_scene("res://scenes/play.tscn", {"level": _get_level(name)})
4745

48-
func get_level(name):
46+
func _get_level(name):
4947
for level in LEVELS:
5048
if level.NAME == name:
5149
return level

views/array_view.gd

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,48 @@ class_name ArrayView
66
extends HBoxContainer
77

88
const SWAP_DURATION = 0.1
9+
const SEPARATION = 8
910

1011
var _tween = Tween.new()
1112
var _level: ComparisonSort
1213
var _rects = []
13-
# Save initial positions to prevent animation race condition
14-
var _positions = []
14+
var _unit_width: int
1515
var _unit_height: int
1616

1717
func _init(level):
18-
"""Add colored rectangles."""
1918
_level = level
20-
_level.array.connect("swapped", self, "_on_ArrayModel_swapped")
2119
add_child(_level) # NOTE: This is necessary for it to read input
22-
for i in range(level.array.size):
23-
var rect = ColorRect.new()
24-
rect.size_flags_horizontal = Control.SIZE_EXPAND_FILL
25-
rect.size_flags_vertical = Control.SIZE_SHRINK_END
26-
_rects.append(rect)
27-
add_child(rect)
2820
add_child(_tween) # NOTE: This is necessary for it to animate
29-
_positions.resize(_level.array.size)
3021

3122
func _ready():
32-
_level.connect("mistake", get_parent(), "flash")
33-
34-
func _draw():
35-
# HACK: Workaround for resized signal not firing when window resized
23+
yield(get_tree(), "idle_frame")
24+
_unit_width = rect_size.x / _level.array.size
3625
_unit_height = rect_size.y / _level.array.size
37-
for i in range(_rects.size()):
38-
_rects[i].rect_scale.y = -1
39-
_positions[i] = _rects[i].rect_position
26+
_level.connect("mistake", get_parent(), "flash")
27+
for i in range(_level.array.size):
28+
var rect = Polygon2D.new()
29+
rect.polygon = [
30+
Vector2(0, 0),
31+
Vector2(0, rect_size.y),
32+
Vector2(_unit_width - SEPARATION, rect_size.y),
33+
Vector2(_unit_width - SEPARATION, 0),
34+
]
35+
rect.position = Vector2(i * _unit_width, rect_size.y)
36+
_rects.append(rect)
37+
add_child(rect)
38+
_level.array.connect("swapped", self, "_on_ArrayModel_swapped")
4039

4140
func _process(delta):
42-
"""Update heights of rectangles based on array values."""
4341
for i in range(_rects.size()):
4442
_rects[i].color = _level.get_effect(i)
45-
_rects[i].rect_size.y = _level.array.at(i) * _unit_height
43+
_rects[i].scale.y = -float(_level.array.at(i)) / _level.array.size
4644

4745
func _on_ArrayModel_swapped(i, j):
48-
"""Produce a swapping animation."""
4946
var time = SWAP_DURATION * (1 + float(j - i) / _level.array.size)
5047
_tween.interpolate_property(
51-
_rects[i], "rect_position", null, _positions[j], time)
48+
_rects[i], "position:x", null, j * _unit_width, time)
5249
_tween.interpolate_property(
53-
_rects[j], "rect_position", null, _positions[i], time)
50+
_rects[j], "position:x", null, i * _unit_width, time)
5451
var temp = _rects[i]
5552
_rects[i] = _rects[j]
5653
_rects[j] = temp

0 commit comments

Comments
 (0)