#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>
This commit is contained in:
parent
5392ae09af
commit
49e94874a6
7 changed files with 643 additions and 28 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue