Skip to content

Commit 26c84e9

Browse files
committed
feat: implement big preview mode
Took more hacks than I would have liked, but getting rid of those would require a refactor across the entire codebase.
1 parent 74e22b2 commit 26c84e9

File tree

4 files changed

+83
-37
lines changed

4 files changed

+83
-37
lines changed

project.godot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ change_data={
202202
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":67,"unicode":0,"echo":false,"script":null)
203203
]
204204
}
205+
big_preview={
206+
"deadzone": 0.5,
207+
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":66,"unicode":0,"echo":false,"script":null)
208+
]
209+
}
205210

206211
[rendering]
207212

scenes/levels.tscn

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,6 @@ size_flags_horizontal = 4
9999
size_flags_vertical = 4
100100
script = ExtResource( 3 )
101101

102-
[node name="Types" type="VBoxContainer" parent="Levels/Level/Right/Display/TypesContainer"]
103-
margin_left = 20.0
104-
margin_top = 20.0
105-
margin_right = 20.0
106-
margin_bottom = 20.0
107-
size_flags_horizontal = 4
108-
size_flags_vertical = 4
109-
110102
[node name="HBoxContainer" type="HBoxContainer" parent="Levels/Level/Right/Display"]
111103
margin_left = 20.0
112104
margin_top = 20.0
@@ -187,6 +179,29 @@ INF"
187179
align = 2
188180
uppercase = true
189181

182+
[node name="BigDisplay" type="MarginContainer" parent="Levels"]
183+
visible = false
184+
margin_top = 640.0
185+
margin_right = 1240.0
186+
margin_bottom = 680.0
187+
size_flags_vertical = 3
188+
script = ExtResource( 3 )
189+
190+
[node name="TypesContainer" type="MarginContainer" parent="Levels/BigDisplay"]
191+
visible = false
192+
margin_left = 600.0
193+
margin_right = 640.0
194+
margin_bottom = 40.0
195+
size_flags_horizontal = 4
196+
size_flags_vertical = 4
197+
script = ExtResource( 3 )
198+
199+
[node name="HBoxContainer" type="HBoxContainer" parent="Levels/BigDisplay"]
200+
margin_left = 20.0
201+
margin_top = 20.0
202+
margin_right = 1220.0
203+
margin_bottom = 20.0
204+
190205
[node name="Timer" type="Timer" parent="Levels"]
191206
[connection signal="pressed" from="Levels/NamesContainer/Names/Current" to="Levels" method="_on_Current_pressed"]
192207
[connection signal="timeout" from="Levels/Timer" to="Levels" method="_on_Timer_timeout"]

