touch

On-screen buttons for a phone (since API 3).

A phone has no keyboard: a mod that reacts to keys can put a button on the screen that presses its key, or calls a function. Buttons show on Android only, drawn over the game's HUD in its colours (a dark disc, a yellow ring and label, lit while pressed); the finger that presses one never reaches the game (no shot, no turn). On the PC they never show: the keys are there. So one mod works on both, unchanged on the PC.

Places and sizes are in mod pixels, like draw (on a phone the screen's density makes them the same physical size as on a PC screen). x / y below 0 count from the right / bottom edge, so x = -100 stays 100 mod pixels from the right on every screen. Without x / y, buttons line up along the right edge under the game's own buttons.

when says where a button shows: "hunt" (default: in a hunt while nothing covers the 3D view, no menu, map, popup or statistics, the hunter alive), "menus" (outside hunts) or "always". The buttons go with the mod when the scripts are reloaded (F9, Reload scripts).

Functions

# touch.button(options) → integer

since API 3

Adds an on-screen button (Android). It needs a key, an onTap function, or both. Without x and y it takes the next free place along the right edge, under the game's own buttons there, with the other buttons placed that way (a column further left when one is full); x / y below 0 count from the right / bottom edge.

ParameterType
optionstablelabel (text), key (a key name: a tap presses it, the mod's keys.bind runs), onTap (fn(id): called on a tap), x, y, size (default 72) or w, h (mod pixels), when ("hunt", "menus", "always")
Returns
integerbutton id
keys.bind("B", spawnBoss)
touch.button({label = "BOSS", key = "B"})            -- or: keys.bind("B", spawnBoss, {touch = {label = "BOSS"}})

touch.button({label = "FLARE", x = -100, y = -260, size = 80, onTap = function() fireFlare() end})

# touch.remove(id) → boolean

since API 3

Removes a button.

ParameterType
idintegera button id
Returns
booleantrue when it was there

# touch.label(id, text)

since API 3

Changes a button's text (a toggle can show its state).

ParameterType
idintegera button id
textstring
local god = false
local button
button = touch.button({label = "GOD: OFF", onTap = function()
  god = not god
  world.god(god)
  touch.label(button, god and "GOD: ON" or "GOD: OFF")
end})

# touch.available() → boolean

since API 3

Whether touch buttons show here.

Returns
booleantrue on a phone, where the buttons show
if touch.available() then game.message("Tap BOSS to call the boss") else game.message("Press B to call the boss") end