TableTower/tests/test_card_size.gd
RobinFiredrake 9f06af9676 Fix: resize corner caught clicks far outside cards; left-drag selects in Play too
CardGrip._has_point only checked 'right of and below' the corner, and
Godot asks controls about points outside their box, so everything to the
bottom-right of every card acted as its resize handle. It's now bounded to
the corner square inside the card.

A left-drag on empty space now draws a selection box in Play as well as
Build, and selected cards move together in both; panning is middle-drag or
Space+drag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 19:38:15 -06:00

107 lines
4.3 KiB
GDScript

extends SceneTree
## Resizable cards: the grip's corner hit area, a dragged width and list
## height (the list scrolls), saving them, and double-click back to automatic.
## Run: godot --headless --path . -s res://tests/test_card_size.gd -- --sample
var _failures := 0
var main: Main
func _init() -> void:
if not OS.get_cmdline_user_args().has("--sample"):
print("Run with -- --sample so the test can't touch saved data.")
quit(2)
return
main = load("res://ui/main.tscn").instantiate()
root.add_child(main)
await _frames(5)
await _test_resize()
await _test_compact()
await _test_play_drag_selects()
print("%s: %d failure(s)" % ["PASS" if _failures == 0 else "FAIL", _failures])
quit(1 if _failures else 0)
func _test_resize() -> void:
var t := main.data.get_table("road")
var card := main.card_for("road")
var grip := card.grip
_check(grip != null and grip.get_index() == card.get_child_count() - 2 and card.shield.get_index() == card.get_child_count() - 1, "the grip, then the shield, are the card's last children")
_check(grip._has_point(grip.size - Vector2(4, 4)) and not grip._has_point(Vector2(4, 4)), "only the corner takes the mouse")
_check(not grip._has_point(grip.size + Vector2(5, 5)) and not grip._has_point(grip.size + Vector2(300, -4)) and not grip._has_point(Vector2(grip.size.x - 4, grip.size.y + 200)), "nothing outside the card is the resize corner")
var auto_w := card.size.x
card.set_user_size(380.0, 120.0)
await _frames()
_check(is_equal_approx(card.size.x, 380.0 + card.extra_width()), "the card takes the dragged width")
_check(card.list_scroll != null and is_equal_approx(card.list_scroll.size.y, 120.0), "a set height makes the results scroll")
_check(card.grip.get_index() == card.get_child_count() - 2, "the grip stays on top after a rebuild")
var d := t.to_dict()
_check(d.get("w") == 380 and d.get("list_h") == 120, "the size is saved")
var back := RollTable.from_dict(d)
_check(back.width == 380.0 and back.list_height == 120.0, "and loaded")
card.set_user_size(0.0, 0.0)
await _frames()
_check(is_equal_approx(card.size.x, auto_w) and card.list_scroll == null, "back to automatic")
## Zoomed out, cards take only drag, right-click and (in Play) a click to roll.
func _test_compact() -> void:
main.board.set_zoom(0.3)
await _frames()
var card := main.card_for("road")
_check(main.board.compact and card.shield.visible, "zoomed out, the shield covers the card")
_check(card.shield.tooltip_text.begins_with("Road Encounter") and card.shield.tooltip_text.contains("Click to roll"), "its name is a tooltip: " + card.shield.tooltip_text)
var click := InputEventMouseButton.new()
click.button_index = MOUSE_BUTTON_LEFT
click.pressed = true
click.global_position = Vector2(10, 10)
card.shield._gui_input(click)
var up := click.duplicate() as InputEventMouseButton
up.pressed = false
card.shield._gui_input(up)
await _frames(3)
_check(main.current.get("table_id", "") == "road" or str(main.current).contains("road"), "a click rolls it in Play")
main.board.set_zoom(1.0)
await _frames()
_check(not main.board.compact and not card.shield.visible, "zoomed in, the shield is gone")
## In Play, a left-drag on empty space draws a selection box; it doesn't pan.
func _test_play_drag_selects() -> void:
main.set_mode(Main.Mode.PLAY)
main.clear_selection()
main.board.set_zoom(0.5)
await _frames()
var card := main.card_for("road")
var r := card.get_global_rect()
var from := r.position - Vector2(20, 20) - main.board.global_position
var to := r.position + Vector2(40, 40) - main.board.global_position
var before := main.board.world.position
var press := InputEventMouseButton.new()
press.button_index = MOUSE_BUTTON_LEFT
press.pressed = true
press.position = from
main.board._gui_input(press)
var move := InputEventMouseMotion.new()
move.position = to
move.relative = to - from
main.board._gui_input(move)
var up := press.duplicate() as InputEventMouseButton
up.pressed = false
up.position = to
main.board._gui_input(up)
await _frames()
_check(main.board.world.position == before, "a left-drag doesn't pan")
_check(main.selection.has("road"), "it selects what the box touches")
main.clear_selection()
func _frames(n: int = 2) -> void:
for i in n:
await process_frame
func _check(ok: bool, what: String) -> void:
if not ok:
_failures += 1
print("FAIL ", what)