Class TouchControls

java.lang.Object
com.codename1.gaming.TouchControls

public class TouchControls extends Object

On-screen controls for touch devices: an optional analog VirtualJoystick plus any number of VirtualButtons. The framework draws them over the game and routes touches into the view's GameInput, so a touch game reads input exactly like a keyboard game -- GameInput#isGameKeyDown(int), GameInput#isKeyDown(int) and the joystick's analog GameInput#getAxisX() / getAxisY() all work whether the player is on a phone or a desktop.

Get the instance from GameView#getControls() and add the controls you want, placing them in the view's pixel coordinates (origin at the top left):

int fire = Display.getInstance().getKeyCode(Display.GAME_FIRE);
getControls().addJoystick(140, getHeight() - 140, 90);
getControls().addButton(fire, getWidth() - 120, getHeight() - 120, 55).setLabel("A");

Multi-touch is supported, so the player can steer with the stick and press a button at the same time.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Vertical anchor: pin to the bottom safe-area edge.
    static final int
    Horizontal/vertical anchor: center on the safe area.
    static final int
    Horizontal anchor: pin to the left safe-area edge.
    static final int
    Horizontal anchor: pin to the right safe-area edge.
    static final int
    Vertical anchor: pin to the top safe-area edge.
  • Method Summary

    Modifier and Type
    Method
    Description
    addButton(int keyCode, float centerX, float centerY, float radius)
    Adds a button mapped to the given key code.
    addButton(int keyCode, float radius, int hAlign, int vAlign, float margin)
    Adds a button anchored to a corner of the view's safe area (see #addJoystick(float, int, int, float)), so it stays inside the safe area and follows rotation automatically.
    addButton(int keyCode, float radius, int hAlign, int vAlign, float marginX, float marginY)
    Adds a button anchored to a corner of the view's safe area with its own horizontal and vertical gaps, so a cluster of buttons can sit in an arc around a bigger one rather than on the corner's diagonal.
    addJoystick(float centerX, float centerY, float radius)
    Adds (or replaces) the analog joystick at the given center and radius.
    addJoystick(float radius, int hAlign, int vAlign, float margin)
    Adds (or replaces) the analog joystick anchored to a corner of the view's safe area, so it stays clear of notches and home indicators and follows rotation automatically (the framework repositions it; you never recompute coordinates).
    findButton(int keyCode)
    The button that holds keyCode, to place a hint beside it or to press it from a test.
     
    boolean
    Whether the controls are showing a replay rather than touches.
    boolean
     
    void
    setVisible(boolean visible)
    Whether the controls are drawn and active.
    void
    showReplayed(float axisX, float axisY, int[] heldKeys)
    Shows input that came from somewhere other than a finger -- a recorded run playing back -- so a viewer sees what is being pressed.
    void
    Ends #showReplayed(float, float, int[]): touches drive the controls again.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • LEFT

      public static final int LEFT
      Horizontal anchor: pin to the left safe-area edge.
      See Also:
    • CENTER

      public static final int CENTER
      Horizontal/vertical anchor: center on the safe area.
      See Also:
    • TOP

      public static final int TOP
      Vertical anchor: pin to the top safe-area edge.
      See Also:
    • BOTTOM

      public static final int BOTTOM
      Vertical anchor: pin to the bottom safe-area edge.
      See Also:
  • Method Details

    • addJoystick

      public VirtualJoystick addJoystick(float centerX, float centerY, float radius)
      Adds (or replaces) the analog joystick at the given center and radius. Returns it so you can tune the dead zone etc.
    • addJoystick

      public VirtualJoystick addJoystick(float radius, int hAlign, int vAlign, float margin)
      Adds (or replaces) the analog joystick anchored to a corner of the view's safe area, so it stays clear of notches and home indicators and follows rotation automatically (the framework repositions it; you never recompute coordinates). hAlign is one of #LEFT / #CENTER / #RIGHT, vAlign one of #TOP / #CENTER / #BOTTOM, and margin is the gap from the safe-area edges.
    • addButton

      public VirtualButton addButton(int keyCode, float centerX, float centerY, float radius)
      Adds a button mapped to the given key code. Use com.codename1.ui.Display#getKeyCode(int) to map a game action.
    • addButton

      public VirtualButton addButton(int keyCode, float radius, int hAlign, int vAlign, float margin)
      Adds a button anchored to a corner of the view's safe area (see #addJoystick(float, int, int, float)), so it stays inside the safe area and follows rotation automatically.
    • addButton

      public VirtualButton addButton(int keyCode, float radius, int hAlign, int vAlign, float marginX, float marginY)
      Adds a button anchored to a corner of the view's safe area with its own horizontal and vertical gaps, so a cluster of buttons can sit in an arc around a bigger one rather than on the corner's diagonal.
    • setVisible

      public void setVisible(boolean visible)
      Whether the controls are drawn and active. Hide them on platforms with a real keyboard if you prefer.
    • showReplayed

      public void showReplayed(float axisX, float axisY, int[] heldKeys)

      Shows input that came from somewhere other than a finger -- a recorded run playing back -- so a viewer sees what is being pressed. While replaying, real touches are ignored and nothing reaches GameInput.

      Parameters
      • axisX: the joystick's horizontal axis, -1 to 1
      • axisY: the joystick's vertical axis, -1 to 1
      • heldKeys: the key codes of the buttons held, or null for none
    • stopReplay

      public void stopReplay()
      Ends #showReplayed(float, float, int[]): touches drive the controls again.
    • isReplaying

      public boolean isReplaying()

      Whether the controls are showing a replay rather than touches.

      Returns

      true between #showReplayed(float, float, int[]) and #stopReplay()

    • isVisible

      public boolean isVisible()
    • getJoystick

      public VirtualJoystick getJoystick()
    • findButton

      public VirtualButton findButton(int keyCode)

      The button that holds keyCode, to place a hint beside it or to press it from a test.

      Parameters
      • keyCode: the key code the button was added with
      Returns

      the button, or null when there is none