2026-11-10 19:10:11 -06:00
|
|
|
class_name RollPauses
|
|
|
|
|
extends RefCounted
|
|
|
|
|
## Paused rolls (threads set to wait, Roller.OP_PAUSE). A roll that reaches
|
|
|
|
|
## one waits with "Continue ▸" and "Stop here" in the tray. Starting another
|
|
|
|
|
## roll sets the paused one aside, and its chip stays; Continue makes it the
|
|
|
|
|
## current roll again and carries on from where it stopped.
|
|
|
|
|
|
|
|
|
|
class Waiter:
|
|
|
|
|
extends RefCounted
|
|
|
|
|
signal done(reply: Variant)
|
|
|
|
|
|
|
|
|
|
var m: Main
|
|
|
|
|
## {run, waiter, label, current, ctx}, oldest first.
|
|
|
|
|
var waiting: Array[Dictionary] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func _init(p_main: Main) -> void:
|
|
|
|
|
m = p_main
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## The prompt hook's answer to a "pause" request for roll `run`.
|
|
|
|
|
func wait(req: Dictionary, run: Dictionary) -> Variant:
|
|
|
|
|
var w := Waiter.new()
|
2026-11-14 14:23:07 -06:00
|
|
|
var cost := str(req.get("cost", ""))
|
2026-11-10 19:10:11 -06:00
|
|
|
var label := "%s: before %s" % [Formatter.strip_tags(str(m.current.get("root_name", ""))), Formatter.strip_tags(str(req.get("title", "")))]
|
2026-11-14 14:23:07 -06:00
|
|
|
if cost != "":
|
|
|
|
|
label = str(req.get("prompt", label))
|
|
|
|
|
var item := {"run": run, "waiter": w, "label": label, "current": m.current, "ctx": m.rolling_ctx, "cost": cost != ""}
|
|
|
|
|
waiting.append(item)
|
2026-11-10 19:10:11 -06:00
|
|
|
m.tray.render()
|
2026-11-14 14:23:07 -06:00
|
|
|
item["dialog"] = _popup(item)
|
2026-11-10 19:10:11 -06:00
|
|
|
var reply: Variant = await w.done
|
|
|
|
|
return reply
|
|
|
|
|
|
|
|
|
|
|
2026-11-14 14:23:07 -06:00
|
|
|
## The pop-up for a waiting roll: Continue / Stop here (Pay / Don't pay for a
|
|
|
|
|
## cost), or Later, which leaves it waiting in the tray's strip.
|
|
|
|
|
func _popup(item: Dictionary) -> ModalCard:
|
|
|
|
|
var dialog := ModalCard.new("Pay to roll?" if item["cost"] else "Paused", 420.0)
|
|
|
|
|
dialog.body.add_child(ModalCard.note(str(item["label"]) if item["cost"] else "The roll is waiting %s." % str(item["label"]).get_slice(": ", 1), Style.INK))
|
|
|
|
|
var reply := func(go_on: bool) -> void:
|
|
|
|
|
var at := waiting.find(item)
|
|
|
|
|
if at >= 0:
|
|
|
|
|
answer(at, go_on)
|
|
|
|
|
dialog.add_button("Later", "plain", dialog.close)
|
|
|
|
|
dialog.add_button("Don't pay" if item["cost"] else "Stop here", "plain", reply.bind(false))
|
|
|
|
|
dialog.add_button("Pay ▸" if item["cost"] else "Continue ▸", "primary", reply.bind(true))
|
2026-11-15 12:25:08 -06:00
|
|
|
dialog.animate_in = not (item["run"] as Dictionary).get("fast", false)
|
2026-11-14 14:23:07 -06:00
|
|
|
m._add_modal(dialog)
|
|
|
|
|
return dialog
|
|
|
|
|
|
|
|
|
|
|
2026-11-10 19:10:11 -06:00
|
|
|
## Continue (true) or Stop here (false) for the paused roll at `i`.
|
|
|
|
|
func answer(i: int, go_on: bool) -> void:
|
|
|
|
|
if i < 0 or i >= waiting.size():
|
|
|
|
|
return
|
|
|
|
|
var it: Dictionary = waiting[i]
|
|
|
|
|
waiting.remove_at(i)
|
2026-11-14 14:23:07 -06:00
|
|
|
var dialog: ModalCard = it.get("dialog")
|
|
|
|
|
if dialog and is_instance_valid(dialog) and dialog.is_inside_tree():
|
|
|
|
|
dialog.close()
|
2026-11-10 19:10:11 -06:00
|
|
|
var run: Dictionary = it["run"]
|
|
|
|
|
if int(run["token"]) != m.rolling._token:
|
|
|
|
|
# A set-aside roll: it becomes the current roll again.
|
|
|
|
|
m.rolling._archive_current()
|
|
|
|
|
m.rolling._token += 1
|
|
|
|
|
run["token"] = m.rolling._token
|
|
|
|
|
m.current = it["current"]
|
|
|
|
|
m.rolling_ctx = it["ctx"]
|
|
|
|
|
(it["waiter"] as Waiter).done.emit(go_on)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## True while roll `run` (or any, for {}) is waiting.
|
|
|
|
|
func has_waiting(run: Dictionary = {}) -> bool:
|
|
|
|
|
if run.is_empty():
|
|
|
|
|
return not waiting.is_empty()
|
|
|
|
|
return waiting.any(func(it: Dictionary) -> bool: return is_same(it["run"], run))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## The tray's strip of waiting rolls: one row per pause.
|
|
|
|
|
func make_strip() -> Control:
|
|
|
|
|
var v := VBoxContainer.new()
|
|
|
|
|
v.add_theme_constant_override("separation", 6)
|
|
|
|
|
for i in waiting.size():
|
|
|
|
|
var row := HBoxContainer.new()
|
|
|
|
|
row.add_theme_constant_override("separation", 8)
|
|
|
|
|
var l := Style.make_label("⏸ " + str(waiting[i]["label"]), Style.body_bold(), 14, Style.ACCENT)
|
|
|
|
|
l.size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
|
|
|
|
row.add_child(l)
|
2026-11-14 14:23:07 -06:00
|
|
|
var is_cost: bool = waiting[i].get("cost", false)
|
|
|
|
|
var go := Style.bar_button("Pay ▸" if is_cost else "Continue ▸", true)
|
2026-11-10 19:10:11 -06:00
|
|
|
go.pressed.connect(answer.bind(i, true))
|
|
|
|
|
row.add_child(go)
|
2026-11-14 14:23:07 -06:00
|
|
|
var stop := Style.bar_button("Don't pay" if is_cost else "Stop here")
|
|
|
|
|
stop.tooltip_text = "Skip this card; what the roll gave so far stays" if is_cost else "End this roll here; what it rolled so far stays"
|
2026-11-10 19:10:11 -06:00
|
|
|
stop.pressed.connect(answer.bind(i, false))
|
|
|
|
|
row.add_child(stop)
|
|
|
|
|
v.add_child(row)
|
|
|
|
|
return v
|