-
-
Notifications
You must be signed in to change notification settings - Fork 22
Developing a System Module
Token Action HUD is a Foundry VTT application generated by two modules: the Token Action HUD Core module and a dedicated Token Action HUD system module (e.g., Token Action HUD D&D5e for the D&D 5e system).
TAH Core drives the Token Action HUD application. It provides base classes for Token Action HUD system modules to extend. The HTML templates and CSS styles are part of TAH Core. TAH Core is also responsible for building out all of the subcategories and actions that are common to all Foundry VTT systems: compendiums, macros and actions such as making a token visible/invisible and adding tokens to the combat tracker.
TAH system modules are responsible for building out the subcategories and actions related to the system. They also define the base layout of the HUD as well as the system-defined subcategories available to the user.
A TAH system module will have the following at a minimum:
- An ActionHandler class, which extends TAH Core's ActionHandler class.
- A RollHandler class, which extends TAH Core's RollHandler class.
- A SystemManager class, which extends TAH Core's SystemManager class.
- A callback handler for TAH Core's tokenActionHudCoreApiReady hook.
- A tokenActionHudSystemReady hook to pass its SystemManager class and its required core module version to TAH Core.
- A required core module version.
Extends TAH Core's ActionHandler class and builds system-defined actions for the HUD.
-
buildSystemActions(groupIds)
: Called by TAH Core to initiate the build of system-defined actions by the TAH system module.
Extends TAH Core's RollHandler class and handles action events triggered when an action is clicked.
-
doHandleActionEvent(event, encodedValue)
: Called by TAH Core to initiate the handling of action events by the TAH system module.- event: The event.
- encodedValue: The encoded value stored against the button.
Example:
- The 'Club' action is clicked on the HUD.
- TAH Core calls the TAH system module's
doHandleActionEvent
method and passesevent
andencodedValue
(e.g.,'item|lbeVmFOH3ZzSRn7V'
). - The TAH system module decodes
encodedValue
into its component parts usingencodedValue.split(this.delimiter)
and passes the result into the variables:actionType
andactionId
. - The component parts are resolved using
if
andswitch
functions to identify the action as an item use. - The item object is retrieved from the actor object using
coreModule.api.Utils.getItem(this.actor, actionId)
. - The item object's
use
method is called.
Extends TAH Core's SystemManager class and used by TAH Core to register the TAH system module's extended classes. This class must be passed from the TAH system module to TAH Core via the 'tokenActionHudSystemReady' hook. TAH Core will then call the following TAH system module methods:
-
doGetActionHandler
: Initialise and return the system module's ActionHandler. -
doGetRollHandler
: Initialise and return the system module's RollHandler. -
doRegisterDefaultFlags
: Register the system module's default HUD layout and groups. -
doRegisterSettings
: Register the system module's settings. -
getAvailableRollHandlers
: Return a list of available roll handlers.
To extend TAH Core's classes, the TAH system module classes must be wrapped in a callback handler for TAH Core's tokenActionHudCoreApiReady hook. The hook passes TAH Core's module api enabling the TAH system module to access TAH Core's classes and utils. This example code shows the expected format:
export let SystemManager = null
Hooks.once('tokenActionHudCoreApiReady', async (coreModule) => {
SystemManager = class SystemManager extends coreModule.api.SystemManager {
// CODE
}
})
A call of the tokenActionHudSystemReady
hook to pass its SystemManager class and its required core module version to TAH Core. This example code shows the expected format:
import { SystemManager } from './system-manager.js'
import { MODULE, REQUIRED_CORE_MODULE_VERSION } from './constants.js'
Hooks.once('ready', async () => {
const module = game.modules.get(MODULE.ID)
module.api = {
requiredCoreModuleVersion: REQUIRED_CORE_MODULE_VERSION,
SystemManager
}
Hooks.call('tokenActionHudSystemReady', module)
})
This is the required core module version in the format: Major.Minor
or Major.Minor.Patch
, e.g., 1.1
. TAH Core's module version is in the format: Major.Minor.Patch
, however patch releases should never include breaking changes, therefore Major.Minor
should suffice and allow for easy updating of TAH Core patch releases without requiring a corresponding TAH system module release.
Builds the action list and saves the HUD's actions to the actor.
-
actor
: Object. The actor object. -
characterName
: String. The actor's character name. -
delimiter
: String. The delimiter character for using in the action'sencodedValue
. The default is|
. -
token
: Object. The token object.
-
addGroup(groupData, parentGroupData, update = false)
: Add a group to the HUD under the given parent group.-
subcategoryData
:{ id, type, settings, isSelected, info1, info2, info3, subtitleClass }
-
id
: The group id, e.g., 'weapons'. -
name
: The group name as displayed on the HUD. -
type
: The group type: 'system' or 'system-derived'. -
settings
: Optional.{ characterCount, collapse, showTitle , style }
-
characterCount
: Optional. The number of characters to show for each action's name within the group. -
collapse
: Optional. Whether to collapse the group on the HUD:true
orfalse
. -
showTitle
: Optional. Whether to show the group's title on the HUD:true
orfalse
. -
style
: Optional. The group style:list
ortab
.
-
-
isSelected
: Optional. Whether the group is shown on the HUD:true
orfalse
. -
info1, info2, info3
: Optional.{ class, text, title }
-
class
: Optional. The CSS class applied to the DIV. -
text
: The text. -
title
: Optional. The title applied to the DIV.
-
-
subtitleClass
: The CSS class applied to the DIV.
-
-
parentGroup
:{ id, type }
-
id
: The parent group's id, e.g., 'inventory'. -
type
: The parent group's type: 'system' or 'system-derived'.
-
-
update
: Whether to update existing groups. Default isfalse
.
-
-
addActions(actionsData, groupData)
: Add a list of actions to the HUD under the given group.-
actions = [{ id, name, encodedValue, cssClass, info1, info2, info3, selected }]
: The list of actions.-
id
: The action id. Most commonly the item id. -
name
: The action name displayed on the button. -
encodedValue
: The value passed to theRollHandler
when an action is clicked. -
cssClass
: Optional. The CSS class applied to the button. -
icon
: Optional. The icon displayed on the button in HTML format, e.g.,<i class="fas fa-plus" title="Bonus"></i>
. -
img
: Optional. The image displayed on the button. SeeUtils#getImage
for respecting TAH Core's 'Display Icons' module setting. -
info1, info2, info3
:{ class, text, title }
-
class
: Optional. The CSS class applied to the DIV. -
text
: The text. -
title
: Optional. The title applied to the DIV.
-
-
selected
: Optional. Whether the action is selected in the HUD:true
orfalse
.
-
-
groupData = { id, type }
: The group data.-
id
: The group id. -
type
: The group type.
-
-
-
addGroupInfo(groupData)
: Add group info to an existing group.-
groupData = { id, type, info }
: The group data.-
id
: The group id. -
type
: The group type. -
info = { info1, info2, info3 }
: The group info object.-
info1|info2|info3 = { class, text, title }
: The group info.-
class
: The CSS class. -
text
: The text. -
title
: The tooltip.
-
-
-
-
Extends the ActionHandler with the extendActionList
method.
Handles action events before passing to the TAH system module.
Registers key presses, handles core-defined actions and calls the TAH system module's RollHandler#doHandleActionEvent
.
-
actor
: Object. The actor object. -
alt
: Boolean. Whether the ALT key was pressed when the button was clicked. -
ctrl
: Boolean. Whether the CTRL key was pressed when the button was clicked. -
delimiter
: String. The delimiter character for using in the action'sencodedValue
. The default is|
. -
doRenderItem(actor, itemId)
: Renders the item's sheet.-
actor
: The actor object. -
itemId
: The item's id.
-
-
isRenderItem()
: Returns a boolean for whether the item sheet can be rendered. -
rightClick
: Boolean. Whether the button was right-clicked. -
shift
: Boolean. Whether the SHIFT key was pressed when the button was clicked. -
token
: Object. The token object.
Intialises core and system classes.
Provides common functions.
-
checkAllow(userRole)
: Returns a boolean for whether the user is allowed to use the HUD.-
userRole
: The user role as obtained withgame.user.role
.
-
-
deepClone(original, options)
: A copy of Foundry VTT'sdeepClone
function. -
getActor(actorId, tokenId)
: Returns the actor object either from the actor id or token id.-
actorId
: The actor id. -
tokenId
: The token id.
-
-
getImage(entity, defaultImages = [])
: If the 'Display Icons' module setting is enabled, return the image url from an entity (actor, token, item, etc.).-
entity
: Object or String. The entity object (actor, token, item, etc.) or the URL for the image. -
defaultImages
: Array. A list of default images to not return if the entity uses one.
-
-
getItem(actor, itemId)
: Return the item object from the actor.-
actor
: The actor object. -
itemId
: The item id.
-
-
getToken(tokenId)
: Return the token object.-
tokenId
: The token id.
-
-
getControlledTokens()
: Returns a list of controlled token objects. -
getFirstControlledToken()
: Returns the first controlled token object. -
i18n(toTranslate)
: A shorthand for Foundry VTT'sgame.i18n.localize()
function.-
toTranslate
: The value to translate.
-
-
isModuleActive(moduleId)
: Returns a boolean for whther the module is active.moduleId
: The module id. -
getModuleTitle(moduleId)
: Returns the module's title.-
moduleId
: The module id.
-
-
getSetting(key, defaultValue = null)
: Returns the setting's value for the Token Action HUD Core module.-
key
: The setting. -
defaultValue
: The default value to return if the setting does not exist.
-
-
setSetting(key, value)
: Sets the setting's value for the Token Action HUD core module.-
key
: The setting. -
value
: The value to set.
-
-
getUserFlag(key)
: Returns the user flag's value for the Token Action HUD Core module.-
key
: The flag.
-
-
setUserFlag(key, value)
: Sets the user flag's value for the Token Action HUD Core module.-
key
: The flag' -
value
: The value to set.
-
-
unsetUserFlag(key)
: Unsets the user flag for the Token Action HUD Core module.-
key
: The flag.
-
Logs messages to the console
-
info(message, notify = false)
: Log an info message to the console.-
message
: The message. -
notify
: Optional. Whether to notify the user.
-
-
error(message, notify = false)
: Log an error message to the console.-
message
: The message. -
notify
: Optional. Whether to notify the user.
-
-
debug(message, data)
: Log a debug message to the console where the 'Enable Debugging' module setting is enabled.-
message
: The message. -
data
: Optional. The data to include.
-
Token Action HUD Core's CSS stylesheets include classes for use within the action's cssClass
or action info's class
property.
-
active
: Used alongside thetoggle
class to indicate that an action is active. -
disabled
: Indicate that an action is disabled. -
tah-spotlight
: Color an action's info text. Good for spotlighting specific text when there are several on the action. -
toggle
: Indicate that an action is toggleable.
Hooks.callAll('forceUpdateTokenActionHud')
Call this hook to trigger a Token Action HUD update. Most commonly used in TAH system modules in the RollHandler class to update the HUD after handling an actino event.
A pipe-separated list of identifiers built by the ActionHandler
and resolved by the RollHandler
. Most commonly, encodedValue
will include an action type to identify how to handle the action and id to identify the source object. For example, an encodedValue
of 'item|lbeVmFOH3ZzSRn7V'
identifies the action as an item use (item
) and the item object lbeVmFOH3ZzSRn7V
. The encodedValue
is stored as a string within the HTML button value. All handling is done on the system module side.