- Card threads: a port on each card's bottom edge. Plain runs after any result (once the result's own threads finish); Otherwise runs only when no thread of the result ran. Saved as card_threads (LOGIC.md 3g). - Typing on long cards no longer rebuilds the inspector or the card per key (~175 ms -> ~7 ms). Timing tool: tests/perf_typing.gd. - "+ Add result" scrolls to the new row and focuses it. - Mouse wheel zooms; Shift+wheel pans sideways; scrolling lists keep it. - "SPREAD" caption over the switcher; Morgan's first-run welcome. - docs/MECHANICS.md: checklist of table mechanics for planning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
341 lines
14 KiB
GDScript
341 lines
14 KiB
GDScript
class_name TableCardEditing
|
||
extends RefCounted
|
||
## A TableCard's Build-mode controls: the inline result-text, weight and range
|
||
## fields, router case rows with move and delete, the footer (add a result,
|
||
## option or case), the card's ⋯ menu (settings, template, collapse, delete)
|
||
## and the row × that shows only on hover or selection. Everything else is
|
||
## edited in Main's inspector.
|
||
|
||
|
||
var card: TableCard
|
||
## Build mode: each result's inline text field by result id (refilled on rebuild).
|
||
var text_fields: Dictionary = {}
|
||
## True while "+ Add result" rebuilds the card: it scrolls to the new (last)
|
||
## row instead of the last-rolled one (TableCardRebuild).
|
||
var adding := false
|
||
|
||
|
||
func _init(p_card: TableCard) -> void:
|
||
card = p_card
|
||
|
||
|
||
## Build mode: the inline result-text field of a row. Focusing it selects the
|
||
## result for the inspector.
|
||
func text_edit(e: TableEntry, index: int, chosen: bool) -> LineEdit:
|
||
var table := card.table
|
||
var edit := LineEdit.new()
|
||
edit.text = e.text
|
||
FormatMenu.attach(edit)
|
||
if table.kind != RollTable.KIND_LOOP:
|
||
SpellUnderline.attach(edit, card.main)
|
||
if chosen:
|
||
edit.placeholder_text = "(the option's name)"
|
||
edit.tooltip_text = "The result text. Leave it as Chosen (or empty) to show the picked option's name; {option_label}, {option_value} and {@[option]} work"
|
||
edit.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
edit.custom_minimum_size = Vector2(40, 0)
|
||
edit.add_theme_font_size_override("font_size", 15)
|
||
var loop_rows := TableCardRows.LOOP_ROWS
|
||
if table.kind == RollTable.KIND_LOOP:
|
||
edit.placeholder_text = loop_rows[clampi(index, 0, 1)]
|
||
edit.tooltip_text = "%s: shown each pass ({i} is the pass number)" % loop_rows[0] if index == 0 else "%s: runs once when the repeat ends" % loop_rows[1]
|
||
elif table.kind == RollTable.KIND_ROUTER:
|
||
edit.placeholder_text = "Result text"
|
||
elif not chosen:
|
||
edit.placeholder_text = "(nothing)"
|
||
edit.text_changed.connect(func(s: String) -> void:
|
||
e.text = s
|
||
e.page = ""
|
||
card.main.mark_dirty()
|
||
)
|
||
edit.focus_entered.connect(func() -> void: card.select_entry(e.id))
|
||
text_fields[e.id] = edit
|
||
return edit
|
||
|
||
|
||
## After "+ Add result": scrolls the list to the new row and puts the cursor
|
||
## in its text field, with the starter text selected so typing replaces it.
|
||
func focus_new(eid: String) -> void:
|
||
await card.get_tree().process_frame
|
||
if not is_instance_valid(card):
|
||
return
|
||
var edit: LineEdit = text_fields.get(eid)
|
||
if is_instance_valid(edit):
|
||
edit.grab_focus()
|
||
edit.select_all()
|
||
|
||
|
||
## Puts a result's text into its inline field without a rebuild. False when
|
||
## the card has no such field (Play mode, or a kind without one).
|
||
func sync_text(eid: String) -> bool:
|
||
var edit: LineEdit = text_fields.get(eid)
|
||
var e := card.table.find_entry(eid)
|
||
if not is_instance_valid(edit) or e == null:
|
||
return false
|
||
if edit.text != e.text:
|
||
var caret := edit.caret_column
|
||
edit.text = e.text
|
||
edit.caret_column = caret
|
||
var spell: Variant = edit.get_meta("spell", null)
|
||
if spell is SpellUnderline:
|
||
(spell as SpellUnderline).recheck()
|
||
return true
|
||
|
||
|
||
## Build mode: a click on a row (outside its fields) selects the result for
|
||
## the inspector.
|
||
func hook_select(row: Control, eid: String) -> void:
|
||
row.gui_input.connect(func(ev: InputEvent) -> void:
|
||
var mb := ev as InputEventMouseButton
|
||
if mb and mb.button_index == MOUSE_BUTTON_LEFT and mb.pressed:
|
||
card.select_entry(eid)
|
||
)
|
||
|
||
|
||
## Build mode: one router case. The first line is its condition, in plain
|
||
## words or as written (edited in the inspector), with move up / down and
|
||
## delete; the second is its result text, then its effects chip.
|
||
func make_case_row(e: TableEntry, index: int, numw: float) -> Control:
|
||
var table := card.table
|
||
var main := card.main
|
||
var plain := card.plain
|
||
var row := PanelContainer.new()
|
||
row.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
row.custom_minimum_size = Vector2(0, 36)
|
||
var outer := TableCard.hbox(4)
|
||
row.add_child(outer)
|
||
var num := Style.make_label(str(index + 1), 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_BEGIN
|
||
outer.add_child(num)
|
||
var v := TableCard.vbox(3)
|
||
v.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
outer.add_child(v)
|
||
|
||
var top := TableCard.hbox(4)
|
||
v.add_child(top)
|
||
var w := e.when.strip_edges()
|
||
var bad := w != "" and Expr.check(TemplateKit.probe(w)) != ""
|
||
var cond := Style.make_label(RulesView.case_label(e, main.data, plain), Style.body_bold(true), 14, Style.DANGER_INK if bad else Style.TRIM_DARK)
|
||
cond.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
cond.size_flags_horizontal = Control.SIZE_EXPAND_FILL
|
||
cond.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
cond.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
cond.tooltip_text = ("⚠ " + Expr.check(TemplateKit.probe(w))) if bad else (("if " + w) if plain and w != "" else "Cases are checked top to bottom; the first true one is chosen")
|
||
cond.set_meta("case_line", true)
|
||
top.add_child(cond)
|
||
top.add_child(mini_button("↑", "Move this case up", index > 0, func() -> void: _move_case(index, -1)))
|
||
top.add_child(mini_button("↓", "Move this case down", index < table.entries.size() - 1, func() -> void: _move_case(index, 1)))
|
||
var del := mini_button("×", "Delete this case", true, func() -> void: main.delete_entry(table.id, e.id))
|
||
top.add_child(del)
|
||
|
||
v.add_child(text_edit(e, index, false))
|
||
card.rows_ui.add_rule_chips(v, e)
|
||
hook_select(row, e.id)
|
||
card.rows_ui.register(e, row, num, null, index)
|
||
reveal_on_hover(e.id, row, del)
|
||
return row
|
||
|
||
|
||
static func mini_button(glyph: String, tip: String, enabled: bool, on_press: Callable) -> Button:
|
||
var b := Button.new()
|
||
b.text = glyph
|
||
b.tooltip_text = tip
|
||
b.disabled = not enabled
|
||
b.custom_minimum_size = Vector2(22, 22)
|
||
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
b.add_theme_font_size_override("font_size", 16 if glyph == "×" else 13)
|
||
Style.skin_button(b, Style.box(Color(0, 0, 0, 0), 5), Style.box(Style.CARD_HOVER, 5), Style.box(Style.CARD_HOVER, 5), Style.MUTED, Style.DANGER_INK if glyph == "×" else Style.INK)
|
||
b.add_theme_color_override("font_disabled_color", Color(Style.MUTED, 0.35))
|
||
b.pressed.connect(on_press)
|
||
return b
|
||
|
||
|
||
## Moves router case `index` up (-1) or down (+1).
|
||
func _move_case(index: int, by: int) -> void:
|
||
move_entry(index, index + by)
|
||
|
||
|
||
## Moves the result at `from` to `to` (an index in the list without it). On a
|
||
## card with number ranges the ranges stay where they were, so the results
|
||
## move through them and the numbers stay in order.
|
||
func move_entry(from: int, to: int) -> void:
|
||
var entries := card.table.entries
|
||
if from == to or from < 0 or from >= entries.size() or to < 0 or to >= entries.size():
|
||
return
|
||
# A row moves whole, with its range or weight; Weights then lay the ranges
|
||
# out again, and auto-sort may put a Ranges card back in roll order.
|
||
var moved := entries[from]
|
||
entries.remove_at(from)
|
||
entries.insert(to, moved)
|
||
if RangeWeights.after_edit(card.table):
|
||
card.main.toast("Auto-sort put the rows back in roll order (turn it off in the card's settings)")
|
||
card.rebuild()
|
||
card.main.mark_dirty()
|
||
|
||
|
||
## Adds a router case just before the Otherwise case (or at the end). It
|
||
## starts as "if false", so it never matches until its condition is written.
|
||
func add_case() -> void:
|
||
var table := card.table
|
||
var e := TableEntry.new(TowerBoard.new_id("e"), "New case")
|
||
e.when = "false"
|
||
var at := table.otherwise_index()
|
||
if at < 0 or at != table.entries.size() - 1:
|
||
at = table.entries.size()
|
||
table.entries.insert(at, e)
|
||
table.collapsed = false
|
||
card.rebuild()
|
||
card.main.mark_dirty()
|
||
|
||
|
||
## Weight on a plain table, shown as ×N. Also the number of copies in card mode.
|
||
## The weight box. `faces`: a Weights-mode ranged card, where the weight is
|
||
## how many die faces the result covers (shown plain, in a range-style box).
|
||
func weight_edit(e: TableEntry, faces: bool = false) -> LineEdit:
|
||
var edit := LineEdit.new()
|
||
var shown := func(w: int) -> String: return str(w) if faces else "×%d" % w
|
||
edit.text = shown.call(e.weight)
|
||
edit.custom_minimum_size = Vector2(40 if faces else 34, 0)
|
||
edit.alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
edit.max_length = 4
|
||
edit.tooltip_text = "Weight: how many die faces this result covers (0 = never). The ranges follow the row order." if faces \
|
||
else "Weight: how likely this result is, relative to the others (0 = never)"
|
||
edit.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||
edit.add_theme_font_override("font", Style.display(true))
|
||
edit.add_theme_font_size_override("font_size", 15 if faces else 14)
|
||
if faces:
|
||
edit.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.RULE))
|
||
edit.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.TRIM))
|
||
else:
|
||
edit.add_theme_color_override("font_color", Style.MUTED)
|
||
var commit := func() -> void:
|
||
var digits := ""
|
||
for ch in edit.text:
|
||
if ch >= "0" and ch <= "9":
|
||
digits += ch
|
||
var w := clampi(digits.to_int(), 0, 9999 if faces else 999) if digits != "" else e.weight
|
||
edit.text = shown.call(w)
|
||
if w != e.weight:
|
||
e.weight = w
|
||
card.update_deck()
|
||
card.main.mark_dirty()
|
||
if RangeWeights.uses_weights(card.table):
|
||
RangeWeights.reflow(card.table)
|
||
card.rebuild.call_deferred()
|
||
edit.text_submitted.connect(func(_t: String) -> void: commit.call())
|
||
edit.focus_exited.connect(commit)
|
||
return edit
|
||
|
||
|
||
## Largest range value the card's range boxes take, either way.
|
||
const RANGE_LIMIT := 99999
|
||
|
||
|
||
func range_edit(e: TableEntry, is_lo: bool) -> LineEdit:
|
||
var table := card.table
|
||
var edit := LineEdit.new()
|
||
edit.text = str(e.range_lo if is_lo else e.range_hi)
|
||
edit.custom_minimum_size = Vector2(40, 0)
|
||
edit.alignment = HORIZONTAL_ALIGNMENT_CENTER
|
||
edit.max_length = 6
|
||
edit.tooltip_text = ("Range start" if is_lo else "Range end") + "\nValues past the dice (such as -3 or 24) need a modifier to come up"
|
||
edit.add_theme_font_override("font", Style.display(true))
|
||
edit.add_theme_font_size_override("font_size", 15)
|
||
edit.add_theme_stylebox_override("normal", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.RULE))
|
||
edit.add_theme_stylebox_override("focus", Style.bordered(Style.box(Color.WHITE, 4, [2, 2, 2, 2]), 1, Style.TRIM))
|
||
var commit := func() -> void:
|
||
# Any whole number: results past the dice are reached with a modifier.
|
||
var typed := edit.text.strip_edges()
|
||
var v := clampi(typed.to_int(), -RANGE_LIMIT, RANGE_LIMIT) if typed.is_valid_int() else (e.range_lo if is_lo else e.range_hi)
|
||
if is_lo:
|
||
e.range_lo = v
|
||
else:
|
||
e.range_hi = v
|
||
edit.text = str(e.range_lo if is_lo else e.range_hi)
|
||
card.main.mark_dirty()
|
||
if RangeWeights.after_edit(table):
|
||
card.rebuild.call_deferred()
|
||
else:
|
||
card.update_warning()
|
||
edit.text_submitted.connect(func(_t: String) -> void: commit.call())
|
||
edit.focus_exited.connect(commit)
|
||
return edit
|
||
|
||
|
||
func make_footer(ranged: bool) -> Control:
|
||
var table := card.table
|
||
var main := card.main
|
||
var wrap := MarginContainer.new()
|
||
wrap.mouse_filter = Control.MOUSE_FILTER_PASS
|
||
wrap.add_theme_constant_override("margin_left", 12)
|
||
wrap.add_theme_constant_override("margin_right", 10)
|
||
wrap.add_theme_constant_override("margin_bottom", 10)
|
||
var h := TableCard.hbox(8)
|
||
wrap.add_child(h)
|
||
# Loops, sheets, variables and number/text choices have fixed rows.
|
||
if table.kind == RollTable.KIND_ROLL or (table.kind == RollTable.KIND_CHOICE and table.choice == "pick"):
|
||
var label := "+ Add range" if ranged and not RangeWeights.uses_weights(table) else ("+ Add option" if table.kind == RollTable.KIND_CHOICE else "+ Add result")
|
||
var add := footer_button(label, Style.INK)
|
||
add.pressed.connect(func() -> void:
|
||
# The rebuild inside add_entry scrolls to the new row, not the lit one.
|
||
adding = true
|
||
main.add_entry(table.id)
|
||
adding = false
|
||
if not table.entries.is_empty():
|
||
focus_new(table.entries[-1].id))
|
||
h.add_child(add)
|
||
elif table.kind == RollTable.KIND_ROUTER:
|
||
var addc := footer_button("+ Add case", Style.INK)
|
||
addc.tooltip_text = "Add a case before Otherwise"
|
||
addc.pressed.connect(add_case)
|
||
h.add_child(addc)
|
||
# Deleting the table lives in the card's ⋯ menu.
|
||
return wrap
|
||
|
||
|
||
static func footer_button(text: String, hover_color: Color) -> Button:
|
||
var b := Button.new()
|
||
b.text = text
|
||
b.add_theme_font_size_override("font_size", 14)
|
||
var normal := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, Style.DASH)
|
||
var hover := Style.bordered(Style.box(Color(0, 0, 0, 0), 6, [10, 4, 10, 4]), 1, hover_color if hover_color != Style.INK else Style.MUTED)
|
||
Style.skin_button(b, normal, hover, hover, Style.MUTED, hover_color)
|
||
return b
|
||
|
||
|
||
## Build mode's "⋯" menu on the card: settings, save as template, collapse
|
||
## and delete (the same pattern as a group card's ⋯ menu).
|
||
func make_card_menu() -> MenuButton:
|
||
var mb := Style.overflow_button("Table actions")
|
||
var pm := mb.get_popup()
|
||
Actions.fill(pm, Actions.card(card.main, card.table))
|
||
pm.about_to_popup.connect(func() -> void: Actions.fill(pm, Actions.card(card.main, card.table)))
|
||
return mb
|
||
|
||
|
||
## Build mode: a row's delete × shows only while the row is hovered, the ×
|
||
## has keyboard focus, or the row is the inspector's selected result.
|
||
func reveal_on_hover(eid: String, row: Control, button: Button) -> void:
|
||
card.rows_ui.parts[eid]["delete"] = button
|
||
row.mouse_entered.connect(func() -> void:
|
||
row.set_meta("hovered", true)
|
||
sync_delete(eid))
|
||
row.mouse_exited.connect(func() -> void:
|
||
# Moving onto a child control still counts as inside the row.
|
||
row.set_meta("hovered", row.get_global_rect().has_point(row.get_global_mouse_position()))
|
||
sync_delete(eid))
|
||
button.focus_entered.connect(sync_delete.bind(eid))
|
||
button.focus_exited.connect(sync_delete.bind(eid))
|
||
sync_delete(eid)
|
||
|
||
|
||
## Shows or hides a row's ×; TableCardRows.set_row_state calls it too.
|
||
func sync_delete(eid: String) -> void:
|
||
var b: Button = card.rows_ui.parts.get(eid, {}).get("delete")
|
||
if b == null or not is_instance_valid(b):
|
||
return
|
||
var row: Control = card.rows.get(eid)
|
||
var hovered := row != null and bool(row.get_meta("hovered", false))
|
||
b.modulate.a = 1.0 if hovered or b.has_focus() or eid == card.selected_entry() else 0.0
|