From 29f1501f918108be8f73ba0bdce27fbccdeb9f83 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] 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 | 147 +++++++++++------- 6 files changed, 98 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..6bfe5504 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,91 @@ 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)) + if block_type == Types.BlockType.ENTRY: + var shape = _get_entry_shape() + fill_polygon = shape + stroke_polygon = shape + elif block_type == Types.BlockType.STATEMENT: + var shape = _get_statement_shape() + fill_polygon = shape + stroke_polygon = shape + elif block_type == 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)