Touch Controls
@shell/touch-controls draws a stick and buttons over the game on a touch screen. Each moves or
presses an action of the input manager, beside its keys and gamepad buttons, so a script that reads
the action does not change. Engine2D installs TouchControlsPlugin.
Components
Section titled “Components”TouchButton presses an action while a finger is on it; TouchStick moves a two-axis action from
its centre toward the finger, 1 at its rim, with up as +y (as wasd()). Both are plain data:
| Field | Type | Default | Means |
|---|---|---|---|
action | string | jump / move | The action of the global input map it presses or moves |
label | string | A button’s words | |
corner | string | bottom-right / bottom-left | The corner of the screen it is placed by |
x, y | number | 24, 24 | How far in from that corner’s two edges, inside the safe area, px |
size | number | 88 / 140 | Across, in CSS pixels |
shownOn | string | touch | touch (touch screens only), always, or never |
The code prefabs touch:button and touch:stick place one of each.
How it is heard
Section titled “How it is heard”TouchControls (the touchControls resource) is an input source of the binding kinds
touchButton(action) and touchStick(action). Each frame TouchControlsSync places the controls
shown, by the viewport’s size and safeArea, and adds the binding of each to its action in the
global inputMap. A press on a control is claimed from @shell/dom’s pointer, so it is no press
in the game’s world, and its finger is followed until it lifts: several fingers hold several
controls at once. TouchControlsLook gives each shown control its look as entities of their own, on the UI layer where the control is on the screen:
a ring (a Shape), a stick’s knob (a Shape) or a button’s label (a bitmap Text), which either renderer draws.