Skip to content

Commit 29e1d3e

Browse files
committed
feat: add duplication feature
1 parent a9c7193 commit 29e1d3e

File tree

2 files changed

+101
-18
lines changed

2 files changed

+101
-18
lines changed

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

+74-18
Original file line numberDiff line numberDiff line change
@@ -163,24 +163,30 @@ func _on_block_extension_changed():
163163

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

185191

186192
func remove_from_tree():
@@ -191,6 +197,31 @@ func remove_from_tree():
191197
modified.emit()
192198

193199

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

@@ -239,3 +270,28 @@ func _count_child_blocks(node: Node) -> int:
239270
count += _count_child_blocks(child)
240271

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

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

+27
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,6 +17,7 @@ signal drag_started(offset: Vector2)
1617
@export var drag_outside: bool = false
1718

1819
var _drag_start_position: Vector2 = Vector2.INF
20+
var parent_block: Block
1921

2022

2123
func _gui_input(event: InputEvent) -> void:
@@ -27,6 +29,18 @@ func _gui_input(event: InputEvent) -> void:
2729

2830
var button_event: InputEventMouseButton = event as InputEventMouseButton
2931

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

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

0 commit comments

Comments
 (0)