@@ -3,23 +3,25 @@ extends Control
33
44const BlockTreeUtil = preload ("res://addons/block_code/ui/block_tree_util.gd" )
55const Constants = preload ("res://addons/block_code/ui/constants.gd" )
6+ const Types = preload ("res://addons/block_code/types/types.gd" )
7+
8+ enum ControlPart {
9+ TOP ,
10+ BOTTOM ,
11+ }
612
713var outline_color : Color
814var parent_block : Block
915
1016@export var color : Color :
1117 set = _set_color
1218
13- @export var show_top : bool = true :
14- set = _set_show_top
15-
16- ## Horizontally shift the top knob
17- @export var shift_top : float = 0.0 :
18- set = _set_shift_top
19+ @export var block_type : Types .BlockType = Types .BlockType .STATEMENT :
20+ set = _set_block_type
1921
20- ## Horizontally shift the bottom knob
21- @export var shift_bottom : float = 0.0 :
22- set = _set_shift_bottom
22+ ## Only relevant if block_type is CONTROL.
23+ @export var control_part : ControlPart = ControlPart . TOP :
24+ set = _set_control_part
2325
2426
2527func _set_color (new_color ):
@@ -28,18 +30,13 @@ func _set_color(new_color):
2830 queue_redraw ()
2931
3032
31- func _set_show_top (new_show_top ):
32- show_top = new_show_top
33- queue_redraw ()
34-
35-
36- func _set_shift_top (new_shift_top ):
37- shift_top = new_shift_top
33+ func _set_block_type (new_block_type ):
34+ block_type = new_block_type
3835 queue_redraw ()
3936
4037
41- func _set_shift_bottom ( new_shift_bottom ):
42- shift_bottom = new_shift_bottom
38+ func _set_control_part ( new_control_part ):
39+ control_part = new_control_part
4340 queue_redraw ()
4441
4542
@@ -55,47 +52,92 @@ func _get_border_color() -> Color:
5552 return outline_color
5653
5754
58- func _draw ():
59- var fill_polygon : PackedVector2Array
60- fill_polygon .append (Vector2 (0.0 , 0.0 ))
61- if show_top :
62- fill_polygon .append (Vector2 (Constants .KNOB_X + shift_top , 0.0 ))
63- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_top , Constants .KNOB_H ))
64- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_top , Constants .KNOB_H ))
65- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_top , 0.0 ))
66-
67- fill_polygon .append (Vector2 (size .x , 0.0 ))
68- fill_polygon .append (Vector2 (size .x , size .y ))
69- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_bottom , size .y ))
70- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_bottom , size .y + Constants .KNOB_H ))
71- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_bottom , size .y + Constants .KNOB_H ))
72- fill_polygon .append (Vector2 (Constants .KNOB_X + shift_bottom , size .y ))
73- fill_polygon .append (Vector2 (0.0 , size .y ))
74- fill_polygon .append (Vector2 (0.0 , 0.0 ))
55+ func _get_box_shape (box_size : Vector2 = Vector2 .ONE ) -> PackedVector2Array :
56+ return PackedVector2Array (
57+ [
58+ Vector2 (0.0 , 0.0 ),
59+ Vector2 (box_size .x , 0.0 ),
60+ Vector2 (box_size .x , box_size .y ),
61+ Vector2 (0.0 , box_size .y ),
62+ Vector2 (0.0 , 0.0 ),
63+ ]
64+ )
7565
76- var stroke_polygon : PackedVector2Array
7766
78- if shift_bottom > 0 or shift_top == 0 :
79- stroke_polygon .append (Vector2 (0.0 , size .y ))
67+ func _get_knob_shape (displacement : Vector2 = Vector2 .ZERO ) -> PackedVector2Array :
68+ return PackedVector2Array (
69+ [
70+ Vector2 (displacement .x , displacement .y ),
71+ Vector2 (displacement .x + Constants .KNOB_Z , displacement .y + Constants .KNOB_H ),
72+ Vector2 (displacement .x + Constants .KNOB_Z + Constants .KNOB_W , displacement .y + Constants .KNOB_H ),
73+ Vector2 (displacement .x + Constants .KNOB_Z * 2 + Constants .KNOB_W , displacement .y ),
74+ ]
75+ )
76+
77+
78+ func _get_entry_shape () -> PackedVector2Array :
79+ var box_shape = _get_box_shape (size )
80+ var bottom_knob_shape = _get_knob_shape (Vector2 (Constants .KNOB_X , size .y ))
81+ bottom_knob_shape .reverse ()
82+ return box_shape .slice (0 , 3 ) + bottom_knob_shape + box_shape .slice (3 )
83+
8084
81- stroke_polygon .append (Vector2 (shift_top , 0.0 ))
85+ func _get_statement_shape () -> PackedVector2Array :
86+ var box_shape = _get_box_shape (size )
87+ var top_knob_shape = _get_knob_shape (Vector2 (Constants .KNOB_X , 0.0 ))
88+ var bottom_knob_shape = _get_knob_shape (Vector2 (Constants .KNOB_X , size .y ))
89+ bottom_knob_shape .reverse ()
90+ return box_shape .slice (0 , 1 ) + top_knob_shape + box_shape .slice (1 , 3 ) + bottom_knob_shape + box_shape .slice (3 )
8291
83- if show_top :
84- stroke_polygon .append (Vector2 (Constants .KNOB_X + shift_top , 0.0 ))
85- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_top , Constants .KNOB_H ))
86- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_top , Constants .KNOB_H ))
87- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_top , 0.0 ))
8892
89- stroke_polygon .append (Vector2 (size .x , 0.0 ))
90- stroke_polygon .append (Vector2 (size .x , size .y ))
91- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_bottom , size .y ))
92- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_bottom , size .y + Constants .KNOB_H ))
93- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_bottom , size .y + Constants .KNOB_H ))
94- stroke_polygon .append (Vector2 (Constants .KNOB_X + shift_bottom , size .y ))
95- stroke_polygon .append (Vector2 (shift_bottom , size .y ))
93+ func _get_control_top_fill_shape () -> PackedVector2Array :
94+ var box_shape = _get_box_shape (size )
95+ var top_knob_shape = _get_knob_shape (Vector2 (Constants .KNOB_X , 0.0 ))
96+ var bottom_knob_shape = _get_knob_shape (Vector2 (Constants .CONTROL_MARGIN + Constants .KNOB_X , size .y ))
97+ bottom_knob_shape .reverse ()
98+ return box_shape .slice (0 , 1 ) + top_knob_shape + box_shape .slice (1 , 3 ) + bottom_knob_shape + box_shape .slice (3 )
99+
100+
101+ func _get_control_top_stroke_shape () -> PackedVector2Array :
102+ var shape = _get_control_top_fill_shape ()
103+ shape = shape .slice (shape .size () - 2 ) + shape .slice (0 , shape .size () - 2 )
104+ shape .append (Vector2 (Constants .CONTROL_MARGIN - Constants .OUTLINE_WIDTH / 2 , size .y ))
105+ return shape
106+
107+
108+ func _get_control_bottom_fill_shape () -> PackedVector2Array :
109+ var box_shape = _get_box_shape (size )
110+ var top_knob_shape = _get_knob_shape (Vector2 (Constants .CONTROL_MARGIN + Constants .KNOB_X , 0.0 ))
111+ var bottom_knob_shape = _get_knob_shape (Vector2 (Constants .KNOB_X , size .y ))
112+ bottom_knob_shape .reverse ()
113+ return box_shape .slice (0 , 1 ) + top_knob_shape + box_shape .slice (1 , 3 ) + bottom_knob_shape + box_shape .slice (3 )
114+
115+
116+ func _get_control_bottom_stroke_shape () -> PackedVector2Array :
117+ var shape = PackedVector2Array ([Vector2 (Constants .CONTROL_MARGIN - Constants .OUTLINE_WIDTH / 2 , 0.0 )])
118+ return shape + _get_control_bottom_fill_shape ().slice (1 )
119+
120+
121+ func _draw ():
122+ var fill_polygon : PackedVector2Array
123+ var stroke_polygon : PackedVector2Array
96124
97- if shift_top > 0 :
98- stroke_polygon .append (Vector2 (0.0 , 0.0 ))
125+ match block_type :
126+ Types .BlockType .ENTRY :
127+ var shape = _get_entry_shape ()
128+ fill_polygon = shape
129+ stroke_polygon = shape
130+ Types .BlockType .STATEMENT :
131+ var shape = _get_statement_shape ()
132+ fill_polygon = shape
133+ stroke_polygon = shape
134+ Types .BlockType .CONTROL :
135+ if control_part == ControlPart .TOP :
136+ fill_polygon = _get_control_top_fill_shape ()
137+ stroke_polygon = _get_control_top_stroke_shape ()
138+ else :
139+ fill_polygon = _get_control_bottom_fill_shape ()
140+ stroke_polygon = _get_control_bottom_stroke_shape ()
99141
100142 draw_colored_polygon (fill_polygon , color )
101143 draw_polyline (stroke_polygon , _get_border_color (), Constants .OUTLINE_WIDTH )
0 commit comments