TableTower/ui/choice_option_row.gd

85 lines
3.6 KiB
GDScript3
Raw Normal View History

class_name ChoiceOptionRow
extends RefCounted
## One option button of a ChoiceDialog pick question.
## One option: number key, text and details as a subtitle; the whole row is a
## button.
static func build(d: ChoiceDialog, k: int, o: Dictionary) -> Control:
var b := Button.new()
b.focus_mode = Control.FOCUS_ALL
b.size_flags_horizontal = Control.SIZE_EXPAND_FILL
var m := [14, 8, 14, 9]
var n := Style.bordered(Style.box(Color.WHITE, 9, m), 1, Style.RULE)
var h := Style.bordered(Style.box(Style.LIT, 9, m), 1, Style.ACCENT_DARK)
Style.skin_button(b, n, h, h, Style.INK)
b.add_theme_stylebox_override("focus", Style.focus_ring(9))
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 12)
row.mouse_filter = Control.MOUSE_FILTER_IGNORE
row.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
row.offset_left = 14
row.offset_right = -14
row.offset_top = 8
row.offset_bottom = -9
var key := Style.make_label(str(k + 1) if k < 9 else "", Style.display(), 20, Style.TRIM)
key.custom_minimum_size = Vector2(16, 0)
key.size_flags_vertical = Control.SIZE_SHRINK_CENTER
key.mouse_filter = Control.MOUSE_FILTER_IGNORE
row.add_child(key)
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 1)
col.size_flags_horizontal = Control.SIZE_EXPAND_FILL
col.mouse_filter = Control.MOUSE_FILTER_IGNORE
row.add_child(col)
var dynamic: bool = o.get("dynamic", false)
var shown := Formatter.escape_literal(str(o.get("label", ""))) if dynamic else str(o.get("text", ""))
var text := RichText.label(RichText.decorate(shown), 17, Style.INK, false, true, true)
text.mouse_filter = Control.MOUSE_FILTER_IGNORE
col.add_child(text)
var det := Formatter.strip_tags(str(o.get("details", ""))).strip_edges()
if det != "":
var sub := Style.make_label(det, Style.body(), 13, Style.MUTED)
sub.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
sub.max_lines_visible = 2
sub.text_overrun_behavior = TextServer.OVERRUN_TRIM_ELLIPSIS
sub.mouse_filter = Control.MOUSE_FILTER_IGNORE
col.add_child(sub)
b.tooltip_text = det
var vt := str(o.get("value_text", ""))
if dynamic and vt != "":
var badge := PanelContainer.new()
badge.mouse_filter = Control.MOUSE_FILTER_IGNORE
badge.size_flags_vertical = Control.SIZE_SHRINK_CENTER
badge.add_theme_stylebox_override("panel", Style.bordered(Style.box(Color(Style.ACCENT, 0.18), 6, [9, 1, 9, 2]), 1, Color(Style.ACCENT_DARK, 0.4)))
var vl := Style.make_label(vt, Style.display(), 18, Style.TRIM_DARK)
vl.mouse_filter = Control.MOUSE_FILTER_IGNORE
vl.set_meta("option_value", true)
badge.add_child(vl)
row.add_child(badge)
b.add_child(row)
# Buttons don't size to their children, so follow the row's height.
var fit := func() -> void: b.custom_minimum_size.y = row.get_combined_minimum_size().y + 17.0
row.minimum_size_changed.connect(fit)
fit.call_deferred()
if o.has("cost"):
# What it costs; greyed out with the reason when it can't be afforded.
var cost := Style.make_label(str(o.get("disabled", o["cost"])), Style.body_bold(), 13, Style.DANGER_INK if o.has("disabled") else Style.TRIM_DARK)
cost.mouse_filter = Control.MOUSE_FILTER_IGNORE
col.add_child(cost)
if o.has("disabled"):
b.disabled = true
b.modulate.a = 0.55
b.tooltip_text = str(o["disabled"])
var index := int(o.get("index", k))
b.pressed.connect(func() -> void:
if not b.disabled:
d.answer(index))
b.mouse_entered.connect(func() -> void: d.preview.set_hover(k))
b.mouse_exited.connect(func() -> void:
if d.preview.hover == k:
d.preview.set_hover(-1)
)
b.focus_entered.connect(d.preview.refresh)
b.focus_exited.connect(d.preview.refresh.call_deferred)
return b