TableTower/ui/sheet_fields_trackers.gd

183 lines
7.4 KiB
GDScript3
Raw Permalink Normal View History

class_name SheetFieldsTrackers
extends RefCounted
## Tracker boxes: current / max bars, tick boxes, clocks, dot ratings, step
## dice, usage dice and word ladders. Clicks change the value through
## SheetEditor.set_value (one undo step each).
static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control:
var ink: Color = look["ink"]
match box.kind:
SheetKinds.RESOURCE:
return _resource(ed, box, ink)
SheetKinds.TICKS:
return _marks(ed, box, ink, int(box.config.get("count", 5)), false)
SheetKinds.DOTS:
return _marks(ed, box, ink, int(box.config.get("max", 5)), true)
SheetKinds.CLOCK:
return _clock(ed, box, ink)
SheetKinds.STEP_DIE:
return _step_die(ed, box, ink)
SheetKinds.USAGE_DIE:
return _usage_die(ed, box, ink)
return _ladder(ed, box, ink)
## − [bar] + and "6 / 10".
static func _resource(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var v: Dictionary = box.value if box.value is Dictionary else {"cur": 0, "max": 10}
var cur := SheetValues.number(v.get("cur", 0))
var top := maxf(1.0, SheetValues.number(v.get("max", 10)))
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 6)
row.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var set_cur := func(n: float) -> void:
var next := v.duplicate()
next["cur"] = int(clampf(n, 0, top))
ed.set_value(box, next)
var minus := SheetFieldViews.step_button("−", ink)
minus.name = "Minus"
minus.pressed.connect(func() -> void: set_cur.call(cur - 1))
row.add_child(minus)
var bar := Control.new()
bar.custom_minimum_size = Vector2(40, 14)
bar.size_flags_horizontal = Control.SIZE_EXPAND_FILL
bar.size_flags_vertical = Control.SIZE_SHRINK_CENTER
var fill := Color(str(box.config.get("color", "#a33a2c")))
bar.draw.connect(func() -> void:
var r := Rect2(Vector2.ZERO, bar.size)
bar.draw_style_box(Style.box(Color(ink, 0.15), 7), r)
if cur > 0:
bar.draw_style_box(Style.box(fill, 7), Rect2(r.position, Vector2(r.size.x * clampf(cur / top, 0, 1), r.size.y))))
row.add_child(bar)
var plus := SheetFieldViews.step_button("+", ink)
plus.name = "Plus"
plus.pressed.connect(func() -> void: set_cur.call(cur + 1))
row.add_child(plus)
row.add_child(Style.make_label("%s / %s" % [SheetValues.show(cur), SheetValues.show(top)], Style.body_bold(), 15, ink))
return row
## A row of boxes (ticks) or dots: click the n-th to mark up to it, click the
## last marked one to clear it.
static func _marks(ed: SheetEditor, box: SheetBox, ink: Color, count: int, round: bool) -> Control:
var flow := HFlowContainer.new()
flow.add_theme_constant_override("h_separation", 5)
flow.add_theme_constant_override("v_separation", 5)
var marked := int(SheetValues.number(box.value))
for i in count:
var b := Button.new()
b.name = "Mark%d" % (i + 1)
b.focus_mode = Control.FOCUS_NONE
b.custom_minimum_size = SheetFieldViews.tap_size(Vector2(18, 18))
var on := i < marked
var radius := 9 if round else 3
var sb := Style.bordered(Style.box(ink if on else Color(0, 0, 0, 0), radius), 2, ink if on else Color(ink, 0.55))
Style.skin_button(b, sb, sb, sb, ink)
var n := i + 1
b.pressed.connect(func() -> void: ed.set_value(box, n - 1 if n == marked else n))
flow.add_child(b)
return flow
## A round clock in segments: click to fill the next one, right-click to empty one.
static func _clock(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var segments := maxi(2, int(box.config.get("segments", 6)))
var filled := int(SheetValues.number(box.value))
var c := Control.new()
c.name = "Clock"
c.custom_minimum_size = Vector2(48, 48)
c.tooltip_text = "%d of %d · click to fill, right-click to empty" % [filled, segments]
c.draw.connect(func() -> void:
var center := c.size / 2.0
var r := minf(c.size.x, c.size.y) / 2.0 - 3.0
for i in segments:
var a0 := -PI / 2.0 + TAU * i / segments
var a1 := -PI / 2.0 + TAU * (i + 1) / segments
if i < filled:
var pts := PackedVector2Array([center])
for k in 9:
pts.append(center + Vector2.from_angle(lerpf(a0, a1, k / 8.0)) * r)
c.draw_colored_polygon(pts, Color(ink, 0.85))
c.draw_line(center, center + Vector2.from_angle(a0) * r, Color(ink, 0.6), 1.5, true)
c.draw_arc(center, r, 0, TAU, 48, ink, 2.0, true))
c.gui_input.connect(func(ev: InputEvent) -> void:
var mb := ev as InputEventMouseButton
if mb and mb.pressed and not ed.building():
if mb.button_index == MOUSE_BUTTON_LEFT:
ed.set_value(box, mini(filled + 1, segments))
elif mb.button_index == MOUSE_BUTTON_RIGHT:
ed.set_value(box, maxi(filled - 1, 0)))
return c
## ◀ d8 ▶: steps down or up through d4 … d12.
static func _step_die(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var i := maxi(0, SheetKinds.DIE_STEPS.find(str(box.value)))
var row := HBoxContainer.new()
row.alignment = BoxContainer.ALIGNMENT_CENTER
row.add_theme_constant_override("separation", 8)
var down := SheetFieldViews.step_button("◀", ink)
down.name = "Down"
down.disabled = i == 0
down.pressed.connect(func() -> void: ed.set_value(box, SheetKinds.DIE_STEPS[i - 1]))
row.add_child(down)
row.add_child(Style.make_label(SheetKinds.DIE_STEPS[i], Style.display(), 26, ink))
var up := SheetFieldViews.step_button("▶", ink)
up.name = "Up"
up.disabled = i == SheetKinds.DIE_STEPS.size() - 1
up.pressed.connect(func() -> void: ed.set_value(box, SheetKinds.DIE_STEPS[i + 1]))
row.add_child(up)
return row
## "d8 [Use]": rolls the die; on a 1 or 2 it shrinks a step (d4 runs out).
static func _usage_die(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var die := str(box.value)
var row := HBoxContainer.new()
row.alignment = BoxContainer.ALIGNMENT_CENTER
row.add_theme_constant_override("separation", 10)
var gone := die == "" or die == "out"
row.add_child(Style.make_label("Out" if gone else die, Style.display(), 26, ink))
var use := SheetFieldViews.step_button("Refill" if gone else "Use", ink)
use.name = "Use"
use.pressed.connect(func() -> void:
if gone:
ed.set_value(box, "d8")
return
var sides := die.trim_prefix("d").to_int()
var rolled := ed.m.rng.randi_range(1, maxi(sides, 1))
var i := SheetKinds.DIE_STEPS.find(die)
var next := die
if rolled <= 2:
next = SheetKinds.DIE_STEPS[i - 1] if i > 0 else "out"
ed.m.toast("%s: rolled %d on %s%s" % [box.title, rolled, die, (" · now %s" % ("out" if next == "out" else next)) if next != die else " · it holds"])
if next != die:
ed.set_value(box, next))
row.add_child(use)
return row
## ◀ Fair (+2) ▶ through the box's word steps (the Fate ladder by default).
static func _ladder(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var steps: Array = box.config.get("steps", SheetKinds.FATE_LADDER)
var i := clampi(int(SheetValues.number(box.value)), 0, maxi(steps.size() - 1, 0))
var row := HBoxContainer.new()
row.alignment = BoxContainer.ALIGNMENT_CENTER
row.add_theme_constant_override("separation", 8)
var down := SheetFieldViews.step_button("◀", ink)
down.name = "Down"
down.disabled = i == 0
down.pressed.connect(func() -> void: ed.set_value(box, i - 1))
row.add_child(down)
var word := Style.make_label(str(steps[i]) if not steps.is_empty() else "—", Style.display(), 22, ink)
word.size_flags_horizontal = Control.SIZE_EXPAND_FILL
word.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
row.add_child(word)
var up := SheetFieldViews.step_button("▶", ink)
up.name = "Up"
up.disabled = i >= steps.size() - 1
up.pressed.connect(func() -> void: ed.set_value(box, i + 1))
row.add_child(up)
return row