Menus and pages in the game's style

Script mods can work in the game's own menus, with the game's own look: add buttons to the main menu, MORE, the three hunt menu pages and the hunt's menu; hide or move the game's buttons; open whole pages drawn with the game's backgrounds, fonts and buttons; and react to the menu actions before the game does. No ImGui: these are the game's own GUI controls, so they light up, fade and take touches (Android) exactly like the game's. Everything here works in both games (API 3, "api": 3 in mod.json). The reference: menu, the gui and menu_* events, draw (draw.sprite, the game's fonts).

A button and a page

local page = menu.page("mymod:help", {title = "Field guide"})
page:text("Shots to the head and the heart kill at once. Walk, do not run: running is loud.", {width = 320})
page:image("button_dino_active_9")
page:button("GOT IT", function() page:close() end)

menu.button("main", "GUIDE", function() page:open() end)

GUIDE appears in the main menu's grid under the game's buttons, as one more button of the same kind; pressing it fades to the page like the game changes screens. The page has the MORE screen's background, a red title, your text, picture and button in one centred column, and a BACK button where the game has its own. BACK, Esc and the Android back button close it.

Make menus and pages at load time: they stay until the scripts reload (F9, Apply in the Mods screen), when everything a mod made goes away and its new run makes them again.

Where things go

The first argument of menu.button (and menu.text, menu.image, menu.list) says where:

where What Mods' buttons go (without x, y)
main the main menu the grid goes on below the game's rows, two to a row (an odd last one centred), three to a row from five on; a second extra row moves the whole grid up one row. Six fit: more need places of their own (or a page)
more MORE the column below OPTIONS / SWITCH TO / ABOUT, then a second column to the right
hunt_area, hunt_species, hunt_weapon the hunt menu's pages the right column, upwards from above the option buttons (species page: from the bottom bar); the small button style
game the hunt's menu (ESCAPE / RESTART / OPTIONS, the MENU tab) cells of the same kind around the game's, sliding in with them
a page id your page one centred column from the top (top of the page, 64 by default)

Positions are game units, the units the game lays out its menus in: the screen is 320 units high (480 wide on a screen narrower than 3:2, a tablet), x from the left edge, y from the top. menu.screen() gives the size; it is the same on every screen of that shape, so a layout made on the PC fits the phone. x, y is an item's centre (a text's y is its top). Give both to place an item yourself; item:move(x, y) moves it later, item:move() puts it back.

on("menu_ready", function(e)
  -- a button in the free corner of the species page
  menu.button("hunt_species", "TIPS", showTips, {x = e.width - 70, y = 40})
end)

menu_ready comes once the game has made its menus (the mods load before that): measure there (menu.screen, menu.spriteSize, menu.textSize, menu.buttons).

The game's buttons

Menu Names
main hunt, survive (Dinosaur Hunter), trophy, more, mods, quit
more options, switch, about, back
hunt_area back, next, dawn, day, night, observer
hunt_species back, next
hunt_weapon back, hunt, camouflage, cover_scent, tranquilizer, radar
game escape, restart, options (they can be hidden, not moved)
menu.hide("main", "survive")              -- gone, and takes no presses
menu.move("more", "about", 400, 120)      -- its new centre (an icon on it moves along)
for _, b in ipairs(menu.buttons("hunt_area")) do print(b.name, b.x, b.y, b.shown) end
menu.reset()                              -- all of them back

The hunt menu's option buttons are also equipment: a content pack hides them or adds its own items with "hidden", which is usually the better way.

Events

menu_action comes when the player presses one of the game's buttons, before the game acts on it; set e.cancelled = true and the game never sees the press. menu_select does the same for the hunt menu's map, species and weapon cells (e.kind, e.id, e.selected).

on("menu_action", function(e)
  if e.menu == "hunt_weapon" and e.button == "hunt" then
    local s = menu.selection()
    if #s.weapons > 2 then
      game.message("Two weapons at most in this mod")
      e.cancelled = true            -- the hunt does not start
    end
  end
end)

on("menu_select", function(e)
  if e.kind == "species" and e.id == "trex" and e.selected and not unlockedByQuest then
    e.cancelled = true              -- the T-Rex cell cannot be chosen yet
  end
end)

menu.selection() is the hunt menu's choice as it stands: map, time, observer, species, weapons, equipment (ids) and the prices. menu_open / menu_close tell other mods about pages (e.page); your own page has onOpen, onClose and onBack (return false from it to stay open).

Pages

local shop = menu.page("trader:main", {
  title = "Trader",
  background = "menu_hunt_background",   -- any sprite; false: the screen below, dimmed
  back = "LEAVE",                          -- the BACK button's label (false: none)
  onOpen = function(p) refresh() end,
  draw = function(p)                       -- every frame, with the game's renderer (see below)
    draw.text(20, 20, "Credits: " .. player.credits(), "#ffe020", nil, "left", "ccra14")
  end,
})
local credits = shop:text("")
shop:list({"Ammunition (20)", "Resupply (50)", "Map of the area (80)"}, function(i, row) buy(i) end, {rows = 3})
shop:button("BUY ALL", buyAll)

Drawing with the game's assets

The gui event runs inside the game's own GUI pass, every frame, in the menus and in hunts. There draw.sprite, draw.text, draw.textSize, draw.fillRect, draw.rect, draw.line and draw.screen use the game's renderer, its sprites and its fonts, in game units: what you draw sits among the game's own pictures (over its menus, under its popups) instead of on top of the screen like the draw event's ImGui drawing. A page's draw function is the same pass, above the page's background.

on("gui", function(e)
  if e.menu == "main" then
    draw.sprite("icon_radar", 10, 10, {scale = 0.8})                    -- a sprite of the game's ui/*.cfg
    draw.text(40, 12, "Modded edition", "#ffe020", nil, "left", "ccra14") -- one of the game's fonts
  end
end)

Sounds

A button or list option sound = "menugo.wav" plays one of the game's .wav files (or your mod's) when it is pressed (the game's own menus are silent; menugo.wav, menugo2.wav and menumov.wav are its menu sounds). sound.play plays any sound at any time.

Phones and tablets

Nothing changes: the mods' buttons are the game's GUI controls, so taps reach them like the game's, the game's tablet layout (480 units wide) has the same names and the automatic places follow it. A three-finger tap still opens the mod menu; a page closes with the back button.

Limits

The example examples/mods/ice-age-lodge adds an animal and an equipment item to Ice Age's hunt menu and a LODGE page listing every animal with its picture; examples/tests/test_menu_api uses every function here.