Skip to content

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.

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:

FieldTypeDefaultMeans
actionstringjump / moveThe action of the global input map it presses or moves
labelstringA button’s words
cornerstringbottom-right / bottom-leftThe corner of the screen it is placed by
x, ynumber24, 24How far in from that corner’s two edges, inside the safe area, px
sizenumber88 / 140Across, in CSS pixels
shownOnstringtouchtouch (touch screens only), always, or never

The code prefabs touch:button and touch:stick place one of each.

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.