TableTower/ui/sheet_top_bar.gd

177 lines
7.5 KiB
GDScript3
Raw Permalink Normal View History

class_name SheetTopBar
extends PanelContainer
## The sheet editor's top bar, the same layout as the spread's (MainTopBar):
## the brand (click: main menu), Play | Build, a SHEET caption over the sheet's
## name (click: switch sheet, all sheets, rename), the mode hint, and in Build
## Field palette, Undo, Redo and Save as template…; Lore Index and Export ▾
## (both modes, SheetExportMenu) and ☰ at the end. The back button leads to
## the sheet an item sheet was opened from, or to the spread (SheetItemNav).
const HINTS := {
Main.Mode.PLAY: "Type in the boxes to change values.",
Main.Mode.BUILD: "Drag a box to move it, its corner to resize it.",
}
var ed: SheetEditor
var play_button: Button
var build_button: Button
var name_button: MenuButton
var palette_button: Button
var undo_button: Button
var redo_button: Button
var _hint: Label
var _tools: HBoxContainer
## "← Spread name" when the sheet was opened from a spread.
var back_button: Button
var export_button: MenuButton
var lore_button: Button
## The "Table Tarot" words beside the logo (hidden on phones).
var brand_title: Label
func setup(p_editor: SheetEditor) -> void:
ed = p_editor
add_theme_stylebox_override("panel", Style.panel_box([16, 10, 16, 10]))
var h := HBoxContainer.new()
h.add_theme_constant_override("separation", 10)
add_child(h)
var brand := MainTopBar.make_brand(func() -> void: ed.m.screens.show(MainScreens.MENU))
brand_title = brand.get_child(brand.get_child_count() - 1)
h.add_child(brand)
back_button = Style.bar_button("")
back_button.name = "BackToSpread"
back_button.tooltip_text = "Back to where you opened this sheet from"
back_button.pressed.connect(func() -> void: ed.nav.back())
h.add_child(back_button)
var seg := PanelContainer.new()
seg.add_theme_stylebox_override("panel", Style.box(Color(0, 0, 0, 0.28), 9, [3, 3, 3, 3]))
seg.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var seg_row := HBoxContainer.new()
seg_row.add_theme_constant_override("separation", 0)
seg.add_child(seg_row)
var group := ButtonGroup.new()
play_button = MainTopBar.seg_button("Play", group)
build_button = MainTopBar.seg_button("Build", group)
play_button.pressed.connect(func() -> void: ed.set_mode(Main.Mode.PLAY))
build_button.pressed.connect(func() -> void: ed.set_mode(Main.Mode.BUILD))
seg_row.add_child(play_button)
seg_row.add_child(build_button)
h.add_child(seg)
var name_box := VBoxContainer.new()
name_box.size_flags_vertical = Control.SIZE_SHRINK_CENTER
name_box.add_theme_constant_override("separation", 0)
var caption := Style.make_label("SHEET", Style.caps(), 10, Style.ON_BG_MUTED)
caption.tooltip_text = "A sheet keeps track of one thing: a character, a vehicle… Click its name to switch sheets."
caption.mouse_filter = Control.MOUSE_FILTER_PASS
name_box.add_child(caption)
name_button = Style.menu_button("", false)
name_button.tooltip_text = "Switch sheets, see all sheets, or rename this one"
name_button.get_popup().about_to_popup.connect(_fill_name_menu)
name_box.add_child(name_button)
h.add_child(name_box)
_hint = Style.make_label("", Style.body(), 14, Style.ON_BG_MUTED)
_hint.size_flags_horizontal = SIZE_EXPAND_FILL
_hint.size_flags_vertical = Control.SIZE_SHRINK_CENTER
_hint.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
_hint.custom_minimum_size = Vector2(60, 0)
h.add_child(_hint)
_tools = HBoxContainer.new()
_tools.add_theme_constant_override("separation", 6)
h.add_child(_tools)
palette_button = Style.bar_button("Field palette")
palette_button.toggle_mode = true
palette_button.focus_mode = Control.FOCUS_NONE
palette_button.tooltip_text = "Show or hide the field palette"
palette_button.pressed.connect(func() -> void: ed.set_palette_shown(not ed.palette_dock.visible))
_tools.add_child(palette_button)
undo_button = Style.bar_button("↶ Undo")
undo_button.pressed.connect(ed.undo)
_tools.add_child(undo_button)
redo_button = Style.bar_button("↷ Redo")
redo_button.pressed.connect(ed.redo)
_tools.add_child(redo_button)
var template := Style.bar_button("Save as template…")
template.pressed.connect(save_as_template)
_tools.add_child(template)
lore_button = Style.bar_button("Lore Index")
lore_button.name = "LoreIndex"
lore_button.toggle_mode = true
lore_button.focus_mode = Control.FOCUS_NONE
lore_button.tooltip_text = "Show or hide the Lore Index: drag an entry onto a box to add it"
lore_button.pressed.connect(func() -> void: ed.set_lore_shown(not ed.lore_shown))
h.add_child(lore_button)
export_button = SheetExportMenu.make_button(ed)
h.add_child(export_button)
var actions := Style.menu_button("☰")
actions.tooltip_text = "Main menu, all sheets, theme…"
actions.get_popup().about_to_popup.connect(func() -> void: Actions.fill(actions.get_popup(), _actions()))
h.add_child(actions)
func _actions() -> Array:
var list: Array = [
{"text": "Main menu", "do": ed.m.screens.show.bind(MainScreens.MENU)},
{"text": "All sheets", "do": ed.screen.show_list},
{"sep": ""},
{"text": "Rename sheet…", "do": rename},
{"text": "Save as template…", "do": save_as_template},
{"sep": ""},
{"text": "Theme…", "do": func() -> void: ed.m.add_child(ThemeDialog.new(ed.m))},
]
if ed.phone:
# Phones have no room for Lore Index and Export ▾ on the bar.
list.append({"sep": ""})
list.append({"text": "Hide the Lore Index" if ed.lore_shown else "Lore Index", "do": func() -> void: ed.set_lore_shown(not ed.lore_shown)})
list.append_array(SheetExportMenu.actions(ed))
return list
## The name button's menu: your other sheets, All sheets, Rename.
func _fill_name_menu() -> void:
var list: Array = []
for row: Dictionary in ed.screen.store.list():
if str(row["id"]) != ed.doc.id:
list.append({"text": str(row["name"]), "do": ed.screen.open_sheet.bind(str(row["id"]))})
if not list.is_empty():
list.append({"sep": ""})
list.append({"text": "All sheets…", "do": ed.screen.show_list})
list.append({"text": "Rename…", "do": rename})
Actions.fill(name_button.get_popup(), list)
func rename() -> void:
ed.m._add_modal(ModalCard.ask_text("Rename sheet", "Name", ed.doc.name, "Rename", func(text: String) -> void:
ed.edit("Rename sheet", func() -> void: ed.doc.name = text.strip_edges())))
func save_as_template() -> void:
ed.m._add_modal(ModalCard.ask_text("Save as template", "Template name", ed.doc.name, "Save", func(text: String) -> void:
ed.save_now()
ed.screen.store.save_as_template(ed.doc, text.strip_edges())
ed.m.toast("Saved template “%s”. Pick it under + New sheet." % text.strip_edges())))
## Brings the bar in line with the editor: mode, name, undo state.
func sync() -> void:
if ed.doc == null:
return
var build := ed.building()
play_button.set_pressed_no_signal(not build)
build_button.set_pressed_no_signal(build)
name_button.text = "%s ▾" % ed.doc.name
back_button.text = ed.nav.back_label()
back_button.visible = back_button.text != ""
lore_button.set_pressed_no_signal(ed.lore_shown)
_hint.text = HINTS[ed.mode]
# Phones have Undo and "+ Field" in the bottom bar (SheetPhoneLayout).
_tools.visible = build and ed.phone == null
palette_button.set_pressed_no_signal(ed.palette_dock.visible)
undo_button.disabled = not ed.history.can_undo()
redo_button.disabled = not ed.history.can_redo()
undo_button.modulate.a = 1.0 if ed.history.can_undo() else 0.45
redo_button.modulate.a = 1.0 if ed.history.can_redo() else 0.45
undo_button.tooltip_text = "Undo: %s (Ctrl+Z)" % ed.history.undo_label() if ed.history.can_undo() else "Nothing to undo"
redo_button.tooltip_text = "Redo: %s (Ctrl+Shift+Z)" % ed.history.redo_label() if ed.history.can_redo() else "Nothing to redo"
if ed.phone:
ed.phone.sync_undo()