diff --git a/graph_node_renderer/add_node_menu.gd b/graph_node_renderer/add_node_menu.gd index 577a700..c38a5dd 100644 --- a/graph_node_renderer/add_node_menu.gd +++ b/graph_node_renderer/add_node_menu.gd @@ -94,13 +94,7 @@ func _on_search_line_edit_gui_input(event: InputEvent) -> void: # reached the end of items in the current category category.unhighlight_all() - var nc: Category - if category.get_index() + 1 == scroll_content_container.get_child_count(): - # reached the end, get the first category - nc = scroll_content_container.get_child(0) - else: - # there is another category after this - nc = scroll_content_container.get_child(category.get_index() + 1) + var nc := get_next_visible_category(category.get_index()) nc.highlight_item(0) scroll_container.ensure_control_visible(nc.get_child(0)) return @@ -120,13 +114,7 @@ func _on_search_line_edit_gui_input(event: InputEvent) -> void: # reached the beginning of items in the current category category.unhighlight_all() - var nc: Category - if category.get_index() - 1 == -1: - # reached the beginning, get the last category - nc = scroll_content_container.get_child(scroll_content_container.get_child_count() - 1) - else: - # there is another category before this - nc = scroll_content_container.get_child(category.get_index() - 1) + var nc := get_previous_visible_category(category.get_index()) nc.highlight_item(nc.get_item_count() - 1) scroll_container.ensure_control_visible(nc.get_child(nc.get_item_count() - 1)) return @@ -135,6 +123,36 @@ func _on_search_line_edit_gui_input(event: InputEvent) -> void: scroll_container.ensure_control_visible(category.get_child(item - 1)) +## Returns the next uncollapsed category, starting from index [code]at[/code], wrapping around if no other +## categories are uncollapsed. +func get_next_visible_category(at: int) -> Category: + var i := at + var s := 0 + + while s < scroll_content_container.get_child_count(): + i = (i + 1) % scroll_content_container.get_child_count() + if !(scroll_content_container.get_child(i) as Category).is_collapsed(): + return scroll_content_container.get_child(i) + s += 1 + + return scroll_content_container.get_child(at) + + +## Returns the previous uncollapsed category, starting from index [code]at[/code], wrapping around if no other +## categories are uncollapsed. +func get_previous_visible_category(at: int) -> Category: + var i := at + var s := 0 + + while s < scroll_content_container.get_child_count(): + i = (i - 1) % scroll_content_container.get_child_count() + if !(scroll_content_container.get_child(i) as Category).is_collapsed(): + return scroll_content_container.get_child(i) + s += 1 + + return scroll_content_container.get_child(at) + + ## Callback for [member search_line_edit]. Handles emitting [signal node_selected] func _on_search_line_edit_text_submitted(_new_text: String) -> void: var category: Category @@ -261,6 +279,10 @@ class Category extends VBoxContainer: return -1 + func is_collapsed() -> bool: + return collapse_button.button_pressed + + ## Represents an item in a [AddNodeMenu.Category]. ## ## A selectable and highlightable category item with a favorite button.