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() var cost := str(req.get("cost", "")) var label := "%s: before %s" % [Formatter.strip_tags(str(m.current.get("root_name", ""))), Formatter.strip_tags(str(req.get("title", "")))] 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) m.tray.render() item["dialog"] = _popup(item) var reply: Variant = await w.done return reply ## 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)) dialog.animate_in = not (item["run"] as Dictionary).get("fast", false) m._add_modal(dialog) return dialog ## 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) var dialog: ModalCard = it.get("dialog") if dialog and is_instance_valid(dialog) and dialog.is_inside_tree(): dialog.close() 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) var is_cost: bool = waiting[i].get("cost", false) var go := Style.bar_button("Pay ▸" if is_cost else "Continue ▸", true) go.pressed.connect(answer.bind(i, true)) row.add_child(go) 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" stop.pressed.connect(answer.bind(i, false)) row.add_child(stop) v.add_child(row) return v