From ca974ce3d5c07ea5fbc74584b6ceefad4aa630f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Tue, 26 Nov 2024 09:08:44 -0300 Subject: [PATCH 1/3] Blocks background: Factor out outline color For readability, remove the ternary conditional operator, which was a long one-liner. --- .../ui/blocks/utilities/background/background.gd | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/addons/block_code/ui/blocks/utilities/background/background.gd b/addons/block_code/ui/blocks/utilities/background/background.gd index adf74799..42b65356 100644 --- a/addons/block_code/ui/blocks/utilities/background/background.gd +++ b/addons/block_code/ui/blocks/utilities/background/background.gd @@ -49,6 +49,12 @@ func _ready(): parent_block.focus_exited.connect(queue_redraw) +func _get_border_color() -> Color: + if parent_block.has_focus(): + return Constants.FOCUS_BORDER_COLOR + return outline_color + + func _draw(): var fill_polygon: PackedVector2Array fill_polygon.append(Vector2(0.0, 0.0)) @@ -105,5 +111,5 @@ func _draw(): edge_polygon.append(Vector2(0.0, size.y + outline_middle)) draw_colored_polygon(fill_polygon, color) - draw_polyline(stroke_polygon, Constants.FOCUS_BORDER_COLOR if parent_block.has_focus() else outline_color, Constants.OUTLINE_WIDTH) - draw_polyline(edge_polygon, Constants.FOCUS_BORDER_COLOR if parent_block.has_focus() else outline_color, Constants.OUTLINE_WIDTH) + draw_polyline(stroke_polygon, _get_border_color(), Constants.OUTLINE_WIDTH) + draw_polyline(edge_polygon, _get_border_color(), Constants.OUTLINE_WIDTH) From e1497131cfe6270e205b49557dc91a1cd221d61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Tue, 26 Nov 2024 09:54:14 -0300 Subject: [PATCH 2/3] Blocks background: Simplify outline drawing Remove the separate stroke line "edge_polygon" which was used only for control blocks. Since this line is connected to the "stroke_polygon" it can be joined with it. Also the "edge_polygon" was adding a redundant draw to non-control blocks. Also remove the outline_middle calculation. Taking screenshots as proof, this extra calculation has no effect in the drawing. --- .../blocks/utilities/background/background.gd | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/addons/block_code/ui/blocks/utilities/background/background.gd b/addons/block_code/ui/blocks/utilities/background/background.gd index 42b65356..8070c823 100644 --- a/addons/block_code/ui/blocks/utilities/background/background.gd +++ b/addons/block_code/ui/blocks/utilities/background/background.gd @@ -74,13 +74,11 @@ func _draw(): fill_polygon.append(Vector2(0.0, 0.0)) var stroke_polygon: PackedVector2Array - var edge_polygon: PackedVector2Array - var outline_middle = Constants.OUTLINE_WIDTH / 2 - if shift_top > 0: - stroke_polygon.append(Vector2(shift_top - outline_middle, 0.0)) - else: - stroke_polygon.append(Vector2(shift_top, 0.0)) + if shift_bottom > 0 or shift_top == 0: + stroke_polygon.append(Vector2(0.0, size.y)) + + stroke_polygon.append(Vector2(shift_top, 0.0)) if show_top: stroke_polygon.append(Vector2(Constants.KNOB_X + shift_top, 0.0)) @@ -94,22 +92,10 @@ func _draw(): stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + Constants.KNOB_W + shift_bottom, size.y + Constants.KNOB_H)) stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + shift_bottom, size.y + Constants.KNOB_H)) stroke_polygon.append(Vector2(Constants.KNOB_X + shift_bottom, size.y)) - - if shift_bottom > 0: - stroke_polygon.append(Vector2(shift_bottom - outline_middle, size.y)) - else: - stroke_polygon.append(Vector2(shift_bottom, size.y)) + stroke_polygon.append(Vector2(shift_bottom, size.y)) if shift_top > 0: - edge_polygon.append(Vector2(0.0, 0.0)) - else: - edge_polygon.append(Vector2(0.0, 0.0 - outline_middle)) - - if shift_bottom > 0: - edge_polygon.append(Vector2(0.0, size.y)) - else: - edge_polygon.append(Vector2(0.0, size.y + outline_middle)) + stroke_polygon.append(Vector2(0.0, 0.0)) draw_colored_polygon(fill_polygon, color) draw_polyline(stroke_polygon, _get_border_color(), Constants.OUTLINE_WIDTH) - draw_polyline(edge_polygon, _get_border_color(), Constants.OUTLINE_WIDTH) From 0118dcd7d44d8de91bdc0e62390da0afbb789842 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Qui=C3=B1ones?= Date: Tue, 26 Nov 2024 10:46:28 -0300 Subject: [PATCH 3/3] 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. --- .../ui/blocks/control_block/control_block.gd | 2 - .../blocks/control_block/control_block.tscn | 5 +- .../ui/blocks/entry_block/entry_block.tscn | 2 +- .../blocks/statement_block/statement_block.gd | 3 - .../statement_block/statement_block.tscn | 1 - .../blocks/utilities/background/background.gd | 148 +++++++++++------- 6 files changed, 99 insertions(+), 62 deletions(-) diff --git a/addons/block_code/ui/blocks/control_block/control_block.gd b/addons/block_code/ui/blocks/control_block/control_block.gd index e2925d96..d96e1df1 100644 --- a/addons/block_code/ui/blocks/control_block/control_block.gd +++ b/addons/block_code/ui/blocks/control_block/control_block.gd @@ -9,9 +9,7 @@ func _ready(): super() %TopBackground.color = color - %TopBackground.shift_bottom = Constants.CONTROL_MARGIN %BottomBackground.color = color - %BottomBackground.shift_top = Constants.CONTROL_MARGIN %SnapPoint.add_theme_constant_override("margin_left", Constants.CONTROL_MARGIN) %SnapGutter.color = color %SnapGutter.custom_minimum_size.x = Constants.CONTROL_MARGIN diff --git a/addons/block_code/ui/blocks/control_block/control_block.tscn b/addons/block_code/ui/blocks/control_block/control_block.tscn index c234cb20..496012a1 100644 --- a/addons/block_code/ui/blocks/control_block/control_block.tscn +++ b/addons/block_code/ui/blocks/control_block/control_block.tscn @@ -46,7 +46,7 @@ unique_name_in_owner = true layout_mode = 2 script = ExtResource("2_tx0qr") color = Color(1, 1, 1, 1) -shift_bottom = 20.0 +block_type = 4 [node name="DragDropArea" parent="VBoxContainer/MarginContainer/Rows/Row" instance=ExtResource("3_21e8n")] layout_mode = 2 @@ -91,7 +91,8 @@ layout_mode = 2 size_flags_horizontal = 0 script = ExtResource("2_tx0qr") color = Color(1, 1, 1, 1) -shift_top = 20.0 +block_type = 4 +control_part = 1 [node name="SnapPoint" parent="VBoxContainer" instance=ExtResource("3_nhryi")] layout_mode = 2 diff --git a/addons/block_code/ui/blocks/entry_block/entry_block.tscn b/addons/block_code/ui/blocks/entry_block/entry_block.tscn index 3419c956..8e031c01 100644 --- a/addons/block_code/ui/blocks/entry_block/entry_block.tscn +++ b/addons/block_code/ui/blocks/entry_block/entry_block.tscn @@ -35,7 +35,7 @@ layout_mode = 2 mouse_filter = 1 script = ExtResource("2_yrw8l") color = Color(1, 1, 1, 1) -show_top = false +block_type = 1 [node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_swkpp")] layout_mode = 2 diff --git a/addons/block_code/ui/blocks/statement_block/statement_block.gd b/addons/block_code/ui/blocks/statement_block/statement_block.gd index ced571b6..249b0a3a 100644 --- a/addons/block_code/ui/blocks/statement_block/statement_block.gd +++ b/addons/block_code/ui/blocks/statement_block/statement_block.gd @@ -12,9 +12,6 @@ var args_to_add_after_format: Dictionary # Only used when loading func _ready(): super() - - if definition != null and definition.type != Types.BlockType.STATEMENT: - _background.show_top = false _background.color = color diff --git a/addons/block_code/ui/blocks/statement_block/statement_block.tscn b/addons/block_code/ui/blocks/statement_block/statement_block.tscn index 4f4d598b..8e10ac84 100644 --- a/addons/block_code/ui/blocks/statement_block/statement_block.tscn +++ b/addons/block_code/ui/blocks/statement_block/statement_block.tscn @@ -34,7 +34,6 @@ unique_name_in_owner = true layout_mode = 2 mouse_filter = 1 script = ExtResource("2_lctqt") -color = Color(1, 1, 1, 1) [node name="DragDropArea" parent="VBoxContainer/TopMarginContainer" instance=ExtResource("3_mbxhq")] layout_mode = 2 diff --git a/addons/block_code/ui/blocks/utilities/background/background.gd b/addons/block_code/ui/blocks/utilities/background/background.gd index 8070c823..11f79667 100644 --- a/addons/block_code/ui/blocks/utilities/background/background.gd +++ b/addons/block_code/ui/blocks/utilities/background/background.gd @@ -3,6 +3,12 @@ extends Control const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd") const Constants = preload("res://addons/block_code/ui/constants.gd") +const Types = preload("res://addons/block_code/types/types.gd") + +enum ControlPart { + TOP, + BOTTOM, +} var outline_color: Color var parent_block: Block @@ -10,16 +16,12 @@ var parent_block: Block @export var color: Color: set = _set_color -@export var show_top: bool = true: - set = _set_show_top - -## Horizontally shift the top knob -@export var shift_top: float = 0.0: - set = _set_shift_top +@export var block_type: Types.BlockType = Types.BlockType.STATEMENT: + set = _set_block_type -## Horizontally shift the bottom knob -@export var shift_bottom: float = 0.0: - set = _set_shift_bottom +## Only relevant if block_type is CONTROL. +@export var control_part: ControlPart = ControlPart.TOP: + set = _set_control_part func _set_color(new_color): @@ -28,18 +30,13 @@ func _set_color(new_color): queue_redraw() -func _set_show_top(new_show_top): - show_top = new_show_top - queue_redraw() - - -func _set_shift_top(new_shift_top): - shift_top = new_shift_top +func _set_block_type(new_block_type): + block_type = new_block_type queue_redraw() -func _set_shift_bottom(new_shift_bottom): - shift_bottom = new_shift_bottom +func _set_control_part(new_control_part): + control_part = new_control_part queue_redraw() @@ -55,47 +52,92 @@ func _get_border_color() -> Color: return outline_color -func _draw(): - var fill_polygon: PackedVector2Array - fill_polygon.append(Vector2(0.0, 0.0)) - if show_top: - fill_polygon.append(Vector2(Constants.KNOB_X + shift_top, 0.0)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + shift_top, Constants.KNOB_H)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + Constants.KNOB_W + shift_top, Constants.KNOB_H)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z * 2 + Constants.KNOB_W + shift_top, 0.0)) - - fill_polygon.append(Vector2(size.x, 0.0)) - fill_polygon.append(Vector2(size.x, size.y)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z * 2 + Constants.KNOB_W + shift_bottom, size.y)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + Constants.KNOB_W + shift_bottom, size.y + Constants.KNOB_H)) - fill_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + shift_bottom, size.y + Constants.KNOB_H)) - fill_polygon.append(Vector2(Constants.KNOB_X + shift_bottom, size.y)) - fill_polygon.append(Vector2(0.0, size.y)) - fill_polygon.append(Vector2(0.0, 0.0)) +func _get_box_shape(box_size: Vector2 = Vector2.ONE) -> PackedVector2Array: + return PackedVector2Array( + [ + Vector2(0.0, 0.0), + Vector2(box_size.x, 0.0), + Vector2(box_size.x, box_size.y), + Vector2(0.0, box_size.y), + Vector2(0.0, 0.0), + ] + ) - var stroke_polygon: PackedVector2Array - if shift_bottom > 0 or shift_top == 0: - stroke_polygon.append(Vector2(0.0, size.y)) +func _get_knob_shape(displacement: Vector2 = Vector2.ZERO) -> PackedVector2Array: + return PackedVector2Array( + [ + Vector2(displacement.x, displacement.y), + Vector2(displacement.x + Constants.KNOB_Z, displacement.y + Constants.KNOB_H), + Vector2(displacement.x + Constants.KNOB_Z + Constants.KNOB_W, displacement.y + Constants.KNOB_H), + Vector2(displacement.x + Constants.KNOB_Z * 2 + Constants.KNOB_W, displacement.y), + ] + ) + + +func _get_entry_shape() -> PackedVector2Array: + var box_shape = _get_box_shape(size) + var bottom_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, size.y)) + bottom_knob_shape.reverse() + return box_shape.slice(0, 3) + bottom_knob_shape + box_shape.slice(3) + - stroke_polygon.append(Vector2(shift_top, 0.0)) +func _get_statement_shape() -> PackedVector2Array: + var box_shape = _get_box_shape(size) + var top_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, 0.0)) + var bottom_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, size.y)) + bottom_knob_shape.reverse() + return box_shape.slice(0, 1) + top_knob_shape + box_shape.slice(1, 3) + bottom_knob_shape + box_shape.slice(3) - if show_top: - stroke_polygon.append(Vector2(Constants.KNOB_X + shift_top, 0.0)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + shift_top, Constants.KNOB_H)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + Constants.KNOB_W + shift_top, Constants.KNOB_H)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z * 2 + Constants.KNOB_W + shift_top, 0.0)) - stroke_polygon.append(Vector2(size.x, 0.0)) - stroke_polygon.append(Vector2(size.x, size.y)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z * 2 + Constants.KNOB_W + shift_bottom, size.y)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + Constants.KNOB_W + shift_bottom, size.y + Constants.KNOB_H)) - stroke_polygon.append(Vector2(Constants.KNOB_X + Constants.KNOB_Z + shift_bottom, size.y + Constants.KNOB_H)) - stroke_polygon.append(Vector2(Constants.KNOB_X + shift_bottom, size.y)) - stroke_polygon.append(Vector2(shift_bottom, size.y)) +func _get_control_top_fill_shape() -> PackedVector2Array: + var box_shape = _get_box_shape(size) + var top_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, 0.0)) + var bottom_knob_shape = _get_knob_shape(Vector2(Constants.CONTROL_MARGIN + Constants.KNOB_X, size.y)) + bottom_knob_shape.reverse() + return box_shape.slice(0, 1) + top_knob_shape + box_shape.slice(1, 3) + bottom_knob_shape + box_shape.slice(3) + + +func _get_control_top_stroke_shape() -> PackedVector2Array: + var shape = _get_control_top_fill_shape() + shape = shape.slice(shape.size() - 2) + shape.slice(0, shape.size() - 2) + shape.append(Vector2(Constants.CONTROL_MARGIN - Constants.OUTLINE_WIDTH / 2, size.y)) + return shape + + +func _get_control_bottom_fill_shape() -> PackedVector2Array: + var box_shape = _get_box_shape(size) + var top_knob_shape = _get_knob_shape(Vector2(Constants.CONTROL_MARGIN + Constants.KNOB_X, 0.0)) + var bottom_knob_shape = _get_knob_shape(Vector2(Constants.KNOB_X, size.y)) + bottom_knob_shape.reverse() + return box_shape.slice(0, 1) + top_knob_shape + box_shape.slice(1, 3) + bottom_knob_shape + box_shape.slice(3) + + +func _get_control_bottom_stroke_shape() -> PackedVector2Array: + var shape = PackedVector2Array([Vector2(Constants.CONTROL_MARGIN - Constants.OUTLINE_WIDTH / 2, 0.0)]) + return shape + _get_control_bottom_fill_shape().slice(1) + + +func _draw(): + var fill_polygon: PackedVector2Array + var stroke_polygon: PackedVector2Array - if shift_top > 0: - stroke_polygon.append(Vector2(0.0, 0.0)) + match block_type: + Types.BlockType.ENTRY: + var shape = _get_entry_shape() + fill_polygon = shape + stroke_polygon = shape + Types.BlockType.STATEMENT: + var shape = _get_statement_shape() + fill_polygon = shape + stroke_polygon = shape + Types.BlockType.CONTROL: + if control_part == ControlPart.TOP: + fill_polygon = _get_control_top_fill_shape() + stroke_polygon = _get_control_top_stroke_shape() + else: + fill_polygon = _get_control_bottom_fill_shape() + stroke_polygon = _get_control_bottom_stroke_shape() draw_colored_polygon(fill_polygon, color) draw_polyline(stroke_polygon, _get_border_color(), Constants.OUTLINE_WIDTH)