@@ -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+ func _set_block_type ( new_block_type ):
34+ block_type = new_block_type
3335 queue_redraw ()
3436
3537
36- func _set_shift_top (new_shift_top ):
37- shift_top = new_shift_top
38- queue_redraw ()
39-
40-
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
@@ -49,61 +46,98 @@ func _ready():
4946 parent_block .focus_exited .connect (queue_redraw )
5047
5148
49+ func _get_border_color () -> Color :
50+ if parent_block .has_focus ():
51+ return Constants .FOCUS_BORDER_COLOR
52+ return outline_color
53+
54+
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+ )
65+
66+
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+
84+
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 )
91+
92+
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+
52121func _draw ():
53122 var fill_polygon : PackedVector2Array
54- fill_polygon .append (Vector2 (0.0 , 0.0 ))
55- if show_top :
56- fill_polygon .append (Vector2 (Constants .KNOB_X + shift_top , 0.0 ))
57- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_top , Constants .KNOB_H ))
58- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_top , Constants .KNOB_H ))
59- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_top , 0.0 ))
60-
61- fill_polygon .append (Vector2 (size .x , 0.0 ))
62- fill_polygon .append (Vector2 (size .x , size .y ))
63- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_bottom , size .y ))
64- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_bottom , size .y + Constants .KNOB_H ))
65- fill_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_bottom , size .y + Constants .KNOB_H ))
66- fill_polygon .append (Vector2 (Constants .KNOB_X + shift_bottom , size .y ))
67- fill_polygon .append (Vector2 (0.0 , size .y ))
68- fill_polygon .append (Vector2 (0.0 , 0.0 ))
69-
70123 var stroke_polygon : PackedVector2Array
71- var edge_polygon : PackedVector2Array
72- var outline_middle = Constants .OUTLINE_WIDTH / 2
73-
74- if shift_top > 0 :
75- stroke_polygon .append (Vector2 (shift_top - outline_middle , 0.0 ))
76- else :
77- stroke_polygon .append (Vector2 (shift_top , 0.0 ))
78-
79- if show_top :
80- stroke_polygon .append (Vector2 (Constants .KNOB_X + shift_top , 0.0 ))
81- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_top , Constants .KNOB_H ))
82- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_top , Constants .KNOB_H ))
83- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_top , 0.0 ))
84-
85- stroke_polygon .append (Vector2 (size .x , 0.0 ))
86- stroke_polygon .append (Vector2 (size .x , size .y ))
87- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z * 2 + Constants .KNOB_W + shift_bottom , size .y ))
88- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + Constants .KNOB_W + shift_bottom , size .y + Constants .KNOB_H ))
89- stroke_polygon .append (Vector2 (Constants .KNOB_X + Constants .KNOB_Z + shift_bottom , size .y + Constants .KNOB_H ))
90- stroke_polygon .append (Vector2 (Constants .KNOB_X + shift_bottom , size .y ))
91-
92- if shift_bottom > 0 :
93- stroke_polygon .append (Vector2 (shift_bottom - outline_middle , size .y ))
94- else :
95- stroke_polygon .append (Vector2 (shift_bottom , size .y ))
96-
97- if shift_top > 0 :
98- edge_polygon .append (Vector2 (0.0 , 0.0 ))
99- else :
100- edge_polygon .append (Vector2 (0.0 , 0.0 - outline_middle ))
101-
102- if shift_bottom > 0 :
103- edge_polygon .append (Vector2 (0.0 , size .y ))
104- else :
105- edge_polygon .append (Vector2 (0.0 , size .y + outline_middle ))
124+
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 ()
106141
107142 draw_colored_polygon (fill_polygon , color )
108- draw_polyline (stroke_polygon , Constants .FOCUS_BORDER_COLOR if parent_block .has_focus () else outline_color , Constants .OUTLINE_WIDTH )
109- draw_polyline (edge_polygon , Constants .FOCUS_BORDER_COLOR if parent_block .has_focus () else outline_color , Constants .OUTLINE_WIDTH )
143+ draw_polyline (stroke_polygon , _get_border_color (), Constants .OUTLINE_WIDTH )
0 commit comments