TableTower/core/sheet_box.gd

81 lines
2.4 KiB
GDScript3
Raw Permalink Normal View History

class_name SheetBox
extends RefCounted
## One box on a sheet tab: a field (a number, a text box, a heading…) placed
## freely on the tab's canvas. `kind` picks how it looks and behaves
## (SheetKinds); `config` holds the kind's settings and `value` what the
## player filled in, so new kinds need no new fields here.
var id: String = ""
var kind: String = SheetKinds.TEXT
var title: String = ""
## Position and size on the tab, in canvas pixels (snapped to SheetKinds.GRID
## unless placed freely with Alt).
var x: float = 0.0
var y: float = 0.0
var w: float = 192.0
var h: float = 96.0
## Box colour as "#rrggbb", or "" for the theme's card colour.
var color: String = ""
var frameless: bool = false
## Place in the stacked phone view (0 = automatic: reading order).
var phone_order: int = 0
## Tables can read and write the box as @var_name ("" = not used in tables).
var var_name: String = ""
## Locked boxes ignore values written by rolls.
var locked: bool = false
var config: Dictionary = {}
var value: Variant = null
func _init(p_id: String = "", p_kind: String = SheetKinds.TEXT) -> void:
id = p_id
kind = p_kind
func rect() -> Rect2:
return Rect2(x, y, w, h)
func set_rect(r: Rect2) -> void:
x = r.position.x
y = r.position.y
w = r.size.x
h = r.size.y
func to_dict() -> Dictionary:
var d := {"id": id, "kind": kind, "title": title, "x": x, "y": y, "w": w, "h": h}
if color != "":
d["color"] = color
if frameless:
d["frameless"] = true
if phone_order != 0:
d["phone_order"] = phone_order
if var_name != "":
d["var"] = var_name
if locked:
d["locked"] = true
if not config.is_empty():
d["config"] = config.duplicate(true)
if value != null:
d["value"] = value.duplicate(true) if value is Array or value is Dictionary else value
return d
static func from_dict(d: Dictionary) -> SheetBox:
var box := SheetBox.new(str(d.get("id", "")), str(d.get("kind", SheetKinds.TEXT)))
box.title = str(d.get("title", ""))
box.x = float(d.get("x", 0))
box.y = float(d.get("y", 0))
box.w = float(d.get("w", 192))
box.h = float(d.get("h", 96))
box.color = str(d.get("color", ""))
box.frameless = bool(d.get("frameless", false))
box.phone_order = int(d.get("phone_order", 0))
box.var_name = str(d.get("var", ""))
box.locked = bool(d.get("locked", false))
var cfg: Variant = d.get("config", {})
box.config = (cfg as Dictionary).duplicate(true) if cfg is Dictionary else {}
box.value = d.get("value", null)
return box