TableTower/ui/table_card_odds.gd

54 lines
1.8 KiB
GDScript3
Raw Permalink Normal View History

class_name TableCardOdds
extends RefCounted
## The small chance labels ("17%") at the right of ranged rows in Play mode.
## When a card has a roll modifier, the chances move as its value changes, so
## refresh() rewrites the labels from the modifier's value now. TableCardRows
## owns one of these and calls track() for each label it makes.
var card: TableCard
## Chance labels by entry id, for the rows currently shown.
var labels: Dictionary = {}
func _init(p_card: TableCard) -> void:
card = p_card
## Forgets the previous build's labels.
func clear() -> void:
labels.clear()
## Remembers a row's chance label and fills it in now.
func track(chance_label: Label, entry: TableEntry) -> void:
labels[entry.id] = chance_label
set_chance(chance_label, entry, live_mod())
## The roll modifier's value now (RollRange.live_mod): {"known", "value"}.
func live_mod() -> Dictionary:
var main := card.main
return RollRange.live_mod(card.table, Roller.live_context(main.data, main.persistent_vars))
## Updates the chance labels for the modifier's current value.
func refresh() -> void:
if labels.is_empty():
return
var modifier := live_mod()
for eid: String in labels:
var entry := card.table.find_entry(eid)
if entry != null and is_instance_valid(labels[eid]):
set_chance(labels[eid], entry, modifier)
## Writes one label's chance (and its tooltip) for a modifier value.
func set_chance(chance_label: Label, entry: TableEntry, modifier: Dictionary) -> void:
var table := card.table
var chance := TablePreview.chance_text(table, entry, int(modifier["value"])) if modifier["known"] else ""
if chance_label.text != chance:
chance_label.text = chance
if table.has_mod():
chance_label.tooltip_text = "Chance with the modifier at %s" % Describe.signed_number(int(modifier["value"])) if modifier["known"] \
else "The chance depends on the modifier"