#38: touch action buttons
TouchButtons (phones only, Main adds it beside TouchSticks): Pause, Fear, Bomb, Patch, Use (word from the new read-only Interactions.focus_info()) and Build, round Sketch buttons that press the existing actions for at least one physics tick, shown only when they can do something and hidden whenever the tree is paused or a menu, panel or sign is up. Columns follow TouchSticks.move_on_right (now a variable); both join "touch_controls" with apply_settings (Swap sticks), which Main calls at startup; Prompts.move_side() reads the live sticks. Stick fingers keep their drags over buttons. New Sketch icons: fear, patch, use, build, pause. Test: game/tests/mobile/buttons_test.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5392ae09af
commit
e435b76aa2
9 changed files with 1115 additions and 48 deletions
|
|
@ -3,22 +3,27 @@ extends Control
|
|||
## The phone's controls (MobilePort; Main adds it over the HUD): two floating thumbsticks. A
|
||||
## finger down on one half of the screen starts that half's stick where it lands; dragging tilts
|
||||
## it (past the rim, the base follows the finger), and lifting the finger lets it go.
|
||||
## * Move (right half): the move_* actions, as far as the stick is tilted. Screen up is
|
||||
## forward, as with WASD.
|
||||
## * Attack (left half): the aim_* actions, as a gamepad's right stick, and fire held while the
|
||||
## stick is tilted past the aim dead zone, so the main weapon attacks that way each time it's
|
||||
## ready.
|
||||
## The sticks only take touches the HUD and the menus don't (their buttons get them first), and
|
||||
## only while the scarecrow can act: not paused (menus, conversations), not frozen (the intro),
|
||||
## not once the vacation is over. A pause or freeze lets both sticks go. Each stick is drawn only
|
||||
## while a finger is on it.
|
||||
## * Move (right half; the left with the Swap sticks setting, move_on_right): the move_*
|
||||
## actions, as far as the stick is tilted. Screen up is forward, as with WASD.
|
||||
## * Attack (the other half): the aim_* actions, as a gamepad's right stick, and fire held
|
||||
## while the stick is tilted past the aim dead zone, so the main weapon attacks that way each
|
||||
## time it's ready.
|
||||
## The sticks only take touches the HUD and the menus don't (their buttons get them first, as do
|
||||
## TouchButtons'), and only while the scarecrow can act: not paused (menus, conversations), not
|
||||
## frozen (the intro), not once the vacation is over. A finger on a stick keeps it wherever it
|
||||
## slides, over a button too (its drags and its lift are read before the GUI's). A pause or freeze
|
||||
## lets both sticks go. Each stick is drawn only while a finger is on it.
|
||||
## Settings: in group &"touch_controls" with TouchButtons; apply_settings() takes the Swap sticks
|
||||
## setting (GameSettings.swap_sticks), and move_on_right_now() is the side in play (Prompts' tips).
|
||||
## On phones a tap is also a left click (Godot's mouse emulation, which the buttons need), and
|
||||
## the left click is fire: fire and offhand lose their mouse buttons here, so only the attack
|
||||
## stick (or a paired gamepad) attacks.
|
||||
|
||||
enum Stick { MOVE, ATTACK }
|
||||
|
||||
const MOVE_ON_RIGHT := true # the move stick's half of the screen; the attack stick has the other
|
||||
const MOVE_ON_RIGHT := true # the move stick's half of the screen by default (the user's call); the attack stick has the other
|
||||
## The touch controls' group: GameSettings.apply calls apply_settings on each (#41).
|
||||
const GROUP := &"touch_controls"
|
||||
const RADIUS := 150.0 # base to rim, in canvas pixels (the 1920-wide canvas)
|
||||
## Per stick: its left, right, up (forward), and down actions.
|
||||
const ACTIONS: Array = [[&"move_left", &"move_right", &"move_forward", &"move_back"],
|
||||
|
|
@ -26,6 +31,8 @@ const ACTIONS: Array = [[&"move_left", &"move_right", &"move_forward", &"move_ba
|
|||
const COLORS: Array[Color] = [UiTheme.TEXT, Color(1.0, 0.5, 0.38)]
|
||||
|
||||
var sim: FarmSim
|
||||
## The move stick's half: the right (MOVE_ON_RIGHT), or the left with Swap sticks (apply_settings).
|
||||
var move_on_right := MOVE_ON_RIGHT
|
||||
## Per stick (Stick): the finger on it (-1: none), its base, and its tilt (length 0-1).
|
||||
var finger: Array[int] = [-1, -1]
|
||||
var base: Array[Vector2] = [Vector2.ZERO, Vector2.ZERO]
|
||||
|
|
@ -36,7 +43,28 @@ func setup(p_sim: FarmSim) -> void:
|
|||
sim = p_sim
|
||||
|
||||
|
||||
## The move stick's side in play: the move_on_right of the TouchSticks in the tree, else
|
||||
## MOVE_ON_RIGHT (desktop, or before Main has added them).
|
||||
static func move_on_right_now() -> bool:
|
||||
var tree := Engine.get_main_loop() as SceneTree
|
||||
if tree != null:
|
||||
for n in tree.get_nodes_in_group(GROUP):
|
||||
if n is TouchSticks:
|
||||
return (n as TouchSticks).move_on_right
|
||||
return MOVE_ON_RIGHT
|
||||
|
||||
|
||||
## The settings contract (group &"touch_controls"): Swap sticks puts the move stick on the left.
|
||||
## A stick held while it changes lets go.
|
||||
func apply_settings(s: GameSettings) -> void:
|
||||
var right := MOVE_ON_RIGHT != s.swap_sticks
|
||||
if right != move_on_right:
|
||||
let_go()
|
||||
move_on_right = right
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
add_to_group(GROUP)
|
||||
set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
|
||||
mouse_filter = Control.MOUSE_FILTER_IGNORE # never in the way of the HUD's own buttons
|
||||
for action: StringName in [&"fire", &"offhand"]:
|
||||
|
|
@ -57,37 +85,46 @@ func _process(_delta: float) -> void:
|
|||
let_go()
|
||||
|
||||
|
||||
## A finger already on a stick: its drags and its lift, before the GUI sees them (so a button it
|
||||
## slides over neither takes them nor stops the stick).
|
||||
func _input(event: InputEvent) -> void:
|
||||
var drag := event as InputEventScreenDrag
|
||||
if drag != null:
|
||||
for s in 2:
|
||||
if finger[s] == drag.index:
|
||||
_drag(s, drag.position)
|
||||
get_viewport().set_input_as_handled()
|
||||
return
|
||||
var touch := event as InputEventScreenTouch
|
||||
if touch != null and not touch.pressed:
|
||||
for s in 2:
|
||||
if finger[s] == touch.index:
|
||||
_release(s)
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
|
||||
## A finger landing where nothing else took it starts its half's stick.
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
var touch := event as InputEventScreenTouch
|
||||
if touch != null:
|
||||
if touch.pressed:
|
||||
var s := side_of(touch.position)
|
||||
if not can_act() or finger[s] != -1:
|
||||
return
|
||||
finger[s] = touch.index
|
||||
base[s] = touch.position
|
||||
tilt[s] = Vector2.ZERO
|
||||
_apply(s)
|
||||
get_viewport().set_input_as_handled()
|
||||
else:
|
||||
for s in 2:
|
||||
if finger[s] == touch.index:
|
||||
_release(s)
|
||||
get_viewport().set_input_as_handled()
|
||||
if touch == null or not touch.pressed:
|
||||
return
|
||||
var drag := event as InputEventScreenDrag
|
||||
if drag == null:
|
||||
var s := side_of(touch.position)
|
||||
if not can_act() or finger[s] != -1:
|
||||
return
|
||||
for s in 2:
|
||||
if finger[s] != drag.index:
|
||||
continue
|
||||
var d := drag.position - base[s]
|
||||
if d.length() > RADIUS:
|
||||
base[s] += d.normalized() * (d.length() - RADIUS)
|
||||
d = drag.position - base[s]
|
||||
tilt[s] = d / RADIUS
|
||||
_apply(s)
|
||||
get_viewport().set_input_as_handled()
|
||||
finger[s] = touch.index
|
||||
base[s] = touch.position
|
||||
tilt[s] = Vector2.ZERO
|
||||
_apply(s)
|
||||
get_viewport().set_input_as_handled()
|
||||
|
||||
|
||||
func _drag(s: int, at: Vector2) -> void:
|
||||
var d := at - base[s]
|
||||
if d.length() > RADIUS:
|
||||
base[s] += d.normalized() * (d.length() - RADIUS)
|
||||
d = at - base[s]
|
||||
tilt[s] = d / RADIUS
|
||||
_apply(s)
|
||||
|
||||
|
||||
## Whether the scarecrow can take orders now (see the class notes).
|
||||
|
|
@ -98,7 +135,7 @@ func can_act() -> bool:
|
|||
## Which stick a finger landing at this point (canvas coordinates) starts.
|
||||
func side_of(at: Vector2) -> Stick:
|
||||
var right := at.x >= size.x * 0.5
|
||||
return Stick.MOVE if right == MOVE_ON_RIGHT else Stick.ATTACK
|
||||
return Stick.MOVE if right == move_on_right else Stick.ATTACK
|
||||
|
||||
|
||||
## Lets both sticks go: their actions and fire released.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue