Latest version | 1.0.0 |
---|---|
Minimum Core | 12 |
Compatible Core | 12.327 |
Last updated | 10 months ago |
Created | 10 months ago |
Authors | |
Languages |
|
Systems | All systems |
Project source | Project URL |
This tiny module makes it easy to add custom buttons to item chat cards via item macros.
It's meant to be used as an "plugin" for the Item Macro module.
Two example macros that demonstrate how this module works can be found below:
async function setToken(src) {
let tokens = actor.getActiveTokens()
for (let token of tokens) {
await token.document.update({texture: {src}})
}
await actor.prototypeToken.update({texture: {src}})
}
item.useWithButtons(args, [
{
label: "Create Illusion",
icon: "fa-cat",
callback: () => setToken("some/path/token_1.png")
},
{
label: "Dispel Illusion",
icon: "fa-snake",
callback: () => setToken("some/path/token_2.png")
}
])
Using an item with this macro will create the following chat message:
The buttons, when pressed, will call one of the two callbacks defined in config (and in this case change the image of the item owner's token).
item.useWithButtons(args, [
{
label: "Draw From Roll Table",
icon: "fa-dice",
callback: async () => {
let table = await game.packs.get("world.roll-tables").getDocument("TgjUynKRArpImDkn");
let roll = await new Roll(table.formula, actor.getRollData()).roll()
await game.dice3d?.showForRoll(roll, game.user, true)
table.toMessage(
table.getResultsForRoll(roll.total),
{roll, messageData:{speaker}}
)
}
}
])
Using an item with this macro will create the following chat message:
The first button is an unchanged standard dnd5e button ("don't consume usage" was checked when the item was used to create this screenshot), the second button is the added custom button.
When clicked, the custom button will call the callback set in config (in this case roll from the target roll table).
This module adds two things:
1. `Item.prototype.useWithButtons` function that:
- if `args[0].macroButton` is not set calls `item.use` and amends the resulting `cardData` before calling `ChatMessage.create`.
- otherwise calls one of the callbacks from the button config
2. a pre-render chat message hook that registers a event listener on the custom buttons, this listener decodes the chat card data and re-executes the item's macro passing a custom args value.
Basically the macro gets execute two (or more) times. The first time when used and then each time the chat button is pressed. `args` value is used to detect which one is currently happening.
Buttons get added directly to the chat message html before `ChatMessage.create` is called. `data-action` is set to `"macro-button"` and one additional data attribute gets added: `data-macro-button`, which holds the button index to later find the correct callback.
I tested it only on dnd5e as thats what I use. It may or may not work on other systems.
/** Button configuration */
interface ButtonConfig {
/** label to render in the button */
label: string
/**
* when set an icon will be added to the button,
* this should be a font awesome icon class (like fa-dice)
* see https://fontawesome.com/icons for a full list
*/
icon?: string
/** callback to execute when the button is pressed */
callback: () => void | Promise<void>
}
interface Item {
/**
* @param args: args parameter as passed to the item macro
* @param config: array of buttons to create
* @returns true if callback was called (executed from chat)
* false if the chat message was created (executed from item use)
*/
useWithButtons(args: any[], config: ButtonConfig[]): Promise<boolean>
}
To install this package, open your Foundry Setup screen and navigate to your Module tab and click the Install Module button.
From there, you can either search for the package unique name: item-macro-chat-buttons or copy its manifest URL:
And paste it to the input box at the bottom of your window.
You can install this package directly to your Forge account.
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to deliver visitors with customized advertisements based on the pages they visited before and analyze the effectiveness of the ad campaign.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.