diff --git a/graph_node_renderer/deck_holder_renderer.gd b/graph_node_renderer/deck_holder_renderer.gd index b066270..c4817bc 100644 --- a/graph_node_renderer/deck_holder_renderer.gd +++ b/graph_node_renderer/deck_holder_renderer.gd @@ -117,11 +117,13 @@ func _ready() -> void: bottom_dock.variable_viewer.top_field_edited.connect( func(old_name: String, new_name: String, new_value: Variant) -> void: + get_active_deck_renderer().dirty = true get_active_deck().update_variable(old_name, new_name, new_value) ) bottom_dock.variable_viewer.top_field_removed.connect( func(field_name: String) -> void: + get_active_deck_renderer().dirty = true get_active_deck().remove_variable(field_name) ) @@ -158,6 +160,7 @@ func _on_file_id_pressed(id: int) -> void: _ when id in range(FileMenuId.RECENTS, FileMenuId.RECENTS + max_recents + 1): open_deck_at_path(recent_files[id - FileMenuId.RECENTS - 1]) + ## Adds an empty [DeckRendererGraphEdit] with a corresponding [Deck] for it's data. func add_empty_deck() -> void: var deck := DeckHolder.add_empty_deck() @@ -267,10 +270,20 @@ func get_active_deck() -> Deck: ## Returns the deck at [param tab] in the [member tab_container]. func get_deck_at_tab(tab: int) -> Deck: + return get_deck_renderer_at_tab(tab).deck + + +## Returns the current deck renderer in the [member tab_container]. +func get_active_deck_renderer() -> DeckRendererGraphEdit: + return get_deck_renderer_at_tab(tab_container.get_current_tab()) + + +## Returns the deck renderer at [param tab] in the [member tab_container]. +func get_deck_renderer_at_tab(tab: int) -> DeckRendererGraphEdit: if tab_container.is_empty(): return null - return (tab_container.get_content(tab) as DeckRendererGraphEdit).deck + return tab_container.get_content(tab) as DeckRendererGraphEdit ## Saves the active [Deck] in [member tab_container] diff --git a/graph_node_renderer/variable_viewer.gd b/graph_node_renderer/variable_viewer.gd index 8b66337..48f372c 100644 --- a/graph_node_renderer/variable_viewer.gd +++ b/graph_node_renderer/variable_viewer.gd @@ -27,70 +27,20 @@ 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() - 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() - var r := Rect2i(Vector2i(pos), Vector2i(0, 100)) - - for i in types_popup.get_item_count(): - types_popup.set_item_checked(i, false) - - types_popup.set_item_checked(item.get_metadata(2), true) - 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.has_meta(&"container"): - 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 := item.get_index() - (container as Array)[index] = new_value - # dictionary - else: - var key := new_name - (container as Dictionary).erase(_old_name) - (container as Dictionary)[key] = new_value - - set_item_value(item, new_value) - - ) + variable_tree.item_activated.connect(edit_item) + variable_tree.item_edited.connect(commit_item_change) - variable_tree.nothing_selected.connect(variable_tree.deselect_all) + variable_tree.nothing_selected.connect( + func(): + var item := variable_tree.get_edited() + if item == null: + item = variable_tree.get_selected() + + if item != null: + commit_item_change(item) + item.deselect(variable_tree.get_selected_column()) + ) variable_tree.button_clicked.connect(_on_variable_tree_button_clicked) @@ -104,6 +54,67 @@ func _ready() -> void: disable_new_button() +func commit_item_change(item: TreeItem = variable_tree.get_edited()) -> void: + 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") + + if !item.has_meta(&"container"): + 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 := item.get_index() + (container as Array)[index] = new_value + # dictionary + else: + var key := new_name + (container as Dictionary).erase(_old_name) + (container as Dictionary)[key] = new_value + + set_item_value(item, new_value) + + +func edit_item() -> void: + 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: + var value = item.get_meta(&"value") + if column == 1 && (value is Array || value is Dictionary): + item.collapsed = !item.collapsed + return + _old_name = item.get_text(0) + + variable_tree.edit_selected(true) + elif column == 2: + var pos := get_global_mouse_position() + var r := Rect2i(Vector2i(pos), Vector2i(0, 100)) + + for i in types_popup.get_item_count(): + types_popup.set_item_checked(i, false) + + types_popup.set_item_checked(item.get_metadata(2), true) + types_popup.popup(r) + + func rebuild_variable_tree(data: Dictionary = {}) -> void: #variable_tree.clear() # godot will raw dog a nullptr later if we clear the whole tree for i in root.get_children(): @@ -151,6 +162,7 @@ func set_item_value(item: TreeItem, value: Variant) -> void: item.set_cell_mode(1, TreeItem.CELL_MODE_CHECK) item.set_checked(1, value) _: + item.set_cell_mode(1, TreeItem.CELL_MODE_STRING) item.set_text(1, str(value)) if item.has_meta(&"container"):