radial menu

A ring of choices you pick by pointing, not by aiming. Each item owns a whole slice of the circle, out to the edge of the screen, so a flick of a stick (or a mouse moved roughly that way) picks it. That makes a radial menu the fastest menu there is on a gamepad, and one of the few in a 3D view where the targets have no edge to miss (Fitts' law with unbounded targets).

import { b3d, b3dLight, b3dGround, fitPanel, label3d, color3d, button3d } from 'tosijs-3d'

const COLOURS = [
  { label: 'red', color: '#dd3333' },
  { label: 'gold', color: '#ddbb33' },
  { label: 'green', color: '#33bb55' },
  { label: 'blue', color: '#3377dd' },
  { label: 'violet', color: '#9933cc' },
]
let scene = null
let box = null
let babylon = null
let at = 0
let custom = '#ff8800'
const say = document.createElement('p')
const hidden = () => box.visibility < 1
const tell = () => {
  const what = at < 0 ? custom : COLOURS[at].label
  say.textContent = hidden() ? 'The box is hidden.' : `The box is ${what}.`
}
const paint = (i) => {
  at = i
  box.visibility = 1
  box.material.diffuseColor = babylon.Color3.FromHexString(i < 0 ? custom : COLOURS[i].color)
  tell()
}
// A colour of your own, picked in the scene's modal (so it works in a headset).
const pickCustom = () => {
  let close = null
  const { svg, height } = fitPanel([
    label3d({ text: 'Custom colour' }),
    color3d({ value: custom, alpha: false, handleChange: (hex) => { custom = hex; paint(-1) } }),
    button3d({ label: 'done', default: true, handleClick: () => close?.() }),
  ], { width: 300 })
  close = scene.showModal(svg, height)
}
// Each slice wears its colour (`color`). HIDE is gated: pull straight out
// into it, then sideways either way to confirm. CUSTOM is set apart: small,
// at the bottom, with empty space round it.
const wheel = () => [
  ...COLOURS.map((c, i) => ({ ...c, checked: i === at })),
  { label: hidden() ? 'show' : 'hide', gated: !hidden() },
  { label: 'custom', icon: 'colorPicker', color: custom, apart: true, checked: at < 0 },
]
const pick = (item) => {
  if (item.label === 'hide' || item.label === 'show') {
    // Hidden is a ghost, so it can still be pressed to bring it back.
    box.visibility = hidden() ? 1 : 0.15
    tell()
  } else if (item.label === 'custom') pickCustom()
  else paint(COLOURS.findIndex((c) => c.label === item.label))
}
const painting = {
  title: 'Paint',
  tap: () => paint((Math.max(0, at) + 1) % COLOURS.length),
  menu: wheel,
  handleSelect: pick,
}
scene = b3d(
  {
    sceneCreated(el, BABYLON) {
      babylon = BABYLON
      box = el.make.box({ size: 1.5, y: 0.75, color: COLOURS[0].color })
      // Press and hold the box: drag toward a colour and let go.
      el.meshMenu(box, painting)
    },
  },
  b3dLight({ intensity: 1 }),
  b3dGround({ width: 20, height: 20, texture: 'checker', textureTiles: 10 })
)
scene.setDpad('right', painting)
const open = document.createElement('button')
open.textContent = 'Open the ring'
// Press and drag off the button to pick; or click, then click a slice.
open.onpointerdown = (e) =>
  scene.openRadialMenu({
    title: 'Paint',
    items: wheel(),
    handleSelect: pick,
    pointer: { x: e.clientX, y: e.clientY },
  })
say.textContent = 'The box is red.'
preview.append(scene, say, open)
.preview { display: flex; flex-direction: column; }
tosi-b3d { flex: 1; }
p { margin: 4px 8px; }
button { margin: 4px 8px; align-self: start; }

Tap the D-pad right (K on a keyboard) and the box takes the next colour. Hold it and the ring opens: point either stick (or the mouse) at a slice and let go to paint it. Hide is gated: push straight out into it (it half lights), then move sideways either way to confirm. Custom, small at the bottom, opens a colour picker. To get out without choosing, let the stick go back to the middle (or move the mouse there) and let go: nothing is lit in the middle, and that undoes an armed or confirmed gate too. So one button does its everyday job on a tap and offers the menu on a hold, as GTA VI does with its D-pad.

With the mouse (or a finger, or a headset's trigger), press and hold the box: the ring opens where you pressed, drag toward a colour and let go. A quick click on the box is the tap. The button does the same from code: press it and drag off toward a slice, or just click it and then click a slice.

Using it

b3d.setDpad(direction, { tap, menu, title, handleSelect }) gives a D-pad direction ('down', 'left' or 'right'; up is the system bar's) a tap and a ring: menu() is read each time it opens, so it lists what is true now. It returns its undo. While a direction has one, a game does not see that button (a biped's reload on the left stops, for instance).

Any button can do the same: b3d.holdMenu('leftBumper', { tap, menu, key, handleSelect }) is GTA V's weapon wheel (key is a keyboard key that does the same, a KeyboardEvent.code).

b3d.openRadialMenu({ title, items, handleSelect }) opens one from code: either stick or the mouse points, a click or A picks, and B or a click in the middle dismisses. It returns its close. In a headset, opened from code it hangs ahead of you and either thumbstick points. Opened by a press and hold with the trigger (a mesh, a panel control), it is drawn where the press landed, a hand-span wide and turned to you, and the controller's ray points at it: aim at a slice and let go, or aim at the middle and let go to pick nothing. Rest on a chevron to go down a level. Given pointer (a press's client x, y, from a pointerdown handler), it is drawn there and the press holds it open: the drag from that point lights a slice and letting go picks. Let go without dragging and it stays open, to click.

b3d.meshMenu(mesh, { tap, menu, title, handleSelect }) puts a ring on a press and hold on a mesh (or its children; or pass a test on the mesh pressed). A quick press is tap, and a press that moves before the hold turns the view as usual, so it costs the camera nothing.

A page with its own pointer handling (an editor that selects on release) asks the scene whose press it was before acting. b3d.ringTookPress is true once a press opens a ring or lands on an open one, until the next press: read it in your pointerup and stand down. (The ring opens after the hold, so by then the press already looked like yours.) In a headset, b3d.uiHolds(hand) says a trigger is the UI's: pressed on a panel or an open ring, or its hold opened one, until it is let go. b3d.radialOpen says whether a ring is open at all. A quick press on a meshMenu target is its tap, and is not taken.

Clicking the right stick (either thumbstick in a headset) picks what is lit and closes the ring, held or not; with nothing lit it dismisses. A pick blinks before it acts, as a Mac menu's chosen item did: lit, then off, on, off, on, a thirtieth of a second each, then the ring goes or opens the next level (RADIAL_BLINK_MS). A keyboard ring's pick blinks off and on, shorter, because typing waits on it.

Either stick picks, since a ring held open by B leaves only the left thumb free. The left stick keeps moving you until you push it past the middle of its travel; then it is the ring's until the ring closes.

In a panel, a control can offer a ring for a press and hold (menu() in its semantics): holding B on it with the pad opens the ring, either stick points, letting go picks; a quick press still presses it. In a headset a press and hold with the trigger does the same: aim toward a slice and let go. A mouse does not, since it can already drag. The inventory does this: hold B on a thing (in a list or a slot) and the ring is the places it can go. On an empty slot, the things that fit, from both halves of the loadout; on a full slot, where its thing can go and a swap sub-ring of what could take its place (the pistol for the sword in your hand: the two change places). A sub-ring's choices call their own handleSelect; the ring's handleSelect hears only its top level.

An item is { label, icon?, color?, disabled?, checked?, weight?, apart?, gated? } (any other fields are yours: handleSelect gets the item back). weight is its share of the ring. color (any CSS colour) is its slice's colour, toned down at rest and full when lit, with ink that reads on it. apart is for the choice you should not make by accident (drop, delete): it points straight down, opposite the rest, narrower, with dead space either side that picks nothing. A big target is easy to hit, so a dangerous one is made small and lonely: Fitts' law, used backwards. gated goes further, like a car's reverse gear: pull straight out into it, which arms it and picks nothing yet, then move sideways, either way and however far, to confirm. Arriving on it from another slice never arms it, so no sweep round the ring lands there by accident. A gated slice is narrow, with empty space either side, and is drawn as a T: the stem from the middle out, an arm along the rim each way. The inventory's drop and close are gated; so would be a shop's "sell all junk" or "repair all". icon is an icon name. Up to eight read well.

Sub-rings. An item with items (a list, or a function read when it opens) opens them in its place, marked by a chevron at its rim. Its way back is a slice of its own at twelve o'clock (back). In a ring opened by a hold (D-pad up, B, the trigger, the grips), the hold lasts the whole way: see resting, below; letting go picks the lit choice or closes, on a chevron or back too. In a ring nothing holds (opened from code, or clicked), picking a chevron opens it; press A (or what opened it) again, point and let go; a click picks with a mouse, and B goes back. An item's own handleSelect is called when it is picked, as well as the ring's.

Resting opens a level, as in a marking menu. Rest on a sub-ring item for half a second (RADIAL_DWELL_MS) and it opens, still held by whatever was holding the ring, so one continuous drag (or one held button and a stick) can go down several levels and let go on the choice. In the new level the slice you are pointing at does not light until you move off it, so the push that opened it does not pick in it; any other choice is one move away. Rest on back and it goes back up a level the same way.

The system bar as a ring. Holding D-pad up opens the scene's system bar as a ring, each button a slice with its own icon, and a bar menu a sub-ring (a menu of choices shows the current one on the bar and on its slice, and in its sub-ring's middle). A panel that is showing is ticked, and picking it closes it; close all sits apart at the bottom, dimmed while nothing is open. Holding up never closes anything by itself. Holding both bumpers (L1 and R1) for half a second opens it too, and in a headset both grips, unless a game has the bumpers; a stick points and letting go picks. In a headset a quick squeeze of both grips opens it to stay: point a thumbstick and click it, or tap a slice with the trigger (squeeze both again to close it, or tap outside it). When you look away from the headset's system bar it folds to a tab at the edge of the view, and a tap on the tab opens this ring. In a headset a ring that a press did not open is drawn straight ahead of your eyes. b3d.openSystemRing() opens it from code. That is how a scene with no visible UI still has all of it a hold away.

radialMenuSvg(items, options) is the drawing alone (a highlight(i) on it moves the lit slice in place); radialPick (radial-layout) is the pick.