TableTower/ui/edit_labels.gd

279 lines
11 KiB
GDScript3
Raw Normal View History

class_name EditLabels
extends RefCounted
## Diffs between undo snapshots and the step labels derived from them.
## EditHistory keeps one-line delegators for the public functions.
## What changed from snapshot a to b, by id: tables added, removed, changed
## (anything but position) and moved (position only); groups likewise, where
## gmoved (frame only) is ignored for groups with members, whose frames
## follow their cards.
static func diff(a: Dictionary, b: Dictionary) -> Dictionary:
var out := {"added": [], "removed": [], "changed": [], "moved": [],
"gadded": [], "gremoved": [], "gchanged": [], "gmoved": [], "gmoved_empty": []}
_diff_lists(EditHistory._strings_by_id(a, "ids", "tables"), EditHistory._strings_by_id(b, "ids", "tables"),
["x", "y"], out, "added", "removed", "changed", "moved")
_diff_lists(EditHistory._strings_by_id(a, "gids", "groups"), EditHistory._strings_by_id(b, "gids", "groups"),
["x", "y", "w", "h"], out, "gadded", "gremoved", "gchanged", "gmoved")
out["book"] = str(a.get("book", "[]")) != str(b.get("book", "[]"))
var gb := EditHistory._strings_by_id(b, "gids", "groups")
for gid: String in out["gmoved"]:
var d: Variant = JSON.parse_string(gb[gid])
if d is Dictionary and (d.get("members", []) as Array).is_empty():
out["gmoved_empty"].append(gid)
return out
static func _diff_lists(ma: Dictionary, mb: Dictionary, geometry: Array, out: Dictionary,
k_add: String, k_rem: String, k_chg: String, k_mov: String) -> void:
for id: String in mb:
if not ma.has(id):
out[k_add].append(id)
elif ma[id] != mb[id]:
var da: Dictionary = JSON.parse_string(ma[id])
var db: Dictionary = JSON.parse_string(mb[id])
for g: String in geometry:
da.erase(g)
db.erase(g)
out[k_chg if da != db else k_mov].append(id)
for id: String in ma:
if not mb.has(id):
out[k_rem].append(id)
static func is_empty_diff(d: Dictionary) -> bool:
if d.get("book", false):
return false
for k in ["added", "removed", "changed", "moved", "gadded", "gremoved", "gchanged", "gmoved_empty"]:
if not (d[k] as Array).is_empty():
return false
return true
## Table ids whose cards changed in any way (added, changed or moved).
static func touched_tables(d: Dictionary) -> Array:
return (d["added"] as Array) + (d["changed"] as Array) + (d["moved"] as Array)
## A key for edits that may merge with the step before: one table's or one
## group's fields (text, weight, pipe options...), with nothing added or taken
## away. "" for anything else.
static func coalesce_key(a: Dictionary, b: Dictionary, d: Dictionary) -> String:
for k in ["added", "removed", "moved", "gadded", "gremoved", "gmoved_empty"]:
if not (d[k] as Array).is_empty():
return ""
var ch: Array = d["changed"]
var gch: Array = d["gchanged"]
if ch.size() == 1 and gch.is_empty():
var ta: Dictionary = _parsed(a, "ids", "tables", ch[0])
var tb: Dictionary = _parsed(b, "ids", "tables", ch[0])
var e := _entry_changes(ta, tb)
if e["added"].is_empty() and e["removed"].is_empty() and e["pipes_added"].is_empty() and e["pipes_removed"].is_empty():
return "t:" + str(ch[0])
elif gch.size() == 1 and ch.is_empty():
var ga: Dictionary = _parsed(a, "gids", "groups", gch[0])
var gb: Dictionary = _parsed(b, "gids", "groups", gch[0])
if ga.get("members") == gb.get("members"):
return "g:" + str(gch[0])
return ""
## Describes the step from a to b ("Edit Loot", "Delete table Weapons", "Add
## pipe Loot → Gems", "Move 3 cards", "Group Careers"...).
static func label_for(a: Dictionary, b: Dictionary, d: Dictionary = {}) -> String:
if d.is_empty():
d = diff(a, b)
if d.get("book", false) and (d["changed"] as Array).is_empty() and (d["added"] as Array).is_empty():
return "Edit the Book"
var added: Array = d["added"]
var removed: Array = d["removed"]
var gadded: Array = d["gadded"]
var gremoved: Array = d["gremoved"]
if not gadded.is_empty() and not added.is_empty():
return "Add %s" % _count(added.size(), "card")
if gadded.size() == 1:
return "Group %s" % _name(b, "gids", "groups", gadded[0])
if gremoved.size() == 1:
var gname := _name(a, "gids", "groups", gremoved[0])
return ("Delete group %s" if not removed.is_empty() else "Ungroup %s") % gname
if not gadded.is_empty() or not gremoved.is_empty():
return "Change groups"
if added.size() == 1:
return "Add %s %s" % [_kind_word(_parsed(b, "ids", "tables", added[0])), _name(b, "ids", "tables", added[0])]
if not added.is_empty():
return "Add %s" % _count(added.size(), "card")
if removed.size() == 1:
return "Delete %s %s" % [_kind_word(_parsed(a, "ids", "tables", removed[0])), _name(a, "ids", "tables", removed[0])]
if not removed.is_empty():
return "Delete %s" % _count(removed.size(), "card")
var changed: Array = d["changed"]
var gchanged: Array = d["gchanged"]
# Group membership (a card dragged into or out of a group).
for gid: String in gchanged:
var ga := _parsed(a, "gids", "groups", gid)
var gb := _parsed(b, "gids", "groups", gid)
var ma: Array = ga.get("members", [])
var mb: Array = gb.get("members", [])
if ma != mb:
var ins := 0
var outs := 0
for m: Variant in mb:
if not ma.has(m):
ins += 1
for m: Variant in ma:
if not mb.has(m):
outs += 1
var gname := Formatter.strip_tags(str(gb.get("name", "")))
if ins > 0 and outs == 0:
return "Add %s to group %s" % [_count(ins, "card"), gname]
if outs > 0 and ins == 0:
return "Take %s out of group %s" % [_count(outs, "card"), gname]
return "Change group %s" % gname
if changed.size() == 1:
return _table_label(_parsed(a, "ids", "tables", changed[0]), _parsed(b, "ids", "tables", changed[0]), b)
if changed.size() > 1:
return "Edit %s" % _count(changed.size(), "card")
if gchanged.size() == 1:
var ga := _parsed(a, "gids", "groups", gchanged[0])
var gb := _parsed(b, "gids", "groups", gchanged[0])
var gname := Formatter.strip_tags(str(gb.get("name", "")))
if ga.get("name") != gb.get("name"):
return "Rename group %s" % gname
return "Edit group %s" % gname
if gchanged.size() > 1:
return "Edit %s" % _count(gchanged.size(), "group")
var moved: Array = d["moved"]
if not moved.is_empty():
# All the moved cards belong to one group whose frame moved too.
for gid: String in d["gmoved"]:
var gb := _parsed(b, "gids", "groups", gid)
var members: Array = gb.get("members", [])
var all := true
for tid: Variant in moved:
if not members.has(tid):
all = false
break
if all and moved.size() == members.size():
return "Move group %s" % Formatter.strip_tags(str(gb.get("name", "")))
if moved.size() == 1:
return "Move %s" % _name(b, "ids", "tables", moved[0])
return "Move %s" % _count(moved.size(), "card")
if not (d["gmoved_empty"] as Array).is_empty():
return "Move group %s" % _name(b, "gids", "groups", d["gmoved_empty"][0])
return "Edit"
## One table's change: results added or deleted, pipes laid, removed or
## changed, a rename, a reorder, or a plain edit.
static func _table_label(ta: Dictionary, tb: Dictionary, b: Dictionary) -> String:
var tname := Formatter.strip_tags(str(tb.get("name", "")))
var e := _entry_changes(ta, tb)
if not e["added"].is_empty():
return ("Add result to %s" % tname) if e["added"].size() == 1 else "Add %d results to %s" % [e["added"].size(), tname]
if not e["removed"].is_empty():
if e["removed"].size() == 1:
return "Delete result “%s” from %s" % [_short(Formatter.strip_tags(str(e["removed"][0].get("text", "")))), tname]
return "Delete %d results from %s" % [e["removed"].size(), tname]
if not e["pipes_added"].is_empty():
return "Add thread %s → %s" % [tname, _name(b, "ids", "tables", e["pipes_added"][0])]
if not e["pipes_removed"].is_empty():
return "Delete thread %s → %s" % [tname, _name(b, "ids", "tables", e["pipes_removed"][0])]
if not e["pipes_changed"].is_empty():
return "Edit thread %s → %s" % [tname, _name(b, "ids", "tables", e["pipes_changed"][0])]
if ta.get("name") != tb.get("name"):
var rest_a := ta.duplicate()
var rest_b := tb.duplicate()
rest_a.erase("name")
rest_b.erase("name")
if rest_a == rest_b:
return "Rename %s → %s" % [Formatter.strip_tags(str(ta.get("name", ""))), tname]
if e["reordered"]:
return "Reorder %s" % tname
return "Edit %s" % tname
## Entries added and removed (dicts), pipe targets added, removed and changed,
## and whether the order changed.
static func _entry_changes(ta: Dictionary, tb: Dictionary) -> Dictionary:
var ea := {}
var order_a: Array = []
for x: Variant in ta.get("entries", []):
if x is Dictionary:
ea[str(x.get("id", ""))] = x
order_a.append(str(x.get("id", "")))
var eb := {}
var order_b: Array = []
for x: Variant in tb.get("entries", []):
if x is Dictionary:
eb[str(x.get("id", ""))] = x
order_b.append(str(x.get("id", "")))
var out := {"added": [], "removed": [], "pipes_added": [], "pipes_removed": [], "pipes_changed": [], "reordered": false}
for id: String in eb:
if not ea.has(id):
out["added"].append(eb[id])
for id: String in ea:
if not eb.has(id):
out["removed"].append(ea[id])
continue
var pa := _pipes(ea[id])
var pb := _pipes(eb[id])
for to: String in pb:
if not pa.has(to):
out["pipes_added"].append(to)
elif typeof(pa[to]) != typeof(pb[to]) or pa[to] != pb[to]:
out["pipes_changed"].append(to)
for to: String in pa:
if not pb.has(to):
out["pipes_removed"].append(to)
if out["added"].is_empty() and out["removed"].is_empty() and order_a != order_b:
out["reordered"] = true
return out
## target table id -> the pipe as written (an id or a dict of options).
static func _pipes(entry: Dictionary) -> Dictionary:
var out := {}
for p: Variant in entry.get("pipes", []):
if p is Dictionary:
out[str(p.get("table", ""))] = p
else:
out[str(p)] = p
return out
static func _parsed(snap: Dictionary, ids_key: String, strs_key: String, id: String) -> Dictionary:
var ids: PackedStringArray = snap[ids_key]
var i := ids.find(id)
if i < 0:
return {}
var v: Variant = JSON.parse_string((snap[strs_key] as PackedStringArray)[i])
return v if v is Dictionary else {}
static func _name(snap: Dictionary, ids_key: String, strs_key: String, id: String) -> String:
var d := _parsed(snap, ids_key, strs_key, id)
var n := Formatter.strip_tags(str(d.get("name", ""))).strip_edges()
return n if n != "" else id
static func _kind_word(d: Dictionary) -> String:
match str(d.get("kind", RollTable.KIND_ROLL)):
RollTable.KIND_CHOICE:
return "choice"
RollTable.KIND_LOOP:
return "loop"
RollTable.KIND_ROUTER:
return "router"
RollTable.KIND_VARIABLE:
return "variable"
RollTable.KIND_SHEET:
return "sheet"
return "table"
static func _count(n: int, noun: String) -> String:
return "%d %s%s" % [n, noun, "" if n == 1 else "s"]
static func _short(s: String) -> String:
return s if s.length() <= 28 else s.substr(0, 27) + "…"