extends RefCounted ## #42: the panels at phone scale (UiTheme's panels), on the farm's HUD (farm.tscn) with ## MobilePort.forced: ## * Phones (PHONES): a 19.5:9 phone at about 1.6x with a notch and the home indicator inset; an ## iPhone SE at the 1.7 cap (1130x635); a 13 mini at the cap with its insets (the shortest ## room, about 600 tall); and a 4:3 iPad at 1x with an inset. On each, the morning sign (the ## first morning's, in touch words, and the longest later morning's review), the end screens ## (the failure and the final assessment, with the longest totals), and the workbench's and ## the shed's menus sit inside the safe rect (the sign's drawing too: its beam's ends and its ## posts' tops); Start the day, the end screens' buttons, and Close are in view below the ## scrolling part (no scrolling needed to reach them) and at least the touch target tall, as ## is every shop row; the end screens' text wraps to the panel; whatever doesn't fit scrolls, ## and scrolled to its end it's all there. The iPad's room needs no scrolling. ## * Fingers (the 19.5:9 phone): a drag on a shop's list scrolls it and buys nothing, even one ## that comes back to the row it started on; a tap on a row buys it, a little wobble still a ## tap; a drag on the sign scrolls it; Close closes. ## * Desktop (forced 0) at 1920x1080: every rect as it was before #42 (DESKTOP, recorded from the ## code before it), nothing scrolls or wraps, no safe-area margins. const Touch := preload("res://game/tests/mobile/touch.gd") ## name, window pixels, density (0: by the window's shape), insets in window pixels (l, t, r, b). const PHONES := [ ["19.5:9 phone (1.6x), notch and home indicator", Vector2i(2340, 1080), 0.0, [140, 0, 140, 60]], ["iPhone SE (1.7x cap)", Vector2i(1334, 750), 326.0, []], ["iPhone 13 mini (1.7x cap), notch and home indicator", Vector2i(2340, 1080), 476.0, [140, 0, 140, 60]], ["4:3 iPad (1x), status bar and home indicator", Vector2i(1600, 1200), 0.0, [0, 40, 0, 40]], ] ## The desktop layout at 1920x1080 before #42 (position, size), with the content _later(), ## _failure(), _assessment(), and _fixed() give. const DESKTOP := { "sign, first morning": Rect2(408, 179, 1104, 721), "sign, first morning: Start the day": Rect2(810, 736, 300, 62), "sign, later morning": Rect2(408, 119, 1104, 841), "sign, later morning: Start the day": Rect2(810, 796, 300, 62), "sign, later morning: the results": Rect2(598, 315, 724, 202), "sign, later morning: the lists": Rect2(520, 545, 880, 225), "workbench": Rect2(373, 124, 1174, 831), "workbench: first row": Rect2(407, 254, 1106, 46), "workbench: last row": Rect2(407, 827, 1106, 46), "workbench: Close": Rect2(860, 879, 200, 48), "shed": Rect2(373, 237, 1174, 605), "shed: first row": Rect2(407, 367, 1106, 46), "shed: last row": Rect2(407, 714, 1106, 46), "shed: Close": Rect2(860, 766, 200, 48), "failure": Rect2(333, 265, 1254, 549), "failure: Retry the night": Rect2(561, 722, 544, 56), "failure: New vacation": Rect2(1129, 722, 230, 56), "failure: totals": Rect2(369, 554, 1182, 152), "assessment": Rect2(333, 235, 1254, 609), "assessment: New vacation": Rect2(845, 752, 230, 56), "assessment: totals": Rect2(369, 615, 1182, 121), "assessment: shares kept": Rect2(511, 490, 898, 109), } ## Every shop row's text for the desktop check (the real ones follow the tuning). const FIXED_ROW := "Long-range defenses II — Turrets reach 15% farther (18 wood, 26 scrap) Need 16 wood, 24 scrap more" const FIXED_TITLE := "Upgrades at the work area (0 of 10 done)" var t var farm: Node3D var sim: FarmSim func run(p_t) -> void: t = p_t var root: Window = t.root MobilePort.forced = 0 root.size = Vector2i(1920, 1080) GameSettings.new().apply_scale(root) farm = (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D root.add_child(farm) await t.process_frame await t.physics_frame sim = farm.get_node("FarmSim") as FarmSim sim.set_physics_process(false) sim.waves.enabled = false if sim.dialogue != null: sim.dialogue.set_process(false) # no conversation over the panels await _desktop() MobilePort.forced = 1 for phone: Array in PHONES: await _phone(phone) await _fingers() MobilePort.forced = -1 MobilePort.preview_dpi = 0.0 MobilePort.preview_insets = [] farm.queue_free() await t.process_frame func _settle() -> void: for i in 3: await t.process_frame ## Desktop: each panel exactly where and as big as before #42. func _desktop() -> void: var got := {} var c := sim.campaign var sign := sim.review_panel c.new_campaign() sign.refresh() await _settle() got["sign, first morning"] = sign._board.get_global_rect() got["sign, first morning: Start the day"] = sign._start.get_global_rect() _later(c) sign.refresh() await _settle() got["sign, later morning"] = sign._board.get_global_rect() got["sign, later morning: Start the day"] = sign._start.get_global_rect() got["sign, later morning: the results"] = sign._grid.get_global_rect() got["sign, later morning: the lists"] = sign._lists.get_global_rect() t.check(sign._scroll.vertical_scroll_mode == ScrollContainer.SCROLL_MODE_DISABLED and not sign._scroll.clip_contents and _margins(sign._safe) == Vector4.ZERO and sign._start_gap.visible, "desktop: the sign doesn't scroll and has no safe-area margins") c.begin_day() for p: Array in [[sim.upgrade_panel, "workbench"], [sim.shed_panel, "shed"]]: var panel := p[0] as UpgradePanel panel.open() _fixed(panel) await _settle() var rows := panel._rows.values() got[p[1]] = panel._panel.get_global_rect() got[p[1] + ": first row"] = (rows[0] as Control).get_global_rect() got[p[1] + ": last row"] = (rows[-1] as Control).get_global_rect() got[p[1] + ": Close"] = panel._close.get_global_rect() t.check(panel._scroll.vertical_scroll_mode == ScrollContainer.SCROLL_MODE_DISABLED and not panel._scroll.clip_contents and _margins(panel._safe) == Vector4.ZERO and rows.all(func(b: Button) -> bool: return b.autowrap_mode == TextServer.AUTOWRAP_OFF), "desktop: the %s's rows don't scroll or wrap" % p[1]) panel.close() var end := sim.end_panel _failure(c) end.show_failure() await _settle() got["failure"] = end._panel.get_global_rect() got["failure: Retry the night"] = end._retry.get_global_rect() got["failure: New vacation"] = end._new.get_global_rect() got["failure: totals"] = end._stats.get_global_rect() t.check(end._stats.autowrap_mode == TextServer.AUTOWRAP_OFF and end._scroll.vertical_scroll_mode == ScrollContainer.SCROLL_MODE_DISABLED and _margins(end._safe) == Vector4.ZERO, "desktop: the end screen's text doesn't wrap or scroll") _assessment(c) end.show_assessment() await _settle() got["assessment"] = end._panel.get_global_rect() got["assessment: New vacation"] = end._new.get_global_rect() got["assessment: totals"] = end._stats.get_global_rect() got["assessment: shares kept"] = end._bars.get_global_rect() end.hide_panel() var moved: Array[String] = [] for k: String in DESKTOP: var want: Rect2 = DESKTOP[k] var r: Rect2 = got.get(k, Rect2()) if not (r.position.distance_to(want.position) < 0.5 and r.size.distance_to(want.size) < 0.5): moved.append("%s: %s (was %s)" % [k, r, want]) t.check(moved.is_empty(), "desktop 1920x1080: all %d panel rects are as before %s" % [DESKTOP.size(), moved]) ## One phone: every panel in its safe rect, the buttons in view and big enough, the rest scrolling. func _phone(phone: Array) -> void: var name: String = phone[0] var root: Window = t.root MobilePort.preview_dpi = phone[2] MobilePort.preview_insets = phone[3] root.size = phone[1] GameSettings.new().apply_scale(root) await _settle() var safe := MobilePort.safe_rect(sim.review_panel) var target := MobilePort.minimum_target(sim.review_panel) var ipad := is_equal_approx(root.content_scale_factor, 1.0) t.info("%s: canvas %s, safe rect %s, touch target %.0f" % [name, root.get_visible_rect().size, safe, target]) var c := sim.campaign var sign := sim.review_panel c.new_campaign() sign.refresh() await _settle() t.check(sign._intro.visible and sign._intro.text.contains("(hold Use)"), "%s: (the first morning's sign, in touch words)" % name) _check_sign(name + ", the first morning's sign", sign, safe, target, sign._intro, ipad) await _reach_end(name + ", the first morning's sign", sign._scroll, sign._intro) _later(c) sign.refresh() await _settle() t.check(sign._lists.visible and sign.enemies_text().count(",") == 3 and sign.materials_text().count(",") == 3, "%s: (the later morning's review, every enemy and material listed)" % name) _check_sign(name + ", the later morning's review", sign, safe, target, sign._lists, ipad) await _reach_end(name + ", the later morning's review", sign._scroll, sign._lists) c.begin_day() await t.process_frame for p: Array in [[sim.upgrade_panel, "workbench"], [sim.shed_panel, "shed"]]: var panel := p[0] as UpgradePanel panel.open() await _settle() _check_shop("%s, the %s's menu" % [name, p[1]], panel, safe, target, ipad) await _reach_end("%s, the %s's menu" % [name, p[1]], panel._scroll, panel._rows.values()[-1]) panel.close() var end := sim.end_panel _failure(c) end.show_failure() await _settle() _check_end(name + ", the failure screen", end, safe, target, [end._retry, end._new]) await _reach_end(name + ", the failure screen", end._scroll, end._stats) _assessment(c) end.show_assessment() await _settle() _check_end(name + ", the final assessment", end, safe, target, [end._new]) await _reach_end(name + ", the final assessment", end._scroll, end._stats) end.hide_panel() func _check_sign(what: String, sign: ReviewPanel, safe: Rect2, target: float, last: Control, ipad: bool) -> void: var board := sign._board.get_global_rect() var top := sign._post_top + ReviewPanel.END_GRAIN var drawn := Rect2(board.position - Vector2(ReviewPanel.BEAM_END, top), board.size + Vector2(2.0 * ReviewPanel.BEAM_END, top)) t.check(_inside(safe, drawn), "%s: inside the safe rect, its beam's ends and posts' tops too (%s in %s)" % [what, drawn, safe]) var start := sign._start.get_global_rect() var paper := sign._scroll.get_global_rect() t.check(start.size.y >= target - 0.01 and _inside(board, start) and start.position.y >= paper.end.y, "%s: Start the day is in view below the text, %.0f tall (target %.0f)" % [what, start.size.y, target]) t.check(_inside(board, paper), "%s: the text's view is on the paper" % what) if ipad: t.check(not sign._scroll.scrolls() and _inside(paper, last.get_global_rect()), "%s: it all fits, no scrolling" % what) func _check_shop(what: String, panel: UpgradePanel, safe: Rect2, target: float, ipad: bool) -> void: var rect := panel._panel.get_global_rect() t.check(_inside(safe, rect), "%s: inside the safe rect (%s in %s)" % [what, rect, safe]) var close := panel._close.get_global_rect() var list := panel._scroll.get_global_rect() t.check(close.size.y >= target - 0.01 and _inside(rect, close) and close.position.y >= list.end.y, "%s: Close is in view below the list, %.0f tall" % [what, close.size.y]) var short: Array[String] = [] var wide := 0 for id: String in panel._rows: var r := (panel._rows[id] as Control).get_global_rect() if r.size.y < target - 0.01: short.append("%s %.0f" % [id, r.size.y]) if r.position.x < list.position.x - 0.5 or r.end.x > list.end.x + 0.5: wide += 1 t.check(short.is_empty() and wide == 0, "%s: every row (%d) is at least %.0f tall and fits across, its text wrapped %s" % [what, panel._rows.size(), target, short]) if ipad: t.check(not panel._scroll.scrolls(), "%s: all its rows fit, no scrolling" % what) func _check_end(what: String, end: EndPanel, safe: Rect2, target: float, buttons: Array) -> void: var rect := end._panel.get_global_rect() t.check(_inside(safe, rect), "%s: inside the safe rect (%s in %s)" % [what, rect, safe]) var view := end._scroll.get_global_rect() var ok := true for b: Button in buttons: var r := b.get_global_rect() ok = ok and b.visible and r.size.y >= target - 0.01 and _inside(rect, r) and r.position.y >= view.end.y t.check(ok, "%s: its buttons are in view below the text, at least %.0f tall" % [what, target]) var stats := end._stats.get_global_rect() t.check(end._stats.autowrap_mode != TextServer.AUTOWRAP_OFF and stats.position.x >= view.position.x - 0.5 and stats.end.x <= view.end.x + 0.5, "%s: the totals wrap to the panel (%d lines in %.0f px)" % [what, end._stats.get_line_count(), stats.size.x]) ## Scrolled to its end, the last of a panel's scrolling part is in its view. func _reach_end(what: String, scroll: UiTheme.TouchScroll, last: Control) -> void: scroll.scroll_vertical = 1000000 await _settle() var view := scroll.get_global_rect() var r := last.get_global_rect() t.check(r.end.y <= view.end.y + 0.5 and r.end.y >= view.position.y + minf(r.size.y, view.size.y) - 0.5, "%s: scrolled to the end, all of it is there (%s scrolls: %s)" % [what, "it" if scroll.scrolls() else "nothing", scroll.scrolls()]) scroll.scroll_vertical = 0 await _settle() ## Fingers on the 19.5:9 phone: drags scroll and never buy; taps buy; Close closes. func _fingers() -> void: var root: Window = t.root MobilePort.preview_dpi = 0.0 MobilePort.preview_insets = [140, 0, 140, 60] root.size = Vector2i(2340, 1080) GameSettings.new().apply_scale(root) await _settle() var c := sim.campaign var slop := UiTheme.TouchScroll.DRAG_PT * MobilePort.minimum_target(sim.review_panel) / MobilePort.TARGET_PT # The sign: a drag scrolls its paper. c.new_campaign() var s := c.state _later(c) sim.review_panel.refresh() await _settle() var paper := sim.review_panel._scroll var at := paper.get_global_rect().get_center() t.check(paper.scrolls(), "(the later morning's review scrolls on this phone)") await _drag(at, at + Vector2(0.0, -120.0)) t.check(paper.scroll_vertical >= 110 and s.phase == CampaignController.Phase.MORNING, "a finger dragging the sign's paper up scrolls it (%d px), and the day doesn't start" % paper.scroll_vertical) c.begin_day() await _settle() t.check(not sim.review_panel.visible, "(day: the sign is down)") for p: Array in [[sim.upgrade_panel, "workbench"], [sim.shed_panel, "shed"]]: var panel := p[0] as UpgradePanel var shop: String = p[1] s.resources.shed = {"wood": 9999, "scrap": 9999, "straw": 9999, "alien_goop": 9999} s.night = 20 # everything in stock s.upgrades.clear() s.upgrades["corn_shooter"] = 1 # (the corn shooter's upgrades open) var bought: Array[String] = [] var count := func(id: String) -> void: bought.append(id) panel.bought.connect(count) panel.open() await _settle() var list := panel._scroll t.check(list.scrolls(), "(the %s's list scrolls on this phone)" % shop) # Press on a row and drag up: the list scrolls, nothing's bought. var row := _row_in_view(panel) var from := (panel._rows[row] as Control).get_global_rect().get_center() var levels := s.upgrades.duplicate() await _drag(from, from + Vector2(0.0, -150.0)) t.check(list.scroll_vertical >= 140 and bought.is_empty() and s.upgrades == levels, "%s: a finger that presses a row and drags up scrolls the list (%d px) and buys nothing" % [shop, list.scroll_vertical]) # Press, drag down, and back up to the same row: still a drag, not a buy. row = _row_in_view(panel) from = (panel._rows[row] as Control).get_global_rect().get_center() var was := list.scroll_vertical Touch.touch(0, from, true) Touch.drag(0, from, from + Vector2(0.0, 80.0)) Touch.drag(0, from + Vector2(0.0, 80.0), from) await t.process_frame Touch.touch(0, from, false) await _settle() t.check(bought.is_empty() and s.upgrades == levels and list.scroll_vertical == was and panel.is_open, "%s: one that drags away and comes back to the row it pressed buys nothing either" % shop) # A tap (with a little wobble, under the slop) buys. row = _row_in_view(panel) from = (panel._rows[row] as Control).get_global_rect().get_center() Touch.touch(0, from, true) Touch.drag(0, from, from + Vector2(0.0, slop * 0.4)) Touch.touch(0, from + Vector2(0.0, slop * 0.4), false) await _settle() t.check(bought == [row] and Upgrades.level(s, row) == int(levels.get(row, 0)) + 1, "%s: a tap on a row buys it, even with a little wobble (%s)" % [shop, bought]) row = _row_in_view(panel) Touch.tap(0, (panel._rows[row] as Control).get_global_rect().get_center()) await _settle() t.check(bought.size() == 2 and bought[1] == row, "%s: and a plain tap on another buys that (%s)" % [shop, bought]) Touch.tap(0, panel._close.get_global_rect().get_center()) await _settle() t.check(not panel.is_open and not panel.visible, "%s: a tap on Close closes the menu" % shop) panel.bought.disconnect(count) ## A row all in the list's view that can be bought. func _row_in_view(panel: UpgradePanel) -> String: var view := panel._scroll.get_global_rect() for id: String in panel._rows: var b := panel._rows[id] as Button if not b.disabled and view.encloses(b.get_global_rect()): return id return "" ## Finger 0 presses at `from`, drags to `to` in one move, and lifts there. func _drag(from: Vector2, to: Vector2) -> void: Touch.touch(0, from, true) Touch.drag(0, from, to) await t.process_frame Touch.touch(0, to, false) await _settle() static func _inside(outer: Rect2, inner: Rect2) -> bool: return outer.grow(0.5).encloses(inner) static func _margins(m: MarginContainer) -> Vector4: return Vector4(m.get_theme_constant("margin_left"), m.get_theme_constant("margin_top"), m.get_theme_constant("margin_right"), m.get_theme_constant("margin_bottom")) ## The longest later morning: 28 nights logged, the last with night 3's title (the longest), every ## loss, every enemy type and material, and three-digit counts. static func _later(c: CampaignController) -> void: var s := c.state s.night_log.clear() for n in range(1, 29): var e := {} for key in FarmState.LOG_KEYS: e[key] = 0 e["night"] = n for key in ["crops_stolen", "crops_recovered", "crops_lost", "animals_stolen", "animals_strayed", "animals_recovered", "animals_lost", "supplies_stolen", "supplies_recovered", "supplies_lost"]: e[key] = 12 + n for key in ["killed_crow", "killed_thief", "killed_breaker", "killed_ufo"]: e[key] = 100 + n * 3 for key in ["gathered_wood", "gathered_scrap", "gathered_straw", "gathered_alien_goop", "spent_wood", "spent_scrap", "spent_straw", "spent_alien_goop"]: e[key] = 140 + n for key in ["escaped", "scarecrow_damage", "patches", "structures_built", "structures_damaged", "structures_destroyed", "repairs", "upgrades_bought"]: e[key] = 9 + n s.night_log.append(e) s.night_log[s.night_log.size() - 1]["night"] = 3 s.night = 29 ## The failure screen's longest: the scarecrow fallen on night 29 of a vacation like _later's. static func _failure(c: CampaignController) -> void: _later(c) c.outcome = CampaignController.Outcome.GAME_OVER_SCARECROW c.state.night = 29 static func _assessment(c: CampaignController) -> void: _later(c) c.outcome = CampaignController.Outcome.VACATION_COMPLETE ## Fixed row and title texts, so the desktop check doesn't follow the costs' tuning. static func _fixed(p: UpgradePanel) -> void: p._title.text = FIXED_TITLE for id: String in p._rows: (p._rows[id] as Button).text = FIXED_ROW