Skip to content

Commit 63db182

Browse files
authored
Merge pull request #170 from endlessm/gh162-del-node
Block: Allow deleting with Delete button
2 parents a5d5b65 + 4f98996 commit 63db182

File tree

14 files changed

+110
-16
lines changed

14 files changed

+110
-16
lines changed

addons/block_code/drag_manager/drag.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var target_snap_point: SnapPoint:
3434

3535
var snap_block: Block:
3636
get:
37-
return target_snap_point.get_parent_block() if target_snap_point else null
37+
return BlockTreeUtil.get_parent_block(target_snap_point) if target_snap_point else null
3838

3939

4040
func _init(block: Block, block_scope: String, offset: Vector2, block_canvas: BlockCanvas):

addons/block_code/drag_manager/drag_manager.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func drag_ended():
8080

8181
if block:
8282
connect_block_canvas_signals(block)
83+
block.grab_focus()
84+
85+
# Allow the block to be deleted now that it's on the canvas.
86+
block.can_delete = true
8387

8488
_block_canvas.release_scope()
8589

addons/block_code/ui/block_tree_util.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,11 @@ static func get_tree_scope(node: Node) -> String:
7878
if scope != "":
7979
return scope
8080
return ""
81+
82+
83+
## Get the nearest Block node that is a parent of the provided node.
84+
static func get_parent_block(node: Node) -> Block:
85+
var parent = node.get_parent()
86+
while parent and not parent is Block:
87+
parent = parent.get_parent()
88+
return parent as Block

addons/block_code/ui/blocks/block/block.gd

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ signal modified
3939
# https://github.com/godotengine/godot/issues/82670
4040
var bottom_snap: SnapPoint
4141

42+
## Whether the block can be deleted by the Delete key.
43+
var can_delete: bool = true
44+
4245

4346
func _set_bottom_snap_path(value: NodePath):
4447
bottom_snap_path = value
@@ -48,9 +51,40 @@ func _set_bottom_snap_path(value: NodePath):
4851
func _ready():
4952
if bottom_snap == null:
5053
_set_bottom_snap_path(bottom_snap_path)
54+
focus_mode = FocusMode.FOCUS_ALL
5155
mouse_filter = Control.MOUSE_FILTER_IGNORE
5256

5357

58+
func _gui_input(event):
59+
if event is InputEventKey:
60+
if event.pressed and event.keycode == KEY_DELETE:
61+
# Always accept the Delete key so it doesn't propagate to the
62+
# BlockCode node in the scene tree.
63+
accept_event()
64+
65+
if not can_delete:
66+
return
67+
68+
var dialog := ConfirmationDialog.new()
69+
var num_blocks = _count_child_blocks(self) + 1
70+
# FIXME: Maybe this should use block_name or label, but that
71+
# requires one to be both unique and human friendly.
72+
if num_blocks > 1:
73+
dialog.dialog_text = "Delete %d blocks?" % num_blocks
74+
else:
75+
dialog.dialog_text = "Delete block?"
76+
dialog.confirmed.connect(remove_from_tree)
77+
EditorInterface.popup_dialog_centered(dialog)
78+
79+
80+
func remove_from_tree():
81+
var parent = get_parent()
82+
if parent:
83+
parent.remove_child(self)
84+
queue_free()
85+
modified.emit()
86+
87+
5488
static func get_block_class():
5589
push_error("Unimplemented.")
5690

@@ -124,3 +158,14 @@ func _make_custom_tooltip(for_text) -> Control:
124158
var tooltip = preload("res://addons/block_code/ui/tooltip/tooltip.tscn").instantiate()
125159
tooltip.text = for_text
126160
return tooltip
161+
162+
163+
func _count_child_blocks(node: Node) -> int:
164+
var count = 0
165+
166+
for child in node.get_children():
167+
if child is SnapPoint and child.has_snapped_block():
168+
count += 1
169+
count += _count_child_blocks(child)
170+
171+
return count

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
[node name="ControlBlock" type="MarginContainer"]
77
size_flags_horizontal = 0
8+
focus_mode = 2
89
mouse_filter = 2
910
script = ExtResource("1_2hbir")
1011
block_name = &"control_block"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[ext_resource type="Script" path="res://addons/block_code/ui/blocks/entry_block/entry_block.gd" id="2_3ik8h"]
55

66
[node name="EntryBlock" instance=ExtResource("1_byjbb")]
7+
focus_mode = 2
78
script = ExtResource("2_3ik8h")
89
signal_name = ""
910
block_name = &"entry_block"

addons/block_code/ui/blocks/parameter_block/parameter_block.gd

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
class_name ParameterBlock
33
extends Block
44

5+
const Constants = preload("res://addons/block_code/ui/constants.gd")
56
const Util = preload("res://addons/block_code/ui/util.gd")
67

78
@export var block_format: String = ""
@@ -16,16 +17,24 @@ var param_name_input_pairs: Array
1617
var param_input_strings: Dictionary # Only loaded from serialized
1718
var spawned_by: ParameterOutput
1819

20+
var _panel_normal: StyleBox
21+
var _panel_focus: StyleBox
22+
1923

2024
func _ready():
2125
super()
2226

27+
_panel_normal = _panel.get_theme_stylebox("panel").duplicate()
28+
_panel_normal.bg_color = color
29+
_panel_normal.border_color = color.darkened(0.2)
30+
31+
_panel_focus = _panel.get_theme_stylebox("panel").duplicate()
32+
_panel_focus.bg_color = color
33+
_panel_focus.border_color = Constants.FOCUS_BORDER_COLOR
34+
2335
block_type = Types.BlockType.VALUE
2436
if not Util.node_is_part_of_edited_scene(self):
25-
var new_panel = _panel.get_theme_stylebox("panel").duplicate()
26-
new_panel.bg_color = color
27-
new_panel.border_color = color.darkened(0.2)
28-
_panel.add_theme_stylebox_override("panel", new_panel)
37+
_panel.add_theme_stylebox_override("panel", _panel_normal)
2938

