harvest/game/scripts/resource_hud.gd
Forrest 49e94874a6 #39: tappable HUD
On phones (MobilePort.active()) the HUD's widgets take taps through small
STOP hit targets (ScarecrowHud.HitTarget, at least minimum_target()), and
everything beside them is still the thumbsticks':
- hotbar: a tap on a main slot picks it through the number keys' path
  (weapon_k, applied only when combat may switch); no number keys on phones
  (Prompts.hotbar_key)
- backpack: a tap toggles the pouch panel, a finger elsewhere (not on the
  panel) closes it; the panel stays inside safe_rect
- tip box: an × hides tips as Backspace does (Tutorial.hide_tips)
- dialogue: a Skip button on pausing conversations; a tap elsewhere advances
Desktop has no targets and draws as before. Test: mobile/hud_taps_test.gd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:10:56 -04:00

154 lines
5.7 KiB
GDScript

class_name ResourceHud
extends Control
## Bottom-left backpack, hand-drawn (Sketch): this machine's scarecrow's pouch, "used/capacity"
## 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)
const PANEL_WIDTH := 410.0
const ROW := 46.0
const DOT := Color(0.92, 0.12, 0.1)
const SHED := Color(1.0, 0.9, 0.62)
var campaign: CampaignController
## The avatar slot whose pouch is shown (FarmSim.set_local: the partner's machine shows P2's).
var slot: int = 0
## The pouch panel is open.
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:
campaign = p_campaign
_last = campaign.state.resources.pouch_of(slot).duplicate()
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:
if campaign != null and is_visible_in_tree() and event.is_action_pressed("pouch", false):
toggle()
get_viewport().set_input_as_handled()
func toggle() -> void:
open = not open
if open:
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
return "%d/%d" % [r.pouch_total(slot), r.pouch_capacity]
## The pouch panel's rows: {material, pouch, shed}, one per material (ResourceStore.MATERIALS).
func rows() -> Array[Dictionary]:
var out: Array[Dictionary] = []
var r := campaign.state.resources
var p := r.pouch_of(slot)
for m in ResourceStore.MATERIALS:
out.append({"material": m, "pouch": int(p.get(m, 0)), "shed": int(r.shed.get(m, 0))})
return out
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
var p := r.pouch_of(slot)
for m in ResourceStore.MATERIALS:
if int(p.get(m, 0)) > int(_last.get(m, 0)) and not open:
new_items = true
_last = p.duplicate()
if r.pouch_total(slot) == 0:
new_items = false
queue_redraw()
func _draw() -> void:
if campaign == null:
return
var icon := icon_rect()
Sketch.icon(self, "backpack", icon)
if new_items:
var at := icon.position + Vector2(icon.size.x - 16.0, 16.0)
draw_colored_polygon(Sketch.blob(at, 11.0, 11.0, 401.0, 0.04, 20), DOT)
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()
func _draw_panel() -> void:
var list := rows()
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
Sketch.text(self, r.position + Vector2(18.0, 8.0), "Pouch", 32)
Sketch.text(self, Vector2(pouch_x, r.position.y + 14.0), "Carried", 24, Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT, 6)
Sketch.text(self, Vector2(shed_x, r.position.y + 14.0), "Shed", 24, Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT, 6)
Sketch.rule(self, r.position.x + 14.0, r.end.x - 14.0, r.position.y + 50.0, 412.0)
for i in list.size():
var row: Dictionary = list[i]
var m: String = row["material"]
var y := r.position.y + 56.0 + i * ROW
Sketch.icon(self, m, Rect2(Vector2(r.position.x + 14.0, y), Vector2(40.0, 40.0)))
Sketch.text(self, Vector2(r.position.x + 62.0, y + 2.0), m.capitalize(), 30, GatherNode.COLORS[m])
var carried: int = row["pouch"]
Sketch.text(self, Vector2(pouch_x, y + 2.0), str(carried), 30, Sketch.TEXT if carried > 0 else Sketch.DIM, HORIZONTAL_ALIGNMENT_RIGHT)
Sketch.text(self, Vector2(shed_x, y + 2.0), str(row["shed"]), 30, SHED, HORIZONTAL_ALIGNMENT_RIGHT)