Build mode: drag a result's handle to reorder a card's results
Ranged cards keep their ranges in place so the results move through them. The router case arrows share the same move_entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
2d247e34b4
commit
194f157140
6 changed files with 206 additions and 6 deletions
58
tests/test_entry_drag.gd
Normal file
58
tests/test_entry_drag.gd
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
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")
|
||||
|
||||
|
||||
func _test_ranged() -> void:
|
||||
var t := main.data.get_table("wilderness")
|
||||
var card := main.card_for(t.id)
|
||||
var ranges := t.entries.map(func(e: TableEntry) -> Vector2i: return Vector2i(e.range_lo, e.range_hi))
|
||||
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()
|
||||
await _frames()
|
||||
_eq(t.entries.find(t.find_entry(moved)), 2, "the result moved down two places")
|
||||
_eq(t.entries.map(func(e: TableEntry) -> Vector2i: return Vector2i(e.range_lo, e.range_hi)), ranges,
|
||||
"the ranges stay in place")
|
||||
_eq(t.range_problems(), "", "the ranges still cover the dice")
|
||||
1
tests/test_entry_drag.gd.uid
Normal file
1
tests/test_entry_drag.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://c2kadad08k3dj
|
||||
124
ui/entry_handle.gd
Normal file
124
ui/entry_handle.gd
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
class_name EntryHandle
|
||||
extends Control
|
||||
## Build mode: the six-dot drag handle at the start of a result row. Drag it
|
||||
## up or down the card to reorder results; a line shows where it will land.
|
||||
## On a card with number ranges the ranges stay in place and the results move
|
||||
## through them (TableCardEditing.move_entry).
|
||||
|
||||
const W := 10.0
|
||||
|
||||
var card: TableCard
|
||||
var eid: String
|
||||
var _dragging := false
|
||||
var _target := -1
|
||||
var _line: ColorRect
|
||||
## Holds the line: the card is a container, so a direct child would be stretched.
|
||||
var _holder: Control
|
||||
|
||||
|
||||
func _init(p_card: TableCard, p_eid: String) -> void:
|
||||
card = p_card
|
||||
eid = p_eid
|
||||
custom_minimum_size = Vector2(W, 22)
|
||||
size_flags_vertical = Control.SIZE_SHRINK_CENTER
|
||||
mouse_filter = MOUSE_FILTER_STOP
|
||||
mouse_default_cursor_shape = CURSOR_MOVE
|
||||
tooltip_text = "Drag to move this result"
|
||||
mouse_entered.connect(queue_redraw)
|
||||
mouse_exited.connect(queue_redraw)
|
||||
|
||||
|
||||
func _draw() -> void:
|
||||
var hot := _dragging or get_global_rect().has_point(get_global_mouse_position())
|
||||
var c := Style.MUTED if hot else Style.FAINT
|
||||
for col in 2:
|
||||
for r in 3:
|
||||
draw_circle(Vector2(2.5 + col * 5.0, size.y / 2.0 + (r - 1) * 5.0), 1.4, c)
|
||||
|
||||
|
||||
func _gui_input(ev: InputEvent) -> void:
|
||||
var mb := ev as InputEventMouseButton
|
||||
if mb and mb.button_index == MOUSE_BUTTON_LEFT:
|
||||
if mb.pressed:
|
||||
_dragging = true
|
||||
_target = -1
|
||||
var row: Control = card.rows.get(eid)
|
||||
if row:
|
||||
row.modulate.a = 0.5
|
||||
elif _dragging:
|
||||
finish()
|
||||
accept_event()
|
||||
elif ev is InputEventMouseMotion and _dragging:
|
||||
track(get_global_mouse_position())
|
||||
accept_event()
|
||||
|
||||
|
||||
## Where the result would land for a pointer at `global`, as an index into
|
||||
## the card's entries once this one is taken out. Shows the insertion line.
|
||||
func track(global: Vector2) -> int:
|
||||
var order := _others()
|
||||
var slot := order.size()
|
||||
for i in order.size():
|
||||
var r := (card.rows[order[i].id] as Control).get_global_rect()
|
||||
if global.y < r.get_center().y:
|
||||
slot = i
|
||||
break
|
||||
var entries := card.table.entries
|
||||
_target = entries.size() - 1
|
||||
if slot < order.size():
|
||||
_target = _index_without_self(order[slot])
|
||||
elif not order.is_empty():
|
||||
_target = _index_without_self(order[-1]) + 1
|
||||
_show_line(order, slot)
|
||||
return _target
|
||||
|
||||
|
||||
## Applies the move and tidies up.
|
||||
func finish() -> void:
|
||||
_dragging = false
|
||||
if _holder and is_instance_valid(_holder):
|
||||
_holder.queue_free()
|
||||
_holder = null
|
||||
_line = null
|
||||
var row: Control = card.rows.get(eid)
|
||||
if row and is_instance_valid(row):
|
||||
row.modulate.a = 1.0
|
||||
queue_redraw()
|
||||
var from := card.table.entries.find(card.table.find_entry(eid))
|
||||
if _target >= 0 and _target != from:
|
||||
card.edit_ui.move_entry(from, _target)
|
||||
|
||||
|
||||
## The other results whose rows are on the card, top to bottom.
|
||||
func _others() -> Array[TableEntry]:
|
||||
var out: Array[TableEntry] = []
|
||||
for e in card.table.entries:
|
||||
var row: Control = card.rows.get(e.id)
|
||||
if e.id != eid and row and is_instance_valid(row) and row.is_visible_in_tree():
|
||||
out.append(e)
|
||||
return out
|
||||
|
||||
|
||||
func _index_without_self(e: TableEntry) -> int:
|
||||
var i := card.table.entries.find(e)
|
||||
var me := card.table.entries.find(card.table.find_entry(eid))
|
||||
return i - 1 if me < i else i
|
||||
|
||||
|
||||
func _show_line(order: Array[TableEntry], slot: int) -> void:
|
||||
if order.is_empty():
|
||||
return
|
||||
var above := slot >= order.size()
|
||||
var r := (card.rows[order[mini(slot, order.size() - 1)].id] as Control).get_global_rect()
|
||||
if _line == null or not is_instance_valid(_line):
|
||||
_holder = Control.new()
|
||||
_holder.mouse_filter = MOUSE_FILTER_IGNORE
|
||||
card.add_child(_holder)
|
||||
_line = ColorRect.new()
|
||||
_line.color = Style.ACCENT
|
||||
_line.mouse_filter = MOUSE_FILTER_IGNORE
|
||||
_holder.add_child(_line)
|
||||
var to_local := _holder.get_global_transform().affine_inverse()
|
||||
var y: float = (to_local * Vector2(r.position.x, r.end.y if above else r.position.y)).y
|
||||
_line.position = Vector2(0, y - 1.5)
|
||||
_line.size = Vector2(_holder.size.x, 3)
|
||||
1
ui/entry_handle.gd.uid
Normal file
1
ui/entry_handle.gd.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://8plsydw4dmto
|
||||
|
|
@ -114,13 +114,27 @@ static func mini_button(glyph: String, tip: String, enabled: bool, on_press: Cal
|
|||
|
||||
## Moves router case `index` up (-1) or down (+1).
|
||||
func _move_case(index: int, by: int) -> void:
|
||||
var table := card.table
|
||||
var to := index + by
|
||||
if to < 0 or to >= table.entries.size():
|
||||
move_entry(index, index + by)
|
||||
|
||||
|
||||
## Moves the result at `from` to `to` (an index in the list without it). On a
|
||||
## card with number ranges the ranges stay where they were, so the results
|
||||
## move through them and the numbers stay in order.
|
||||
func move_entry(from: int, to: int) -> void:
|
||||
var entries := card.table.entries
|
||||
if from == to or from < 0 or from >= entries.size() or to < 0 or to >= entries.size():
|
||||
return
|
||||
var e := table.entries[index]
|
||||
table.entries.remove_at(index)
|
||||
table.entries.insert(to, e)
|
||||
var ranges: Array = []
|
||||
for e in entries:
|
||||
ranges.append([e.has_range, e.range_lo, e.range_hi])
|
||||
var moved := entries[from]
|
||||
entries.remove_at(from)
|
||||
entries.insert(to, moved)
|
||||
if card.table.is_ranged():
|
||||
for i in entries.size():
|
||||
entries[i].has_range = ranges[i][0]
|
||||
entries[i].range_lo = ranges[i][1]
|
||||
entries[i].range_hi = ranges[i][2]
|
||||
card.rebuild()
|
||||
card.main.mark_dirty()
|
||||
|
||||
|
|
|
|||
|
|
@ -86,6 +86,8 @@ func make_row(e: TableEntry, index: int, build: bool, ranged: bool, numw: float)
|
|||
var fixed := table.kind == RollTable.KIND_LOOP or (table.kind == RollTable.KIND_CHOICE and table.choice != "pick")
|
||||
var chosen := _is_chosen_row(index)
|
||||
|
||||
if build and not fixed and not chosen and table.entries.size() > 1:
|
||||
h.add_child(EntryHandle.new(card, e.id))
|
||||
var num: Label = null
|
||||
if build and ranged:
|
||||
var r := TableCard.hbox(2)
|
||||
|
|
|
|||
Loading…
Reference in a new issue