Skip to content

Commit 640ede8

Browse files
committed
feat: add duplication feature
1 parent a9c7193 commit 640ede8

File tree

2 files changed

+98
-22
lines changed

2 files changed

+98
-22
lines changed

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

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -160,28 +160,32 @@ func _get_or_create_block_extension() -> BlockExtension:
160160
func _on_block_extension_changed():
161161
_update_template_editor()
162162

163-
164163
func _gui_input(event):
165-
if event is InputEventKey:
166-
if event.pressed and event.keycode == KEY_DELETE:
167-
# Always accept the Delete key so it doesn't propagate to the
168-
# BlockCode node in the scene tree.
169-
accept_event()
170-
171-
if not can_delete:
172-
return
173-
174-
var dialog := ConfirmationDialog.new()
175-
var num_blocks = _count_child_blocks(self) + 1
176-
# FIXME: Maybe this should use block_name or label, but that
177-
# requires one to be both unique and human friendly.
178-
if num_blocks > 1:
179-
dialog.dialog_text = "Delete %d blocks?" % num_blocks
180-
else:
181-
dialog.dialog_text = "Delete block?"
182-
dialog.confirmed.connect(remove_from_tree)
183-
EditorInterface.popup_dialog_centered(dialog)
184-
164+
if event is InputEventKey:
165+
if event.pressed:
166+
if event.keycode == KEY_DELETE:
167+
# Always accept the Delete key so it doesn't propagate to the
168+
# BlockCode node in the scene tree.
169+
accept_event()
170+
171+
if not can_delete:
172+
return
173+
174+
var dialog := ConfirmationDialog.new()
175+
var num_blocks = _count_child_blocks(self) + 1
176+
# FIXME: Maybe this should use block_name or label, but that
177+
# requires one to be both unique and human friendly.
178+
if num_blocks > 1:
179+
dialog.dialog_text = "Delete %d blocks?" % num_blocks
180+
else:
181+
dialog.dialog_text = "Delete block?"
182+
dialog.confirmed.connect(remove_from_tree)
183+
EditorInterface.popup_dialog_centered(dialog)
184+
elif event.ctrl_pressed and not event.shift_pressed and not event.alt_pressed and not event.meta_pressed:
185+
if event.keycode == KEY_D:
186+
# Handle duplicate key
187+
accept_event()
188+
confirm_duplicate()
185189

186190
func remove_from_tree():
187191
var parent = get_parent()
@@ -190,6 +194,30 @@ func remove_from_tree():
190194
queue_free()
191195
modified.emit()
192196

197+
func confirm_duplicate():
198+
if not can_delete:
199+
return
200+
201+
var new_block: Block = _context.block_script.instantiate_block(definition)
202+
203+
var new_parent: Node = get_parent()
204+
while not new_parent.name == "Window":
205+
new_parent = new_parent.get_parent()
206+
207+
if not _block_canvas:
208+
_block_canvas = get_parent()
209+
while not _block_canvas.name == "BlockCanvas":
210+
_block_canvas = _block_canvas.get_parent()
211+
212+
new_parent.add_child(new_block)
213+
new_block.global_position = global_position + (Vector2(100, 50) * new_parent.scale)
214+
215+
_copy_snapped_blocks(self, new_block)
216+
217+
_block_canvas.reconnect_block.emit(new_block)
218+
219+
modified.emit()
220+
193221

194222
static func get_block_class():
195223
push_error("Unimplemented.")
@@ -239,3 +267,27 @@ func _count_child_blocks(node: Node) -> int:
239267
count += _count_child_blocks(child)
240268

241269
return count
270+
271+
func _copy_snapped_blocks(copy_from: Node, copy_to: Node):
272+
var copy_to_child: Node
273+
var child_index := 0
274+
var maximum_count := copy_to.get_child_count()
275+
276+
for copy_from_child in copy_from.get_children():
277+
if child_index + 1 > maximum_count:
278+
return
279+
280+
copy_to_child = copy_to.get_child(child_index)
281+
child_index += 1
282+
283+
if copy_from_child is SnapPoint and copy_from_child.has_snapped_block():
284+
copy_to_child.add_child(_context.block_script.instantiate_block(copy_from_child.snapped_block.definition))
285+
_block_canvas.reconnect_block.emit(copy_to_child.snapped_block)
286+
elif copy_from_child.name.begins_with("ParameterInput"):
287+
var raw_input = copy_from_child.get_raw_input()
288+
289+
if not raw_input is Block:
290+
copy_to_child.set_raw_input(raw_input)
291+
292+
if copy_from_child is Container:
293+
_copy_snapped_blocks(copy_from_child, copy_to_child)

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
extends Control
99

1010
const Constants = preload("res://addons/block_code/ui/constants.gd")
11+
const BlockTreeUtil = preload("res://addons/block_code/ui/block_tree_util.gd")
1112

1213
signal drag_started(offset: Vector2)
1314

@@ -16,7 +17,7 @@ signal drag_started(offset: Vector2)
1617
@export var drag_outside: bool = false
1718

1819
var _drag_start_position: Vector2 = Vector2.INF
19-
20+
var parent_block: Block
2021

2122
func _gui_input(event: InputEvent) -> void:
2223
# Watch for mouse clicks using _gui_input, so events are filtered based on
@@ -27,6 +28,18 @@ func _gui_input(event: InputEvent) -> void:
2728

2829
var button_event: InputEventMouseButton = event as InputEventMouseButton
2930

31+
if button_event.button_index == MOUSE_BUTTON_RIGHT and button_event.pressed:
32+
if not parent_block:
33+
parent_block = BlockTreeUtil.get_parent_block(self)
34+
if parent_block and parent_block.can_delete:
35+
accept_event()
36+
var _context_menu := PopupMenu.new()
37+
_context_menu.add_icon_item(EditorInterface.get_editor_theme().get_icon("Duplicate", "EditorIcons"), "Duplicate")
38+
_context_menu.id_pressed.connect(_menu_pressed.bind(_context_menu))
39+
_context_menu.position = DisplayServer.mouse_get_position()
40+
add_child(_context_menu)
41+
_context_menu.show()
42+
3043
if button_event.button_index != MOUSE_BUTTON_LEFT:
3144
return
3245

@@ -64,3 +77,14 @@ func _input(event: InputEvent) -> void:
6477
get_viewport().set_input_as_handled()
6578
drag_started.emit(_drag_start_position - motion_event.global_position)
6679
_drag_start_position = Vector2.INF
80+
81+
func _menu_pressed(_index: int, _context_menu: PopupMenu):
82+
var _pressed_label: String = _context_menu.get_item_text(_index)
83+
84+
if _pressed_label == "Duplicate":
85+
parent_block.confirm_duplicate()
86+
87+
func _cleanup():
88+
for child in get_children():
89+
remove_child(child)
90+
child.queue_free()

0 commit comments

Comments
 (0)