TableTower/ui/sheet_fields_list.gd

234 lines
8.8 KiB
GDScript3
Raw Permalink Normal View History

class_name SheetFieldsList
extends RefCounted
## A list box ("repeating section" in other sheet tools): many entries in one
## box, shown as compact rows, a mini table or chips (config "style"). Entries
## are Dictionaries keyed by column name, plus an optional "details" text.
## Hovering an entry shows a short excerpt of its details; ⓘ opens them in
## the side panel (SheetDetailPanel). With a point pool (config "pool") the
## cost column's total comes off the pool; going over is allowed and shows red.
## A row from a Lore Entry (SheetLoreDrop) may hold "lore" (linked: shown with
## §) and "sheet" (its item sheet: ▤ opens it).
const EXCERPT := 110
static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control:
var ink: Color = look["ink"]
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 4)
var pool_label: Label = null
if SheetValues.number(box.config.get("pool", 0)) > 0:
pool_label = Style.make_label("", Style.body_bold(), 13, ink)
pool_label.name = "Pool"
col.add_child(pool_label)
_show_pool(box, pool_label, ink)
var scroll := ScrollContainer.new()
scroll.size_flags_vertical = Control.SIZE_EXPAND_FILL
scroll.horizontal_scroll_mode = ScrollContainer.SCROLL_MODE_DISABLED
col.add_child(scroll)
var style := str(box.config.get("style", "rows"))
if style == "chips":
scroll.add_child(_chips(ed, box, ink))
else:
scroll.add_child(_rows(ed, box, ink, style == "table", pool_label))
return col
static func entries(box: SheetBox) -> Array:
return (box.value as Array).duplicate(true) if box.value is Array else []
static func columns(box: SheetBox) -> Array:
var cols: Array = box.config.get("columns", [])
return cols if not cols.is_empty() else [{"name": "Name", "type": "text"}]
## "Pool 20 · spent 17 · left 3", red with "over by 1: allowed" below zero.
static func _show_pool(box: SheetBox, label: Label, ink: Color) -> void:
var p := SheetValues.pool(box)
var left := float(p["left"])
label.text = "Pool %s · spent %s · left %s" % [SheetValues.show(p["total"]), SheetValues.show(p["spent"]), SheetValues.show(left)]
if left < 0:
label.text += " · over by %s: allowed" % SheetValues.show(-left)
label.add_theme_color_override("font_color", Style.DANGER_INK if left < 0 else ink)
## Rows (label left, value right) or a mini table (a header and a cell per column).
static func _rows(ed: SheetEditor, box: SheetBox, ink: Color, table: bool, pool_label: Label) -> VBoxContainer:
var list := VBoxContainer.new()
list.size_flags_horizontal = Control.SIZE_EXPAND_FILL
list.add_theme_constant_override("separation", 2)
var cols := columns(box)
if table:
var head := HBoxContainer.new()
for c: Dictionary in cols:
var h := Style.make_label(str(c["name"]).to_upper(), Style.caps(), 10, Color(ink, 0.6))
_size_cell(h, c)
head.add_child(h)
head.add_child(_spacer(48))
list.add_child(head)
var rows := entries(box)
for i in rows.size():
list.add_child(_row(ed, box, ink, cols, rows, i, table, pool_label))
if not ed.building():
var add := SheetFieldViews.step_button("+ Add", ink)
add.name = "Add"
add.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
add.pressed.connect(func() -> void:
var next := entries(box)
var blank := {}
for c: Dictionary in cols:
blank[c["name"]] = 0 if c.get("type", "text") == "number" else ""
next.append(blank)
ed.set_value(box, next))
list.add_child(add)
return list
static func _row(ed: SheetEditor, box: SheetBox, ink: Color, cols: Array, rows: Array, i: int, table: bool, pool_label: Label) -> HBoxContainer:
var row := HBoxContainer.new()
row.name = "Row%d" % i
row.add_theme_constant_override("separation", 4)
var entry: Dictionary = rows[i]
row.tooltip_text = excerpt(entry, cols)
row.mouse_filter = Control.MOUSE_FILTER_PASS
if entry.has("lore"):
var mark := Style.make_label("§", Style.body_bold(), 14, Style.REF_INK)
mark.name = "LoreMark"
mark.tooltip_text = "Linked to a Lore Entry: ⓘ to read it"
mark.mouse_filter = Control.MOUSE_FILTER_PASS
row.add_child(mark)
for k in cols.size():
var c: Dictionary = cols[k]
var key := str(c["name"])
var number: bool = c.get("type", "text") == "number"
var cell := LineEdit.new()
cell.name = key.validate_node_name()
cell.text = SheetValues.show(entry.get(key, 0)) if number else str(entry.get(key, ""))
cell.flat = true
cell.tooltip_text = row.tooltip_text
cell.add_theme_constant_override("minimum_character_width", 1)
cell.add_theme_color_override("font_color", ink)
cell.add_theme_font_override("font", Style.body_bold() if k == 0 else Style.body())
cell.add_theme_font_size_override("font_size", 14)
if number:
cell.alignment = HORIZONTAL_ALIGNMENT_RIGHT
if table:
_size_cell(cell, c)
else:
cell.size_flags_horizontal = Control.SIZE_EXPAND_FILL if k == 0 else Control.SIZE_SHRINK_END
if k > 0:
cell.custom_minimum_size.x = 44
cell.text_changed.connect(func(text: String) -> void:
var next := entries(box)
next[i][key] = SheetFieldViews.parse_number(text, 0) if number else text
ed.edit_value(box, next)
if pool_label:
_show_pool(box, pool_label, ink))
row.add_child(cell)
row.add_child(_info_button(ed, box, i, ink))
if str(entry.get("sheet", "")) != "":
row.add_child(sheet_button(ed, str(entry["sheet"]), ink))
if not ed.building():
var remove := SheetFieldViews.step_button("×", ink)
remove.name = "Remove"
remove.tooltip_text = "Remove this entry"
remove.pressed.connect(func() -> void:
var next := entries(box)
next.remove_at(i)
ed.set_value(box, next))
row.add_child(remove)
return row
## ⓘ: opens the entry's details in the side panel.
static func _info_button(ed: SheetEditor, box: SheetBox, i: int, ink: Color) -> Button:
var b := SheetFieldViews.step_button("ⓘ", ink)
b.name = "Details"
b.tooltip_text = "Details: what it does, in full"
b.pressed.connect(func() -> void: ed.show_details(box.id, i))
return b
## ▤: opens an item's sheet (SheetItemNav, with a way back).
static func sheet_button(ed: SheetEditor, sheet_id: String, ink: Color) -> Button:
var b := SheetFieldViews.step_button("▤", ink)
b.name = "OpenSheet"
b.tooltip_text = "Open its sheet"
b.pressed.connect(func() -> void: ed.nav.open(sheet_id))
return b
## "§ " before a linked entry's name, else "".
static func link_mark(entry: Dictionary) -> String:
return "§ " if entry.has("lore") else ""
static func _size_cell(c: Control, col: Dictionary) -> void:
if col.get("type", "text") == "number":
c.custom_minimum_size.x = 44
c.size_flags_horizontal = Control.SIZE_SHRINK_END
else:
c.size_flags_horizontal = Control.SIZE_EXPAND_FILL
c.custom_minimum_size.x = 40
static func _spacer(w: float) -> Control:
var s := Control.new()
s.custom_minimum_size.x = w
return s
## Short entries side by side: "Name value"; click one for details, + adds.
static func _chips(ed: SheetEditor, box: SheetBox, ink: Color) -> HFlowContainer:
var flow := HFlowContainer.new()
flow.size_flags_horizontal = Control.SIZE_EXPAND_FILL
flow.add_theme_constant_override("h_separation", 5)
flow.add_theme_constant_override("v_separation", 5)
var cols := columns(box)
var rows := entries(box)
for i in rows.size():
var entry: Dictionary = rows[i]
var parts: Array = []
for c: Dictionary in cols:
parts.append(SheetValues.show(entry.get(c["name"], "")) if c.get("type", "text") == "number" else str(entry.get(c["name"], "")))
var chip := SheetFieldViews.step_button(link_mark(entry) + " ".join(parts).strip_edges(), ink)
chip.name = "Chip%d" % i
chip.tooltip_text = excerpt(entry, cols) + "\nClick for details · right-click to remove"
var index := i
chip.pressed.connect(func() -> void: ed.show_details(box.id, index))
chip.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and mb.button_index == MOUSE_BUTTON_RIGHT and not ed.building():
var next := entries(box)
next.remove_at(index)
ed.set_value(box, next))
flow.add_child(chip)
if not ed.building():
var add := LineEdit.new()
add.name = "AddChip"
add.placeholder_text = "+ add"
add.custom_minimum_size.x = 80
add.flat = true
add.add_theme_color_override("font_color", ink)
add.add_theme_color_override("font_placeholder_color", Color(ink, 0.5))
add.text_submitted.connect(func(text: String) -> void:
if text.strip_edges() == "":
return
var next := entries(box)
next.append({str(cols[0]["name"]): text.strip_edges()})
ed.set_value(box, next))
flow.add_child(add)
return flow
## The hover text: the entry's name and the start of its details, cut with "…".
static func excerpt(entry: Dictionary, cols: Array) -> String:
var name := str(entry.get(str(cols[0]["name"]), ""))
var details := str(entry.get("details", "")).strip_edges()
if details == "":
return name + "\nⓘ to add details"
if details.length() > EXCERPT:
details = details.left(EXCERPT).strip_edges() + "…"
return "%s\n%s" % [name, details]