TableTower/ui/tray_chips.gd

175 lines
7 KiB
GDScript3
Raw Normal View History

class_name TrayChips
extends RefCounted
## Builders for the pieces of ResultTray's chain: result chips (with their
## effect lines and "Why this?" button), the brass links between them, the
## open-question chip and the "+N earlier steps" toggle.
## Most effect lines listed under one chip.
const EFFECT_LINES := 3
## The chip for steps[index]; `last` marks the final result of a finished roll.
static func chip(tray: ResultTray, step: Dictionary, last: bool, steps: Array = [], index: int = -1) -> Control:
var main := tray.main
var inline: bool = step.get("inline", false)
var p := PanelContainer.new()
var sb := Style.box(Style.LIT if last else Style.CARD, 9, [10, 5, 10, 6] if inline else [12, 7, 12, 8])
sb.shadow_color = Color(0, 0, 0, 0.25)
sb.shadow_size = 1
sb.shadow_offset = Vector2(0, 3)
if last:
Style.bordered(sb, 2, Style.ACCENT)
sb.set_expand_margin_all(2)
if inline:
sb.bg_color = sb.bg_color.darkened(0.04)
p.add_theme_stylebox_override("panel", sb)
p.size_flags_vertical = Control.SIZE_SHRINK_CENTER
p.mouse_filter = Control.MOUSE_FILTER_PASS
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 1)
p.add_child(v)
var plain := RulesView.plain(main)
var caption := TrayText.caption_for(step, main.data, plain)
var has_why := index >= 0 and WhyPopover.has_why(steps, index)
var has_details := main.details().step_has_details(step)
if has_details:
caption += " " + RichText.INFO_MARK
# The card it came from reads first, bold and dark; how it was rolled follows, muted.
var source := Formatter.strip_tags(str(step["table"]))
var cap := HBoxContainer.new()
cap.add_theme_constant_override("separation", 0)
cap.mouse_filter = Control.MOUSE_FILTER_IGNORE
var fs := 12 if inline else 13
if source != "" and caption.begins_with(source):
cap.add_child(Style.make_label(source, Style.body_bold(), fs, Style.INK))
caption = caption.substr(source.length())
if caption != "":
cap.add_child(Style.make_label(caption, Style.body(), fs - 1, Style.MUTED))
for l in cap.get_children():
(l as Control).mouse_filter = Control.MOUSE_FILTER_IGNORE
v.add_child(cap)
var raw := str(step["text"])
if str(step.get("kind", "")) == RollTable.KIND_SHEET:
raw = TrayText.one_line(raw)
if step.has("answer") and Formatter.strip_tags(raw).strip_edges() == "":
raw = "[i]no answer[/i]"
var shown := RichText.decorate(raw)
var body := RichText.display_label(shown, 16 if inline else 20, Style.DANGER_INK if step.get("stop", false) and not step.get("deck_empty", false) else Style.INK)
if Formatter.strip_tags(shown).length() > 56:
body.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
body.custom_minimum_size = Vector2(420, 0)
RichText.hook_hover(body, main)
v.add_child(body)
var fx: Array = step.get("effects", [])
if not fx.is_empty():
v.add_child(_effect_lines(TrayText.effect_lines_for(step, main.data, plain)))
if has_why:
var wb := _why_button()
wb.pressed.connect(func() -> void: tray.open_why(index, wb))
tray.why_buttons[index] = wb
v.add_child(wb)
if has_details or has_why:
p.mouse_entered.connect(func() -> void:
if not tray.why_popover().visible:
main.details().show_step(p, step, steps, index))
return p
## The small "Why this?" pill under a chip.
static func _why_button() -> Button:
var b := Button.new()
b.text = "Why this?"
b.tooltip_text = "What decided this step"
b.size_flags_horizontal = Control.SIZE_SHRINK_BEGIN
b.focus_mode = Control.FOCUS_NONE
var n := Style.bordered(Style.box(Color.WHITE, 10, [9, 1, 9, 2]), 1, Style.RULE)
var h := Style.bordered(Style.box(Style.LIT, 10, [9, 1, 9, 2]), 1, Style.ACCENT_DARK)
Style.skin_button(b, n, h, h, Style.TRIM_DARK, Style.INK)
b.add_theme_font_override("font", Style.body_bold())
b.add_theme_font_size_override("font_size", 12)
return b
## The effect log under a chip: a few compact lines, errors in red.
static func _effect_lines(fx: Array) -> Control:
var box := VBoxContainer.new()
box.add_theme_constant_override("separation", 0)
box.mouse_filter = Control.MOUSE_FILTER_IGNORE
var shown := 0
for l: Variant in fx:
var d: Dictionary = l if l is Dictionary else {"text": str(l)}
var bad: bool = d.get("error", false)
# Errors are always listed; ordinary lines up to EFFECT_LINES.
if shown >= EFFECT_LINES and not bad:
continue
var lab := Style.make_label(("⚠ " if bad else "") + str(d.get("text", "")), Style.body_bold() if bad else Style.body(), 12,
Style.DANGER_INK if bad else Style.TRIM_DARK)
lab.mouse_filter = Control.MOUSE_FILTER_IGNORE
lab.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
lab.custom_minimum_size.x = 0
box.add_child(lab)
shown += 1
var more := fx.size() - shown
if more > 0:
box.add_child(Style.make_label("+%d more" % more, Style.body(), 11, Style.MUTED))
var tip := Effects.log_text(fx)
box.tooltip_text = "\n".join(tip)
return box
## The brass pipe piece between two chips (thinner for an inline step).
static func link(inline: bool = false) -> Control:
var l := Panel.new()
var sb := Style.box(Style.ACCENT)
sb.border_width_top = 2
sb.border_width_bottom = 2
sb.border_color = Style.ACCENT_DARK
if inline:
sb = Style.box(Color(Style.ACCENT, 0.55))
l.add_theme_stylebox_override("panel", sb)
l.custom_minimum_size = Vector2(16, 4) if inline else Vector2(22, 8)
l.size_flags_vertical = Control.SIZE_SHRINK_CENTER
l.mouse_filter = Control.MOUSE_FILTER_IGNORE
return l
## A dashed placeholder chip for the question that is open right now.
static func asking_chip(title: String) -> Control:
var p := PanelContainer.new()
var sb := Style.box(Color(1, 1, 1, 0.05), 9, [12, 7, 12, 8])
Style.bordered(sb, 1, Color(Style.ACCENT, 0.6))
p.add_theme_stylebox_override("panel", sb)
p.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var v := VBoxContainer.new()
v.add_theme_constant_override("separation", 1)
p.add_child(v)
v.add_child(Style.make_label(title, Style.body(), 12, Style.ON_BG_MUTED))
v.add_child(Style.make_label("Waiting for your answer…", Style.display(), 20, Style.ACCENT))
return p
## "+N earlier steps" (or "Hide earlier steps") at the start of a long chain.
static func earlier_toggle(tray: ResultTray, first: int, total: int) -> Control:
var b := Button.new()
if tray.expanded:
b.text = "Hide earlier steps" if first == 0 else "Hide earlier steps (%s not shown)" % TrayText.thousands(first)
else:
b.text = "+%s earlier steps" % TrayText.thousands(first)
b.tooltip_text = "This chain has %s steps" % TrayText.thousands(total)
b.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var n := Style.bordered(Style.box(Color(1, 1, 1, 0.04), 8, [12, 6, 12, 6]), 1, Color(1, 1, 1, 0.16))
var h := Style.bordered(Style.box(Color(1, 1, 1, 0.1), 8, [12, 6, 12, 6]), 1, Color(Style.ACCENT, 0.5))
Style.skin_button(b, n, h, h, Style.ON_BG_MUTED, Style.ON_BG)
b.add_theme_font_size_override("font_size", 14)
b.pressed.connect(func() -> void:
tray.expanded = not tray.expanded
tray.render()
)
var wrap := HBoxContainer.new()
wrap.add_theme_constant_override("separation", 0)
wrap.add_child(b)
var gap := Control.new()
gap.custom_minimum_size = Vector2(10, 0)
wrap.add_child(gap)
return wrap