TableTower/ui/sheet_item_nav.gd

66 lines
1.8 KiB
GDScript3
Raw Permalink Normal View History

class_name SheetItemNav
extends RefCounted
## Going into an item sheet and back: opening a row's or a Lore Entry's sheet
## from the sheet editor remembers the sheet you were on; the top bar's
## back button ("← Brin Hollowmere") returns there, and past the first
## sheet to the spread it was opened from.
var ed: SheetEditor
## The sheets to go back to, last on top: {id, name, spread}.
var _stack: Array[Dictionary] = []
func _init(p_editor: SheetEditor) -> void:
ed = p_editor
func clear() -> void:
_stack.clear()
func can_go_back() -> bool:
return not _stack.is_empty()
## The back button's words, or "" when there is nowhere to go back to.
func back_label() -> String:
if can_go_back():
return "← %s" % str(_stack.back()["name"])
return "← %s" % ed.from_spread if ed.from_spread != "" else ""
## Opens sheet `id`, keeping the open one to come back to.
func open(id: String) -> void:
var doc := ed.screen.store.load_sheet(id) if id != "" else null
if doc == null:
ed.m.toast("That sheet is gone.")
return
var stack := _stack.duplicate()
stack.append({"id": ed.doc.id, "name": ed.doc.name, "spread": ed.from_spread})
_show(doc, stack, ed.from_spread)
## Back to the sheet before, or to the spread past the first one.
func back() -> void:
if not can_go_back():
ed.back_to_spread()
return
var stack := _stack.duplicate()
var last: Dictionary = stack.pop_back()
var doc := ed.screen.store.load_sheet(str(last["id"]))
if doc == null:
ed.m.toast("That sheet is gone.")
_stack = stack
ed.top_bar.sync()
return
_show(doc, stack, str(last["spread"]))
func _show(doc: SheetDoc, stack: Array[Dictionary], spread: String) -> void:
var keep_mode := ed.mode
ed.save_now()
ed.open(doc)
_stack = stack
ed.from_spread = spread
ed.set_mode(keep_mode)