Titles, result text and details (card, inspector, Lore Entries) get red wavy lines under misspelled words; right-click gives suggestions, Add to dictionary (saved with the spread) and Ignore (this session). Uses the Godot-Hunspell add-on's SpellChecker when it is installed; off on Android and when the add-on is missing. English (en_US) dictionary included. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
297 lines
9.2 KiB
GDScript
297 lines
9.2 KiB
GDScript
class_name TowerBoard
|
|
extends RefCounted
|
|
## A collection: every table on the board, plus loading and saving as JSON.
|
|
## Files carry a format_version; older versions are upgraded by migrate().
|
|
|
|
const SAVE_PATH := "user://board.json" # The single-board file of format version 1.
|
|
const FORMAT_VERSION := 2
|
|
|
|
static var _counter: int = 0
|
|
static var _salt: String = ""
|
|
|
|
## Stable slug used as the file name and as the key for local state.
|
|
var id: String = ""
|
|
## Display name; free to change.
|
|
var name: String = ""
|
|
## The folder it's listed under in the spread switcher ("" = none; "a/b" =
|
|
## folder b inside folder a). Rules: core/spread_folders.gd.
|
|
var folder: String = ""
|
|
var tables: Array[RollTable] = []
|
|
## Group cards (frames around blocks of tables). Optional; a table is in at
|
|
## most one group. See TableGroup.
|
|
var groups: Array[TableGroup] = []
|
|
## The Book: reusable results (Pages) for this spread.
|
|
var book: Array[BookPage] = []
|
|
## Pictures: id -> base64 data (BoardImages). Only used ones are saved.
|
|
var images: Dictionary = {}
|
|
## Words the spellchecker accepts in this spread ("Add to dictionary"): the
|
|
## setting's names and terms. Saved with the spread, so they travel with it.
|
|
var dictionary: Array[String] = []
|
|
|
|
|
|
static func new_id(prefix: String) -> String:
|
|
if _salt == "":
|
|
_salt = "%x" % (int(Time.get_unix_time_from_system()) % 0xfffff)
|
|
_counter += 1
|
|
return "%s%s_%d" % [prefix, _salt, _counter]
|
|
|
|
|
|
func get_table(id: String) -> RollTable:
|
|
for t in tables:
|
|
if t.id == id:
|
|
return t
|
|
return null
|
|
|
|
|
|
## Removes a table, every pipe that pointed at it and its group membership.
|
|
func remove_table(id: String) -> void:
|
|
for i in range(tables.size() - 1, -1, -1):
|
|
if tables[i].id == id:
|
|
tables.remove_at(i)
|
|
for t in tables:
|
|
for e in t.entries:
|
|
e.pipes.erase(id)
|
|
for g in groups:
|
|
g.members.erase(id)
|
|
|
|
|
|
# ---------- Groups ----------
|
|
|
|
func get_group(gid: String) -> TableGroup:
|
|
for g in groups:
|
|
if g.id == gid:
|
|
return g
|
|
return null
|
|
|
|
|
|
## The group a table belongs to, or null.
|
|
func group_of(table_id: String) -> TableGroup:
|
|
for g in groups:
|
|
if g.members.has(table_id):
|
|
return g
|
|
return null
|
|
|
|
|
|
## True when the table is hidden inside a collapsed group (at any depth).
|
|
func is_hidden(table_id: String) -> bool:
|
|
var g := group_of(table_id)
|
|
while g != null:
|
|
if g.collapsed:
|
|
return true
|
|
g = get_group(g.parent)
|
|
return false
|
|
|
|
|
|
# ----- The Book (reusable results) -----
|
|
|
|
func book_list() -> Array:
|
|
var out: Array = []
|
|
for p in book:
|
|
out.append(p.to_dict())
|
|
return out
|
|
|
|
|
|
func load_book(src: Variant) -> void:
|
|
var pages: Array[BookPage] = []
|
|
if src is Array:
|
|
for v: Variant in src:
|
|
if v is Dictionary:
|
|
var p := BookPage.from_dict(v)
|
|
if p.id == "":
|
|
p.id = new_id("p")
|
|
pages.append(p)
|
|
book = pages
|
|
|
|
|
|
func get_page(pid: String) -> BookPage:
|
|
for p in book:
|
|
if p.id == pid:
|
|
return p
|
|
return null
|
|
|
|
|
|
## The texture of a stored picture, or null.
|
|
func picture(id: String) -> Texture2D:
|
|
return BoardImages.texture(id, str(images.get(id, "")))
|
|
|
|
|
|
func add_page(text: String, details: String = "") -> BookPage:
|
|
var p := BookPage.new()
|
|
p.id = new_id("p")
|
|
p.text = text
|
|
p.details = details
|
|
book.append(p)
|
|
return p
|
|
|
|
|
|
## Deletes a page; results linked to it keep their text and become ordinary.
|
|
func remove_page(pid: String) -> void:
|
|
for t in tables:
|
|
for e in t.entries:
|
|
if e.page == pid:
|
|
e.page = ""
|
|
book = book.filter(func(p: BookPage) -> bool: return p.id != pid)
|
|
|
|
|
|
## Links a result to a page and copies the page's text and details onto it.
|
|
func link_page(e: TableEntry, pid: String) -> void:
|
|
var p := get_page(pid)
|
|
if p:
|
|
e.page = pid
|
|
e.text = p.text
|
|
e.details = p.details
|
|
e.image = p.image
|
|
|
|
|
|
## Copies a page onto every result linked to it. Returns the tables touched.
|
|
func sync_page(pid: String) -> Array[String]:
|
|
var p := get_page(pid)
|
|
var touched: Array[String] = []
|
|
if p == null:
|
|
return touched
|
|
for t in tables:
|
|
for e in t.entries:
|
|
if e.page == pid:
|
|
e.text = p.text
|
|
e.details = p.details
|
|
e.image = p.image
|
|
if not touched.has(t.id):
|
|
touched.append(t.id)
|
|
return touched
|
|
|
|
|
|
## How many results are linked to a page.
|
|
func page_uses(pid: String) -> int:
|
|
var n := 0
|
|
for t in tables:
|
|
for e in t.entries:
|
|
if e.page == pid:
|
|
n += 1
|
|
return n
|
|
|
|
|
|
# ----- Nested groups (the work happens in BoardGroups) -----
|
|
|
|
func child_groups(gid: String) -> Array[TableGroup]: return BoardGroups.child_groups(self, gid)
|
|
## Every table inside a group, its inner groups' tables included.
|
|
func tables_in(gid: String) -> Array[String]: return BoardGroups.tables_in(self, gid)
|
|
## The group and every group inside it, outermost first.
|
|
func groups_in(gid: String) -> Array[TableGroup]: return BoardGroups.groups_in(self, gid)
|
|
## The outermost collapsed group around `g` (not `g` itself), or null.
|
|
func hidden_by(g: TableGroup) -> TableGroup: return BoardGroups.hidden_by(self, g)
|
|
## How many groups `g` sits inside.
|
|
func depth_of(g: TableGroup) -> int: return BoardGroups.depth_of(self, g)
|
|
## Puts group `gid` inside `parent_id` ("" = the spread). False if it would loop.
|
|
func set_group_parent(gid: String, parent_id: String) -> bool: return BoardGroups.set_group_parent(self, gid, parent_id)
|
|
## Makes a group of the given tables (taking them out of any other group).
|
|
func add_group(gname: String, member_ids: Array, gid: String = "") -> TableGroup: return BoardGroups.add_group(self, gname, member_ids, gid)
|
|
## Puts a table in a group, taking it out of its old one. False if either is missing.
|
|
func add_to_group(table_id: String, gid: String) -> bool: return BoardGroups.add_to_group(self, table_id, gid)
|
|
func remove_from_group(table_id: String) -> void: BoardGroups.remove_from_group(self, table_id)
|
|
## Removes the group only; its cards and inner groups move up a level.
|
|
func ungroup(gid: String) -> void: BoardGroups.ungroup(self, gid)
|
|
## Removes the group, its inner groups and every table in them. Returns the table ids.
|
|
func delete_group(gid: String) -> Array[String]: return BoardGroups.delete_group(self, gid)
|
|
## Moves a group's frame, its inner groups and every table in them by `delta`.
|
|
func move_group(gid: String, delta: Vector2) -> void: BoardGroups.move_group(self, gid, delta)
|
|
## Drops bad members, keeps each table in one group, and makes group ids unique.
|
|
func clean_groups() -> void: BoardGroups.clean_groups(self)
|
|
|
|
|
|
## Upgrades a parsed file to FORMAT_VERSION (BoardFileIO.migrate).
|
|
static func migrate(d: Dictionary) -> Dictionary: return BoardFileIO.migrate(d)
|
|
|
|
|
|
func to_dict() -> Dictionary:
|
|
var list: Array = []
|
|
for t in tables:
|
|
list.append(t.to_dict())
|
|
var d := {"format_version": FORMAT_VERSION, "id": id, "name": name, "tables": list}
|
|
if folder != "":
|
|
d["folder"] = folder
|
|
if not groups.is_empty():
|
|
var gl: Array = []
|
|
for g in groups:
|
|
gl.append(g.to_dict())
|
|
d["groups"] = gl
|
|
if not book.is_empty():
|
|
d["book"] = book_list()
|
|
if not dictionary.is_empty():
|
|
d["dictionary"] = dictionary.duplicate()
|
|
var pics := BoardImages.used(self)
|
|
if not pics.is_empty():
|
|
d["images"] = pics
|
|
return d
|
|
|
|
|
|
func to_json() -> String:
|
|
return JSON.stringify(to_dict(), " ")
|
|
|
|
|
|
## Replaces the board's tables (and id/name, when the text has them) from JSON
|
|
## text of any known version. Returns false if the text isn't a board.
|
|
func load_json(text: String) -> bool:
|
|
var parsed: Variant = JSON.parse_string(text)
|
|
if not (parsed is Dictionary) or not (parsed.get("tables") is Array):
|
|
return false
|
|
return load_dict(parsed as Dictionary)
|
|
|
|
|
|
## Same as load_json for an already parsed Dictionary.
|
|
func load_dict(src: Dictionary) -> bool:
|
|
if not (src.get("tables") is Array):
|
|
return false
|
|
var d: Dictionary = migrate(src.duplicate())
|
|
var loaded: Array[RollTable] = []
|
|
for td in d["tables"]:
|
|
if td is Dictionary:
|
|
loaded.append(RollTable.from_dict(td))
|
|
tables = loaded
|
|
var loaded_groups: Array[TableGroup] = []
|
|
var src_groups: Variant = d.get("groups", [])
|
|
if src_groups is Array:
|
|
for gd: Variant in src_groups:
|
|
if gd is Dictionary:
|
|
loaded_groups.append(TableGroup.from_dict(gd))
|
|
groups = loaded_groups
|
|
clean_groups()
|
|
load_book(d.get("book", []))
|
|
var words: Array[String] = []
|
|
var src_words: Variant = d.get("dictionary", [])
|
|
if src_words is Array:
|
|
for w: Variant in src_words:
|
|
if str(w).strip_edges() != "" and not words.has(str(w).strip_edges()):
|
|
words.append(str(w).strip_edges())
|
|
dictionary = words
|
|
# Undo snapshots carry no pictures: keep the ones already loaded.
|
|
if d.get("images") is Dictionary:
|
|
images.merge(d["images"], true)
|
|
if str(d.get("id", "")) != "":
|
|
id = str(d["id"])
|
|
if str(d.get("name", "")) != "":
|
|
name = str(d["name"])
|
|
if d.has("folder"):
|
|
folder = SpreadFolders.normalize(str(d["folder"]))
|
|
return true
|
|
|
|
|
|
## Writes text to path via a temp file and a rename (BoardFileIO).
|
|
static func write_text_atomic(path: String, text: String) -> Error: return BoardFileIO.write_text_atomic(path, text)
|
|
|
|
|
|
func save(path: String = SAVE_PATH) -> bool:
|
|
var err := write_text_atomic(path, to_json())
|
|
if err != OK:
|
|
push_warning("Couldn't save the spread to %s: %s" % [path, error_string(err)])
|
|
return false
|
|
return true
|
|
|
|
|
|
## Loads a board file, or returns null if there isn't a usable one.
|
|
static func load_saved(path: String = SAVE_PATH) -> TowerBoard:
|
|
if not FileAccess.file_exists(path):
|
|
return null
|
|
var b := TowerBoard.new()
|
|
if not b.load_json(FileAccess.get_file_as_string(path)) or b.tables.is_empty():
|
|
return null
|
|
return b
|