Repository navigation
Pickers
When the user has to choose something on the canvas, CTLib gives you the same frame the Combat Tools pickers use: a bar across the top with a title, a status line and Confirm and Cancel buttons, the rest of the scene dimmed with holes cut around what matters, and the rest of Foundry's UI tucked away.
Only one picker overlay is open at a time. Opening a new one closes the last.
const { beginPickerOverlay } = ctlib;
const overlay = beginPickerOverlay({
title: 'Mark a Target',
status: 'Click a creature. Enter to confirm, Escape to cancel.',
tokens: candidates,
onConfirm: () => finish(true),
onCancel: () => finish(false),
});| Option | Default | What it does |
|---|---|---|
title |
Bold text at the left of the bar. | |
status |
'' |
The instruction line. |
detail |
'' |
A second, smaller line under the bar. Hidden when empty. |
tokens |
[] |
Tokens to cut holes around in the dimmed canvas. |
holeRects |
null |
Holes as canvas rectangles { x, y, w, h }, in place of tokens. |
dim |
true |
Dim the canvas outside the holes. |
frame |
follows dim
|
Pan and zoom to fit the holes, then pan back when the picker ends. |
hideUi |
true |
Hide Foundry's sidebar, controls and other UI while picking. |
uiToggle |
false |
Show a button that brings the UI back and hides it again. |
onUiToggle |
null |
Called with true when the UI is hidden, false when shown. |
showConfirm, showCancel
|
true |
Show those buttons. |
onConfirm, onCancel
|
null |
Called when a button is clicked. |
extraButtons |
[] |
More buttons before Confirm: { label, icon, kbd, tooltip, className, onClick }. An entry that is an array of two buttons stacks them in one column, like the Area Picker's Raise over Lower. |
buttonGrid |
false |
Lay the buttons out in a grid, for pickers with many. |
controls |
[] |
Rows for the ? badge: { input, action }, as text or lang keys. List what your picker reacts to that no button shows. |
onRightClick |
null |
Called on a right-click on the map. A right press that moves before release is Foundry moving the view and doesn't count, so right-drag keeps working during your pick. |
cancelOnRightClick |
false |
Add a "Right-click: Cancel" row. Pass whether your onRightClick cancels. |
rightDragPans |
true |
List right-drag as moving the view. Set it to false only if your picker uses the right button some other way. |
scrollZooms |
true |
List scroll as zooming. |
The Confirm and Cancel buttons show Enter and Escape, but the overlay doesn't listen for keys: handle them in your picker and call the same functions.
Every bar ends with a round ? badge. Hovering it shows your controls rows, then the map controls Foundry keeps during a pick: right-drag and the arrow keys to move the view, scroll to zoom. Leave out anything a button already shows, like Enter and Escape.
It returns a handle:
| Method | What it does |
|---|---|
setStatus(text) |
Replace the status line. |
flashWarning(text) |
Show text in the status line in red with a shake, for 2.5 seconds, then go back to the status. |
setReady(on) |
Highlight Confirm, for when the choice is complete. |
setTokens(list), setHoleRects(rects)
|
Move the holes. |
end(focus) |
Close the overlay, the same as endPickerOverlay. |
| Function | What it does |
|---|---|
endPickerOverlay(focus = null) |
Closes the overlay and pans back to where the view was. Pass a point { x, y } to pan there instead. Safe to call when nothing is open. |
revealPickerUi() |
Brings Foundry's UI back without closing the picker, for when your picker opens a dialog. |
watchRightClick(callback) |
Calls callback on a right-click on the map, ignoring right-drags, and returns a function that stops watching. The same test as onRightClick, for a picker without the bar. |
Animated marks that sit on tokens during a pick.
| Function | What it does |
|---|---|
setPickerArrow(token, color = null, alphaMult = 1) |
Four pulsing arrows pointing in at the token: "this one could be chosen". |
setPickerTarget(token, color = null, alphaMult = 1) |
Four corner brackets, like Foundry's own targeting marks: "this one is chosen". |
removePickerArrow(token), removePickerTarget(token)
|
Remove one token's mark. |
clearPickerArrows() |
Remove every mark. Call it when your picker ends. |
color is a number such as 0x66aaff. Leave it out and the mark takes the token's disposition colour, like Foundry's own target marks. alphaMult fades a mark, for candidates that are allowed but not suggested. Calling either again for the same token updates its colour and fade.
A path is an animated line through grid squares that belongs to a token, such as the way an effect reaches it through an opening (see openingPath in Line of Effect and Cover).
| Function | What it does |
|---|---|
setPickerPath(token, points, color = 0xa060ff, { style, colors, key, lit }) |
Draws a path through points, squares as { i, j } grid offsets, row then column. Setting it again for the same key replaces it. |
lightPickerPath(token, lit) |
Brightens or fades the path keyed by token's id. |
| Option | Default | What it does |
|---|---|---|
style |
'wisp' |
'wisp' for one drifting strand, or 'helix' for strands twisting around each other, as for a chain from target to target. |
colors |
null |
One colour per helix strand. |
key |
the token's id | Lets one token carry more than one path. |
lit |
false |
Start bright. |
A path stays faint until it's lit: by lit or lightPickerPath, while its token has an arrow or a target mark, or while the user targets it. With reducedEffects() on, paths are drawn as plain lines with an arrowhead. clearPickerArrows() removes paths too.
runColoredTokenPicker(options) asks the user to click one token. Each candidate is lit in its own colour, and the hovered one brightens and gets arrows. It resolves with the chosen token, or null on Escape (or a right click, when the cancelOnRightClick config is on), and null straight away when there are no candidates.
const picked = await runColoredTokenPicker({
tokens: eligible,
colorMap: new Map(eligible.map((t) => [t.id, '#d9a63f'])),
hint: 'Choose who takes the hit.',
});| Option | Default | What it does |
|---|---|---|
tokens |
The tokens the user can pick from. | |
colorMap |
empty | A Map of token id to a CSS hex colour such as '#d9a63f'. A token not in it is lit blue. |
hint |
'' |
The status line in the picker bar. |
title |
"Pick a Token" | The bar's title. |
arrowColor |
from colorMap
|
A function (token) => number for the hovered token's arrows, such as (t) => t._getBorderColor() to use its disposition colour. |
autoPick |
false |
When there's only one candidate, resolve with it without asking. |
control |
false |
Select the chosen token on the canvas, as if the user had clicked it outside a picker. |
preselect |
null |
A candidate to start with: it's lit and marked, and Enter or Confirm picks it. Clicking another token still picks that one. Ignored when it isn't one of tokens. |
Combat Tools picks the source of an effect this way, with autoPick and control both on: a player who owns one token isn't asked at all.
hitToken(point, tokens) returns the first of tokens whose footprint covers a canvas point, or null. It's the test the pickers use for what's under the pointer, for a picker of your own that should behave the same.
chooseFreeSquare(token, landedOn = null, options = {}) asks the user where a creature ends up when it can't stay where it is: it rings the square around token (or around landedOn, the creature it fell onto) outward until it finds free squares, shows them in green with the blocked ones in red, draws an arrow to the hovered square, and resolves with the chosen grid square { x, y }.
| Option | Default | What it does |
|---|---|---|
maxRadius |
10 |
How many rings out to look. |
forceOnCancel |
false |
On Escape, resolve with the first free square instead of null. |
title, status
|
CTLib's own | The bar's text. |
A square is free when no living creature and no unbroken obstacle tile is on it. It resolves null straight away when there's nowhere free.
pickCanvasTarget({ draw, hitTest, hint }) is a bare picker for anything that isn't a token: you decide what's under the pointer and how to draw it.
const square = await pickCanvasTarget({
hint: 'Pick a square.',
hitTest: (point) => toGrid(point),
draw: (graphics, hover) => { if (hover) graphics.beginFill(0x44cc44, 0.4).drawRect(hover.x * GRID(), hover.y * GRID(), GRID(), GRID()).endFill(); },
});hitTest(point) gets the canvas point under the pointer and returns what's there, or null. draw(graphics, hover) redraws whenever that changes. A left click on something resolves with it; Escape resolves null, and so does a right click when the cancelOnRightClick config is on. hint shows as a notification when the picker opens. It uses no overlay bar: open one yourself if you want it.
stackedPrompt({ title, heading, options, width = 380, count = 1 }) is a small dialog of large buttons stacked one above the other, each with an image or icon, for a choice between a few named options.
const answer = await stackedPrompt({
title: 'Brace',
heading: 'When the blow lands',
options: [
{ action: 'stand', label: 'Stand your ground', img: 'icons/equipment/shield/heater-steel-worn.webp' },
{ action: 'roll', label: 'Roll with it', icon: 'fa-solid fa-person-running' },
],
});It resolves with the chosen option's action, or null when the dialog is closed. With count above 1 the user picks that many, clicking an option again to unpick it, and it resolves with an array of actions in the order they were picked.
Getting Started
Working With Documents
The Canvas
Interface