61 lines
2.5 KiB
GDScript3
61 lines
2.5 KiB
GDScript3
|
|
class_name SheetItems
|
||
|
|
extends RefCounted
|
||
|
|
## Item sheets: a Lore Entry (BookPage) can carry a small sheet of its own,
|
||
|
|
## e.g. the Ashen Blade with Damage, Charges and Notes (BookPage.sheet holds
|
||
|
|
## its SheetStore id). Dropped into a sheet box, an entry becomes a row or an
|
||
|
|
## item; a copy gets its own copy of the item sheet (copy_sheet), a linked one
|
||
|
|
## (Shift) points at the entry's sheet. The rows' keys: "lore" (a link ref,
|
||
|
|
## SheetLoreLinks) and "sheet" (a sheet id).
|
||
|
|
|
||
|
|
|
||
|
|
## A new item sheet for `page`, saved in `store`; the page now links to it.
|
||
|
|
static func make_sheet(store: SheetStore, page: BookPage) -> SheetDoc:
|
||
|
|
var name := Formatter.strip_tags(page.label()).strip_edges()
|
||
|
|
var doc := SheetDoc.new("", name if name != "" else "Item")
|
||
|
|
var boxes: Array = doc.tabs[0]["boxes"]
|
||
|
|
SheetKinds.place(doc, boxes, SheetKinds.HEADING, doc.name, Rect2i(1, 1, 24, 3)).frameless = true
|
||
|
|
SheetKinds.place(doc, boxes, SheetKinds.SHORT_TEXT, "Damage", Rect2i(1, 5, 11, 4))
|
||
|
|
SheetKinds.place(doc, boxes, SheetKinds.NUMBER, "Charges", Rect2i(13, 5, 8, 5))
|
||
|
|
var notes := SheetKinds.place(doc, boxes, SheetKinds.TEXT, "Notes", Rect2i(1, 11, 24, 8))
|
||
|
|
notes.value = (page.text + "\n\n" + page.details).strip_edges()
|
||
|
|
doc.id = store.free_id(doc.name)
|
||
|
|
store.save(doc)
|
||
|
|
page.sheet = doc.id
|
||
|
|
return doc
|
||
|
|
|
||
|
|
|
||
|
|
## Saves a copy of sheet `id` under a new id; returns it ("" when `id` is gone).
|
||
|
|
static func copy_sheet(store: SheetStore, id: String) -> String:
|
||
|
|
var doc := store.load_sheet(id) if id != "" else null
|
||
|
|
if doc == null:
|
||
|
|
return ""
|
||
|
|
var copied := doc.copy()
|
||
|
|
copied.id = store.free_id(doc.name)
|
||
|
|
store.save(copied)
|
||
|
|
return copied.id
|
||
|
|
|
||
|
|
|
||
|
|
## The row or item a dropped entry becomes. `fields` holds the box's own
|
||
|
|
## keys (name, details or size); "lore" and "sheet" are added here.
|
||
|
|
static func with_links(fields: Dictionary, page: BookPage, ref: String, linked: bool, store: SheetStore) -> Dictionary:
|
||
|
|
var out := fields.duplicate()
|
||
|
|
if linked:
|
||
|
|
out["lore"] = ref
|
||
|
|
if page.sheet != "":
|
||
|
|
out["sheet"] = page.sheet
|
||
|
|
elif page.sheet != "":
|
||
|
|
var copied := copy_sheet(store, page.sheet)
|
||
|
|
if copied != "":
|
||
|
|
out["sheet"] = copied
|
||
|
|
return out
|
||
|
|
|
||
|
|
|
||
|
|
## The entry's text and details without formatting, for a row's details.
|
||
|
|
static func plain_text(page: BookPage) -> String:
|
||
|
|
var parts := PackedStringArray()
|
||
|
|
for part in [page.text, page.details]:
|
||
|
|
var plain := Formatter.strip_tags(part).strip_edges()
|
||
|
|
if plain != "" and plain != Formatter.strip_tags(page.label()).strip_edges():
|
||
|
|
parts.append(plain)
|
||
|
|
return "\n\n".join(parts)
|