add ability to change type in variable viewer

This commit is contained in:
Lera Elvoé 2024-01-18 11:40:56 +03:00
parent b2439d9209
commit ec7acbdce8
No known key found for this signature in database
2 changed files with 55 additions and 17 deletions

View file

@ -25,7 +25,6 @@ func _ready() -> void:
variable_tree.set_column_expand_ratio(2, 15)
root = variable_tree.create_item()
rebuild_variable_tree()
variable_tree.item_activated.connect(
func():
var column := variable_tree.get_selected_column()
@ -86,9 +85,13 @@ func _ready() -> void:
(container as Dictionary)[key] = new_value
)
for i in DeckType.Types.size():
variable_tree.button_clicked.connect(_on_variable_tree_button_clicked)
for i in DeckType.Types.size() - 1:
types_popup.add_radio_check_item(DeckType.type_str(i))
types_popup.id_pressed.connect(_on_types_popup_id_pressed)
func rebuild_variable_tree(data: Dictionary = {}) -> void:
#variable_tree.clear() # godot will raw dog a nullptr later if we clear the whole tree
@ -104,22 +107,12 @@ func add_item(item_name: String, item_value: Variant, parent: TreeItem = root, c
# 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)]
match type:
DeckType.Types.NUMERIC:
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)
set_item_value(item, item_value)
item.set_text(2, DeckType.type_str(type))
item.set_metadata(2, type)
@ -135,10 +128,28 @@ func add_item(item_name: String, item_value: Variant, parent: TreeItem = root, c
return item
func set_item_value(item: TreeItem, value: Variant) -> void:
item.set_meta(&"value", value)
var type: DeckType.Types = DeckType.INVERSE_GODOT_TYPES_MAP[typeof(value)]
match type:
DeckType.Types.NUMERIC:
item.set_cell_mode(1, TreeItem.CELL_MODE_RANGE)
item.set_range_config(1, -9999, 9999, 0.0001)
item.set_range(1, value)
DeckType.Types.BOOL:
item.set_cell_mode(1, TreeItem.CELL_MODE_CHECK)
item.set_checked(1, value)
_:
item.set_text(1, str(value))
func _on_variable_tree_button_clicked(item: TreeItem, column: int, id: int, mouse_button_index: int) -> void:
if mouse_button_index != MOUSE_BUTTON_LEFT:
return
# we only have a delete button for now, so assume it is what's clicked
if item.get_meta(&"container", -1) is int:
var key := item.get_text(0)
top_field_removed.emit(key)
@ -161,3 +172,32 @@ func _on_variable_tree_button_clicked(item: TreeItem, column: int, id: int, mous
else:
(container as Dictionary).erase(item.get_text(0))
item.free()
func _on_types_popup_id_pressed(id: int) -> void:
var current_item := variable_tree.get_selected()
if current_item.get_metadata(2) == id:
return
current_item.set_metadata(2, id)
var new_value: Variant
match id as DeckType.Types:
DeckType.Types.BOOL, DeckType.Types.STRING, DeckType.Types.NUMERIC:
# simple types that can generally be converted between each other
var target_type: Variant.Type = DeckType.GODOT_TYPES_MAP[id]
new_value = type_convert(current_item.get_meta(&"value"), target_type)
set_item_value(current_item, new_value)
DeckType.Types.ARRAY:
new_value = []
set_item_value(current_item, new_value)
DeckType.Types.DICTIONARY:
new_value = {}
set_item_value(current_item, new_value)
current_item.set_text(2, DeckType.type_str(id))
if current_item.get_meta(&"container", -1) is int:
var field_name := current_item.get_text(0)
top_field_edited.emit(field_name, field_name, new_value)

View file

@ -27,5 +27,3 @@ hide_root = true
[node name="TypesPopup" type="PopupMenu" parent="."]
unique_name_in_owner = true
[connection signal="button_clicked" from="VariableTree" to="." method="_on_variable_tree_button_clicked"]