From ec7acbdce851adf8d4c3eb1f1b382a68de0a9462 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Thu, 18 Jan 2024 11:40:56 +0300 Subject: [PATCH] add ability to change type in variable viewer --- graph_node_renderer/variable_viewer.gd | 70 +++++++++++++++++++----- graph_node_renderer/variable_viewer.tscn | 2 - 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/graph_node_renderer/variable_viewer.gd b/graph_node_renderer/variable_viewer.gd index d0a0ea2..cdeea34 100644 --- a/graph_node_renderer/variable_viewer.gd +++ b/graph_node_renderer/variable_viewer.gd @@ -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,8 +85,12 @@ 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: @@ -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) diff --git a/graph_node_renderer/variable_viewer.tscn b/graph_node_renderer/variable_viewer.tscn index e96066f..ad1cbb9 100644 --- a/graph_node_renderer/variable_viewer.tscn +++ b/graph_node_renderer/variable_viewer.tscn @@ -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"]