diff --git a/game/scripts/dialogue_box.gd b/game/scripts/dialogue_box.gd index 23da62a..05a1a66 100644 --- a/game/scripts/dialogue_box.gd +++ b/game/scripts/dialogue_box.gd @@ -12,6 +12,8 @@ extends Control ## [Backspace / Back] skips the rest of the conversation. One that doesn't pause the game takes no ## input and goes on by itself (Conversation.line_hold). Works while the game is paused ## (DialogueDirector pauses it for a conversation); waits while blocked (a menu is up over it). +## On a phone (MobilePort.active()) a tap is the click, and a conversation that pauses the game +## has a Skip button on the paper (a ScarecrowHud.HitTarget) that skips it, as Backspace does. signal finished(conversation: Conversation) @@ -31,12 +33,19 @@ const OPEN_TIME := 0.18 const PAUSES := {".": 0.32, "!": 0.32, "?": 0.32, "…": 0.32, ",": 0.14, ";": 0.14, ":": 0.14} const TALK_FPS := 9.0 const SEED := 701.0 +## Phones: the Skip button drawn on the paper (its hit target may be bigger), its right edge and +## bottom this far in from the paper's bottom-right corner (left of the pen arrow). +const SKIP_SIZE := Vector2(118.0, 50.0) +const SKIP_INSET := Vector2(104.0, 26.0) +const SKIP_FONT := 30 ## Optional: whether a menu is up over the box (it waits: no input, no writing). var blocked: Callable ## The conversation up (null when none), and its line on the paper. var conversation: Conversation var index: int = -1 +## Phones: the Skip button (null on desktop), shown while a conversation pauses the game. +var skip_target: ScarecrowHud.HitTarget static var _narrator := DialogueSpeaker.new() static var _variants := {} # "font id/slant/bold" -> FontVariation @@ -99,6 +108,13 @@ func _ready() -> void: _more.size = Vector2(40.0, 40.0) _more.draw.connect(_draw_more) _sheet.add_child(_more) + if MobilePort.active(): + skip_target = ScarecrowHud.HitTarget.new() + skip_target.name = "Skip" + skip_target.visible = false + skip_target.tapped.connect(_on_skip_tapped) + skip_target.draw.connect(_draw_skip) + _sheet.add_child(skip_target) _voice = AudioStreamPlayer.new() _voice.bus = &"SFX" add_child(_voice) @@ -368,10 +384,25 @@ func _place() -> void: _text.size = Vector2(inner, r.size.y - top - 36.0) _more.position = Vector2(r.size.x - 50.0 - _more.size.x, r.size.y - 30.0 - _more.size.y) _more.visible = conversation != null and conversation.pause_game and is_written() + if skip_target != null: + skip_target.visible = conversation != null and conversation.pause_game + skip_target.cover(skip_rect()) + skip_target.queue_redraw() _stage.queue_redraw() _more.queue_redraw() +## Phones: where the Skip button is drawn on the paper (the sheet's coordinates). +func skip_rect() -> Rect2: + var r := _sheet_rect() + return Rect2(r.size - SKIP_INSET - SKIP_SIZE, SKIP_SIZE) + + +func _on_skip_tapped() -> void: + if conversation != null and conversation.pause_game and not is_blocked(): + skip() + + func _sheet_rect() -> Rect2: var w := minf(WIDTH, size.x - 2.0 * SIDE_GAP) return Rect2((size.x - w) * 0.5, size.y - BOTTOM - HEIGHT, w, HEIGHT) @@ -450,6 +481,25 @@ func _frame(h: Head) -> Texture2D: return h.face if k == 0 else sp.talk_frames[k - 1] +## Phones: the Skip button, a pen-drawn box on the paper with the word and a double arrow. +func _draw_skip() -> void: + var ink := Sketch.PAPER_INK + var r := Rect2((skip_target.size - SKIP_SIZE) * 0.5, SKIP_SIZE) + Sketch.box(skip_target, r, Color(Sketch.PAPER.darkened(0.1), 0.9), Color(ink, 0.85), 2.0, SEED + 21.0) + var word := Sketch.text_width("Skip", SKIP_FONT) + var arrows := 26.0 + var x := r.get_center().x - (word + 8.0 + arrows) * 0.5 + var f := Sketch.font() + var top := r.get_center().y - (f.get_ascent(SKIP_FONT) + f.get_descent(SKIP_FONT)) * 0.5 + Sketch.text(skip_target, Vector2(x, top), "Skip", SKIP_FONT, ink, HORIZONTAL_ALIGNMENT_LEFT, 0) + var c := Vector2(x + word + 8.0, r.get_center().y) + for k in 2: + var a := c + Vector2(k * 12.0, 0.0) + var tip := PackedVector2Array([a + Vector2(0.0, -9.0), a + Vector2(13.0, 0.0), a + Vector2(0.0, 9.0)]) + skip_target.draw_colored_polygon(tip, Color(ink, 0.85)) + Sketch.outline(skip_target, tip, ink, 1.5, SEED + 30.0 + k, 0.6, 1) + + ## The pen arrow in the corner when the line is written and the player goes on. func _draw_more() -> void: var ink: Color = _text.get_theme_color("default_color") diff --git a/game/scripts/prompts.gd b/game/scripts/prompts.gd index feadbc8..6fc580f 100644 --- a/game/scripts/prompts.gd +++ b/game/scripts/prompts.gd @@ -132,3 +132,11 @@ static func sign_night() -> String: if touch(): return ("At night, stab aliens with your pitchfork (%s thumb), scare them off (Fear), and pick up anything they drop. Animals roam loose until you lead them into the pen: walk up to one and it follows you. The supply shed sells a corn shooter (switch weapons on the hotbar), and more weapons arrive there over the days." % attack_side()) return "At night, stab aliens with your pitchfork (left mouse), scare them off (Q), and pick up anything they drop. Animals roam loose until you lead them into the pen: walk up to one and it follows you. The supply shed sells a corn shooter (switch weapons with Tab or the number keys), and more weapons arrive there over the days." + + +# --- The hotbar --- + +## A hotbar slot's number: the key that picks its weapon (ScarecrowHud; 0: none, the off-hand's). +## Phones pick by tapping the slot, so there it's none. +static func hotbar_key(number: int) -> String: + return "" if touch() or number <= 0 else str(number) diff --git a/game/scripts/resource_hud.gd b/game/scripts/resource_hud.gd index e425f48..de64f6e 100644 --- a/game/scripts/resource_hud.gd +++ b/game/scripts/resource_hud.gd @@ -4,6 +4,10 @@ extends Control ## beside it, and a red dot once something new has gone into it since it was last opened (gone ## again when the pouch is emptied). The pouch action (I / R3) opens the pouch panel over it, and ## closes it again: each material carried, and how much of each the supply shed holds. +## On a phone (MobilePort.active()) the backpack is a hit target (ScarecrowHud.HitTarget): a tap +## opens or closes the panel, and a finger landing anywhere else but on the panel closes it (and +## goes on to whatever is there: a thumbstick, a button). There the panel stays inside the safe +## area (MobilePort.safe_rect). ## The backpack icon, from the screen's bottom-left corner. const ICON := Rect2(26.0, -136.0, 108.0, 108.0) @@ -19,8 +23,11 @@ var slot: int = 0 var open := false ## Something went into the pouch since it was last opened. var new_items := false +## Phones: the backpack's hit target (null on desktop). +var backpack_target: ScarecrowHud.HitTarget var _last := {} +var _phone := false func setup(p_campaign: CampaignController) -> void: @@ -31,6 +38,24 @@ func setup(p_campaign: CampaignController) -> void: func _ready() -> void: set_anchors_preset(Control.PRESET_FULL_RECT) mouse_filter = Control.MOUSE_FILTER_IGNORE + _phone = MobilePort.active() + set_process_input(_phone) + if _phone: + backpack_target = ScarecrowHud.HitTarget.new() + backpack_target.name = "Backpack" + backpack_target.visible = false + backpack_target.tapped.connect(toggle) + add_child(backpack_target) + + +## Phones: a finger landing outside the backpack and the open panel closes the panel. It isn't +## taken: it goes on to the HUD's buttons and the thumbsticks. +func _input(event: InputEvent) -> void: + var touch := event as InputEventScreenTouch + if touch == null or not touch.pressed or not open or campaign == null or not is_visible_in_tree(): + return + if not backpack_target.get_global_rect().has_point(touch.position) and not panel_rect().has_point(touch.position): + toggle() func _unhandled_input(event: InputEvent) -> void: @@ -45,6 +70,22 @@ func toggle() -> void: new_items = false +## The backpack icon, at the screen's bottom-left. +func icon_rect() -> Rect2: + return Rect2(Vector2(ICON.position.x, size.y + ICON.position.y), ICON.size) + + +## The pouch panel, over the backpack; on a phone moved inside the safe area if it isn't. +func panel_rect() -> Rect2: + var icon := icon_rect() + var height := 52.0 + ResourceStore.MATERIALS.size() * ROW + 12.0 # its heading, then a row per material (rows()) + var r := Rect2(Vector2(icon.position.x - 4.0, icon.position.y - 14.0 - height), Vector2(PANEL_WIDTH, height)) + if _phone: + var safe := MobilePort.safe_rect(self) + r.position = r.position.clamp(safe.position, (safe.end - r.size).max(safe.position)) + return r + + ## "6/15": what the pouch holds, of what it can. func count_text() -> String: var r := campaign.state.resources @@ -62,6 +103,9 @@ func rows() -> Array[Dictionary]: func _process(_delta: float) -> void: + if backpack_target != null: + backpack_target.visible = campaign != null + backpack_target.cover(icon_rect()) if campaign == null: return var r := campaign.state.resources @@ -78,7 +122,7 @@ func _process(_delta: float) -> void: func _draw() -> void: if campaign == null: return - var icon := Rect2(Vector2(ICON.position.x, size.y + ICON.position.y), ICON.size) + var icon := icon_rect() Sketch.icon(self, "backpack", icon) if new_items: var at := icon.position + Vector2(icon.size.x - 16.0, 16.0) @@ -86,13 +130,12 @@ func _draw() -> void: Sketch.circle(self, at, 11.5, Sketch.INK, 2.0, 402.0) Sketch.text(self, Vector2(icon.end.x + 4.0, icon.get_center().y - 18.0), count_text(), 42) if open: - _draw_panel(icon) + _draw_panel() -func _draw_panel(icon: Rect2) -> void: +func _draw_panel() -> void: var list := rows() - var height := 52.0 + list.size() * ROW + 12.0 - var r := Rect2(Vector2(icon.position.x - 4.0, icon.position.y - 14.0 - height), Vector2(PANEL_WIDTH, height)) + var r := panel_rect() Sketch.panel(self, r, 411.0) var pouch_x := r.position.x + 290.0 var shed_x := r.end.x - 26.0 diff --git a/game/scripts/scarecrow_hud.gd b/game/scripts/scarecrow_hud.gd index 826e409..665e928 100644 --- a/game/scripts/scarecrow_hud.gd +++ b/game/scripts/scarecrow_hud.gd @@ -12,6 +12,9 @@ extends Control ## bolder frame, darkened from the top while a bomb recharges. Hidden in build mode (BuildHud ## has the bottom then). ## * A red tint over the screen when the scarecrow is hit. +## On a phone (MobilePort.active()) each main weapon's slot is a hit target (HitTarget, below): a +## tap picks that weapon as its number key does (tap_slot), and the slots show no numbers +## (Prompts.hotbar_key). The off-hand's slot isn't one (there's nothing to pick; Bomb throws). const PORTRAIT_CENTER := Vector2(78.0, 76.0) const PORTRAIT_RADIUS := 50.0 @@ -36,7 +39,11 @@ var combat: ScarecrowCombat var campaign: CampaignController ## Set by FarmSim: the hotbar hides in build mode. var build: BuildMode +## Phones: the main weapons' slots' hit targets, left to right (the ones past the slots shown are +## hidden). Empty on desktop. +var slot_targets: Array[HitTarget] = [] +var _phone := false var _tint: ColorRect var _hit: float = 0.0 var _ready_flash: float = 0.0 @@ -75,6 +82,7 @@ func _ready() -> void: _tint.color = Color(0.85, 0.05, 0.0, 0.0) _tint.mouse_filter = Control.MOUSE_FILTER_IGNORE add_child(_tint) + _phone = MobilePort.active() func _process(delta: float) -> void: @@ -82,25 +90,87 @@ func _process(delta: float) -> void: _hit = maxf(_hit - delta * 2.5, 0.0) _ready_flash = maxf(_ready_flash - delta * 2.0, 0.0) _tint.color.a = 0.22 * _hit + if _phone: + _place_targets() queue_redraw() ## The hotbar's slots as shown, left to right: {weapon, key (the number that picks it; 0 for -## the off-hand), offhand, selected}. Empty while it's hidden (build mode, no weapons). +## the off-hand), label (the number drawn in the slot: Prompts.hotbar_key), offhand, selected}. +## Empty while it's hidden (build mode, no weapons). func slots() -> Array[Dictionary]: var out: Array[Dictionary] = [] if combat == null or (build != null and build.active): return out var owned := combat.owned_main() for k in owned.size(): - out.append({"weapon": owned[k], "key": k + 1 if k < ScarecrowCombat.SWITCH_ACTIONS.size() - 1 else 0, + var key := k + 1 if k < ScarecrowCombat.SWITCH_ACTIONS.size() - 1 else 0 + out.append({"weapon": owned[k], "key": key, "label": Prompts.hotbar_key(key), "offhand": false, "selected": owned[k] == combat.weapon}) for w in ScarecrowCombat.OFFHAND_WEAPONS: if combat.owns(w): - out.append({"weapon": w, "key": 0, "offhand": true, "selected": _offhand_lit(w)}) + out.append({"weapon": w, "key": 0, "label": Prompts.hotbar_key(0), "offhand": true, "selected": _offhand_lit(w)}) return out +## Where the slots (slots()) are drawn, left to right, before the one in hand is raised: centered +## at the screen's bottom, the off-hand's past a divider. +func slot_rects(list: Array[Dictionary]) -> Array[Rect2]: + var out: Array[Rect2] = [] + var mains := 0 + for s in list: + if not s["offhand"]: + mains += 1 + var width := list.size() * SLOT.x + (list.size() - 1) * SLOT_GAP + (DIVIDER_GAP * 2.0 - SLOT_GAP if mains < list.size() else 0.0) + var x := (size.x - width) * 0.5 + var y := size.y - HOTBAR_BOTTOM - SLOT.y + for i in list.size(): + if i == mains and i > 0: + x += DIVIDER_GAP * 2.0 - SLOT_GAP + out.append(Rect2(Vector2(x, y), SLOT)) + x += SLOT.x + SLOT_GAP + return out + + +## A tap on main slot i (phones): picks its weapon as its number key does, through the same path +## (ScarecrowCombat notes weapon_k, and takes it up on its next tick only when it may switch: not +## in build mode, frozen, or under a menu). +func tap_slot(i: int) -> void: + var list := slots() + if i < 0 or i >= list.size() or list[i]["offhand"] or int(list[i]["key"]) <= 0: + return + _press_switch.call_deferred(ScarecrowCombat.SWITCH_ACTIONS[int(list[i]["key"])]) # outside the touch's dispatch + + +func _press_switch(action: StringName) -> void: + for pressed in [true, false]: + var e := InputEventAction.new() + e.action = action + e.pressed = pressed + Input.parse_input_event(e) + + +## Phones: a hit target over each main weapon's slot. +func _place_targets() -> void: + var list := slots() + var rects := slot_rects(list) + var n := 0 + for i in list.size(): + if list[i]["offhand"]: + continue + if n == slot_targets.size(): + var target := HitTarget.new() + target.name = "Slot%d" % (n + 1) + target.tapped.connect(tap_slot.bind(n)) # (the main slots come first: slot n is list[n]) + add_child(target) + slot_targets.append(target) + slot_targets[n].cover(rects[i]) + slot_targets[n].visible = true + n += 1 + for k in range(n, slot_targets.size()): + slot_targets[k].visible = false + + ## Whether the off-hand weapon is in the off-hand and ready (a pumpkin bomb not recharging). func _offhand_lit(w: int) -> bool: return w == combat.offhand and not (w == ScarecrowCombat.Weapon.PUMPKIN_BOMB and combat.bomb_left > 0.0) @@ -171,21 +241,14 @@ func _draw_hotbar() -> void: var list := slots() if list.is_empty(): return - var mains := 0 - for s in list: - if not s["offhand"]: - mains += 1 - var width := list.size() * SLOT.x + (list.size() - 1) * SLOT_GAP + (DIVIDER_GAP * 2.0 - SLOT_GAP if mains < list.size() else 0.0) - var x := (size.x - width) * 0.5 - var y := size.y - HOTBAR_BOTTOM - SLOT.y + var rects := slot_rects(list) for i in list.size(): var s: Dictionary = list[i] var w: int = s["weapon"] - if i == mains and i > 0: - var div := x - SLOT_GAP + DIVIDER_GAP - Sketch.line(self, Vector2(div, y - 10.0), Vector2(div, y + SLOT.y + 12.0), Sketch.INK, 3.0, 200.0, 1.5) - x += DIVIDER_GAP * 2.0 - SLOT_GAP - var r := Rect2(Vector2(x, y), SLOT) + var r := rects[i] + if i > 0 and s["offhand"] and not list[i - 1]["offhand"]: + var div := rects[i - 1].end.x + DIVIDER_GAP + Sketch.line(self, Vector2(div, r.position.y - 10.0), Vector2(div, r.end.y + 12.0), Sketch.INK, 3.0, 200.0, 1.5) var lit: bool = s["selected"] and not s["offhand"] # the off-hand's ready shows by not being darkened if lit: r = r.grow(3.0) @@ -199,6 +262,37 @@ func _draw_hotbar() -> void: Color(0.04, 0.05, 0.04, 0.6)) if lit: Sketch.box(self, r.grow(2.0), Color(0, 0, 0, 0), Sketch.GOLD, 3.5, 240.0 + i) - if int(s["key"]) > 0: - Sketch.text(self, r.position + Vector2(7.0, 1.0), str(s["key"]), 20, Sketch.TEXT, HORIZONTAL_ALIGNMENT_LEFT, 6) - x += SLOT.x + SLOT_GAP + if s["label"] != "": + Sketch.text(self, r.position + Vector2(7.0, 1.0), s["label"], 20, Sketch.TEXT, HORIZONTAL_ALIGNMENT_LEFT, 6) + + +## A phone's hit target (only on a phone: MobilePort.active()) over a widget the HUD draws: a STOP +## Control over the widget only, at least MobilePort.minimum_target() across (so it may reach past +## a small widget). A finger on it is the widget's and starts no thumbstick (TouchSticks takes +## only the touches the HUD leaves); beside it the screen is the sticks'. tapped comes when a +## finger lands on it (or, in the desktop preview, on a click); the tap's emulated click (Godot's +## touch-to-mouse) is taken without counting twice. The HUD's slots, the backpack (ResourceHud), +## the tip box's × (Tutorial), and the conversation's Skip (DialogueBox) are these. +class HitTarget extends Control: + signal tapped + + func _init() -> void: + mouse_filter = Control.MOUSE_FILTER_STOP + focus_mode = Control.FOCUS_NONE + + ## Covers r (in the parent's coordinates), grown about its center to the minimum target. + func cover(r: Rect2) -> void: + var least := MobilePort.minimum_target(self) + var s := r.size.max(Vector2(least, least)) + position = r.get_center() - s * 0.5 + size = s + + func _gui_input(event: InputEvent) -> void: + var touch := event as InputEventScreenTouch + var click := event as InputEventMouseButton + if touch == null and click == null: + return + accept_event() + if (touch != null and touch.pressed) or (click != null and click.pressed and click.button_index == MOUSE_BUTTON_LEFT + and click.device != InputEvent.DEVICE_ID_EMULATION): + tapped.emit() diff --git a/game/scripts/tutorial.gd b/game/scripts/tutorial.gd index 256cdb0..3a9ed39 100644 --- a/game/scripts/tutorial.gd +++ b/game/scripts/tutorial.gd @@ -7,7 +7,8 @@ extends Control ## Night: fight (once enemies are in), fear (when crowded), recover (when something is ## dropped), patch up (when hurt) ## The most urgent one wins (recover, fear, patch, shoot, dusk, then the day's in order). -## [Backspace / Back] hides them until the game is restarted; Settings turns them off. +## [Backspace / Back] hides them until the game is restarted (hide_tips); on a phone so does the × +## on the tip box's corner (a ScarecrowHud.HitTarget). Settings turns them off. ## Finished tips stay finished for the session, so a retry or a second vacation doesn't repeat ## them. ## None show while a talking-head conversation or the opening intro is up. @@ -28,6 +29,11 @@ const TEXT := { const PRIORITY: Array[String] = ["recover", "fear", "patch", "shoot", "dusk", "move", "gather", "store", "build"] const DONE_TIME := 0.9 const CROWD_RADIUS := 7.0 +## Phones: the ×'s disc, centered this far in from the tip box's top-right corner. +const HIDE_RADIUS := 20.0 +const HIDE_INSET := 6.0 +const BOX_FILL := Color(0.08, 0.1, 0.08, 0.86) +const BOX_EDGE := Color(0.95, 0.82, 0.45) var sim: FarmSim ## Off when the player turned tips off in Settings. @@ -36,6 +42,8 @@ var enabled := true var dismissed := false var done := {} var current := "" +## Phones: the tip box's × (null on desktop). +var hide_target: ScarecrowHud.HitTarget var _box: PanelContainer var _text: Label @@ -63,8 +71,8 @@ func _ready() -> void: mouse_filter = Control.MOUSE_FILTER_IGNORE _box = PanelContainer.new() var bg := StyleBoxFlat.new() - bg.bg_color = Color(0.08, 0.1, 0.08, 0.86) - bg.border_color = Color(0.95, 0.82, 0.45) + bg.bg_color = BOX_FILL + bg.border_color = BOX_EDGE bg.set_border_width_all(2) bg.set_corner_radius_all(6) bg.content_margin_left = 18.0 @@ -89,6 +97,14 @@ func _ready() -> void: _hint.add_theme_color_override("font_color", Color(0.7, 0.72, 0.65)) col.add_child(_hint) _box.visible = false + if MobilePort.active(): + hide_target = ScarecrowHud.HitTarget.new() + hide_target.name = "HideTips" + hide_target.visible = false + hide_target.tapped.connect(hide_tips) + hide_target.draw.connect(_draw_hide) + add_child(hide_target) + _box.item_rect_changed.connect(_place_hide) func _process(delta: float) -> void: @@ -163,15 +179,25 @@ func _finish(id: String) -> void: _done_left = DONE_TIME +## Hides the tips until the game is restarted: [Backspace / Back], or the × on a phone. +func hide_tips() -> void: + dismissed = true + _show("") + + func _show(id: String) -> void: current = id _box.visible = id != "" + if hide_target != null: + hide_target.visible = _box.visible if id == "": return _text.text = Prompts.tip(id) _text.remove_theme_color_override("font_color") _box.reset_size() _box.position = Vector2((size.x - _box.size.x) * 0.5, size.y - 250.0 - _box.size.y) + if hide_target != null: + _place_hide() func _enemies_near() -> int: @@ -195,6 +221,23 @@ func _on_phase(phase: CampaignController.Phase, _previous: CampaignController.Ph func _unhandled_input(event: InputEvent) -> void: if _box.visible and event.is_action_pressed("hide_tips"): - dismissed = true - _show("") + hide_tips() get_viewport().set_input_as_handled() + + +## Phones: the × at the tip box's top-right corner, over it. +func _place_hide() -> void: + var r := _box.get_rect() + var at := Vector2(r.end.x - HIDE_INSET, r.position.y + HIDE_INSET) + hide_target.cover(Rect2(at - Vector2(HIDE_RADIUS, HIDE_RADIUS), Vector2(HIDE_RADIUS, HIDE_RADIUS) * 2.0)) + hide_target.queue_redraw() + + +## The ×: a disc of the tip box's own fill and edge, on its corner. +func _draw_hide() -> void: + var c := hide_target.size * 0.5 + hide_target.draw_circle(c, HIDE_RADIUS, Color(BOX_FILL, 0.97)) + Sketch.circle(hide_target, c, HIDE_RADIUS, BOX_EDGE, 2.5, 3.0) + var k := HIDE_RADIUS * 0.42 + Sketch.line(hide_target, c + Vector2(-k, -k), c + Vector2(k, k), Sketch.TEXT, 3.5, 11.0, 0.5) + Sketch.line(hide_target, c + Vector2(k, -k), c + Vector2(-k, k), Sketch.TEXT, 3.5, 12.0, 0.5) diff --git a/game/tests/mobile/hud_taps_test.gd b/game/tests/mobile/hud_taps_test.gd new file mode 100644 index 0000000..957679e --- /dev/null +++ b/game/tests/mobile/hud_taps_test.gd @@ -0,0 +1,376 @@ +extends RefCounted +## #39, the tappable HUD, by touch through the game's entry scene (main.tscn, with test save and +## settings paths) on a 19.5:9 phone (2340x1080 at its interface scale), with a simulated notch +## and home indicator (MobilePort.preview_insets): +## * Every hit target (ScarecrowHud.HitTarget) covers its drawn widget, at least +## MobilePort.minimum_target() across, and a finger just beside one starts a thumbstick. +## * Dialogue: a conversation that pauses the game has Skip; a tap elsewhere writes the line, +## then goes on; Skip skips the rest (and the game goes on). One that doesn't pause has none. +## * Tips: the tip box's × hides the tips as Backspace (hide_tips) does: dismissed for the +## session, tips still on in Settings. +## * Hotbar: a tap on each main weapon's slot picks that weapon, in any order; frozen (no +## switching) a tap picks nothing, then or later; in build mode there are no slot targets. The +## slots show no number keys. +## * Backpack: a tap opens the pouch panel and another closes it; a finger anywhere else closes +## it (and still starts a stick), one on the panel doesn't. The panel lies inside the safe rect +## at 2340x1080 with the inset, and on a 1334x750 window (at the preview's scale and at an +## iPhone SE's 1.7 cap), with and without it. +## * Desktop (forced 0): no hit targets, and the hotbar's number keys as before. +## The InputMap's mouse buttons for fire and offhand (TouchSticks erases them) are put back. + +const Touch := preload("res://game/tests/mobile/touch.gd") +const SAVE := "user://test_hud_taps_save.json" +const SETTINGS := "user://test_hud_taps_settings.cfg" +const PHONE := Vector2i(2340, 1080) +const SMALL := Vector2i(1334, 750) +const INSETS := [140, 0, 140, 60] +const SE_DPI := 326.0 # an iPhone SE's density: past the 1.7 cap +const W := ScarecrowCombat.Weapon +const TALK := "res://game/resources/dialogue/morning_day_2.tres" + +var t: GameTestRunner +var main: Main +var sticks: TouchSticks + + +func run(p_t) -> void: + t = p_t + _clean() + var bindings := {} + for action: StringName in [&"fire", &"offhand"]: + bindings[action] = InputMap.action_get_events(action) + var size := t.root.size + var factor := t.root.content_scale_factor + MobilePort.forced = 1 + MobilePort.preview_insets = INSETS.duplicate() + t.root.size = PHONE + await _start() + sticks = main.farm.get_node_or_null("Hud/TouchSticks") as TouchSticks + t.check(sticks != null and t.root.content_scale_factor > 1.5, "a phone at its interface scale (%.2f), with the sticks" % t.root.content_scale_factor) + if sticks != null: + await _day() + await _dialogue() + await _tips() + await _hotbar() + await _backpack() + await _fit() + await _stop() + MobilePort.preview_insets = [] + MobilePort.preview_dpi = 0.0 + t.root.size = size + t.root.content_scale_factor = factor + MobilePort.forced = 0 + await _start() + await _desktop() + await _stop() + for action: StringName in bindings: + InputMap.action_erase_events(action) + for e: InputEvent in bindings[action]: + InputMap.action_add_event(action, e) + for action in [&"fire", &"offhand", &"move_left", &"move_right", &"move_forward", &"move_back", + &"aim_left", &"aim_right", &"aim_up", &"aim_down"]: + Input.action_release(action) + MobilePort.forced = -1 + t.root.size = size + t.root.content_scale_factor = factor + _clean() + + +func _clean() -> void: + for p in [SAVE, SAVE + ".tmp", SETTINGS]: + if FileAccess.file_exists(p): + DirAccess.remove_absolute(ProjectSettings.globalize_path(p)) + + +func _start() -> void: + main = (load("res://game/scenes/main.tscn") as PackedScene).instantiate() as Main + main.save_path = SAVE + main.settings_path = SETTINGS + t.root.add_child(main) + await t.process_frame + await t.process_frame + + +func _stop() -> void: + main.queue_free() + t.paused = false + await t.process_frame + main = null + + +func _frames(n: int) -> void: + for k in n: + await t.process_frame + + +func _physics(n: int) -> void: + for k in n: + await t.physics_frame + + +## Every hit target under Main (visible or not). +func _targets() -> Array: + return main.find_children("*", "", true, false).filter(func(n: Node) -> bool: return n is ScarecrowHud.HitTarget) + + +## A new vacation's first day (no intro), its conversations over, with three main weapons and +## pumpkin bombs owned. +func _day() -> void: + main.new_vacation(false) + await t.process_frame + main.sim.campaign.advance_phase() # the first morning's sign -> day 1 + for k in 20: + await t.process_frame + if t.paused and main.sim.dialogue != null and main.sim.dialogue.is_playing(): + main.sim.dialogue.box.skip() + var s := main.sim.campaign.state + for id in ["corn_shooter", "potato_cannon", "pumpkin_bomb"]: + s.upgrades[id] = 1 + await _physics(2) + await _frames(2) + t.check(main.screen == Main.Screen.PLAYING and not t.paused and sticks.can_act(), "day 1 under way, the sticks can act") + + +## The target covers the drawn rect r (canvas coordinates), at least the minimum target across. +func _covers(target: Control, r: Rect2, what: String) -> void: + var g := target.get_global_rect() + var least := MobilePort.minimum_target(target) + t.check(target.is_visible_in_tree() and g.grow(0.01).encloses(r) and g.size.x >= least - 0.01 and g.size.y >= least - 0.01, + "%s: its target %s covers it (%s), at least %.0f across" % [what, g, r, least]) + + +## A finger landing just beside the target (the canvas point at) starts a stick, and lifting it +## lets the stick go. +func _beside(at: Vector2, what: String) -> void: + var s := sticks.side_of(at) + Touch.touch(0, at, true) + await t.process_frame + t.check(sticks.finger[s] == 0, "a finger just beside %s (%s) starts the %s stick" % [what, at, "move" if s == TouchSticks.Stick.MOVE else "attack"]) + Touch.touch(0, at, false) + await t.process_frame + t.check(sticks.finger[s] == -1, "(and lifting it lets it go)") + + +func _dialogue() -> void: + var box := main.sim.dialogue.box + var skip := box.skip_target + t.check(skip != null and not skip.is_visible_in_tree(), "the dialogue box has a Skip target, hidden with no conversation up") + if skip == null: + return + var c := load(TALK) as Conversation + t.check(c != null and c.pause_game and c.lines.size() >= 3, "(a conversation that pauses the game: %s)" % TALK) + main.sim.dialogue.play(c) + await _frames(3) + t.check(t.paused and box.is_playing() and skip.is_visible_in_tree(), "a conversation that pauses the game shows Skip") + var drawn := Rect2(box._sheet.global_position + box.skip_rect().position, box.skip_rect().size) + _covers(skip, drawn, "Skip") + t.check(MobilePort.safe_rect(box).encloses(skip.get_global_rect()), "Skip is inside the safe rect") + # A tap elsewhere on the paper: the rest of the line, then the next one. + var paper := Rect2(box._sheet.global_position, box._sheet.size) + var elsewhere := paper.get_center() + t.check(not skip.get_global_rect().has_point(elsewhere) and not box.is_written(), "(the line is being written)") + Touch.tap(0, elsewhere) + await t.process_frame + t.check(box.is_playing() and box.index == 0 and box.is_written(), "a tap elsewhere writes the rest of the line") + Touch.tap(0, elsewhere) + await t.process_frame + t.check(box.is_playing() and box.index == 1, "another goes on to the next line (%d)" % box.index) + # A tap just beside Skip goes on too (a stick can't start: the game is paused). + var g := skip.get_global_rect() + Touch.tap(0, Vector2(g.position.x - 8.0, g.get_center().y)) + await t.process_frame + t.check(box.is_playing() and box.index == 1 and box.is_written() and sticks.finger == [-1, -1], + "a tap just beside Skip writes the line, as anywhere else") + Touch.tap(0, g.get_center()) + await t.process_frame + t.check(not box.is_playing(), "a tap on Skip skips the rest of the conversation") + await _frames(3) + t.check(not t.paused and not skip.is_visible_in_tree(), "the game goes on, and Skip is gone") + # One that doesn't pause the game has no Skip. + var quiet := Conversation.new() + quiet.pause_game = false + quiet.lines = c.lines + main.sim.dialogue.play(quiet) + await _frames(3) + t.check(box.is_playing() and not t.paused and not skip.is_visible_in_tree(), "one that doesn't pause the game has no Skip") + box.stop() + await _frames(2) + + +func _tips() -> void: + var tut := main.sim.tutorial + var x := tut.hide_target + t.check(x != null, "the tip box has a × target") + if x == null: + return + await _frames(3) + t.check(tut.current != "" and tut._box.visible and x.is_visible_in_tree(), "a tip is up (%s), with its ×" % tut.current) + var box := tut._box.get_global_rect() + var drawn := Rect2(Vector2(box.end.x - Tutorial.HIDE_INSET, box.position.y + Tutorial.HIDE_INSET) - Vector2.ONE * Tutorial.HIDE_RADIUS, + Vector2.ONE * Tutorial.HIDE_RADIUS * 2.0) + _covers(x, drawn, "the ×") + var g := x.get_global_rect() + await _beside(Vector2(g.end.x + 8.0, g.get_center().y), "the ×") + await _beside(Vector2(box.position.x + 40.0, box.get_center().y), "the tip box (only the × is a target)") + t.check(tut.current != "" and not tut.dismissed, "(the tip is still up)") + Touch.tap(0, g.get_center()) + await t.process_frame + var by_x := [tut.dismissed, tut.enabled, tut.current, tut._box.visible, main.settings.show_tips] + t.check(by_x == [true, true, "", false, true], "a tap on the × hides the tips for the session; Settings still has them on: %s" % [by_x]) + await _frames(3) + t.check(not tut._box.visible and not x.is_visible_in_tree(), "they stay hidden, the × with them") + # Backspace does the same. + tut.dismissed = false + await _frames(3) + t.check(tut.current != "" and x.is_visible_in_tree(), "(tips back for the comparison: %s)" % tut.current) + for pressed in [true, false]: + var e := InputEventAction.new() + e.action = &"hide_tips" + e.pressed = pressed + Input.parse_input_event(e) + Input.flush_buffered_events() + await t.process_frame + var by_key := [tut.dismissed, tut.enabled, tut.current, tut._box.visible, main.settings.show_tips] + t.check(by_key == by_x, "Backspace (hide_tips) leaves the same: %s" % [by_key]) + + +func _hotbar() -> void: + var hud := main.sim.hud + var combat := main.sim.combat + var list := hud.slots() + var mains := combat.owned_main() + t.check(mains == [W.PITCHFORK, W.CORN_SHOOTER, W.POTATO_CANNON] and list.size() == 4, "(three main weapons and the bombs: %s)" % [mains]) + t.check(list.all(func(s: Dictionary) -> bool: return s["label"] == ""), "no number keys in the slots on a phone") + var shown := hud.slot_targets.filter(func(c: Control) -> bool: return c.is_visible_in_tree()) + t.check(shown.size() == 3, "a target on each main weapon's slot, none on the off-hand's (%d)" % shown.size()) + if shown.size() != 3: + return + var rects := hud.slot_rects(list) + for i in 3: + _covers(hud.slot_targets[i], rects[i], "slot %d" % (i + 1)) + t.check(not hud.slot_targets[0].get_global_rect().intersects(hud.slot_targets[1].get_global_rect()) + and not hud.slot_targets[1].get_global_rect().intersects(hud.slot_targets[2].get_global_rect()), "the slots' targets don't overlap") + t.check(not shown.any(func(c: Control) -> bool: return c.get_global_rect().intersects(rects[3])), "the bombs' slot has no target") + for i in [1, 2, 0, 2]: + Touch.tap(0, hud.slot_targets[i].get_global_rect().get_center()) + await _physics(3) + t.check(combat.weapon == mains[i] and sticks.finger == [-1, -1], "a tap on slot %d picks %s (in hand: %s), no stick" % [i + 1, + ScarecrowCombat.WEAPON_NAMES[mains[i]], ScarecrowCombat.WEAPON_NAMES[combat.weapon]]) + var g: Rect2 = hud.slot_targets[1].get_global_rect() + await _beside(Vector2(g.get_center().x, g.position.y - 8.0), "a slot") + await _beside(Vector2(rects[3].get_center().x, rects[3].get_center().y), "the slots (on the bombs' slot)") + # No switching while frozen (as under a co-op menu): the tap is dropped, not kept for later. + main.sim.local_frozen = true + Touch.tap(1, hud.slot_targets[0].get_global_rect().get_center()) + await _physics(3) + t.check(combat.weapon == W.POTATO_CANNON, "frozen, a tap on a slot picks nothing") + main.sim.local_frozen = false + await _physics(3) + t.check(combat.weapon == W.POTATO_CANNON, "nor once it can switch again") + main.sim.build.set_active(true) + await _frames(2) + t.check(hud.slots().is_empty() and not hud.slot_targets.any(func(c: Control) -> bool: return c.is_visible_in_tree()), + "in build mode the hotbar and its targets are gone") + main.sim.build.set_active(false) + await _frames(2) + Touch.tap(0, hud.slot_targets[0].get_global_rect().get_center()) + await _physics(3) + t.check(combat.weapon == W.PITCHFORK, "back from build mode, a tap picks again") + + +func _backpack() -> void: + var rh := main.sim.resource_hud + var bag := rh.backpack_target + t.check(bag != null, "the backpack has a target") + if bag == null: + return + main.sim.campaign.state.resources.add_to_pouch("wood", 3) + await _frames(2) + t.check(not rh.open, "(the pouch panel is closed)") + _covers(bag, rh.icon_rect(), "the backpack") + var g := bag.get_global_rect() + Touch.tap(0, g.get_center()) + await t.process_frame + t.check(rh.open and sticks.finger == [-1, -1], "a tap on the backpack opens the pouch panel, no stick") + t.check(MobilePort.safe_rect(rh).encloses(rh.panel_rect()), "the panel %s is inside the safe rect %s" % [rh.panel_rect(), MobilePort.safe_rect(rh)]) + Touch.tap(0, g.get_center()) + await t.process_frame + t.check(not rh.open, "another closes it") + # A finger elsewhere closes it, and still starts its stick. + Touch.tap(0, g.get_center()) + await t.process_frame + var away := Vector2(rh.size.x * 0.75, rh.size.y * 0.4) + Touch.touch(0, away, true) + await t.process_frame + t.check(not rh.open and sticks.finger[TouchSticks.Stick.MOVE] == 0, "a finger elsewhere closes it, and starts the move stick") + Touch.touch(0, away, false) + await t.process_frame + # One on the panel leaves it open. + Touch.tap(0, g.get_center()) + await t.process_frame + Touch.tap(0, rh.panel_rect().get_center()) + await t.process_frame + t.check(rh.open, "a finger on the panel leaves it open") + Touch.tap(1, main.sim.hud.slot_targets[1].get_global_rect().get_center()) + await _physics(3) + t.check(not rh.open and main.sim.combat.weapon == W.CORN_SHOOTER, "a tap on a hotbar slot closes it and picks the slot") + await _beside(Vector2(g.get_center().x, g.position.y - 8.0), "the backpack") + t.check(not rh.open, "(still closed)") + + +## The pouch panel and the targets (the tip's × up too) at other windows: 1334x750 at the +## preview's scale and at an iPhone SE's (the 1.7 cap), with the inset and without. +func _fit() -> void: + var rh := main.sim.resource_hud + if not rh.open: + rh.toggle() + main.sim.tutorial.dismissed = false + for dpi: float in [0.0, SE_DPI]: + for inset in [INSETS, []]: + MobilePort.preview_dpi = dpi + MobilePort.preview_insets = inset.duplicate() + t.root.size = SMALL + main.settings.apply_scale(t.root) + await _frames(3) + var canvas := t.root.get_visible_rect().size + var safe := MobilePort.safe_rect(rh) + var what := "%s at x%.2f (canvas %dx%d)%s" % [SMALL, t.root.content_scale_factor, roundi(canvas.x), roundi(canvas.y), + " with the inset" if not inset.is_empty() else ""] + t.check(safe.encloses(rh.panel_rect()), "%s: the pouch panel %s is inside the safe rect %s" % [what, rh.panel_rect(), safe]) + var least := MobilePort.minimum_target(rh) + var small := _targets().filter(func(c: Control) -> bool: + return c.is_visible_in_tree() and (c.size.x < least - 0.01 or c.size.y < least - 0.01)) + t.check(small.is_empty(), "%s: every target is at least %.0f across (%s)" % [what, least, small.map(func(c: Control) -> String: return c.name)]) + var shown := _targets().filter(func(c: Control) -> bool: return c.is_visible_in_tree()) + var off := shown.filter(func(c: Control) -> bool: return not Rect2(Vector2.ZERO, canvas).encloses(c.get_global_rect())) + t.check(shown.size() == 5 and off.is_empty(), "%s: the 5 targets up (3 slots, the backpack, the ×) are on the canvas (off it: %s)" % [ + what, off.map(func(c: Control) -> String: return c.name)]) + t.check(is_equal_approx(t.root.content_scale_factor, MobilePort.MAX_SCALE) and t.root.get_visible_rect().size.y < 640.0, + "(the SE's density came to the 1.7 cap: %.2f)" % t.root.content_scale_factor) + rh.toggle() + main.sim.tutorial.hide_tips() + + +func _desktop() -> void: + main.new_vacation(false) + await t.process_frame + main.sim.campaign.advance_phase() + for k in 20: + await t.process_frame + if t.paused and main.sim.dialogue != null and main.sim.dialogue.is_playing(): + main.sim.dialogue.box.skip() + var s := main.sim.campaign.state + for id in ["corn_shooter", "potato_cannon", "pumpkin_bomb"]: + s.upgrades[id] = 1 + await _physics(2) + await _frames(2) + t.check(main.farm.get_node_or_null("Hud/TouchSticks") == null, "desktop: no sticks") + t.check(_targets().is_empty() and main.sim.hud.slot_targets.is_empty() and main.sim.resource_hud.backpack_target == null + and main.sim.tutorial.hide_target == null and main.sim.dialogue.box.skip_target == null, "desktop: no hit targets") + var labels := main.sim.hud.slots().map(func(d: Dictionary) -> String: return d["label"]) + t.check(labels == ["1", "2", "3", ""], "desktop: the slots' numbers as before (%s)" % [labels]) + t.check(Prompts.hotbar_key(1) == "1" and Prompts.hotbar_key(6) == "6" and Prompts.hotbar_key(0) == "", "desktop: Prompts.hotbar_key is the number") + var rh := main.sim.resource_hud + var icon := rh.icon_rect() + t.check(rh.panel_rect() == Rect2(Vector2(icon.position.x - 4.0, icon.position.y - 14.0 - 248.0), Vector2(ResourceHud.PANEL_WIDTH, 248.0)), + "desktop: the pouch panel where it was (%s)" % rh.panel_rect()) diff --git a/game/tests/mobile/hud_taps_test.gd.uid b/game/tests/mobile/hud_taps_test.gd.uid new file mode 100644 index 0000000..97ded25 --- /dev/null +++ b/game/tests/mobile/hud_taps_test.gd.uid @@ -0,0 +1 @@ +uid://b4y1im78s3fj3