class_name SheetDoc extends RefCounted ## A sheet: a character, a vehicle, anything you keep track of. It has tabs ## (Main, Spells, Notes…), each a free canvas of boxes (SheetBox). Saved as ## JSON by SheetStore; a template is a sheet saved in the templates folder. ## Spec: docs/SHEETS.md. const FORMAT_VERSION := 1 var id: String = "" var name: String = "New sheet" ## Sheet tint as "#rrggbb" ("" = the theme's), e.g. to tell vehicles apart. var tint: String = "" ## The template this sheet was made from ("" = blank). var from_template: String = "" ## Each tab: {"id", "name", "boxes": Array[SheetBox]}. var tabs: Array[Dictionary] = [] func _init(p_id: String = "", p_name: String = "New sheet") -> void: id = p_id name = p_name if tabs.is_empty(): add_tab("Main") func add_tab(tab_name: String) -> Dictionary: var boxes: Array[SheetBox] = [] var tab := {"id": new_tab_id(), "name": tab_name, "boxes": boxes} tabs.append(tab) return tab func new_tab_id() -> String: var n := tabs.size() + 1 while tabs.any(func(t: Dictionary) -> bool: return t["id"] == "tab%d" % n): n += 1 return "tab%d" % n func get_tab(tab_id: String) -> Dictionary: for tab in tabs: if tab["id"] == tab_id: return tab return {} ## The box with `box_id` on any tab, or null. func find_box(box_id: String) -> SheetBox: for tab in tabs: for box: SheetBox in tab["boxes"]: if box.id == box_id: return box return null ## The tab that holds `box_id` ({} if none). func tab_of(box_id: String) -> Dictionary: for tab in tabs: for box: SheetBox in tab["boxes"]: if box.id == box_id: return tab return {} ## A box id not used on this sheet. func new_box_id() -> String: var n := 1 while find_box("b%d" % n) != null: n += 1 return "b%d" % n ## True when `r` would cover a box on `tab` other than `skip_id` (touching ## edges is fine). static func overlaps(tab: Dictionary, r: Rect2, skip_id: String = "") -> bool: for box: SheetBox in tab["boxes"]: if box.id != skip_id and box.rect().grow(-0.5).intersects(r.grow(-0.5)): return true return false ## The nearest free place for `r` on `tab`: scanning grid steps down and ## then across from where it was asked for. Boxes don't overlap unless ## placed freely (Alt). static func free_spot(tab: Dictionary, r: Rect2, step: float, skip_id: String = "") -> Vector2: if not overlaps(tab, r, skip_id): return r.position for ring in range(1, 200): for dy in range(0, ring + 1): for dx: int in [ring - dy, -(ring - dy)]: var at := r.position + Vector2(dx, dy) * step if at.x >= 0 and not overlaps(tab, Rect2(at, r.size), skip_id): return at return r.position func remove_box(box_id: String) -> void: var tab := tab_of(box_id) if not tab.is_empty(): (tab["boxes"] as Array).assign((tab["boxes"] as Array).filter(func(b: SheetBox) -> bool: return b.id != box_id)) func to_dict() -> Dictionary: var tab_list: Array = [] for tab in tabs: var boxes: Array = [] for box: SheetBox in tab["boxes"]: boxes.append(box.to_dict()) tab_list.append({"id": tab["id"], "name": tab["name"], "boxes": boxes}) var d := {"format_version": FORMAT_VERSION, "id": id, "name": name, "tabs": tab_list} if tint != "": d["tint"] = tint if from_template != "": d["from_template"] = from_template return d static func from_dict(d: Dictionary) -> SheetDoc: var doc := SheetDoc.new(str(d.get("id", "")), str(d.get("name", "New sheet"))) doc.tint = str(d.get("tint", "")) doc.from_template = str(d.get("from_template", "")) var tab_list: Variant = d.get("tabs", []) if tab_list is Array and not (tab_list as Array).is_empty(): doc.tabs.clear() for td: Variant in tab_list: if not td is Dictionary: continue var boxes: Array[SheetBox] = [] for bd: Variant in (td as Dictionary).get("boxes", []): if bd is Dictionary: boxes.append(SheetBox.from_dict(bd)) doc.tabs.append({"id": str(td.get("id", doc.new_tab_id())), "name": str(td.get("name", "Tab")), "boxes": boxes}) return doc ## A deep copy (through the saved form), e.g. a new sheet from a template. func copy() -> SheetDoc: return SheetDoc.from_dict(to_dict())