@@ -5,20 +5,27 @@ Visualization of an array as rectangles of varying heights.
55class_name ArrayView
66extends HBoxContainer
77
8+ const SWAP_DURATION = 0.1
9+ const TRANS_TYPE = Tween .TRANS_LINEAR
10+ const EASE_TYPE = Tween .EASE_IN_OUT
11+
12+ var _tween = Tween .new ()
813var _level : ComparisonSort
914var _rects = []
1015
1116func _init (level ):
1217 """Add colored rectangles."""
1318 _level = level
1419 _level .connect ("mistake" , self , "_on_Level_mistake" )
20+ _level .array .connect ("swapped" , self , "_on_ArrayModel_swapped" )
1521 add_child (_level ) # NOTE: This is necessary for it to read input
1622 for i in range (level .array .size ):
1723 var rect = ColorRect .new ()
1824 rect .size_flags_horizontal = Control .SIZE_EXPAND_FILL
1925 rect .size_flags_vertical = Control .SIZE_SHRINK_END
2026 _rects .append (rect )
2127 add_child (rect )
28+ add_child (_tween ) # NOTE: This is necessary for it to animate
2229
2330func _process (delta ):
2431 """Update heights of rectangles based on array values."""
@@ -30,3 +37,14 @@ func _process(delta):
3037func _on_Level_mistake ():
3138 """Flash the border red on mistakes."""
3239 get_parent ().flash ()
40+
41+ func _on_ArrayModel_swapped (i , j ):
42+ """Produce a swapping animation."""
43+ _tween .interpolate_property (_rects [i ], "rect_position" ,
44+ null , _rects [j ].rect_position , SWAP_DURATION , TRANS_TYPE , EASE_TYPE )
45+ _tween .interpolate_property (_rects [j ], "rect_position" ,
46+ null , _rects [i ].rect_position , SWAP_DURATION , TRANS_TYPE , EASE_TYPE )
47+ var temp = _rects [i ]
48+ _rects [i ] = _rects [j ]
49+ _rects [j ] = temp
50+ _tween .start ()
0 commit comments