TableTower/ui/table_card_rows.gd

485 lines
20 KiB
GDScript3
Raw Normal View History

class_name TableCardRows
extends RefCounted
## A TableCard's result rows: building them (plain, check outlet, sheet and the
## collapsed card's "more" row), their Play-mode text and rule chips, and their
## highlight state (normal, flickering during a roll, lit, with the copper bar
## of the inspector's selected result). Build-only controls come from
## TableCardEditing.
enum RowState { NORMAL, FLICK, LIT }
const LOOP_ROWS := ["Each time", "Afterwards"]
## Longest effects summary shown on a chip.
const CHIP_CHARS := 30
const PLAIN_CHIP_CHARS := 38
var card: TableCard
## entry id -> {num, text, entry, index}, for the rows currently shown.
var parts: Dictionary = {}
var more_label: Label
var sheet_label: RichTextLabel
## Row styles by state, then +3 for the inspector's selected result, then +6
## for a striped (every second) row.
var _row_styles: Array[StyleBoxFlat] = []
var _flick_id := ""
## Chance labels by entry id (ranged rows in Play mode), kept current by
## refresh_odds as the modifier's values change.
var _odds: Dictionary = {}
func _init(p_card: TableCard) -> void:
card = p_card
# Normal, flicker and lit; then the same with the copper bar of the
# inspector's selected result. Striped rows tint only the normal state, so
# the flicker and the lit result always read over the stripe.
for striped in [false, true]:
for selected in [false, true]:
for c in [Style.STRIPE if striped else Color(0, 0, 0, 0), Style.FLICK, Style.LIT]:
var sb := Style.box(c, 0, [12, 6, 22, 6])
if selected:
sb.border_width_left = 3
sb.border_color = Style.TRIM
sb.anti_aliasing = false
_row_styles.append(sb)
## Forgets the previous build's rows.
func clear() -> void:
parts.clear()
_odds.clear()
more_label = null
_flick_id = ""
sheet_label = null
func register(e: TableEntry, row: PanelContainer, num: Label, text: RichTextLabel, index: int) -> void:
card.rows[e.id] = row
parts[e.id] = {"num": num, "text": text, "entry": e, "index": index}
func _is_pick() -> bool:
return card.table.kind == RollTable.KIND_CHOICE and card.table.choice == "pick"
## True for the Chosen row of a dynamic pick card.
func _is_chosen_row(index: int) -> bool:
return index == 0 and card.table.is_dynamic()
func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float) -> Control:
var table := card.table
var main := card.main
var edit := card.edit_ui
var row := PanelContainer.new()
row.mouse_filter = Control.MOUSE_FILTER_PASS
row.custom_minimum_size = Vector2(0, 36)
var h := TableCard.hbox(8)
row.add_child(h)
# Loop rows and the Answer row of number/text choices are fixed: no number,
# weight or delete.
var fixed := table.kind == RollTable.KIND_LOOP or (table.kind == RollTable.KIND_CHOICE and table.choice != "pick")
var chosen := _is_chosen_row(index)
var num: Label = null
if build and ranged:
var r := TableCard.hbox(2)
r.custom_minimum_size = Vector2(numw, 0)
r.size_flags_vertical = Control.SIZE_SHRINK_CENTER
r.add_child(edit.range_edit(e, true))
r.add_child(Style.make_label("–", Style.display(true), 15, Style.MUTED))
r.add_child(edit.range_edit(e, false))
h.add_child(r)
else:
var nl := "" if fixed else (str(index + 1) if table.kind == RollTable.KIND_ROUTER else table.num_label(e, index))
if table.is_dynamic() and not ranged:
nl = "★" if chosen else str(index)
num = Style.make_label(nl, Style.display(true), 16, Style.MUTED)
num.custom_minimum_size = Vector2(numw, 0)
num.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
num.size_flags_vertical = Control.SIZE_SHRINK_CENTER
if fixed:
num.text = "↻" if table.kind == RollTable.KIND_LOOP and index == 0 else "→"
num.add_theme_font_override("font", Style.body_bold(true))
num.add_theme_font_size_override("font_size", 14)
h.add_child(num)
var text: RichTextLabel = null
var delete_button: Button = null
if build:
h.add_theme_constant_override("separation", 4)
var col := TableCard.vbox(2)
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
col.size_flags_vertical = Control.SIZE_SHRINK_CENTER
h.add_child(col)
if table.kind == RollTable.KIND_CHOICE and table.choice != "pick":
var al := Style.make_label("Answer", Style.body_italic(true), 15, Style.MUTED)
al.size_flags_horizontal = Control.SIZE_EXPAND_FILL
al.size_flags_vertical = Control.SIZE_SHRINK_CENTER
al.mouse_filter = Control.MOUSE_FILTER_PASS
al.tooltip_text = "Its effects and threads run after the player answers ({answer} holds the answer)"
col.add_child(al)
else:
if chosen:
# A caption over the text field, so the field keeps its width.
var cl := Style.make_label("Chosen · runs for the picked option", Style.body_italic(true), 11, Style.TRIM_DARK)
cl.mouse_filter = Control.MOUSE_FILTER_PASS
cl.tooltip_text = "Runs for whichever option from %s is picked: option, option_label and option_value hold it" % table.options_from.strip_edges()
col.add_child(cl)
col.add_child(edit.text_edit(e, index, chosen))
add_rule_chips(col, e)
if not ranged and not fixed:
h.add_child(edit.weight_edit(e))
if not fixed and not chosen:
delete_button = TableCardEditing.mini_button("×", "Delete this result", true, func() -> void: main.delete_entry(table.id, e.id))
h.add_child(delete_button)
edit.hook_select(row, e.id)
else:
var col := TableCard.vbox(3)
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
col.size_flags_vertical = Control.SIZE_SHRINK_CENTER
text = RichText.label(row_text(e), 16, Style.INK, true)
text.size_flags_horizontal = Control.SIZE_EXPAND_FILL
RichText.hook_hover(text, main)
col.add_child(text)
add_rule_chips(col, e)
h.add_child(col)
row.mouse_entered.connect(func() -> void: main.details().show_entry(row, table.id, e.id))
if ranged and table.has_chances():
var pct := Style.make_label("", Style.body(true), 12, Style.MUTED)
pct.size_flags_vertical = Control.SIZE_SHRINK_CENTER
pct.mouse_filter = Control.MOUSE_FILTER_PASS
h.add_child(pct)
_odds[e.id] = pct
_set_odds(pct, e, live_mod())
if ranged and table.has_mod() and num != null and not RollRange.natural_reach(table, e):
var nat := RollRange.natural(table)
num.tooltip_text = "Only a modifier reaches this: the dice alone roll %s–%s" % [table.fmt(nat.x), table.fmt(nat.y)]
num.mouse_filter = Control.MOUSE_FILTER_PASS
if e.page != "":
# A small § says the result follows a Book page.
var pg := main.data.get_page(e.page)
var mark := Style.make_label("§", Style.body_bold(true), 13, Style.TRIM)
mark.size_flags_vertical = Control.SIZE_SHRINK_CENTER
mark.mouse_filter = Control.MOUSE_FILTER_PASS
mark.tooltip_text = "From the Book: %s" % (pg.label() if pg else "a deleted page")
h.add_child(mark)
h.move_child(mark, mini(1, h.get_child_count() - 1))
register(e, row, num, text, index)
if delete_button:
edit.reveal_on_hover(e.id, row, delete_button)
return row
## The plain chips under a row's text: a pick option's "only if …" and the
## entry's effects ("Luck −1"). Used in Play and Build alike.
func add_rule_chips(col: VBoxContainer, e: TableEntry) -> void:
var plain := card.plain
if _is_pick() and e.show_if.strip_edges() != "":
var bad := Expr.check(TemplateKit.probe(e.show_if.strip_edges())) != ""
var si := Style.make_label(RulesView.show_if_line(e, card.main.data, plain), Style.body_bold(true), 12, Style.DANGER_INK if bad else Style.TRIM_DARK)
si.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
si.mouse_filter = Control.MOUSE_FILTER_PASS
si.tooltip_text = ("only if " + e.show_if.strip_edges()) if plain else ""
si.set_meta("show_if_line", true)
col.add_child(si)
if e.effects.strip_edges() != "":
col.add_child(_effects_chip(e))
## One outlet of a check card: "8+ Success · the bars give" / "≤7 Failure",
## with the outlet named in green or red. Build edits the text after the name.
func make_check_row(e: TableEntry, index: int, build: bool, numw: float) -> Control:
var table := card.table
var main := card.main
var row := PanelContainer.new()
row.mouse_filter = Control.MOUSE_FILTER_PASS
row.custom_minimum_size = Vector2(0, 36)
var h := TableCard.hbox(8)
row.add_child(h)
var outs := RulesView.check_outlets(table)
var num := Style.make_label(outs[index] if index < 2 else str(index + 1), Style.display(true), 16, Style.MUTED)
num.custom_minimum_size = Vector2(maxf(numw, 34.0), 0)
num.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
num.size_flags_vertical = Control.SIZE_SHRINK_CENTER
h.add_child(num)
var col := TableCard.vbox(2)
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
col.size_flags_vertical = Control.SIZE_SHRINK_CENTER
h.add_child(col)
var text: RichTextLabel = null
if build:
var line := TableCard.hbox(6)
col.add_child(line)
var nm := Style.make_label(TableCardText.check_outlet_name(index), Style.body_bold(true), 15, TableCardText.check_outlet_color(index))
nm.size_flags_vertical = Control.SIZE_SHRINK_CENTER
line.add_child(nm)
var edit := card.edit_ui.text_edit(e, index, false)
# The outlet name is already shown, so a result text that just repeats
# it ("Success") shows as an empty field with a hint instead.
if e.text.strip_edges() == TableCardText.check_outlet_name(index):
edit.text = ""
edit.placeholder_text = "What happens on a %s (optional)" % TableCardText.check_outlet_name(index).to_lower()
line.add_child(edit)
add_rule_chips(col, e)
card.edit_ui.hook_select(row, e.id)
else:
text = RichText.label(row_text(e), 16, Style.INK, true)
text.size_flags_horizontal = Control.SIZE_EXPAND_FILL
RichText.hook_hover(text, main)
col.add_child(text)
add_rule_chips(col, e)
row.mouse_entered.connect(func() -> void: main.details().show_entry(row, table.id, e.id))
register(e, row, num, text, index)
return row
## Play-mode text of a row: the last rolled (expanded) text when the row is
## lit, otherwise the entry as written with its references shown readably.
## Loop rows read "Each time · …"; a number/text choice's row is its answer;
## a router case leads with its condition, a check outlet with its name.
func row_text(e: TableEntry) -> String:
var table := card.table
var main := card.main
var rolled := main.lit_text(table.id, e.id)
var s := RichText.decorate(rolled) if rolled != "" else RichText.preview(e.text, main.data)
var muted := Style.MUTED.to_html(false)
if table.kind == RollTable.KIND_LOOP:
var index := table.entries.find(e)
var cap: String = LOOP_ROWS[clampi(index, 0, 1)]
var plain_text := Formatter.strip_tags(e.text).strip_edges()
if plain_text == "" or plain_text == cap:
s = "[b]%s[/b]" % cap
else:
s = "[color=#%s]%s ·[/color] %s" % [muted, cap, s]
elif table.kind == RollTable.KIND_ROUTER:
var bad := e.when.strip_edges() != "" and Expr.check(e.when.strip_edges()) != ""
var label := RulesView.case_label(e, main.data, card.plain)
var cond := "[color=#%s][b]%s[/b][/color]" % [(Style.DANGER_INK if bad else Style.TRIM_DARK).to_html(false), RichText.plain(label)]
var plain_text := Formatter.strip_tags(e.text).strip_edges()
if plain_text == "" or plain_text == TableCardText.case_label(e) or plain_text == label or plain_text == "if …" or (plain_text == "Otherwise" and e.when.strip_edges() == ""):
s = cond
else:
s = "%s\n%s" % [cond, s]
elif table.kind == RollTable.KIND_CHECK:
var index := table.entries.find(e)
var nm := TableCardText.check_outlet_name(index)
var head := "[color=#%s][b]%s[/b][/color]" % [TableCardText.check_outlet_color(index).to_html(false), nm]
var own := Formatter.strip_tags(rolled if rolled != "" else e.text).strip_edges()
s = head if own == "" or own == nm else "%s · %s" % [head, s]
elif table.is_dynamic() and table.entries.find(e) == 0:
var own := Formatter.strip_tags(e.text).strip_edges()
if rolled != "":
s = "[color=#%s]Chosen ·[/color] [b]%s[/b]" % [muted, RichText.decorate(rolled)]
elif own == "" or own == Roller.CHOSEN:
s = "[i][color=#%s]Chosen: the picked option[/color][/i]" % muted
else:
s = "[color=#%s]Chosen ·[/color] %s" % [muted, s]
elif table.kind == RollTable.KIND_CHOICE and table.choice != "pick":
if rolled != "":
s = "[color=#%s]Answer ·[/color] [b]%s[/b]" % [muted, RichText.decorate(rolled)]
else:
s = "[i][color=#%s]Answer[/color][/i]" % muted
if e.details.strip_edges() != "":
s += RichText.info_mark()
return s
## A subtle brass chip summarising an entry's effects: "Luck −1 · Gold + 2D"
## in plain words, "@luck -= 1 · @gold += 2d6" as text. Its tooltip has the
## other form, every statement on its own line.
func _effects_chip(e: TableEntry) -> Control:
var plain := card.plain
var bad := not Effects.validate(TemplateKit.probe(e.effects)).is_empty()
var chip := PanelContainer.new()
chip.mouse_filter = Control.MOUSE_FILTER_PASS
chip.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
var bg := Color(Style.DANGER_INK, 0.1) if bad else Color(Style.ACCENT, 0.2)
var line := Color(Style.DANGER_INK, 0.4) if bad else Color(Style.ACCENT_DARK, 0.3)
chip.add_theme_stylebox_override("panel", Style.bordered(Style.box(bg, 5, [6, 0, 6, 1]), 1, line))
var summary := RulesView.effects_line(e.effects, card.main.data, card.table, plain)
# Cut long summaries by hand: a clipped Label would collapse to nothing.
summary = RulesView.cut(summary, PLAIN_CHIP_CHARS if plain else CHIP_CHARS)
var l := Style.make_label(summary, Style.body_bold(true) if plain else Style.body(true), 12, Style.DANGER_INK if bad else Style.TRIM_DARK)
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
chip.add_child(l)
chip.set_meta("effects_chip", summary)
if plain:
chip.tooltip_text = "\n".join(Effects.split(e.effects))
else:
chip.tooltip_text = "\n".join(Describe.effects(e.effects, card.main.data, card.table))
return chip
## A sheet card's one row: the rendered template in Play, its editor in Build.
func make_sheet_row(e: TableEntry, build: bool) -> Control:
var main := card.main
var row := PanelContainer.new()
row.mouse_filter = Control.MOUSE_FILTER_PASS
row.custom_minimum_size = Vector2(0, 36)
var v := TableCard.vbox(6)
row.add_child(v)
if build:
var te := TextEdit.new()
te.text = e.text
te.placeholder_text = "[b]Name[/b] {@name}\nHP {@hp} · Skills {@skill_*}"
te.wrap_mode = TextEdit.LINE_WRAPPING_BOUNDARY
te.custom_minimum_size = Vector2(0, 90)
te.scroll_fit_content_height = true
te.add_theme_font_override("font", Style.body(true))
te.add_theme_font_size_override("font_size", 14)
te.add_theme_color_override("font_color", Style.INK)
te.add_theme_color_override("font_placeholder_color", Style.MUTED)
te.add_theme_color_override("caret_color", Style.INK)
te.add_theme_color_override("selection_color", Color(Style.ACCENT, 0.55))
te.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 5, [7, 4, 7, 4]), 1, Style.RULE))
te.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 5, [7, 4, 7, 4]), 1, Style.TRIM))
te.tooltip_text = "The sheet's template: BBCode with {@var}, {@prefix_*} and {= expr}"
te.text_changed.connect(func() -> void:
e.text = te.text
e.page = ""
main.mark_dirty()
)
te.focus_entered.connect(func() -> void: card.select_entry(e.id))
v.add_child(te)
var tools := TableCard.vbox(2)
if e.effects.strip_edges() != "":
tools.add_child(_effects_chip(e))
var pv := Style.make_label("Preview on the spread in Play", Style.body_italic(true), 12, Style.MUTED)
pv.size_flags_vertical = Control.SIZE_SHRINK_CENTER
tools.add_child(pv)
v.add_child(tools)
card.edit_ui.hook_select(row, e.id)
sheet_label = null
else:
sheet_label = RichText.label("", 15, Style.INK, true)
sheet_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
RichText.hook_hover(sheet_label, main)
v.add_child(sheet_label)
row.mouse_entered.connect(func() -> void: main.details().show_entry(row, card.table.id, e.id))
register(e, row, null, null, 0)
return row
## The roll modifier's value now (RollRange.live_mod): {"known", "value"}.
func live_mod() -> Dictionary:
var main := card.main
return RollRange.live_mod(card.table, Roller.live_context(main.data, main.persistent_vars))
## Updates the chance labels for the modifier's current value.
func refresh_odds() -> void:
if _odds.is_empty():
return
var lm := live_mod()
for eid: String in _odds:
var e := card.table.find_entry(eid)
if e != null and is_instance_valid(_odds[eid]):
_set_odds(_odds[eid], e, lm)
func _set_odds(pct: Label, e: TableEntry, lm: Dictionary) -> void:
var t := card.table
var txt := TablePreview.chance_text(t, e, int(lm["value"])) if lm["known"] else ""
if pct.text != txt:
pct.text = txt
if t.has_mod():
pct.tooltip_text = "Chance with the modifier at %s" % Describe.signed_number(int(lm["value"])) if lm["known"] \
else "The chance depends on the modifier"
## Re-renders a sheet card's template from the current variables.
func refresh_sheet() -> void:
if is_instance_valid(sheet_label):
var main := card.main
var s := RichText.decorate(Roller.render_sheet(main.data, main.persistent_vars, card.table))
if sheet_label.text != s:
sheet_label.text = s
func make_more_row(numw: float) -> PanelContainer:
var more_row := PanelContainer.new()
var normal := Style.box(Color(0, 0, 0, 0), 0, [12, 6, 22, 6])
var hover := Style.box(Style.HOVER_ROW, 0, [12, 6, 22, 6])
more_row.add_theme_stylebox_override("panel", normal)
more_row.custom_minimum_size = Vector2(0, 36)
more_row.mouse_filter = Control.MOUSE_FILTER_STOP
more_row.mouse_default_cursor_shape = Control.CURSOR_POINTING_HAND
more_row.tooltip_text = "Show every result"
more_row.mouse_entered.connect(func() -> void: more_row.add_theme_stylebox_override("panel", hover))
more_row.mouse_exited.connect(func() -> void: more_row.add_theme_stylebox_override("panel", normal))
more_row.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.button_index == MOUSE_BUTTON_LEFT and not mb.pressed:
card.main.toggle_collapse.call_deferred(card.table.id)
)
var h := TableCard.hbox(8)
more_row.add_child(h)
var dots := Style.make_label("…", Style.display(true), 16, Style.MUTED)
dots.custom_minimum_size = Vector2(numw, 0)
dots.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
h.add_child(dots)
more_label = Style.make_label(more_text(), Style.body_italic(true), 14, Style.MUTED)
more_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
more_label.size_flags_vertical = Control.SIZE_SHRINK_CENTER
h.add_child(more_label)
return more_row
func more_text() -> String:
var table := card.table
var hidden := table.entries.size() - card.rows.size()
if card.rows.size() > 0:
return "%d more · show all" % hidden
return "%d %s · show all" % [table.entries.size(), "ranges" if table.is_ranged() else "results"]
# ---------- State ----------
func set_row_state(eid: String, state: RowState) -> void:
if not card.rows.has(eid):
return
var p: Dictionary = parts[eid]
var k := int(state) + (3 if eid == card.selected_entry() else 0)
if int(p["index"]) % 2 == 1 and card.main.local_state.striped_rows():
k += 6
(card.rows[eid] as PanelContainer).add_theme_stylebox_override("panel", _row_styles[k])
card.edit_ui.sync_delete(eid)
var num: Label = p["num"]
if num:
num.add_theme_color_override("font_color", Style.INK if state == RowState.LIT else Style.MUTED)
var text: RichTextLabel = p["text"]
if text:
RichText.set_fonts(text, 16, true, state == RowState.LIT)
## Sets a row lit or normal, from whether its result is lit now.
func restate(eid: String) -> void:
set_row_state(eid, RowState.LIT if card.main.is_entry_lit(card.table.id, eid) else RowState.NORMAL)
## Re-renders a Play-mode row's text (it shows the rolled text while lit).
func update_row_text(eid: String) -> void:
var p: Dictionary = parts.get(eid, {})
var text: RichTextLabel = p.get("text")
if text:
var want := row_text(p["entry"])
if text.text != want:
text.text = want
func flick_random() -> void:
clear_flick()
if card.rows.is_empty():
return
var keys := card.rows.keys()
_flick_id = keys[card.main.rng.randi_range(0, keys.size() - 1)]
set_row_state(_flick_id, RowState.FLICK)
func clear_flick() -> void:
if _flick_id != "":
restate(_flick_id)
_flick_id = ""