From 5f5cbc9d9f8c9ea693a580c0ec154337d0fd9393 Mon Sep 17 00:00:00 2001 From: Daze <183644421+DoomTas3r@users.noreply.github.com> Date: Thu, 7 Aug 2025 14:26:51 -0400 Subject: [PATCH] Adds delete variable button and dialogue Fix for #360 Adds the delete variable button with a dialogue box and check boxes to select variable names and delete them if they exist. The delete variable functionality resembles the create variable functionality, including the ability to undo. Functionality missing from this commit might include hiding the delete variables button when there are no variables, and disabling the delete button if no check boxes are checked. --- addons/block_code/ui/main_panel.gd | 23 +++++++++++ .../delete_variables_button.gd | 20 ++++++++++ .../delete_variables_button.gd.uid | 1 + .../delete_variables_button.tscn | 37 ++++++++++++++++++ .../delete_variables_dialog.gd | 38 +++++++++++++++++++ .../delete_variables_dialog.gd.uid | 1 + .../delete_variables_dialog.tscn | 22 +++++++++++ .../variable_category_display.gd | 5 +++ .../variable_category_display.tscn | 9 ++++- addons/block_code/ui/picker/picker.gd | 2 + 10 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd.uid create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd.uid create mode 100644 addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn diff --git a/addons/block_code/ui/main_panel.gd b/addons/block_code/ui/main_panel.gd index cdc8e8f7..83d47618 100644 --- a/addons/block_code/ui/main_panel.gd +++ b/addons/block_code/ui/main_panel.gd @@ -50,6 +50,7 @@ func _ready(): _picker.block_picked.connect(_drag_manager.copy_picked_block_and_drag) _picker.variable_created.connect(_create_variable) + _picker.variables_deleted.connect(_delete_variables) _block_canvas.reconnect_block.connect(_drag_manager.connect_block_canvas_signals) _drag_manager.block_dropped.connect(save_script) _drag_manager.block_modified.connect(save_script) @@ -284,3 +285,25 @@ func _create_variable(variable: VariableDefinition): undo_redo.commit_action() _picker.reload_blocks() + + +func _delete_variables(variables_to_delete: Array): + if _context.block_code_node == null: + print("No script loaded to delete variables from.") + return + + var block_script: BlockScriptSerialization = _context.block_script + + undo_redo.create_action("Delete variables %s in %s's block code script" % [variables_to_delete, _context.parent_node.name]) + undo_redo.add_undo_property(_context.block_script, "variables", _context.block_script.variables) + + var new_variables = block_script.variables.duplicate() + for index in range(new_variables.size() - 1, -1, -1): + var variable = new_variables[index] + if variable.var_name in variables_to_delete: + new_variables.erase(variable) + + undo_redo.add_do_property(_context.block_script, "variables", new_variables) + undo_redo.commit_action() + + _picker.reload_blocks() diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd new file mode 100644 index 00000000..1306b932 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd @@ -0,0 +1,20 @@ +@tool +extends MarginContainer + +signal delete_variables(variables: Array[String]) + +@onready var _delete_variables_dialog := %DeleteVariablesDialog +@onready var _delete_button := %DeleteButton +@onready var _delete_variables_icon = _delete_button.get_theme_icon("Remove", "EditorIcons") + + +func _ready() -> void: + _delete_button.icon = _delete_variables_icon + + +func _on_delete_button_pressed(): + _delete_variables_dialog.popup() + + +func _on_delete_variables_dialog_delete_variables(variables): + delete_variables.emit(variables) diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd.uid b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd.uid new file mode 100644 index 00000000..105c60b0 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd.uid @@ -0,0 +1 @@ +uid://8b8f5cd61gic diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn new file mode 100644 index 00000000..7f360703 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn @@ -0,0 +1,37 @@ +[gd_scene load_steps=5 format=3 uid="uid://ba2ckluuotftw"] + +[ext_resource type="PackedScene" uid="uid://bxkdgyj0kpexu" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn" id="1_5id0e"] +[ext_resource type="Script" uid="uid://8b8f5cd61gic" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd" id="1_fr423"] + +[sub_resource type="Image" id="Image_fr423"] +data = { +"data": PackedByteArray(255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 92, 92, 127, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 255, 255, 92, 92, 127, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 92, 92, 127, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 92, 92, 127, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 231, 255, 90, 90, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 231, 255, 90, 90, 54, 255, 94, 94, 57, 255, 93, 93, 233, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 42, 255, 90, 90, 0, 255, 94, 94, 0, 255, 91, 91, 42, 255, 93, 93, 233, 255, 92, 92, 232, 255, 93, 93, 41, 255, 90, 90, 0, 255, 94, 94, 0, 255, 91, 91, 42, 255, 93, 93, 233, 255, 92, 92, 232, 255, 92, 92, 0, 255, 92, 92, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 45, 255, 93, 93, 44, 255, 91, 91, 0, 255, 91, 91, 42, 255, 91, 91, 42, 255, 93, 93, 0, 255, 91, 91, 45, 255, 93, 93, 44, 255, 91, 91, 0, 255, 91, 91, 42, 255, 91, 91, 42, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 45, 255, 92, 92, 235, 255, 92, 92, 234, 255, 89, 89, 43, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 45, 255, 92, 92, 235, 255, 92, 92, 234, 255, 89, 89, 43, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 91, 91, 0, 255, 92, 92, 0, 255, 92, 92, 0, 255, 92, 92, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 91, 91, 59, 255, 92, 92, 61, 255, 92, 92, 235, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 233, 255, 91, 91, 59, 255, 92, 92, 61, 255, 92, 92, 0, 255, 92, 92, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 255, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0, 255, 93, 93, 0), +"format": "RGBA8", +"height": 16, +"mipmaps": false, +"width": 16 +} + +[sub_resource type="ImageTexture" id="ImageTexture_sdiwn"] +image = SubResource("Image_fr423") + +[node name="DeleteVariablesButton" type="MarginContainer"] +offset_right = 186.0 +offset_bottom = 43.0 +size_flags_horizontal = 0 +theme_override_constants/margin_bottom = 12 +script = ExtResource("1_fr423") + +[node name="DeleteButton" type="Button" parent="."] +unique_name_in_owner = true +layout_mode = 2 +theme_type_variation = &"InspectorActionButton" +text = "Delete Variables" +icon = SubResource("ImageTexture_sdiwn") + +[node name="DeleteVariablesDialog" parent="." instance=ExtResource("1_5id0e")] +unique_name_in_owner = true +visible = false + +[connection signal="pressed" from="DeleteButton" to="." method="_on_delete_button_pressed"] +[connection signal="delete_variables" from="DeleteVariablesDialog" to="." method="_on_delete_variables_dialog_delete_variables"] diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd new file mode 100644 index 00000000..47644204 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd @@ -0,0 +1,38 @@ +@tool +extends ConfirmationDialog + +const BlockCategoryDisplay = preload("res://addons/block_code/ui/picker/categories/block_category_display.gd") + +signal delete_variables(variables: Array[String]) + +@onready var _variables_container := %VariablesContainer +var _checkbox_template := CheckBox.new() +var _main_panel: Node + + +func _ready(): + _main_panel = get_parent() + + +func _on_confirmed(): + var variables := [] + for checkbox in _variables_container.get_children(): + if checkbox.button_pressed: + variables.append(checkbox.text) + + delete_variables.emit(variables) + + hide() + + +func _on_about_to_popup() -> void: + for checkbox in _variables_container.get_children(): + _variables_container.remove_child(checkbox) + checkbox.queue_free() + + while _main_panel.name != "MainPanel": + _main_panel = _main_panel.get_parent() + + for variable in _main_panel._context.block_script.variables: + _checkbox_template.text = variable.var_name + _variables_container.add_child(_checkbox_template.duplicate()) diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd.uid b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd.uid new file mode 100644 index 00000000..3fb38ae6 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd.uid @@ -0,0 +1 @@ +uid://duu6fvcrvhxrh diff --git a/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn new file mode 100644 index 00000000..8fa29e15 --- /dev/null +++ b/addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn @@ -0,0 +1,22 @@ +[gd_scene load_steps=2 format=3 uid="uid://bxkdgyj0kpexu"] + +[ext_resource type="Script" uid="uid://duu6fvcrvhxrh" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd" id="1_l45s3"] + +[node name="DeleteVariablesDialog" type="ConfirmationDialog"] +title = "Delete Variables" +initial_position = 1 +size = Vector2i(300, 183) +visible = true +ok_button_text = "Delete" +dialog_hide_on_ok = false +script = ExtResource("1_l45s3") + +[node name="VariablesContainer" type="VBoxContainer" parent="."] +unique_name_in_owner = true +offset_left = 8.0 +offset_top = 8.0 +offset_right = 292.0 +offset_bottom = 134.0 + +[connection signal="about_to_popup" from="." to="." method="_on_about_to_popup"] +[connection signal="confirmed" from="." to="." method="_on_confirmed"] diff --git a/addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd b/addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd index 23a0bd51..f497cea1 100644 --- a/addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd +++ b/addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd @@ -7,6 +7,7 @@ const VariableDefinition = preload("res://addons/block_code/code_generation/vari @onready var h_separator := %HSeparator signal variable_created(variable: VariableDefinition) +signal variables_deleted(variables: Array[String]) func _ready(): @@ -22,3 +23,7 @@ func _update_blocks(): func _on_create_variable(var_name, var_type): variable_created.emit(VariableDefinition.new(var_name, Types.STRING_TO_VARIANT_TYPE[var_type])) + + +func _on_delete_variables(variables): + variables_deleted.emit(variables) diff --git a/addons/block_code/ui/picker/categories/variable_category/variable_category_display.tscn b/addons/block_code/ui/picker/categories/variable_category/variable_category_display.tscn index 955747b4..92d687df 100644 --- a/addons/block_code/ui/picker/categories/variable_category/variable_category_display.tscn +++ b/addons/block_code/ui/picker/categories/variable_category/variable_category_display.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=5 format=3 uid="uid://byne4g2yvdf3"] +[gd_scene load_steps=6 format=3 uid="uid://byne4g2yvdf3"] [ext_resource type="PackedScene" uid="uid://duhpwtfo3k0sk" path="res://addons/block_code/ui/picker/categories/block_category_display.tscn" id="1_vermd"] -[ext_resource type="Script" path="res://addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd" id="2_ggvi7"] +[ext_resource type="Script" uid="uid://d1owr45rep8tx" path="res://addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd" id="2_ggvi7"] [ext_resource type="PackedScene" uid="uid://t0eoc4ekvjr1" path="res://addons/block_code/ui/picker/categories/variable_category/create_variable_button.tscn" id="3_gjvnq"] +[ext_resource type="PackedScene" uid="uid://ba2ckluuotftw" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn" id="4_ppkk1"] [sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_0m6vh"] @@ -22,4 +23,8 @@ theme_override_styles/separator = SubResource("StyleBoxEmpty_0m6vh") [node name="CreateVariableButton" parent="VBoxContainer" index="3" instance=ExtResource("3_gjvnq")] layout_mode = 2 +[node name="DeleteVariablesButton" parent="VBoxContainer" index="4" instance=ExtResource("4_ppkk1")] +layout_mode = 2 + [connection signal="create_variable" from="VBoxContainer/CreateVariableButton" to="." method="_on_create_variable"] +[connection signal="delete_variables" from="VBoxContainer/DeleteVariablesButton" to="." method="_on_delete_variables"] diff --git a/addons/block_code/ui/picker/picker.gd b/addons/block_code/ui/picker/picker.gd index 92ffafdf..b09a4b56 100644 --- a/addons/block_code/ui/picker/picker.gd +++ b/addons/block_code/ui/picker/picker.gd @@ -26,6 +26,7 @@ const CATEGORY_ORDER_OVERRIDE = { signal block_picked(block: Block, offset: Vector2) signal variable_created(variable: VariableDefinition) +signal variables_deleted(variables: Array[String]) @onready var _context := BlockEditorContext.get_default() @@ -120,6 +121,7 @@ func _get_or_create_block_category_display(category: BlockCategory) -> BlockCate else: block_category_display = VariableCategoryDisplayScene.instantiate() block_category_display.variable_created.connect(func(variable): variable_created.emit(variable)) + block_category_display.variables_deleted.connect(func(variables): variables_deleted.emit(variables)) block_category_display.title = category.name if category else "" block_category_display.block_picked.connect(func(block: Block, offset: Vector2): block_picked.emit(block, offset))