A ranged card now stores that it is ranged, so an empty card can switch between Plain and Ranged. Cards made ranged are edited by weight (die faces per result; ranges follow the row order); a gap becomes an empty result. Ranges mode keeps typed ranges, rows drag with their range, and auto-sort (on by default) keeps rows in roll order. Old cards load as Ranges. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
83 lines
3.2 KiB
GDScript
83 lines
3.2 KiB
GDScript
extends TestBase
|
|
## Build mode: dragging a result's handle reorders the card's results. Ranged
|
|
## cards keep their ranges in place; the move is one undo step.
|
|
## Run: godot --headless --path . -s res://tests/test_entry_drag.gd -- --sample
|
|
|
|
|
|
func _init() -> void:
|
|
if not await start_main():
|
|
return
|
|
main.set_mode(Main.Mode.BUILD)
|
|
await _frames()
|
|
await _test_plain()
|
|
await _test_ranged()
|
|
finish()
|
|
|
|
|
|
func _handle(card: TableCard, eid: String) -> EntryHandle:
|
|
var hs := (card.rows[eid] as Control).find_children("*", "EntryHandle", true, false)
|
|
return hs[0] if not hs.is_empty() else null
|
|
|
|
|
|
func _ids(t: RollTable) -> Array:
|
|
return t.entries.map(func(e: TableEntry) -> String: return e.id)
|
|
|
|
|
|
func _test_plain() -> void:
|
|
var t := main.data.get_table("monsters")
|
|
var card := main.card_for(t.id)
|
|
var before := _ids(t)
|
|
var h := _handle(card, before[0])
|
|
_check(h != null, "build rows have a drag handle")
|
|
var last := (card.rows[before[-1]] as Control).get_global_rect()
|
|
_eq(h.track(last.end + Vector2(0, 10)), before.size() - 1, "below the last row lands at the end")
|
|
_check(h._line != null and h._line.visible, "an insertion line shows")
|
|
h.finish()
|
|
await _frames()
|
|
var after := _ids(t)
|
|
_check(after[-1] == before[0] and after[0] == before[1], "the first result moved to the end")
|
|
main.undo_redo._commit_edits()
|
|
main.undo_redo.undo()
|
|
await _frames()
|
|
_eq(_ids(main.data.get_table("monsters")), before, "undo puts the order back")
|
|
|
|
|
|
## Drags a ranged card's first row to the third row's place.
|
|
func _drag_first_down_two(t: RollTable) -> String:
|
|
var card := main.card_for(t.id)
|
|
var moved: String = t.entries[0].id
|
|
var h := _handle(card, moved)
|
|
var second := (card.rows[t.entries[2].id] as Control).get_global_rect()
|
|
h.track(second.get_center() + Vector2(0, 2))
|
|
h.finish()
|
|
return moved
|
|
|
|
|
|
func _test_ranged() -> void:
|
|
var t := main.data.get_table("wilderness")
|
|
_eq(t.range_edit, RangeWeights.RANGES, "a loaded card keeps typed ranges")
|
|
var first_range := Vector2i(t.entries[0].range_lo, t.entries[0].range_hi)
|
|
# Auto-sort (on by default) puts a Ranges card back in roll order.
|
|
var moved := _drag_first_down_two(t)
|
|
await _frames()
|
|
_eq(t.entries.find(t.find_entry(moved)), 0, "auto-sort puts the row back in roll order")
|
|
# Without auto-sort the whole row moves, range and all.
|
|
t.auto_sort = false
|
|
moved = _drag_first_down_two(t)
|
|
await _frames()
|
|
var e := t.find_entry(moved)
|
|
_eq(t.entries.find(e), 2, "the result moved down two places")
|
|
_eq(Vector2i(e.range_lo, e.range_hi), first_range, "its range moved with it")
|
|
_eq(t.range_problems(), "", "the ranges still cover the dice")
|
|
# Weights: the moved row's share follows it and the ranges are laid out again.
|
|
t.auto_sort = true
|
|
RangeWeights.set_mode(t, RangeWeights.WEIGHTS)
|
|
main.card_for(t.id).rebuild()
|
|
await _frames()
|
|
_eq(t.entries[0].range_lo, RollRange.natural(t).x, "Weights: the first row starts at the dice's lowest roll")
|
|
var size := e.weight
|
|
moved = _drag_first_down_two(t)
|
|
await _frames()
|
|
_eq(t.find_entry(moved).range_hi - t.find_entry(moved).range_lo + 1, t.find_entry(moved).weight, "Weights: the moved row keeps its share")
|
|
_eq(t.range_problems(), "", "Weights: the ranges still cover the dice")
|
|
_check(size > 0, "weights were read off the ranges")
|