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 var kind_rows: TableCardKindRows var columns_ui: TableCardColumns ## 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 kind_rows = TableCardKindRows.new(self) columns_ui = TableCardColumns.new(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() columns_ui.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) columns_ui.add_cells(h, e, index, true) 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) columns_ui.add_cells(h, e, index, false) 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.image != "": var pic := TextureRect.new() pic.texture = main.data.picture(e.image) pic.custom_minimum_size = Vector2(30, 30) pic.expand_mode = TextureRect.EXPAND_IGNORE_SIZE pic.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED pic.size_flags_vertical = Control.SIZE_SHRINK_CENTER pic.mouse_filter = Control.MOUSE_FILTER_PASS h.add_child(pic) 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(kind_rows.effects_chip(e)) ## 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) if columns_ui.active(): # The lit text is "Resources: Rich · Water: …"; this row shows column 0. rolled = str(columns_ui.rolled_cell(table.entries.find(e), 0).get("text", "")) 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 ## 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 columns_ui.refresh(eid) 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 = "" ## Check outlets and sheet rows live in TableCardKindRows. func make_check_row(e: TableEntry, index: int, build: bool, numw: float) -> Control: return kind_rows.make_check_row(e, index, build, numw) func make_sheet_row(e: TableEntry, build: bool) -> Control: return kind_rows.make_sheet_row(e, build)