TableTower/ui/inspector_sync.gd

71 lines
2.9 KiB
GDScript3
Raw Permalink Normal View History

class_name InspectorSync
extends RefCounted
## What the Inspector watches to know when to rebuild: the selection signature
## and a collection card's members.
## A collection card's members: Array of [item, value] (item without the stem).
static func members(insp: Inspector, t: RollTable) -> Array:
var out: Array = []
var pre := t.var_name + "_"
var names: Array = insp.main.persistent_vars.keys()
names.sort_custom(func(a: String, b: String) -> bool: return a.naturalnocasecmp_to(b) < 0)
for n: String in names:
if n.begins_with(pre) and n.length() > pre.length():
var v: Variant = insp.main.persistent_vars[n]
out.append([n.substr(pre.length()), Expr.format_value(v.get("value") if v is Dictionary else v)])
return out
static func signature(insp: Inspector) -> String:
var t := insp.selected_table()
if insp.selection.is_empty():
return ""
if t == null or (insp.selection.has("eid") and insp.selected_entry() == null):
return "gone"
var d := t.to_dict()
for k in ["x", "y", "collapsed", "name"]:
d.erase(k)
# The name and result text change on every key typed on the card; sync_texts() shows it
# without a rebuild (a rebuild of a long card took ~120 ms per key).
if t.kind != RollTable.KIND_ROUTER:
for ed: Variant in d.get("entries", []):
if ed is Dictionary:
(ed as Dictionary).erase("text")
(ed as Dictionary).erase("page")
var parts := PackedStringArray([JSON.stringify(d), insp.main.rules_view(), str(insp.main.mode)])
for x in insp.main.data.tables:
if x.kind == RollTable.KIND_VARIABLE:
parts.append("%s|%s|%s" % [x.name, x.var_name, x.var_type])
for e in t.entries:
for p in e.pipes:
var target := insp.main.data.get_table(p)
parts.append(("%s|%s" % [target.name, JSON.stringify(target.inputs)]) if target else "-")
parts.append(str(insp.main.lit_pipes.has(Roller.pipe_key(t.id, e.id, p))))
parts.append(str(insp.main.is_entry_lit(t.id, e.id)))
if t.is_collection():
parts.append(JSON.stringify(members(insp, t)))
return str(hash("\n".join(parts)))
## Puts the card's name and each result's text into the headings and fields
## (unless a field is being typed in), so the page needs no rebuild for them.
static func sync_texts(insp: Inspector) -> void:
var t := insp.selected_table()
if t == null:
return
if is_instance_valid(insp._title_label):
insp._title_label.text = Inspector._title_of(t.name, "(no name)")
_sync_field(insp.section_parts.get(InspectorList.CARD, {}), "name", t.name)
if t.kind == RollTable.KIND_VARIABLE:
return
for e in t.entries:
InspectorList.refresh_title(insp, e)
_sync_field(insp.section_parts.get(e.id, {}), "text", e.text)
static func _sync_field(parts: Dictionary, key: String, value: String) -> void:
var le: Variant = (parts.get("fields", {}) as Dictionary).get(key)
if le is LineEdit and is_instance_valid(le) and not (le as LineEdit).has_focus() and (le as LineEdit).text != value:
(le as LineEdit).text = value