TableTower/ui/inspector_range.gd

122 lines
4.7 KiB
GDScript3
Raw Normal View History

class_name InspectorRange
extends RefCounted
## A result's place on its card, in the inspector's result section: its weight
## (die faces on a Weights card, chance on a plain card) or its range (From–To
## on a Ranges card), and its position (▲ ▼ or a typed number). Edits go
## through RangeWeights.after_edit, so Weights reflow and auto-sort apply, as
## they do on the card.
static func build(insp: Inspector, box: VBoxContainer, t: RollTable, e: TableEntry) -> void:
var fixed := t.kind == RollTable.KIND_LOOP or (t.kind == RollTable.KIND_CHOICE and t.choice != "pick")
if fixed or t.kind == RollTable.KIND_SHEET:
return
if t.kind == RollTable.KIND_ROLL or t.kind == RollTable.KIND_CHOICE:
if RangeWeights.uses_weights(t) or not t.is_ranged():
box.add_child(InspectorKit.field("Weight (die faces)" if t.is_ranged() else "Weight", _weight_row(insp, t, e)))
else:
box.add_child(InspectorKit.field("Range", _range_row(insp, t, e)))
if t.entries.size() > 1:
box.add_child(InspectorKit.field("Position", _position_row(insp, t, e)))
static func _weight_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control:
var row := InspectorKit.hbox(8)
var amount := InspectorKit.line(str(e.weight), "1", 64.0)
InspectorKit.tag(amount, "res.weight")
insp.fields["weight"] = amount
amount.text_submitted.connect(func(_s: String) -> void: amount.release_focus())
amount.focus_exited.connect(func() -> void:
var typed := amount.text.strip_edges()
if typed.is_valid_int():
set_weight(insp, t, e, typed.to_int()))
row.add_child(amount)
if t.is_ranged():
row.add_child(InspectorKit.note("rolls %s" % (t.num_label(e, t.entries.find(e)) if e.has_range else "never"), Style.MUTED, 13))
else:
row.add_child(InspectorKit.note("0 = never comes up", Style.MUTED, 13))
return row
static func _range_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control:
var row := InspectorKit.hbox(6)
var from := InspectorKit.line(str(e.range_lo), "1", 64.0)
var to := InspectorKit.line(str(e.range_hi), "1", 64.0)
InspectorKit.tag(from, "res.range_lo")
InspectorKit.tag(to, "res.range_hi")
insp.fields["range_lo"] = from
insp.fields["range_hi"] = to
var commit := func() -> void:
var lo := from.text.strip_edges()
var hi := to.text.strip_edges()
if lo.is_valid_int() and hi.is_valid_int():
set_range(insp, t, e, lo.to_int(), hi.to_int())
for edit: LineEdit in [from, to]:
edit.text_submitted.connect(func(_s: String) -> void: edit.release_focus())
edit.focus_exited.connect(commit)
row.add_child(from)
row.add_child(InspectorKit.word("–"))
row.add_child(to)
return row
static func _position_row(insp: Inspector, t: RollTable, e: TableEntry) -> Control:
var row := InspectorKit.hbox(6)
var at := t.entries.find(e)
var up := ModalCard.make_button("▲")
up.tooltip_text = "Move up one row"
up.disabled = at <= 0
up.pressed.connect(func() -> void: move_to(insp, t, e, at - 1))
var down := ModalCard.make_button("▼")
down.tooltip_text = "Move down one row"
down.disabled = at >= t.entries.size() - 1
down.pressed.connect(func() -> void: move_to(insp, t, e, at + 1))
var number := InspectorKit.line(str(at + 1), "", 52.0)
InspectorKit.tag(number, "res.position")
insp.fields["position"] = number
number.text_submitted.connect(func(_s: String) -> void: number.release_focus())
number.focus_exited.connect(func() -> void:
var typed := number.text.strip_edges()
if typed.is_valid_int() and typed.to_int() - 1 != t.entries.find(e):
move_to(insp, t, e, typed.to_int() - 1))
for c: Control in [up, down, number]:
row.add_child(c)
row.add_child(InspectorKit.note("of %d" % t.entries.size(), Style.MUTED, 13))
return row
static func set_weight(insp: Inspector, t: RollTable, e: TableEntry, w: int) -> void:
w = clampi(w, 0, 9999)
if w == e.weight:
return
e.weight = w
RangeWeights.after_edit(t)
insp.wrote(t, false, true)
static func set_range(insp: Inspector, t: RollTable, e: TableEntry, lo: int, hi: int) -> void:
var limit := TableCardEditing.RANGE_LIMIT
lo = clampi(lo, -limit, limit)
hi = clampi(hi, -limit, limit)
if lo == e.range_lo and hi == e.range_hi:
return
e.range_lo = lo
e.range_hi = hi
e.has_range = true
RangeWeights.after_edit(t)
insp.wrote(t, false, true)
## Moves the result to row `to` (0-based, clamped). On a Ranges card with
## auto-sort the row may go back to its place in roll order; a toast says so.
static func move_to(insp: Inspector, t: RollTable, e: TableEntry, to: int) -> void:
var from := t.entries.find(e)
to = clampi(to, 0, t.entries.size() - 1)
if from < 0 or from == to:
return
t.entries.remove_at(from)
t.entries.insert(to, e)
if RangeWeights.after_edit(t):
insp.main.toast("Auto-sort put the rows back in roll order (turn it off in the card's section)")
insp.wrote(t, false, true)