Skip to content

Commit

Permalink
Added parameter setting in filename:
Browse files Browse the repository at this point in the history
* Wildcard images that include any of the following
_height<number>_
_width<number>_
_scale<number>_
anywhere in the name will use the value of <number>
instead of the prototype token values to set the dimensions of the token
  • Loading branch information
javieros105 committed Jul 21, 2021
1 parent e08771e commit 57935f4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ If you don't want the image on drop to be chosen randomly, you can use the field

If you're gonna use the default image field, I recommend you use an image file that follows the wildcard pattern. but it's not required and you can have a different default image. If you choose an image that doesn't follow the pattern you just won't be able to pick it again after changing it in the images panel.

You can also use the image filename to set the dimensions of the token using the following example format:

- wildcard images use this pattern `name*`
- `name_height<number>_width<number>_scale<number>_.extension`, it's important that each parameter s preceded and followed by an underscore, so if you want to set up height, there has to be a `_height<number>_` somewhere in the name
- you can choose to change `<number>` for a positive integer or floating number and which one to fill. If there are any parameter missing then the module will use the prototype token parameters to fill the ones missing, so you can use images with no parameter setting at all or just set one or two of the parameters.
- these are valid filenames that will be picked up by the module `name.extension`, `name-different.extension`, `name-other_height2_scale1.5_.extension`

## Installation
To install the module, follow any of these methods:

Expand Down
Binary file modified token-hud-wildcard.zip
Binary file not shown.
25 changes: 23 additions & 2 deletions token-hud-wildcard/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ const tokenHUDWildcard = {
}
}

const getTokenDimensions = (token, imgName) => {
const height = imgName.match(/_height(.*)_/)
const width = imgName.match(/_width(.*)_/)
const scale = imgName.match(/_scale(.*)_/)

const prototypeData = token._actor.data.token

return {
height: height ? parseFloat(height[1]) : prototypeData.height,
width: width ? parseFloat(width[1]) : prototypeData.width,
scale: scale ? parseFloat(scale[1]) : prototypeData.scale,
}
}

Hooks.on('init', () => {
tokenHUDWildcard.registerSettings()
})
Expand All @@ -39,8 +53,11 @@ const WildcardDefault = {
Hooks.on('preCreateToken', (token, data, options, userId) => {
const actor = token.actor
const defaultValue = data.flags['token-hud-wildcard'] ? data.flags['token-hud-wildcard'].default : ''

if (defaultValue !== '' && actor?.data.token.randomImg) {
token.data.update({img:defaultValue});
const dimensions = getTokenDimensions(token, defaultValue)
let updateInfo = { img: defaultValue, ...dimensions }
token.data.update(updateInfo)
}
})
},
Expand Down Expand Up @@ -131,7 +148,11 @@ Hooks.on('renderTokenHUD', async (hud, html, token) => {
const index = controlled.findIndex(x => x.data._id === token._id)
const tokenToChange = controlled[index]
const updateTarget = is080 ? tokenToChange.document : tokenToChange
updateTarget.update({ img: event.target.dataset.name })

const dimensions = getTokenDimensions(updateTarget, event.target.dataset.name)
let updateInfo = { img: event.target.dataset.name, ...dimensions }

updateTarget.update(updateInfo)
})
})
})
4 changes: 2 additions & 2 deletions token-hud-wildcard/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "token-hud-wildcard",
"title": "Token HUD Wildcard",
"description": "This module adds a button to the Token HUD if the token has wildcard set and detects more than 1 image to choose from. Pressing the button displays a panel on the right side of the HUD with buttons for each image, pressing one of these allows to change the token image.",
"version": "1.3.1",
"version": "1.4.0",
"minimumCoreVersion": "0.6.0",
"compatibleCoreVersion": "0.8.7",
"compatibleCoreVersion": "0.8.8",
"author": "Javieros",
"scripts": ["main.js"],
"styles": ["./css/token-hud-wildcard.css"],
Expand Down

0 comments on commit 57935f4

Please sign in to comment.