TableTower/core/sheet_kinds.gd

62 lines
2.1 KiB
GDScript3
Raw Normal View History

class_name SheetKinds
extends RefCounted
## Every kind of box you can put on a sheet, in one list: the field palette,
## new boxes' starting size, title and value, and search all read it (the
## sheet's version of CardCatalog). More kinds come in later phases
## (docs/SHEETS.md §4 C).
## The snap grid's step, in canvas pixels.
const GRID := 16.0
const HEADING := "heading"
const TEXT := "text"
const NUMBER := "number"
const GROUP_BASICS := "Basics"
const GROUP_LAYOUT := "Layout"
## Each item: kind, label, group, tip, search words, start size (w, h in grid
## steps), title and value.
const ITEMS := [
{"kind": NUMBER, "label": "Number", "group": GROUP_BASICS, "tip": "A stat or count, like Strength 14 or Gold 30",
"words": "stat score value count", "w": 8, "h": 4, "title": "Number", "value": 0},
{"kind": TEXT, "label": "Text box", "group": GROUP_BASICS, "tip": "Notes or a description, with formatting and live values like {@str}",
"words": "notes description bbcode long", "w": 14, "h": 8, "title": "Notes", "value": ""},
{"kind": HEADING, "label": "Heading", "group": GROUP_LAYOUT, "tip": "A title to label part of the sheet",
"words": "title label section", "w": 14, "h": 3, "title": "Heading", "value": null},
]
static func find(kind: String) -> Dictionary:
for item: Dictionary in ITEMS:
if item["kind"] == kind:
return item
return {}
static func groups() -> Array[String]:
var out: Array[String] = []
for item: Dictionary in ITEMS:
if not out.has(item["group"]):
out.append(item["group"])
return out
## A new box of `kind` with its start size, title and value, top-left at `at`.
static func make_box(kind: String, id: String, at: Vector2) -> SheetBox:
var item := find(kind)
var box := SheetBox.new(id, kind)
box.title = str(item.get("title", ""))
box.x = snap(at.x)
box.y = snap(at.y)
box.w = float(item.get("w", 12)) * GRID
box.h = float(item.get("h", 6)) * GRID
var start: Variant = item.get("value", null)
box.value = start.duplicate(true) if start is Array or start is Dictionary else start
return box
## Rounds to the nearest grid step.
static func snap(v: float) -> float:
return roundf(v / GRID) * GRID