- ChoiceDialog: card-styled pop-up for pick (option rows with details, Random,
1-9 / R keys), number (spin box with min/max/default, Enter) and text.
Cancel, Esc, freeing the dialog, a new roll or a collection switch all
answer null through a separate Reply object, so a roll never hangs.
- Main: real prompt hook; the steps so far light on the board and in the
tray before each question; a roll with questions shows the rest at once;
a new roll cancels an open question cleanly; refresh_live() re-renders
variable, sheet and question text only after rolls and edits.
- TableCard: "?" badge and question for choice cards, loop icon, rule chip
and Each time / Afterwards rows, compact live variable cards (reset in
Build), live sheet cards with one outlet, expression-dice subtitles,
effect chips in Play and a validated effects editor in Build.
- Build: "+ Table" menu for every kind; kind-specific settings (choice type,
question, save as, min/max; loop mode and rule; variable name, type,
starting value; sheet template) checked with Expr.check; expression pipe
counts in the pipe popover and on pipe labels.
- Tray: effect lines under chips, last 12 steps behind "+N earlier steps",
sheet headline, Cancelled / action-limit pill, capped indents and Earlier
rows; Variables tab lists every board variable with Reset all.
- RichText: error markers in red; previews show {@x_*} and {= expr}.
- examples/logic_demo.json and tests/test_logic_ui.gd.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
243 lines
7.9 KiB
GDScript
243 lines
7.9 KiB
GDScript
class_name ChoiceDialog
|
||
extends ModalCard
|
||
## The pop-up a choice card opens mid-roll (docs/LOGIC.md §2, §8): the card's
|
||
## question and either its options (pick), a number field (number) or a text
|
||
## field (text), with Cancel.
|
||
##
|
||
## Keys: 1–9 pick an option, R picks Random, Enter confirms, Esc cancels.
|
||
##
|
||
## The answer arrives through `reply.done`, a signal on a separate object so
|
||
## the waiting roll never hangs: closing or freeing the dialog in any way
|
||
## (Esc, the window closing, a new roll replacing this one) answers null,
|
||
## which is Cancel.
|
||
|
||
## Holds the answer signal. It outlives the dialog, so an awaiting roll
|
||
## resumes even when the dialog is freed.
|
||
class Reply extends RefCounted:
|
||
signal done(value: Variant)
|
||
var finished := false
|
||
|
||
func finish(value: Variant) -> void:
|
||
if finished:
|
||
return
|
||
finished = true
|
||
done.emit(value)
|
||
|
||
|
||
var request: Dictionary
|
||
var reply := Reply.new()
|
||
var kind := "pick"
|
||
|
||
var _spin: SpinBox
|
||
var _text: LineEdit
|
||
var _options: Array[Control] = []
|
||
var _range_note: Label
|
||
var _typed := false
|
||
|
||
|
||
func _init(p_request: Dictionary = {}) -> void:
|
||
request = p_request
|
||
kind = str(request.get("kind", "pick"))
|
||
var title := Formatter.strip_tags(str(request.get("title", ""))).strip_edges()
|
||
super._init(title if title != "" else "A question", 500.0)
|
||
# A stray click on the board behind shouldn't throw the answer away.
|
||
close_on_backdrop = false
|
||
# A lighter dim, so the steps that led here stay readable behind the card.
|
||
var dim := get_child(0) as ColorRect
|
||
if dim:
|
||
dim.color = Color(0, 0, 0, 0.32)
|
||
_build()
|
||
|
||
|
||
func _build() -> void:
|
||
var prompt := str(request.get("prompt", "")).strip_edges()
|
||
if prompt != "":
|
||
var p := RichText.label(RichText.decorate(prompt), 18, Style.INK, false)
|
||
p.size_flags_horizontal = SIZE_EXPAND_FILL
|
||
body.add_child(p)
|
||
match kind:
|
||
"number":
|
||
_build_number()
|
||
"text":
|
||
_build_text()
|
||
_:
|
||
_build_pick()
|
||
|
||
|
||
func _build_pick() -> void:
|
||
var list := VBoxContainer.new()
|
||
list.add_theme_constant_override("separation", 6)
|
||
body.add_child(list)
|
||
var opts: Array = request.get("options", [])
|
||
for k in opts.size():
|
||
var o: Dictionary = opts[k]
|
||
var row := _option_row(k, o)
|
||
list.add_child(row)
|
||
_options.append(row)
|
||
var hint := note("Press 1–%d to choose, R for Random, Esc to cancel." % mini(9, maxi(1, opts.size())))
|
||
hint.add_theme_font_size_override("font_size", 13)
|
||
body.add_child(hint)
|
||
add_button("Cancel", "plain", func() -> void: answer(null))
|
||
var r := add_button("Random", "primary", func() -> void: answer(-1))
|
||
r.tooltip_text = "Roll this card normally instead of choosing (R)"
|
||
|
||
|
||
## One option: number key, text and details as a subtitle; the whole row is a
|
||
## button.
|
||
func _option_row(k: int, o: Dictionary) -> Control:
|
||
var b := Button.new()
|
||
b.focus_mode = Control.FOCUS_ALL
|
||
b.size_flags_horizontal = SIZE_EXPAND_FILL
|
||
var m := [14, 8, 14, 9]
|
||
var n := Style.bordered(Style.box(Color.WHITE, 9, m), 1, Style.RULE)
|
||
var h := Style.bordered(Style.box(Style.LIT, 9, m), 1, Style.BRASS_DARK)
|
||
Style.skin_button(b, n, h, h, Style.INK)
|
||
b.add_theme_stylebox_override("focus", Style.focus_ring(9))
|
||
var row := HBoxContainer.new()
|
||
row.add_theme_constant_override("separation", 12)
|
||
row.mouse_filter = MOUSE_FILTER_IGNORE
|
||
row.set_anchors_and_offsets_preset(PRESET_FULL_RECT)
|
||
row.offset_left = 14
|
||
row.offset_right = -14
|
||
row.offset_top = 8
|
||
row.offset_bottom = -9
|
||
var key := Style.make_label(str(k + 1) if k < 9 else "", Style.display(), 20, Style.COPPER)
|
||
key.custom_minimum_size = Vector2(16, 0)
|
||
key.size_flags_vertical = SIZE_SHRINK_CENTER
|
||
key.mouse_filter = MOUSE_FILTER_IGNORE
|
||
row.add_child(key)
|
||
var col := VBoxContainer.new()
|
||
col.add_theme_constant_override("separation", 1)
|
||
col.size_flags_horizontal = SIZE_EXPAND_FILL
|
||
col.mouse_filter = MOUSE_FILTER_IGNORE
|
||
row.add_child(col)
|
||
var text := RichText.label(RichText.decorate(str(o.get("text", ""))), 17, Style.INK, false, true, true)
|
||
text.mouse_filter = MOUSE_FILTER_IGNORE
|
||
col.add_child(text)
|
||
var det := Formatter.strip_tags(str(o.get("details", ""))).strip_edges()
|
||
if det != "":
|
||
var sub := Style.make_label(det, Style.body(), 13, Style.MUTED)
|
||
sub.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
|
||
sub.max_lines_visible = 2
|
||
sub.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
|
||
sub.mouse_filter = MOUSE_FILTER_IGNORE
|
||
col.add_child(sub)
|
||
b.tooltip_text = det
|
||
b.add_child(row)
|
||
# Buttons don't size to their children, so follow the row's height.
|
||
var fit := func() -> void: b.custom_minimum_size.y = row.get_combined_minimum_size().y + 17.0
|
||
row.minimum_size_changed.connect(fit)
|
||
fit.call_deferred()
|
||
var index := int(o.get("index", k))
|
||
b.pressed.connect(func() -> void: answer(index))
|
||
return b
|
||
|
||
|
||
func _build_number() -> void:
|
||
var lo := int(request.get("min", 0))
|
||
var hi := maxi(lo, int(request.get("max", 99)))
|
||
var def: Variant = request.get("default", lo)
|
||
_spin = SpinBox.new()
|
||
_spin.min_value = lo
|
||
_spin.max_value = hi
|
||
_spin.step = 1
|
||
_spin.rounded = true
|
||
_spin.value = clampi(int(def) if (def is int or def is float) else lo, lo, hi)
|
||
_spin.custom_minimum_size = Vector2(150, 46)
|
||
_spin.size_flags_horizontal = SIZE_SHRINK_BEGIN
|
||
var le := _spin.get_line_edit()
|
||
style_line_edit(le)
|
||
le.add_theme_font_override("font", Style.body_bold())
|
||
le.add_theme_font_size_override("font_size", 22)
|
||
le.add_theme_color_override("font_selected_color", Style.INK)
|
||
_spin.add_theme_color_override("up_icon_modulate", Style.MUTED)
|
||
_spin.add_theme_color_override("down_icon_modulate", Style.MUTED)
|
||
# Typed text is applied on confirm; the arrows (or code) set the value directly.
|
||
le.text_changed.connect(func(_t: String) -> void: _typed = true)
|
||
_spin.value_changed.connect(func(_v: float) -> void: _typed = false)
|
||
le.text_submitted.connect(func(_t: String) -> void: _confirm())
|
||
var row := HBoxContainer.new()
|
||
row.add_theme_constant_override("separation", 12)
|
||
row.add_child(_spin)
|
||
_range_note = note("A whole number from %d to %d." % [lo, hi])
|
||
_range_note.size_flags_vertical = SIZE_SHRINK_CENTER
|
||
row.add_child(_range_note)
|
||
body.add_child(row)
|
||
add_button("Cancel", "plain", func() -> void: answer(null))
|
||
add_button("OK", "primary", _confirm)
|
||
|
||
|
||
func _build_text() -> void:
|
||
_text = LineEdit.new()
|
||
_text.placeholder_text = "Type your answer"
|
||
_text.text = str(request.get("default", ""))
|
||
style_line_edit(_text)
|
||
_text.add_theme_font_size_override("font_size", 18)
|
||
_text.text_submitted.connect(func(_t: String) -> void: _confirm())
|
||
body.add_child(_text)
|
||
add_button("Cancel", "plain", func() -> void: answer(null))
|
||
add_button("OK", "primary", _confirm)
|
||
|
||
|
||
func _ready() -> void:
|
||
super._ready()
|
||
if _spin:
|
||
_spin.get_line_edit().grab_focus()
|
||
_spin.get_line_edit().select_all()
|
||
elif _text:
|
||
_text.grab_focus()
|
||
elif not _options.is_empty():
|
||
_options[0].grab_focus()
|
||
|
||
|
||
## Enter on the number or text field, or OK.
|
||
func _confirm() -> void:
|
||
if _spin:
|
||
if _typed:
|
||
_spin.apply()
|
||
answer(int(_spin.value))
|
||
elif _text:
|
||
answer(_text.text)
|
||
|
||
|
||
## Gives the answer (null = Cancel) and closes. Only the first answer counts.
|
||
func answer(value: Variant) -> void:
|
||
reply.finish(value)
|
||
if is_inside_tree() and not is_queued_for_deletion():
|
||
super.close()
|
||
|
||
|
||
## Esc and every other way of closing answer Cancel.
|
||
func close() -> void:
|
||
answer(null)
|
||
|
||
|
||
func _input(event: InputEvent) -> void:
|
||
if not is_inside_tree():
|
||
return
|
||
var k := event as InputEventKey
|
||
if k and k.pressed and not k.echo and kind == "pick":
|
||
var code := k.keycode
|
||
if code >= KEY_1 and code <= KEY_9:
|
||
var i := code - KEY_1
|
||
if i < _options.size():
|
||
get_viewport().set_input_as_handled()
|
||
_options[i].emit_signal("pressed")
|
||
return
|
||
if code >= KEY_KP_1 and code <= KEY_KP_9:
|
||
var j := code - KEY_KP_1
|
||
if j < _options.size():
|
||
get_viewport().set_input_as_handled()
|
||
_options[j].emit_signal("pressed")
|
||
return
|
||
if code == KEY_R:
|
||
get_viewport().set_input_as_handled()
|
||
answer(-1)
|
||
return
|
||
super._input(event)
|
||
|
||
|
||
func _notification(what: int) -> void:
|
||
# Freed or taken off the screen without an answer: the roll gets Cancel.
|
||
if what == NOTIFICATION_EXIT_TREE or what == NOTIFICATION_PREDELETE:
|
||
reply.finish(null)
|