44 lines
1.4 KiB
GDScript
44 lines
1.4 KiB
GDScript
class_name TabMenu extends Control
|
|
|
|
@export var tabs:TabBar
|
|
@export var tabControls:Array[Control]
|
|
|
|
func _ready() -> void:
|
|
tabs.tab_changed.connect(_onTabChanged)
|
|
_onTabChanged(tabs.current_tab)
|
|
|
|
func _notification(what:int) -> void:
|
|
if what == NOTIFICATION_VISIBILITY_CHANGED and visible:
|
|
tabs.grab_focus()
|
|
|
|
func _input(event:InputEvent) -> void:
|
|
if !is_visible_in_tree():
|
|
return
|
|
if event.is_action_pressed("tab_next"):
|
|
tabs.current_tab = (tabs.current_tab + 1) % tabs.tab_count
|
|
get_viewport().set_input_as_handled()
|
|
elif event.is_action_pressed("tab_prev"):
|
|
tabs.current_tab = (tabs.current_tab - 1 + tabs.tab_count) % tabs.tab_count
|
|
get_viewport().set_input_as_handled()
|
|
elif tabs.has_focus() and event.is_action_pressed("ui_accept"):
|
|
var idx:int = tabs.current_tab
|
|
if idx >= 0 and idx < tabControls.size() and _focusFirstIn(tabControls[idx]):
|
|
get_viewport().set_input_as_handled()
|
|
|
|
func _onTabChanged(tabIndex:int) -> void:
|
|
for control in tabControls:
|
|
control.visible = false
|
|
if tabIndex >= 0 and tabIndex < tabControls.size():
|
|
tabControls[tabIndex].visible = true
|
|
|
|
func _focusFirstIn(container:Control) -> bool:
|
|
for child in container.get_children():
|
|
if not child is Control:
|
|
continue
|
|
if child.focus_mode != Control.FOCUS_NONE and child.is_visible_in_tree():
|
|
child.grab_focus()
|
|
return true
|
|
if _focusFirstIn(child):
|
|
return true
|
|
return false
|