scenes/menu.tscn

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ __meta__ = {
4444
[node name="InstructionsContainer" type="MarginContainer" parent="MainMenu/Display"]
4545
visible = false
4646
margin_left = 484.0
47-
margin_top = 175.0
47+
margin_top = 164.0
4848
margin_right = 714.0
49-
margin_bottom = 423.0
49+
margin_bottom = 434.0
5050
size_flags_horizontal = 4
5151
size_flags_vertical = 4
5252
script = ExtResource( 3 )
@@ -55,45 +55,47 @@ script = ExtResource( 3 )
5555
margin_left = 20.0
5656
margin_top = 20.0
5757
margin_right = 210.0
58-
margin_bottom = 228.0
58+
margin_bottom = 250.0
5959
custom_constants/separation = 16
6060

6161
[node name="Controls" type="HBoxContainer" parent="MainMenu/Display/InstructionsContainer/Instructions"]
6262
margin_right = 190.0
63-
margin_bottom = 173.0
63+
margin_bottom = 195.0
6464
custom_constants/separation = 20
6565

6666
[node name="Keys" type="Label" parent="MainMenu/Display/InstructionsContainer/Instructions/Controls"]
6767
margin_right = 50.0
68-
margin_bottom = 173.0
68+
margin_bottom = 195.0
6969
size_flags_horizontal = 4
7070
text = "W
7171
A
7272
S
7373
D
7474
M
7575
C
76+
B
7677
esc
7778
space"
7879

7980
[node name="Actions" type="Label" parent="MainMenu/Display/InstructionsContainer/Instructions/Controls"]
8081
margin_left = 70.0
8182
margin_right = 190.0
82-
margin_bottom = 173.0
83+
margin_bottom = 195.0
8384
text = "bigger
8485
slower
8586
smaller
8687
faster
8788
toggle sound
8889
change data
90+
big preview
8991
back
9092
confirm"
9193
align = 2
9294

9395
[node name="Button" type="Button" parent="MainMenu/Display/InstructionsContainer/Instructions"]
94-
margin_top = 189.0
96+
margin_top = 211.0
9597
margin_right = 190.0
96-
margin_bottom = 208.0
98+
margin_bottom = 230.0
9799
text = "OK"
98100

99101
[node name="Spacing" type="Control" parent="MainMenu"]

scripts/levels.gd

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ const MAX_WAIT = 4
1818
const MIN_SIZE = 8
1919
const MAX_SIZE = 128
2020

21-
var _index = LEVELS.find(GlobalScene.get_param("level"))
21+
var _index = LEVELS.find(GlobalScene.get_param("level", LEVELS[0]))
2222
var _level: ComparisonSort
2323
var _size = GlobalScene.get_param("size", ArrayModel.DEFAULT_SIZE)
2424
var _data_type = GlobalScene.get_param(
2525
"data_type", ArrayModel.DATA_TYPES.RANDOM_UNIQUE)
2626

2727
func _ready():
28-
var types = $Level/Right/Display/TypesContainer/Types
28+
_load_types($Level/Right/Display/TypesContainer)
29+
_load_types($BigDisplay/TypesContainer)
30+
_reload()
31+
32+
func _load_types(node):
33+
var types = VBoxContainer.new()
34+
types.name = "Types"
35+
node.add_child(types)
2936
for type in ArrayModel.DATA_TYPES:
3037
var button = Button.new()
3138
button.text = type.replace("_", " ")
@@ -35,23 +42,34 @@ func _ready():
3542
var bottom = types.get_child(types.get_child_count() - 1)
3643
top.focus_neighbour_top = bottom.get_path()
3744
bottom.focus_neighbour_bottom = top.get_path()
38-
_reload()
3945

4046
func _reload():
41-
$NamesContainer/Names/Current.grab_focus()
42-
if _index == -1:
43-
_index = 0
44-
_level = LEVELS[_index].new(ArrayModel.new(_size, _data_type))
45-
_level.connect("done", self, "_on_ComparisonSort_done")
47+
# Load everything from scratch
48+
_restart()
4649
_load_scores(_level)
47-
# Load level information
4850
$NamesContainer/Names/Current.text = _level.NAME
4951
$Level/Left/Code.text = _level.DESCRIPTION
5052
$Level/Right/Info/ControlsContainer/Controls.text = _level.CONTROLS
51-
var view = $Level/Right/Display/HBoxContainer
52-
view.get_parent().remove_child(view)
53-
view.queue_free()
54-
$Level/Right/Display.add_child(ArrayView.new(_level), true)
53+
54+
func _restart():
55+
set_process_input(true)
56+
# Only load in a restarted simulation
57+
$NamesContainer/Names/Current.grab_focus()
58+
_level = LEVELS[_index].new(ArrayModel.new(_size, _data_type))
59+
_level.connect("done", self, "_on_ComparisonSort_done")
60+
var view = $Level/Right/Display if $Level.visible else $BigDisplay
61+
var other = $BigDisplay if $Level.visible else $Level/Right/Display
62+
# Delete both ArrayViews if they exist
63+
if other.get_node_or_null("HBoxContainer") != null:
64+
other.get_node("HBoxContainer").queue_free()
65+
var array_view = view.get_node_or_null("HBoxContainer")
66+
if array_view != null:
67+
# XXX: remove_child is needed in order to ensure that the added
68+
# child will be named "HBoxContainer" and not "HBoxContainer2"
69+
# because the other ArrayView hasn't been queue_free'd yet
70+
view.remove_child(array_view)
71+
array_view.queue_free()
72+
view.add_child(ArrayView.new(_level), true)
5573
$Timer.start()
5674

5775
func _load_scores(level):
@@ -91,16 +109,21 @@ func _input(event):
91109
$Timer.wait_time = min($Timer.wait_time * 4, MAX_WAIT)
92110
if event.is_action_pressed("change_data"):
93111
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), true)
94-
$Level/Right/Display/HBoxContainer.hide()
95-
$Level/Right/Display/TypesContainer.show()
112+
var display = $Level/Right/Display if $Level.visible else $BigDisplay
113+
display.get_node("HBoxContainer").hide()
114+
display.get_node("TypesContainer").show()
96115
$Timer.stop()
97-
$Level/Right/Display/TypesContainer/Types.get_child(0).grab_focus()
116+
display.get_node("TypesContainer/Types").get_child(0).grab_focus()
117+
if event.is_action_pressed("big_preview"):
118+
$Level.visible = not $Level.visible
119+
$BigDisplay.visible = not $BigDisplay.visible
120+
_restart()
98121

99122
func _on_ComparisonSort_done():
123+
set_process_input(false)
100124
$Timer.stop()
101125
yield(get_tree().create_timer(1), "timeout")
102-
if _level.array.is_sorted():
103-
_reload()
126+
_restart()
104127

105128
func _on_Timer_timeout():
106129
_level.next(null)
@@ -111,8 +134,9 @@ func _on_Current_pressed():
111134

112135
func _on_Button_pressed(data_type):
113136
AudioServer.set_bus_mute(AudioServer.get_bus_index("Master"), false)
114-
$Level/Right/Display/TypesContainer.hide()
115-
$Level/Right/Display/HBoxContainer.show()
137+
var display = $Level/Right/Display if $Level.visible else $BigDisplay
138+
display.get_node("TypesContainer").hide()
139+
display.get_node("HBoxContainer").show()
116140
$Timer.start()
117141
_data_type = ArrayModel.DATA_TYPES[data_type]
118-
_reload()
142+
_restart()

0 commit comments

Comments
 (0)