Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port node delete action to godot3 #17

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions addons/gd-blender-3d-shortcuts/plugin.gd
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ func forward_spatial_gui_input(camera, event):
forward = true
KEY_H:
commit_hide_nodes()
KEY_X:
if event.shift:
delete_selected_nodes()
else:
confirm_delete_selected_nodes()
else:
if event is InputEventKey:
# Not sure why event.pressed always return false for numpad keys
Expand Down Expand Up @@ -506,6 +511,60 @@ func commit_hide_nodes():
undo_redo.add_undo_method(Utils, "hide_nodes", nodes, false)
undo_redo.commit_action()

## Opens a popup dialog to confirm deletion of selected nodes.
func confirm_delete_selected_nodes():
var selected_nodes = get_editor_interface().get_selection().get_selected_nodes()
if selected_nodes.empty():
return

var editor_theme = get_editor_interface().get_base_control().theme
var popup = ConfirmationDialog.new()
popup.theme = editor_theme

# Setting dialog text dynamically depending on the selection to mimick Godot's normal behavior.
popup.dialog_text = "Delete "
var selection_size = selected_nodes.size()
if selection_size == 1:
popup.dialog_text += selected_nodes[0].get_name()
elif selection_size > 1:
popup.dialog_text += str(selection_size) + " nodes"
for node in selected_nodes:
if node.get_child_count() > 0:
popup.dialog_text += " and children"
break
popup.dialog_text += "?"

add_child(popup)
popup.popup_centered()

popup.get_cancel().connect("pressed", popup, "queue_free")
popup.connect("confirmed", popup, "queue_free")
popup.connect("confirmed", self, "delete_selected_nodes")

## Instantly deletes selected nodes and creates an undo history entry.
func delete_selected_nodes():
var undo_redo = get_undo_redo()

var selected_nodes = get_editor_interface().get_selection().get_selected_nodes()
# Avoid creating an unnecessary history entry if no nodes are selected.
if selected_nodes.empty():
return

undo_redo.create_action("Delete Nodes", UndoRedo.MERGE_DISABLE)
for node in selected_nodes:
# We can't free nodes, they must be kept in memory for undo to work.
# That's why we use remove_child instead and call UndoRedo.add_undo_reference() below.
undo_redo.add_do_method(node.get_parent(), "remove_child", node)
undo_redo.add_undo_method(node.get_parent(), "add_child", node, true)
undo_redo.add_undo_method(node.get_parent(), "move_child", node, node.get_index())
# Every node's owner must be set upon undoing, otherwise, it won't appear in the scene dock
# and it'll be lost upon saving.
undo_redo.add_undo_method(node, "set_owner", node.owner)
for child in Utils.recursive_get_children(node):
undo_redo.add_undo_method(child, "set_owner", node.owner)
undo_redo.add_undo_reference(node)
undo_redo.commit_action()

func revert():
var nodes = get_editor_interface().get_selection().get_transformable_selected_nodes()
Utils.revert_transform(nodes, _cache_global_transforms)
Expand Down