mirror of
https://codeberg.org/StreamGraph/StreamGraph.git
synced 2024-11-13 19:49:55 +01:00
101 lines
3.5 KiB
GDScript
101 lines
3.5 KiB
GDScript
extends VBoxContainer
|
|
class_name TabContainerCustom
|
|
|
|
## Custom Recreation of [TabContainer] for Flexibility
|
|
##
|
|
## Allows for more customizability within the [TabBar] thats used mainly. Extra buttons etc.
|
|
|
|
## Reference to the [TabBar] at the top of the Container.
|
|
@onready var tab_bar: TabBar = %TabBar
|
|
## Reference to the [Button] at the end of the [TabBar] that's
|
|
## used for adding new Tabs
|
|
@onready var add_tab_button: Button = %Button
|
|
## Reference to the [MarginContainer] around the Tabs Contents.
|
|
@onready var content_container: MarginContainer = %ContentContainer
|
|
|
|
## Emitted when the add [Button] within [member tab_bar] is pressed
|
|
signal add_button_pressed
|
|
## Emitted when the current tab in [member tab_bar] is changed.
|
|
signal tab_changed(tab: int)
|
|
## Emitted when a tab in [member tab_bar] has been closed.
|
|
## See [signal TabBar.tab_close_requested]
|
|
signal tab_closed(tab: int)
|
|
## Emitted when a request to close a tab in the [member tab_bar] has been
|
|
## requested using [signal TabBar.tab_close_pressed]
|
|
signal tab_close_requested(tab: int)
|
|
## Emitted when the order of the tabs in [member tab_bar] has been changed.
|
|
signal tab_rearranged(old: int, new: int)
|
|
|
|
## Holds the previously active tab in [member tab_bar]
|
|
var _previous_active_tab: int = -1
|
|
|
|
|
|
func _ready() -> void:
|
|
tab_bar.tab_selected.connect(
|
|
func(tab: int):
|
|
if _previous_active_tab == tab:
|
|
return
|
|
|
|
if _previous_active_tab > -1:
|
|
content_container.get_child(_previous_active_tab).visible = false
|
|
content_container.get_child(tab).visible = true
|
|
|
|
tab_changed.emit(tab)
|
|
_previous_active_tab = tab
|
|
)
|
|
|
|
tab_bar.tab_close_pressed.connect(func(tab: int): tab_close_requested.emit(tab))
|
|
|
|
add_tab_button.pressed.connect(
|
|
func():
|
|
add_button_pressed.emit()
|
|
)
|
|
|
|
tab_bar.active_tab_rearranged.connect(
|
|
func(idx_to: int):
|
|
tab_rearranged.emit(_previous_active_tab, idx_to)
|
|
content_container.move_child(content_container.get_child(_previous_active_tab), idx_to)
|
|
_previous_active_tab = idx_to
|
|
)
|
|
|
|
## Adds the given [Node] as "content" for the given tabs name as a [String]
|
|
func add_content(c: Node, tab_title: String) -> void:
|
|
tab_bar.add_tab(tab_title)
|
|
content_container.add_child(c)
|
|
tab_bar.set_current_tab(tab_bar.tab_count - 1)
|
|
|
|
## Returns the count of tabs in [member tab_bar]
|
|
func get_tab_count() -> int:
|
|
return tab_bar.tab_count
|
|
|
|
## Returns [code]true[/code] if [method get_tab_count] returns 0.
|
|
func is_empty() -> bool:
|
|
return get_tab_count() == 0
|
|
|
|
## Closes the tab that is at the given [param tab] in [member tab_bar]
|
|
func close_tab(tab: int) -> void:
|
|
content_container.get_child(tab).queue_free()
|
|
if !tab_bar.select_previous_available():
|
|
tab_bar.select_next_available()
|
|
tab_bar.remove_tab(tab)
|
|
tab_closed.emit(tab)
|
|
if tab_bar.tab_count == 0:
|
|
_previous_active_tab = -1
|
|
|
|
## Returns the currently selected tab in [member tab_bar]
|
|
func get_current_tab() -> int:
|
|
return tab_bar.current_tab
|
|
|
|
## Returns the child of [member content_container] at the given [param idx]
|
|
func get_content(idx: int) -> Control:
|
|
return content_container.get_child(idx)
|
|
|
|
## Sets the metadata value for the tab at index [param tab_idx], which can be
|
|
## retrieved later using [method TabBar.get_tab_metadata()]
|
|
func set_tab_metadata(tab: int, metadata: Variant) -> void:
|
|
tab_bar.set_tab_metadata(tab, metadata)
|
|
|
|
## Returns the metadata value set to the tab at index [param tab_idx] using set_tab_metadata().
|
|
## If no metadata was previously set, returns null by default.
|
|
func get_tab_metadata(tab: int) -> Variant:
|
|
return tab_bar.get_tab_metadata(tab)
|