Skip to main content

Tool

OBR.tool

Manage custom tools, tool modes and tool actions.

Reference

Methods

create

async create(tool)

Create a new tool.

Parameters

NAMETYPEDESCRIPTION
toolToolThe new tool to create

remove

async remove(id)

Remove an existing tool.

Parameters

NAMETYPEDESCRIPTION
idstringThe ID of the tool to remove

createAction

async createAction(action)

Create a new tool action.

Parameters

NAMETYPEDESCRIPTION
actionToolActionThe new tool action to create

removeAction

async removeAction(id)

Remove an existing tool action.

Parameters

NAMETYPEDESCRIPTION
idstringThe ID of the tool action to remove

createMode

async createMode(mode)

Create a new tool mode.

Parameters

NAMETYPEDESCRIPTION
modeToolModeThe new tool mode to create

removeMode

async removeMode(id)

Remove an existing tool mode.

Parameters

NAMETYPEDESCRIPTION
idstringThe ID of the tool mode to remove

activateMode

async activateMode(toolId, modeId)

Activate a specific mode for a given tool.

Parameters

NAMETYPEDESCRIPTION
toolIdstringThe ID of the tool
modeIdstringThe ID of the tool mode to activate

getMetadata

async getMetadata(id)

Get the metadata for the given tool.

Parameters

NAMETYPEDESCRIPTION
idstringThe ID of the tool

Returns a Metadata object.


setMetadata

async setMetadata(toolId, update)

Update the metadata for a given tool.

Example

Update the stroke color of a custom drawing tool

async function handleChange(value: string) {
await OBR.tool.setMetadata("com.example.drawing", {
strokeColor: value,
});
}

This does not override any metadata properties that already exist. For example if the metadata looks like this:

{
strokeWidth: 1,
strokeColor: "red"
}

And handleChange("blue") is called in the example above then the resulting metadata will look like this:

{
strokeWidth: 1,
strokeColor: "blue"
}

Parameters

NAMETYPEDESCRIPTION
toolIdstringThe ID of the tool
updatePartial<Metadata>The metadata data update to use. The metadata is spread into the existing metadata.

Type Definitions

Tool

A tool in the toolbar.

TYPE
object

Properties

NAMETYPEDESCRIPTION
idstringThe ID of this tool
iconsToolIcon[]The icons for this tool
disabledToolFilterAn optional filter for checking if the tool should be disabled
onClick(context: ToolContext, elementId: string) => void | undefined | booleanAn optional callback for when the tool icon is clicked. By default the clicked tool will be activated. If defined return true to perform the default click action.
shortcutstringAn optional key shortcut for this tool
defaultModestringAn optional ID of the mode to be activated by default
defaultMetadataMetadataAn optional Metadata object to use as default

ToolAction

A tool action defines an item in the tool menu bar.

Unlike a tool mode an action is a clickable item but won't be activated when clicked.

TYPE
object

Properties

NAMETYPEDESCRIPTION
idstringThe ID of this action
iconsToolIcon[]The icons for this action
disabledToolFilterAn optional filter for checking if the action should be disabled
onClick(context: ToolContext, elementId: string) => voidAn optional callback for when the action icon is clicked
shortcutstringAn optional key shortcut for this action

ToolMode

A tool mode will be activated when clicked.

Once a mode is active it will receive pointer events such as onToolMove and onToolDragMove.

TYPE
object

Properties

