Skip to content

Commit 5f5cbc9

Browse files
authored
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.
1 parent c81a5f4 commit 5f5cbc9

10 files changed

+156
-2
lines changed

addons/block_code/ui/main_panel.gd

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func _ready():
5050

5151
_picker.block_picked.connect(_drag_manager.copy_picked_block_and_drag)
5252
_picker.variable_created.connect(_create_variable)
53+
_picker.variables_deleted.connect(_delete_variables)
5354
_block_canvas.reconnect_block.connect(_drag_manager.connect_block_canvas_signals)
5455
_drag_manager.block_dropped.connect(save_script)
5556
_drag_manager.block_modified.connect(save_script)
@@ -284,3 +285,25 @@ func _create_variable(variable: VariableDefinition):
284285
undo_redo.commit_action()
285286

286287
_picker.reload_blocks()
288+
289+
290+
func _delete_variables(variables_to_delete: Array):
291+
if _context.block_code_node == null:
292+
print("No script loaded to delete variables from.")
293+
return
294+
295+
var block_script: BlockScriptSerialization = _context.block_script
296+
297+
undo_redo.create_action("Delete variables %s in %s's block code script" % [variables_to_delete, _context.parent_node.name])
298+
undo_redo.add_undo_property(_context.block_script, "variables", _context.block_script.variables)
299+
300+
var new_variables = block_script.variables.duplicate()
301+
for index in range(new_variables.size() - 1, -1, -1):
302+
var variable = new_variables[index]
303+
if variable.var_name in variables_to_delete:
304+
new_variables.erase(variable)
305+
306+
undo_redo.add_do_property(_context.block_script, "variables", new_variables)
307+
undo_redo.commit_action()
308+
309+
_picker.reload_blocks()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@tool
2+
extends MarginContainer
3+
4+
signal delete_variables(variables: Array[String])
5+
6+
@onready var _delete_variables_dialog := %DeleteVariablesDialog
7+
@onready var _delete_button := %DeleteButton
8+
@onready var _delete_variables_icon = _delete_button.get_theme_icon("Remove", "EditorIcons")
9+
10+
11+
func _ready() -> void:
12+
_delete_button.icon = _delete_variables_icon
13+
14+
15+
func _on_delete_button_pressed():
16+
_delete_variables_dialog.popup()
17+
18+
19+
func _on_delete_variables_dialog_delete_variables(variables):
20+
delete_variables.emit(variables)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://8b8f5cd61gic
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[gd_scene load_steps=5 format=3 uid="uid://ba2ckluuotftw"]
2+
3+
[ext_resource type="PackedScene" uid="uid://bxkdgyj0kpexu" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.tscn" id="1_5id0e"]
4+
[ext_resource type="Script" uid="uid://8b8f5cd61gic" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_button.gd" id="1_fr423"]
5+
6+
[sub_resource type="Image" id="Image_fr423"]
7+
data = {
8+
"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),
9+
"format": "RGBA8",
10+
"height": 16,
11+
"mipmaps": false,
12+
"width": 16
13+
}
14+
15+
[sub_resource type="ImageTexture" id="ImageTexture_sdiwn"]
16+
image = SubResource("Image_fr423")
17+
18+
[node name="DeleteVariablesButton" type="MarginContainer"]
19+
offset_right = 186.0
20+
offset_bottom = 43.0
21+
size_flags_horizontal = 0
22+
theme_override_constants/margin_bottom = 12
23+
script = ExtResource("1_fr423")
24+
25+
[node name="DeleteButton" type="Button" parent="."]
26+
unique_name_in_owner = true
27+
layout_mode = 2
28+
theme_type_variation = &"InspectorActionButton"
29+
text = "Delete Variables"
30+
icon = SubResource("ImageTexture_sdiwn")
31+
32+
[node name="DeleteVariablesDialog" parent="." instance=ExtResource("1_5id0e")]
33+
unique_name_in_owner = true
34+
visible = false
35+
36+
[connection signal="pressed" from="DeleteButton" to="." method="_on_delete_button_pressed"]
37+
[connection signal="delete_variables" from="DeleteVariablesDialog" to="." method="_on_delete_variables_dialog_delete_variables"]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
@tool
2+
extends ConfirmationDialog
3+
4+
const BlockCategoryDisplay = preload("res://addons/block_code/ui/picker/categories/block_category_display.gd")
5+
6+
signal delete_variables(variables: Array[String])
7+
8+
@onready var _variables_container := %VariablesContainer
9+
var _checkbox_template := CheckBox.new()
10+
var _main_panel: Node
11+
12+
13+
func _ready():
14+
_main_panel = get_parent()
15+
16+
17+
func _on_confirmed():
18+
var variables := []
19+
for checkbox in _variables_container.get_children():
20+
if checkbox.button_pressed:
21+
variables.append(checkbox.text)
22+
23+
delete_variables.emit(variables)
24+
25+
hide()
26+
27+
28+
func _on_about_to_popup() -> void:
29+
for checkbox in _variables_container.get_children():
30+
_variables_container.remove_child(checkbox)
31+
checkbox.queue_free()
32+
33+
while _main_panel.name != "MainPanel":
34+
_main_panel = _main_panel.get_parent()
35+
36+
for variable in _main_panel._context.block_script.variables:
37+
_checkbox_template.text = variable.var_name
38+
_variables_container.add_child(_checkbox_template.duplicate())
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://duu6fvcrvhxrh
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[gd_scene load_steps=2 format=3 uid="uid://bxkdgyj0kpexu"]
2+
3+
[ext_resource type="Script" uid="uid://duu6fvcrvhxrh" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_dialog.gd" id="1_l45s3"]
4+
5+
[node name="DeleteVariablesDialog" type="ConfirmationDialog"]
6+
title = "Delete Variables"
7+
initial_position = 1
8+
size = Vector2i(300, 183)
9+
visible = true
10+
ok_button_text = "Delete"
11+
dialog_hide_on_ok = false
12+
script = ExtResource("1_l45s3")
13+
14+
[node name="VariablesContainer" type="VBoxContainer" parent="."]
15+
unique_name_in_owner = true
16+
offset_left = 8.0
17+
offset_top = 8.0
18+
offset_right = 292.0
19+
offset_bottom = 134.0
20+
21+
[connection signal="about_to_popup" from="." to="." method="_on_about_to_popup"]
22+
[connection signal="confirmed" from="." to="." method="_on_confirmed"]

addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const VariableDefinition = preload("res://addons/block_code/code_generation/vari
77
@onready var h_separator := %HSeparator
88

99
signal variable_created(variable: VariableDefinition)
10+
signal variables_deleted(variables: Array[String])
1011

1112

1213
func _ready():
@@ -22,3 +23,7 @@ func _update_blocks():
2223

2324
func _on_create_variable(var_name, var_type):
2425
variable_created.emit(VariableDefinition.new(var_name, Types.STRING_TO_VARIANT_TYPE[var_type]))
26+
27+
28+
func _on_delete_variables(variables):
29+
variables_deleted.emit(variables)

addons/block_code/ui/picker/categories/variable_category/variable_category_display.tscn

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=5 format=3 uid="uid://byne4g2yvdf3"]
1+
[gd_scene load_steps=6 format=3 uid="uid://byne4g2yvdf3"]
22

33
[ext_resource type="PackedScene" uid="uid://duhpwtfo3k0sk" path="res://addons/block_code/ui/picker/categories/block_category_display.tscn" id="1_vermd"]
4-
[ext_resource type="Script" path="res://addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd" id="2_ggvi7"]
4+
[ext_resource type="Script" uid="uid://d1owr45rep8tx" path="res://addons/block_code/ui/picker/categories/variable_category/variable_category_display.gd" id="2_ggvi7"]
55
[ext_resource type="PackedScene" uid="uid://t0eoc4ekvjr1" path="res://addons/block_code/ui/picker/categories/variable_category/create_variable_button.tscn" id="3_gjvnq"]
6+
[ext_resource type="PackedScene" uid="uid://ba2ckluuotftw" path="res://addons/block_code/ui/picker/categories/variable_category/delete_variables_button.tscn" id="4_ppkk1"]
67

78
[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_0m6vh"]
89

@@ -22,4 +23,8 @@ theme_override_styles/separator = SubResource("StyleBoxEmpty_0m6vh")
2223
[node name="CreateVariableButton" parent="VBoxContainer" index="3" instance=ExtResource("3_gjvnq")]
2324
layout_mode = 2
2425

26+
[node name="DeleteVariablesButton" parent="VBoxContainer" index="4" instance=ExtResource("4_ppkk1")]
27+
layout_mode = 2
28+
2529
[connection signal="create_variable" from="VBoxContainer/CreateVariableButton" to="." method="_on_create_variable"]
30+
[connection signal="delete_variables" from="VBoxContainer/DeleteVariablesButton" to="." method="_on_delete_variables"]

addons/block_code/ui/picker/picker.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const CATEGORY_ORDER_OVERRIDE = {
2626

2727
signal block_picked(block: Block, offset: Vector2)
2828
signal variable_created(variable: VariableDefinition)
29+
signal variables_deleted(variables: Array[String])
2930

3031
@onready var _context := BlockEditorContext.get_default()
3132

@@ -120,6 +121,7 @@ func _get_or_create_block_category_display(category: BlockCategory) -> BlockCate
120121
else:
121122
block_category_display = VariableCategoryDisplayScene.instantiate()
122123
block_category_display.variable_created.connect(func(variable): variable_created.emit(variable))
124+
block_category_display.variables_deleted.connect(func(variables): variables_deleted.emit(variables))
123125
block_category_display.title = category.name if category else ""
124126
block_category_display.block_picked.connect(func(block: Block, offset: Vector2): block_picked.emit(block, offset))
125127

0 commit comments

Comments
 (0)