TableTower/ui/side_tabs.gd

64 lines
2.5 KiB
GDScript3
Raw Normal View History

class_name SideTabs
extends RefCounted
## In Build, the Inspector and the Lore Index share the right-hand dock as two
## tabs ("Inspector | Lore Index"); only one shows at a time. In Play there is
## no inspector, so the Lore Index opens on its own (no tab row).
## tab_row is the shared tab style; LeftDock ("Cards | List") uses it too.
## The Lore Index is BookPanel in code (TowerBoard.book, BookPage).
const INSPECTOR := "inspector"
const LORE := "lore"
## The tab row put at the top of both panels. `active` is the panel it sits on.
static func make_row(m: Main, active: String) -> HBoxContainer:
return tab_row([[INSPECTOR, "Inspector"], [LORE, "Lore Index"]], active, func(which: String) -> void: show_tab(m, which))
## A row of dock tabs in the shared style (both docks use it). `tabs` holds
## [key, label] pairs; `on_pick` gets the key of the tab pressed.
static func tab_row(tabs: Array, active: String, on_pick: Callable) -> HBoxContainer:
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 4)
for tab: Array in tabs:
var b := Button.new()
b.text = tab[1]
b.name = str(tab[0])
b.toggle_mode = true
b.button_pressed = tab[0] == active
b.focus_mode = Control.FOCUS_NONE
b.size_flags_horizontal = Control.SIZE_EXPAND_FILL
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 14)
var normal := Style.box(Color(0, 0, 0, 0.06), 6, [10, 5, 10, 5])
var hover := Style.box(Color(0, 0, 0, 0.12), 6, [10, 5, 10, 5])
var on := Style.bordered(Style.box(Style.CARD, 6, [10, 5, 10, 5]), 2, Style.TRIM)
Style.skin_button(b, normal, hover, on, Style.MUTED, Style.INK, Style.INK)
var which: String = tab[0]
b.pressed.connect(func() -> void: on_pick.call(which))
row.add_child(b)
return row
## Presses the tab named `which` in a row from tab_row (the others pop up).
static func press(row: HBoxContainer, which: String) -> void:
for b in row.get_children():
(b as Button).set_pressed_no_signal(b.name == which)
## Shows one tab. The Lore Index tab opens the Lore Index; the Inspector tab closes it.
static func show_tab(m: Main, which: String) -> void:
if m.book:
m.book.set_shown(which == LORE)
## Brings the dock in line with the mode: in Build an open Lore Index hides the
## inspector; in Play the inspector is always hidden.
static func sync(m: Main) -> void:
if m.inspector == null or m.book == null:
return
var build := m.mode == Main.Mode.BUILD
m.inspector.visible = build and not m.book.visible
m.inspector._sync_splitter()
m.book.show_tabs(build)