TableTower/tests/test_card_size.gd

190 lines
7.9 KiB
GDScript3
Raw Normal View History

extends AppTestBase
## 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: tools/run_tests.sh card_size
func run() -> void:
if not await start_main():
return
await _test_resize()
await _test_compact()
await _test_play_drag_selects()
await _test_show_less()
await _test_add_scrolls_and_focuses()
await _test_wheel_zooms()
await _test_zoomed_in_anchors()
finish()
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()
## A long card opened with "show all" has "▲ Show less" to fold it up again.
func _test_show_less() -> void:
var t := main.data.get_table("trinkets")
# The suites' demo has short tables: make this one long again.
for entry in SampleBoard.create().get_table("trinkets").entries.slice(t.entries.size()):
t.entries.append(entry)
if not t.collapsed:
main.toggle_collapse("trinkets")
await _frames(2)
main.toggle_collapse("trinkets")
await _frames(2)
var less: Array = []
for node in main.card_for("trinkets").find_children("*", "Button", true, false):
if node.has_meta("show_less"):
less.append(node)
_check(not t.collapsed and less.size() == 1, "an opened long card shows Show less")
if less.size() == 1:
(less[0] as Button).pressed.emit()
await _frames(2)
_check(t.collapsed, "Show less collapses it again")
## "+ Add result" on a card whose list scrolls: the list scrolls to the new
## row and its text field gets the cursor, its starter text selected.
func _test_add_scrolls_and_focuses() -> void:
main.set_mode(Main.Mode.BUILD)
var t := main.data.get_table("road")
t.collapsed = false
var card := main.card_for("road")
card.set_user_size(0.0, 80.0)
# A lit result near the top: the list must still go to the new row.
main.lit_entries[Roller.entry_key("road", t.entries[0].id)] = true
await _frames(3)
var add := _button(card, "+ Add")
_check(add != null, "the card has an add button")
if add == null:
return
add.pressed.emit()
await _frames(3)
var eid := t.entries[-1].id
var edit: LineEdit = card.edit_ui.text_fields.get(eid)
_check(is_instance_valid(edit) and edit.has_focus(), "the new result's field has the cursor")
_check(is_instance_valid(edit) and edit.has_selection() and edit.get_selected_text() == edit.text, "its starter text is selected")
var bar := card.list_scroll.get_v_scroll_bar()
_check(card.list_scroll.scroll_vertical > 0 and card.list_scroll.scroll_vertical >= int(bar.max_value - bar.page) - 1, "the list is scrolled to the bottom")
card.set_user_size(0.0, 0.0)
main.set_mode(Main.Mode.PLAY)
await _frames()
## The wheel zooms the spread; Shift+wheel moves it sideways.
func _test_wheel_zooms() -> void:
main.board.set_zoom(1.0)
var wheel := InputEventMouseButton.new()
wheel.button_index = MOUSE_BUTTON_WHEEL_UP
wheel.pressed = true
wheel.position = main.board.size / 2.0
var y := main.board.world.position.y
main.board._gui_input(wheel)
_check(main.board.zoom > 1.0, "the wheel zooms in")
_check(not is_equal_approx(main.board.world.position.y - y, 60.0), "and doesn't just scroll up")
var x := main.board.world.position.x
var z := main.board.zoom
wheel.shift_pressed = true
main.board._gui_input(wheel)
_check(is_equal_approx(main.board.zoom, z) and is_equal_approx(main.board.world.position.x - x, 60.0), "Shift+wheel moves sideways")
main.board.set_zoom(1.0)
await _frames()
func _button(n: Node, prefix: String) -> Button:
for c in n.get_children():
if c is Button and (c as Button).text.begins_with(prefix):
return c
var found := _button(c, prefix)
if found:
return found
return null
## Zoomed in, a result's dot still sits on its row and shows (the row rect was
## once read at 100% scale, hiding dots low in a scrolling list).
func _test_zoomed_in_anchors() -> void:
var card := main.card_for("road")
card.set_user_size(380.0, 120.0)
main.board.set_zoom(1.5)
await _frames()
var last: String = main.data.get_table("road").entries[-1].id
var row := card.rows[last] as Control
var a := card.anchor_for(last)
var expected := card.position.y + (card.get_global_transform().affine_inverse() * row.get_global_position()).y + row.size.y / 2.0
var in_list := main.board.world_rect_of(card.list_scroll).intersects(main.board.world_rect_of(row))
_check(absf(a["pos"].y - expected) < 1.0 or not in_list, "zoomed in, a result's dot sits on its row: %.1f vs %.1f" % [a["pos"].y, expected])
var scroll_rect := main.board.world_rect_of(card.list_scroll)
_check(is_equal_approx(scroll_rect.size.y, 120.0), "world_rect_of gives the list's size in world units: %.1f" % scroll_rect.size.y)
main.board.set_zoom(1.0)