NAMETYPEDESCRIPTION
idstringThe ID of this mode
iconsToolIcon[]The icons for this mode
disabledToolFilterAn optional filter for checking if the mode should be disabled
onClick(context: ToolContext, elementId: string) => void | undefined | booleanAn optional callback for when the mode icon is clicked. By default the clicked mode will be activated. If defined return true to perform the default click action.
shortcutstringAn optional key shortcut for this mode
cursorsToolCursor[]An optional array of cursors
onToolClick(context: ToolContext, event: ToolEvent) => void | undefined | booleanAn optional callback for when a single click is made by the tool. By default the clicked item will be selected if it is unlocked. If defined return true to perform the default click action.
onToolDoubleClick(context: ToolContext, event: ToolEvent) => void | undefined | booleanAn optional callback for when a double click is made by the tool. By default the clicked item will be selected event if it is locked. If defined return true to perform the default double click action.
onToolDown(context: ToolContext, event: ToolEvent) => voidAn optional callback for when the pointer enters the down state
onToolMove(context: ToolContext, event: ToolEvent) => voidAn optional callback for when the pointer moves
onToolUp(context: ToolContext, event: ToolEvent) => voidAn optional callback for when the pointer enters the up state
onToolDragStart(context: ToolContext, event: ToolEvent) => voidAn optional callback for when a drag starts on the viewport
onToolDragMove(context: ToolContext, event: ToolEvent) => voidAn optional callback for when dragging on the viewport
onToolDragEnd(context: ToolContext, event: ToolEvent) => voidAn optional callback for when a drag ends on the viewport
onToolDragCancel(context: ToolContext, event: ToolEvent) => voidAn optional callback for when a drag event is canceled by either switching modes or pressing the Escape key
onActivate(context: ToolContext)An optional callback for when the tool mode is activated
onDeactivate(context: ToolContext )An optional callback for when the tool mode is deactivated
onKeyDown(context: ToolContext, event: KeyEvent) => voidAn optional callback for when a key is pressed
onKeyUp(context: ToolContext, event: KeyEvent) => voidAn optional callback for when a key is released
preventDragToolModeFilterAn optional filter for checking if the drag event should be prevented. If the filter returns true the default drag operation will be used which mimics the Move tool.

ToolContext

Holds the state of the tool.

TYPE
object

Properties

NAMETYPEDESCRIPTION
activeToolstringThe ID of the current active tool
activeModestringAn optional ID of the current active tool mode
metadataMetadataThe metadata of the current active tool

ToolEvent

A pointer event for a tool mode.

TYPE
object

Properties

NAMETYPEDESCRIPTION
pointerPositionVector2The pointer position relative to the viewport
targetItemAn optional target for the pointer event
transformerbooleanIf we have a target are we selecting a transformer control point
altKeybooleanIs the Alt key pressed down
shiftKeybooleanIs the Shift key pressed down
ctrlKeybooleanIs the Ctrl key pressed down
metaKeybooleanIs the Meta key pressed down

KeyEvent

A key event for a tool mode.

TYPE
object

Properties

NAMETYPEDESCRIPTION
codestringA string representation of the physical key pressed
keystringA string representation of the key
altKeybooleanIs the Alt key pressed down
shiftKeybooleanIs the Shift key pressed down
ctrlKeybooleanIs the Ctrl key pressed down
metaKeybooleanIs the Meta key pressed down
repeatbooleanIs the key being held down such that it is automatically repeating

ToolIcon

An icon for a tool.

TYPE
object

Properties

NAMETYPEDESCRIPTION
iconstringThe url of the icon as either a relative or absolute path
labelstringThe label to use for the tooltip of the icon
filterToolFilterAn optional filter to control when this icon will be shown

ToolFilter

A filter to control if various tools, actions or modes will be shown.

TYPE
object

Properties

NAMETYPEDESCRIPTION
activeToolsstring[]An optional array of tool IDs that can be active
activeModesstring[]An optional array of tool mode IDs that can be active
permissionsPermission[]An optional permissions filter
roles("GM" | "PLAYER")[]An optional array of roles needed for the player, defaults to no role needed
metadataKeyFilter[]An optional array of filters to run on the current tool metadata

ToolModeFilter

A filter to use for a tool mode.

Extends ToolFilter.

TYPE
object

Properties

NAMETYPEDESCRIPTION
targetKeyFilter[]An optional array of filters to run on the current target of a tool mode
draggingbooleanAn optional boolean to check the dragging state of the current tool mode

ToolCursor

A cursor to use for a tool mode.

TYPE
object

Properties

NAMETYPEDESCRIPTION
cursorstringA css cursor string e.g. "pointer"
filterToolModeFilterAn optional filter to run on the current tool mode