Skip to content

Commit 0118dcd

Browse files
committed
Blocks background: Separate draw for each type
Refactor to have an easier to read separate draw function for each block type. Each draw function is now straightforward, without if/else conditionals. Instead of having multiple attributes like "shift_top", "show_top" to instruct the drawing, directly pass the block type. For control blocks that have 2 backgrounds, one for top and one for the bottom part, also expose a control_part attribute.
1 parent e149713 commit 0118dcd

File tree

6 files changed

+99
-62
lines changed

6 files changed

+99
-62
lines changed

addons/block_code/ui/blocks/control_block/control_block.gd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ func _ready():
99
super()
1010

1111
%TopBackground.color = color
12-
%TopBackground.shift_bottom = Constants.CONTROL_MARGIN
1312
%BottomBackground.color = color
14-
%BottomBackground.shift_top = Constants.CONTROL_MARGIN
1513
%SnapPoint.add_theme_constant_override("margin_left", Constants.CONTROL_MARGIN)
1614
%SnapGutter.color = color
1715
%SnapGutter.custom_minimum_size.x = Constants.CONTROL_MARGIN

addons/block_code/ui/blocks/control_block/control_block.tscn

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ unique_name_in_owner = true
4646
layout_mode = 2
4747
script = ExtResource("2_tx0qr")
4848
color = Color(1, 1, 1, 1)
49-
shift_bottom = 20.0
49+
block_type = 4
5050

5151
[node name="DragDropArea" parent="VBoxContainer/MarginContainer/Rows/Row" instance=ExtResource("3_21e8n")]
5252
layout_mode = 2
@@ -91,7 +91,8 @@ layout_mode = 2
9191
size_flags_horizontal = 0
9292
script = ExtResource("2_tx0qr")
9393
color = Color(1, 1, 1, 1)
94-
shift_top = 20.0
94+
block_type = 4
95+
control_part = 1
9596

9697
[node name="SnapPoint" parent="VBoxContainer" instance=ExtResource("3_nhryi")]
9798
layout_mode = 2

addons/block_code/ui/blocks/entry_block/entry_block.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ layout_mode = 2
3535
mouse_filter = 1
3636
script = ExtResource("2_yrw8l")
3737
color = Color(1, 1, 1, 1)
38-
show_top = false
38+
block_type = 1
3939

4040
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_swkpp")]
4141
layout_mode = 2

addons/block_code/ui/blocks/statement_block/statement_block.gd

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ var args_to_add_after_format: Dictionary # Only used when loading
1212

1313
func _ready():
1414
super()
15-
16-
if definition != null and definition.type != Types.BlockType.STATEMENT:
17-
_background.show_top = false
1815
_background.color = color
1916

2017

addons/block_code/ui/blocks/statement_block/statement_block.tscn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ unique_name_in_owner = true
3434
layout_mode = 2
3535
mouse_filter = 1
3636
script = ExtResource("2_lctqt")
37-
color = Color(1, 1, 1, 1)
3837

3938
[node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_mbxhq")]
4039
layout_mode = 2

addons/block_code/ui/blocks/utilities/background/background.gd

Lines changed: 95 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ extends Control
33

44
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
55
const 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

713
var outline_color: Color
814
var 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

2527
func _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

Comments
 (0)