Skip to content

Commit 780bb50

Browse files
committed
feat: add background lines
These serve as part eye candy, part aid for quickly comparing elements that are close in size but far apart in merge sort. To achieve this, ArrayView had to be refactored to no longer use a Viewport, so this also removes the possibility of animating merge sort the way it was.
1 parent 424f9c0 commit 780bb50

File tree

4 files changed

+16
-18
lines changed

4 files changed

+16
-18
lines changed

project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ _global_script_classes=[ {
1414
"language": "GDScript",
1515
"path": "res://models/array_model.gd"
1616
}, {
17-
"base": "ViewportContainer",
17+
"base": "HBoxContainer",
1818
"class": "ArrayView",
1919
"language": "GDScript",
2020
"path": "res://views/array_view.gd"

scripts/score.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static func get_tier(moves, seconds):
1818
return TIERS[get_mps_int(moves, seconds)]
1919

2020
static func get_mps_int(moves, seconds):
21-
return min(moves / seconds, TIERS.size() - 1)
21+
return int(min(moves / seconds, TIERS.size() - 1))
2222

2323
static func get_color(moves, seconds):
2424
return COLORS[min(moves / seconds, COLORS.size() - 1)]

scripts/theme.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Global constants relating to the GUI.
55
extends Node
66

77
const GREEN = Color("33ff33")
8-
const DARK_GREEN = Color("7733ff33")
8+
const DARK_GREEN = Color("1b5e20")
99
const ORANGE = Color("ffb000")
1010
const RED = Color("f44336")
1111
const BLUE = Color("2196f3")

views/array_view.gd

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ Visualization of an array as rectangles of varying heights.
33
"""
44

55
class_name ArrayView
6-
extends ViewportContainer
6+
extends HBoxContainer
77

8+
const MARGIN = 20
9+
const LINE_COLOR = Color("7f1b5e20")
10+
const BOX_SIZE = 50
811
const ANIM_DURATION = 0.1
912

1013
var _tween = Tween.new()
1114
var _level: ComparisonSort
1215
var _rects = []
1316
var _positions = []
14-
var _viewport = Viewport.new()
1517
var _pointer = null
1618
var _pointer_size: int
1719
onready var _separation = 128 / _level.array.size
1820

1921
func _init(level):
2022
_level = level
21-
stretch = true
22-
_viewport.usage = Viewport.USAGE_2D
2323
add_child(_level) # NOTE: This is necessary for it to read input
2424
add_child(_tween) # NOTE: This is necessary for it to animate
25-
add_child(_viewport)
2625

2726
func _ready():
2827
yield(get_tree(), "idle_frame")
@@ -51,9 +50,8 @@ func _ready():
5150
_positions.append(x)
5251
x += unit_width
5352
_rects.append(rect)
54-
_viewport.add_child(rect)
53+
add_child(rect)
5554
_level.array.connect("swapped", self, "_on_ArrayModel_swapped")
56-
_level.array.connect("sorted", self, "_on_ArrayModel_sorted")
5755
if _level.has_method("get_pointer"):
5856
_pointer = Polygon2D.new()
5957
_pointer.polygon = [
@@ -62,7 +60,7 @@ func _ready():
6260
Vector2(width / 2 + _pointer_size, 0),
6361
]
6462
_pointer.color = GlobalTheme.BLUE
65-
_viewport.add_child(_pointer)
63+
add_child(_pointer)
6664

6765
func _process(delta):
6866
if _pointer != null:
@@ -90,10 +88,10 @@ func _on_ArrayModel_swapped(i, j):
9088
_rects[j] = temp
9189
_tween.start()
9290

93-
func _on_ArrayModel_sorted(i, j):
94-
for x in range(i, j):
95-
_rects[x].position.y = 0
96-
for x in range(i, j):
97-
_tween.interpolate_property(
98-
_rects[x], "position:y", null, rect_size.y, ANIM_DURATION)
99-
_tween.start()
91+
func _draw():
92+
var width = rect_size.x + MARGIN
93+
var height = rect_size.y + MARGIN
94+
for i in range(-MARGIN + BOX_SIZE, width, BOX_SIZE):
95+
draw_line(Vector2(i, -MARGIN), Vector2(i, height), LINE_COLOR)
96+
for i in range(-MARGIN + BOX_SIZE, height, BOX_SIZE):
97+
draw_line(Vector2(-MARGIN, i), Vector2(width, i), LINE_COLOR)

0 commit comments

Comments
 (0)