Skip to content

Commit a3cba20

Browse files
authored
Merge pull request #323 from endlessm/shapes-refactor
Shapes refactor
2 parents 3cfcf76 + 0118dcd commit a3cba20

File tree

6 files changed

+109
-80
lines changed

6 files changed

+109
-80
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: 105 additions & 71 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+
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+
52121
func _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

Comments
 (0)