TableTower/ui/sheet_fields_niche.gd

247 lines
10 KiB
GDScript3
Raw Normal View History

class_name SheetFieldsNiche
extends RefCounted
## Boxes from popular RPGs: slot inventory with encumbrance (Knave, Cairn,
## Mausritter), condition chips, spell slots per level, XP with levels, and
## currency with exchange rates.
static func make(ed: SheetEditor, box: SheetBox, look: Dictionary) -> Control:
var ink: Color = look["ink"]
match box.kind:
SheetKinds.SLOTS:
return _slots(ed, box, ink)
SheetKinds.CONDITIONS:
return _conditions(ed, box, ink)
SheetKinds.SPELL_SLOTS:
return _spell_slots(ed, box, ink)
SheetKinds.XP:
return _xp(ed, box, ink)
return _currency(ed, box, ink)
static func _items(box: SheetBox) -> Array:
return (box.value as Array).duplicate(true) if box.value is Array else []
## A grid of slots; items fill them in order (big items take more). Click an
## item for Bigger / Smaller / Remove; type below to add. Warns when full.
static func _slots(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 6)
var total := maxi(1, int(SheetValues.number(box.config.get("slots", 10))))
var items := _items(box)
var used := 0
for it: Dictionary in items:
used += maxi(1, int(SheetValues.number(it.get("size", 1))))
var status := Style.make_label("%d / %d slots%s" % [used, total, " · Pack full" if used >= total else ""], Style.body_bold(), 13,
Style.DANGER_INK if used > total else ink)
status.name = "SlotStatus"
col.add_child(status)
var grid := GridContainer.new()
grid.columns = clampi(int((box.w - 20) / 74.0), 2, 8)
grid.add_theme_constant_override("h_separation", 4)
grid.add_theme_constant_override("v_separation", 4)
col.add_child(grid)
var cell := 0
for i in items.size():
var it: Dictionary = items[i]
var size := maxi(1, int(SheetValues.number(it.get("size", 1))))
for k in size:
var b := SheetFieldViews.step_button(SheetFieldsList.link_mark(it) + str(it.get("name", "")) if k == 0 else "↳", ink)
b.name = "Item%d_%d" % [i, k]
b.custom_minimum_size = SheetFieldViews.tap_size(Vector2(0, 26))
b.size_flags_horizontal = Control.SIZE_EXPAND_FILL
b.clip_text = true
b.tooltip_text = "%s · %d %s · click to change" % [it.get("name", ""), size, "slot" if size == 1 else "slots"]
var index := i
b.pressed.connect(func() -> void: _item_menu(ed, box, index, b))
grid.add_child(b)
cell += 1
while cell < total:
var empty := PanelContainer.new()
empty.custom_minimum_size = SheetFieldViews.tap_size(Vector2(0, 26))
empty.size_flags_horizontal = Control.SIZE_EXPAND_FILL
empty.add_theme_stylebox_override("panel", Style.bordered(Style.box(Color(0, 0, 0, 0), 6), 1, Color(ink, 0.25)))
grid.add_child(empty)
cell += 1
if not ed.building():
var add := LineEdit.new()
add.name = "AddItem"
add.placeholder_text = "+ add an item (Enter)"
add.flat = true
add.add_theme_color_override("font_color", ink)
add.add_theme_color_override("font_placeholder_color", Color(ink, 0.5))
add.text_submitted.connect(func(text: String) -> void:
if text.strip_edges() == "":
return
var next := _items(box)
next.append({"name": text.strip_edges(), "size": 1})
ed.set_value(box, next))
col.add_child(add)
return col
static func _item_menu(ed: SheetEditor, box: SheetBox, index: int, at: Control) -> void:
if ed.building():
return
var change := func(delta: int) -> void:
var next := _items(box)
next[index]["size"] = maxi(1, int(SheetValues.number(next[index].get("size", 1))) + delta)
ed.set_value(box, next)
var item: Dictionary = _items(box)[index]
var extra: Array = []
if str(item.get("sheet", "")) != "":
extra.append({"text": "Open its sheet", "do": ed.nav.open.bind(str(item["sheet"]))})
if item.has("lore"):
extra.append({"text": "Read the Lore Entry", "do": ed.details.open_lore.bind(str(item["lore"]))})
if not extra.is_empty():
extra.append({"sep": ""})
Actions.popup_at(ed.m, extra + [
{"text": "Bigger (one more slot)", "do": change.bind(1)},
{"text": "Smaller", "do": change.bind(-1)},
{"text": "Remove", "do": func() -> void:
var next := _items(box)
next.remove_at(index)
ed.set_value(box, next)},
], at.get_global_rect().get_center())
## Chips for each condition: lit when it applies; click to switch it.
static func _conditions(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var flow := HFlowContainer.new()
flow.add_theme_constant_override("h_separation", 5)
flow.add_theme_constant_override("v_separation", 5)
var active: Array = box.value if box.value is Array else []
var names: Array = (box.config.get("options", []) as Array).duplicate()
for a: Variant in active:
if not names.has(a):
names.append(a)
for n: Variant in names:
var on := active.has(n)
var chip := Button.new()
chip.name = str(n).validate_node_name()
chip.text = str(n)
chip.focus_mode = Control.FOCUS_NONE
var sb := Style.bordered(Style.box(ink if on else Color(0, 0, 0, 0), 6, [8, 3, 8, 3]), 1, Color(ink, 0.7 if on else 0.35))
var fg := Style.CARD if on and ink.get_luminance() < 0.5 else (Style.INK if on else Color(ink, 0.75))
Style.skin_button(chip, sb, sb, sb, fg, fg, fg)
chip.add_theme_font_size_override("font_size", 13)
chip.pressed.connect(func() -> void:
var next := active.duplicate()
if on:
next.erase(n)
else:
next.append(n)
ed.set_value(box, next))
flow.add_child(chip)
return flow
## A row per spell level: its name and a box per slot to mark as used.
static func _spell_slots(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 4)
var levels: Array = box.config.get("levels", [])
var used: Array = (box.value as Array).duplicate() if box.value is Array else []
while used.size() < levels.size():
used.append(0)
for i in levels.size():
var lv: Dictionary = levels[i]
var row := HBoxContainer.new()
row.add_theme_constant_override("separation", 5)
var name := Style.make_label(str(lv.get("name", "")), Style.body_bold(), 14, ink)
name.custom_minimum_size.x = 40
row.add_child(name)
var marked := int(SheetValues.number(used[i]))
for k in int(SheetValues.number(lv.get("max", 0))):
var b := Button.new()
b.name = "Slot%d_%d" % [i, k + 1]
b.focus_mode = Control.FOCUS_NONE
b.custom_minimum_size = SheetFieldViews.tap_size(Vector2(18, 18))
var on := k < marked
var sb := Style.bordered(Style.box(ink if on else Color(0, 0, 0, 0), 9), 2, ink if on else Color(ink, 0.55))
Style.skin_button(b, sb, sb, sb, ink)
var n := k + 1
var level := i
b.pressed.connect(func() -> void:
var next := used.duplicate()
next[level] = n - 1 if n == marked else n
ed.set_value(box, next))
row.add_child(b)
col.add_child(row)
return col
## XP you type, the level it reaches and a bar to the next level.
static func _xp(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 4)
var info := SheetValues.xp_level(box)
var row := HBoxContainer.new()
var value := LineEdit.new()
value.name = "Value"
value.text = SheetValues.show(SheetValues.number(box.value))
value.flat = true
value.custom_minimum_size.x = 70
value.add_theme_font_override("font", Style.display())
value.add_theme_font_size_override("font_size", 22)
value.add_theme_color_override("font_color", ink)
value.text_submitted.connect(func(text: String) -> void: ed.set_value(box, SheetFieldViews.parse_number(text, box.value)))
value.focus_exited.connect(func() -> void:
if SheetFieldViews.parse_number(value.text, box.value) != SheetValues.number(box.value):
ed.set_value(box, SheetFieldViews.parse_number(value.text, box.value)))
row.add_child(value)
var level := Style.make_label("Level %d" % int(info["level"]), Style.body_bold(), 15, ink)
level.name = "Level"
level.size_flags_horizontal = Control.SIZE_EXPAND_FILL
level.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
row.add_child(level)
col.add_child(row)
var next := float(info["next"])
var bar := Control.new()
bar.custom_minimum_size.y = 8
var share := 1.0 if next < 0 else clampf((SheetValues.number(box.value) - float(info["prev"])) / maxf(1.0, next - float(info["prev"])), 0, 1)
bar.draw.connect(func() -> void:
bar.draw_style_box(Style.box(Color(ink, 0.15), 4), Rect2(Vector2.ZERO, bar.size))
bar.draw_style_box(Style.box(Style.ACCENT_DARK if ink.get_luminance() < 0.5 else Style.ACCENT, 4), Rect2(Vector2.ZERO, Vector2(bar.size.x * share, bar.size.y))))
col.add_child(bar)
col.add_child(SheetFieldViews.note("Top level" if next < 0 else "%s to level %d" % [SheetValues.show(next - SheetValues.number(box.value)), int(info["level"]) + 1], ink, 12))
return col
## A field per coin and the total in the first coin.
static func _currency(ed: SheetEditor, box: SheetBox, ink: Color) -> Control:
var col := VBoxContainer.new()
col.add_theme_constant_override("separation", 4)
var coins: Array = box.config.get("coins", [])
var flow := HFlowContainer.new()
flow.add_theme_constant_override("h_separation", 10)
col.add_child(flow)
var held: Dictionary = (box.value as Dictionary).duplicate() if box.value is Dictionary else {}
var total := Style.make_label("", Style.body_bold(), 14, ink)
total.name = "Total"
var show_total := func() -> void:
total.text = "Total %s %s" % [SheetValues.show(SheetValues.currency_total(box)), str(coins[0].get("name", "")) if not coins.is_empty() else ""]
for coin: Dictionary in coins:
var name := str(coin.get("name", ""))
var pair := HBoxContainer.new()
var e := LineEdit.new()
e.name = name.validate_node_name()
e.text = SheetValues.show(SheetValues.number(held.get(name, 0)))
e.alignment = HORIZONTAL_ALIGNMENT_RIGHT
e.custom_minimum_size.x = 56
e.flat = true
e.add_theme_color_override("font_color", ink)
e.add_theme_font_override("font", Style.body_bold())
e.text_changed.connect(func(text: String) -> void:
var next: Dictionary = (box.value as Dictionary).duplicate() if box.value is Dictionary else {}
next[name] = SheetFieldViews.parse_number(text, 0)
ed.edit_value(box, next)
show_total.call())
pair.add_child(e)
pair.add_child(Style.make_label(name, Style.body(), 14, Color(ink, 0.7)))
flow.add_child(pair)
show_total.call()
col.add_child(total)
return col