TableTower/core/book_page.gd

56 lines
1.6 KiB
GDScript3
Raw Normal View History

class_name BookPage
extends RefCounted
## A Page in the spread's Book: a reusable result (its text with any BBCode,
## its details tooltip and tags). Results linked to a page (TableEntry.page)
## show its text and details; editing the page updates every linked result
## (TowerBoard.sync_page). Saved in the spread file under "book".
var id: String = ""
## The page's name in the Book's list ("" = its text without formatting).
var name: String = ""
var text: String = ""
var details: String = ""
var tags: Array[String] = []
## A picture (BoardImages id). "" = none.
var image: String = ""
## Its item sheet (a SheetStore sheet id, e.g. a sword's Damage and Charges).
## "" = none. Saved as "sheet".
var sheet: String = ""
func label() -> String:
return name if name.strip_edges() != "" else Formatter.strip_tags(text).strip_edges()
func to_dict() -> Dictionary:
var d := {"id": id, "text": text}
if name != "":
d["name"] = name
if details != "":
d["details"] = details
if not tags.is_empty():
d["tags"] = tags.duplicate()
if image != "":
d["image"] = image
if sheet != "":
d["sheet"] = sheet
return d
static func from_dict(d: Dictionary) -> BookPage:
var p := BookPage.new()
p.id = str(d.get("id", ""))
p.name = str(d.get("name", ""))
p.text = str(d.get("text", ""))
p.details = str(d.get("details", ""))
p.image = str(d.get("image", ""))
p.sheet = str(d.get("sheet", ""))
for t: Variant in d.get("tags", []):
p.tags.append(str(t))
return p
## A copy with the same fields (the caller gives it a new id).
func copy() -> BookPage:
return BookPage.from_dict(to_dict())