add ability to change variables

This commit is contained in:
Lera Elvoé 2024-01-18 10:17:26 +03:00
parent 46e6966e0c
commit bcdba9818c
No known key found for this signature in database
3 changed files with 70 additions and 4 deletions

View file

@ -204,6 +204,11 @@ func set_variable(var_name: String, value: Variant) -> void:
variables_updated.emit()
func update_variable(old_name: String, new_name: String, new_value: Variant) -> void:
variable_stack.erase(old_name)
variable_stack[new_name] = new_value
## Group the [param nodes_to_group] into a new deck and return it.
## Returns [code]null[/code] on failure.[br]
## Adds a group node to this deck, and adds group input and output nodes in the group.

View file

@ -114,6 +114,11 @@ func _ready() -> void:
file_popup_menu.set_item_shortcut(FileMenuId.SAVE, save_deck_shortcut)
file_popup_menu.set_item_shortcut(FileMenuId.SAVE_AS, save_deck_as_shortcut)
file_popup_menu.set_item_shortcut(FileMenuId.CLOSE, close_deck_shortcut)
bottom_dock.variable_viewer.top_field_edited.connect(
func(old_name: String, new_name: String, new_value: Variant) -> void:
get_active_deck().update_variable(old_name, new_name, new_value)
)
func _on_tab_container_tab_about_to_change(previous_tab: int) -> void:

View file

@ -7,6 +7,10 @@ class_name VariableViewer
@onready var types_popup: PopupMenu = %TypesPopup
var root: TreeItem
var _old_name: String
signal top_field_edited(old_name: String, new_name: String, new_value: Variant)
func _ready() -> void:
variable_tree.set_column_title(0, "Name")
@ -23,7 +27,15 @@ func _ready() -> void:
func():
var column := variable_tree.get_selected_column()
var item := variable_tree.get_selected()
# do nothing if this is an array and user is trying to edit index
# (TODO: proper reordering)
if column == 0 && item.get_meta(&"indexed"):
return
if column < 2:
_old_name = item.get_text(0)
variable_tree.edit_selected(true)
else:
var pos := get_global_mouse_position()
@ -36,6 +48,41 @@ func _ready() -> void:
types_popup.popup(r)
)
variable_tree.item_edited.connect(
func():
var item := variable_tree.get_selected()
var new_name := item.get_text(0)
var new_value
var type: DeckType.Types = item.get_metadata(2)
match type:
DeckType.Types.STRING:
new_value = item.get_text(1)
DeckType.Types.BOOL:
new_value = item.is_checked(1)
DeckType.Types.NUMERIC:
new_value = item.get_range(1)
_:
new_value = item.get_meta(&"value")
item.set_meta(&"value", new_value)
if item.get_meta(&"container", -1) is int:
top_field_edited.emit(_old_name, new_name, new_value)
else:
var container = item.get_meta(&"container")
# array
if item.get_meta(&"indexed", false):
var index := int(new_name)
(container as Array)[index] = new_value
# dictionary
else:
var key := new_name
(container as Dictionary).erase(_old_name)
(container as Dictionary)[key] = new_value
)
for i in DeckType.Types.size():
types_popup.add_radio_check_item(DeckType.type_str(i))
@ -49,7 +96,9 @@ func rebuild_variable_tree(data: Dictionary = {}) -> void:
add_item(i, data[i])
func add_item(item_name: String, item_value: Variant, parent: TreeItem = root) -> TreeItem:
func add_item(item_name: String, item_value: Variant, parent: TreeItem = root, container: Variant = -1, indexed: bool = false) -> TreeItem:
# the container parameter is -1 instead of null because Object#get_metadata() logs an error
# if the default parameter is null
var item := variable_tree.create_item(parent)
item.set_text(0, item_name)
var type: DeckType.Types = DeckType.INVERSE_GODOT_TYPES_MAP[typeof(item_value)]
@ -59,17 +108,24 @@ func add_item(item_name: String, item_value: Variant, parent: TreeItem = root) -
item.set_cell_mode(1, TreeItem.CELL_MODE_RANGE)
item.set_range(1, item_value)
item.set_range_config(1, -9999, 9999, 0.0001)
DeckType.Types.BOOL:
item.set_cell_mode(1, TreeItem.CELL_MODE_CHECK)
item.set_checked(1, item_value)
_:
item.set_text(1, str(item_value))
item.set_meta(&"container", container)
item.set_meta(&"indexed", indexed)
item.set_meta(&"value", item_value)
item.set_text(2, DeckType.type_str(type))
item.set_metadata(2, type)
if item_value is Dictionary:
for i in item_value:
add_item(i, item_value[i], item)
add_item(i, item_value[i], item, item_value)
if item_value is Array:
for i in (item_value as Array).size():
add_item(str(i), item_value[i], item)
add_item(str(i), item_value[i], item, item_value, true)
item.collapsed = true
return item