TableTower/ui/roll_pauses.gd

74 lines
2.5 KiB
GDScript3
Raw Normal View History

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 label := "%s: before %s" % [Formatter.strip_tags(str(m.current.get("root_name", ""))), Formatter.strip_tags(str(req.get("title", "")))]
waiting.append({"run": run, "waiter": w, "label": label, "current": m.current, "ctx": m.rolling_ctx})
m.tray.render()
var reply: Variant = await w.done
return reply
## 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 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 go := Style.bar_button("Continue ▸", true)
go.pressed.connect(answer.bind(i, true))
row.add_child(go)
var stop := Style.bar_button("Stop here")
stop.tooltip_text = "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