3039
format()
3140

@@ -73,3 +82,11 @@ static func get_scene_path():
7382

7483
func format():
7584
param_name_input_pairs = StatementBlock.format_string(self, %HBoxContainer, block_format, defaults)
85+
86+
87+
func _on_focus_entered():
88+
_panel.add_theme_stylebox_override("panel", _panel_focus)
89+
90+
91+
func _on_focus_exited():
92+
_panel.add_theme_stylebox_override("panel", _panel_normal)

addons/block_code/ui/blocks/parameter_block/parameter_block.tscn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ corner_radius_bottom_left = 16
1818
offset_right = 16.0
1919
offset_bottom = 8.0
2020
size_flags_horizontal = 0
21+
focus_mode = 2
2122
mouse_filter = 2
2223
script = ExtResource("1_0hajy")
2324
block_name = &"parameter_block"
@@ -46,4 +47,6 @@ unique_name_in_owner = true
4647
layout_mode = 2
4748
mouse_filter = 2
4849

50+
[connection signal="focus_entered" from="." to="." method="_on_focus_entered"]
51+
[connection signal="focus_exited" from="." to="." method="_on_focus_exited"]
4952
[connection signal="mouse_down" from="DragDropArea" to="." method="_on_drag_drop_area_mouse_down"]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
[node name="StatementBlock" type="MarginContainer"]
99
size_flags_horizontal = 0
10+
focus_mode = 2
1011
mouse_filter = 2
1112
script = ExtResource("1_6wvlf")
1213
block_name = &"statement_block"

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
@tool
22
extends Control
33

4+
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
45
const Constants = preload("res://addons/block_code/ui/constants.gd")
56

67
var outline_color: Color
8+
var parent_block: Block
79

810
@export var color: Color:
911
set = _set_color
@@ -41,6 +43,12 @@ func _set_shift_bottom(new_shift_bottom):
4143
queue_redraw()
4244

4345

46+
func _ready():
47+
parent_block = BlockTreeUtil.get_parent_block(self)
48+
parent_block.focus_entered.connect(queue_redraw)
49+
parent_block.focus_exited.connect(queue_redraw)
50+
51+
4452
func _draw():
4553
var fill_polygon: PackedVector2Array
4654
fill_polygon.append(Vector2(0.0, 0.0))
@@ -97,5 +105,5 @@ func _draw():
97105
edge_polygon.append(Vector2(0.0, size.y + outline_middle))
98106

99107
draw_colored_polygon(fill_polygon, color)
100-
draw_polyline(stroke_polygon, outline_color, Constants.OUTLINE_WIDTH)
101-
draw_polyline(edge_polygon, outline_color, Constants.OUTLINE_WIDTH)
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)

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
@tool
22
extends Control
33

4+
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
45
const Constants = preload("res://addons/block_code/ui/constants.gd")
56

67
var outline_color: Color
8+
var parent_block: Block
79

810
@export var color: Color:
911
set = _set_color
@@ -15,6 +17,12 @@ func _set_color(new_color):
1517
queue_redraw()
1618

1719

20+
func _ready():
21+
parent_block = BlockTreeUtil.get_parent_block(self)
22+
parent_block.focus_entered.connect(queue_redraw)
23+
parent_block.focus_exited.connect(queue_redraw)
24+
25+
1826
func _draw():
1927
var fill_polygon: PackedVector2Array
2028
fill_polygon.append(Vector2(0.0, 0.0))
@@ -33,5 +41,5 @@ func _draw():
3341
right_polygon.append(Vector2(size.x, size.y))
3442

3543
draw_colored_polygon(fill_polygon, color)
36-
draw_polyline(left_polygon, outline_color, Constants.OUTLINE_WIDTH)
37-
draw_polyline(right_polygon, outline_color, Constants.OUTLINE_WIDTH)
44+
draw_polyline(left_polygon, Constants.FOCUS_BORDER_COLOR if parent_block.has_focus() else outline_color, Constants.OUTLINE_WIDTH)
45+
draw_polyline(right_polygon, Constants.FOCUS_BORDER_COLOR if parent_block.has_focus() else outline_color, Constants.OUTLINE_WIDTH)

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,6 @@ func _ready():
2929
_update_snapped_block_from_children()
3030

3131

32-
func get_parent_block() -> Block:
33-
var parent = get_parent()
34-
while parent and not parent is Block:
35-
parent = parent.get_parent()
36-
return parent as Block
37-
38-
3932
func _update_snapped_block_from_children():
4033
# Temporary migration to set the snapped_block property based on children
4134
# of this node.

addons/block_code/ui/constants.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ const KNOB_Z = 5.0
99
const CONTROL_MARGIN = 20.0
1010
const OUTLINE_WIDTH = 3.0
1111
const MINIMUM_SNAP_DISTANCE = 80.0
12+
13+
const FOCUS_BORDER_COLOR = Color(225, 242, 0)

addons/block_code/ui/picker/picker.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ func init_picker(extra_blocks: Array[Block] = [], extra_categories: Array[BlockC
8585
var block: Block = _block as Block
8686
block.drag_started.connect(_block_picked)
8787

88+
# Don't allow the block to be deleted while in the picker.
89+
block.can_delete = false
90+
8891
_block_scroll.scroll_vertical = 0
8992

9093

0 commit comments

Comments
 (0)