Skip to content

Commit

Permalink
Fix default wildcard image
Browse files Browse the repository at this point in the history
  • Loading branch information
mhilbrunner committed Nov 3, 2022
1 parent e0cf162 commit 0bce996
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 117 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ I made this module because having to open the Token Configuration to change the
Hopefully this module can help others!

<p style="text-align: center;">
<img src="https://raw.githubusercontent.com/javieros105/FVTT-TokenHUDWildcard/master/images/prototype-config.png" alt="Prototype Config" width="500"/>
<img src="https://raw.githubusercontent.com/javieros105/FVTT-TokenHUDWildcard/master/images/settings.png" alt="HUD Button" height="150"/>
<img src="https://raw.githubusercontent.com/javieros105/FVTT-TokenHUDWildcard/master/images/hud-options1.png" alt="HUD Options" height="280"/>
<img src="https://raw.githubusercontent.com/javieros105/FVTT-TokenHUDWildcard/master/images/hud-options2.png" alt="HUD Options" height="280"/>
<img src="https://raw.githubusercontent.com/mhilbrunner/token-hud-wildcard/master/images/prototype-config.png" alt="Prototype Config" width="500"/>
<img src="https://raw.githubusercontent.com/mhilbrunner/token-hud-wildcard/master/images/settings.png" alt="HUD Button" height="150"/>
<img src="https://raw.githubusercontent.com/mhilbrunner/token-hud-wildcard/master/images/hud-options1.png" alt="HUD Options" height="280"/>
<img src="https://raw.githubusercontent.com/mhilbrunner/token-hud-wildcard/master/images/hud-options2.png" alt="HUD Options" height="280"/>
</p>

_The token images were done by the amazing Ross McConnell at [2-Minute Tabletop](https://2minutetabletop.com/) and are part of the Hero Tokens 3 Pack._
Expand Down Expand Up @@ -44,12 +44,12 @@ To install the module, follow any of these methods:

### Method 1
- Start up Foundry and in the "Add-on Modules" tab click "Install Module".
- In the "Manifest URL" field, paste the link: `https://github.com/mhilbrunner/FVTT-TokenHUDWildcard/releases/latest/download/module.json`
- In the "Manifest URL" field, paste the link: `https://github.com/mhilbrunner/token-hud-wildcard/releases/latest/download/module.json`
- Click "Install" and wait for it to finish.
- It should be installed and available in your games.

### Method 2
- Download the [.zip file](https://github.com/mhilbrunner/FVTT-TokenHUDWildcard/releases/latest/).
- Download the [.zip file](https://github.com/mhilbrunner/token-hud-wildcard/releases/latest/).
- Extract the contents of the zip in your modules folder.
- Restart Foundry and it should be available for your games.

Expand All @@ -61,6 +61,7 @@ To install the module, follow any of these methods:
- Global opacity setting in module settings for token previews in HUD.
- #### Fixes:
- Fixed v10 compatibility
- Fixed wildcard default image feature
- #### Localization:
- Improved localization of all displayable text.
- Languages:
Expand Down
4 changes: 2 additions & 2 deletions lang/de.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"THWildcard.Title": "Hier klicken, um Bildoptionen anzuzeigen",
"THWildcard.Fieldset": "Wildcard Standard",
"THWildcard.DefaultDescription": "When ein neues Token mit Wildcard auf der Spielfläche abgelegt wird und dieses Feld gefüllt ist, wird dieses Bild standardmäßig statt einem zufällig gewählten angezeigt.",
"THWildcard.Fieldset": "Platzhalter-Standardbild",
"THWildcard.DefaultDescription": "Wenn ein neues Token mit Platzhalter auf der Spielfläche abgelegt wird und dieses Feld gefüllt ist, wird dieses Bild standardmäßig statt einem zufällig gewählten angezeigt.",
"THWildcard.DefaultLabel": "Standard-Bild:",
"THWildcard.ButtonTitle": "Datei wählen",
"THWildcard.DisplaySettingName": "Als Bild anzeigen?",
Expand Down
156 changes: 76 additions & 80 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const tokenHUDWildcard = {
config: true,
type: Boolean,
default: true
})
});
game.settings.register("token-hud-wildcard", "imageOpacity", {
name: game.i18n.format('THWildcard.OpacitySettingName'),
hint: game.i18n.format('THWildcard.OpacitySettingHint'),
Expand All @@ -16,16 +16,16 @@ const tokenHUDWildcard = {
range: { min: 0, max: 100, step: 1 },
type: Number,
default: 50
})
});
}
}

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

const prototypeData = token._actor.data.token
const prototypeData = token._actor.data.token;

return {
height: height ? parseFloat(height[1]) : prototypeData.height,
Expand All @@ -35,124 +35,120 @@ const getTokenDimensions = (token, imgName) => {
}

Hooks.on('init', () => {
tokenHUDWildcard.registerSettings()
})
tokenHUDWildcard.registerSettings();
});

Hooks.on('ready', () => {
const renderTokenConfig = 'renderTokenConfig'
Hooks.on(renderTokenConfig, WildcardDefault.render)
WildcardDefault._hookPreTokenCreate()
})
const renderTokenConfig = 'renderTokenConfig';
Hooks.on(renderTokenConfig, WildcardDefault.render);
WildcardDefault._hookPreTokenCreate();
});

const WildcardDefault = {
getDefaultImg: async (token) => {
const flag = token.getFlag('token-hud-wildcard', 'default') || ''
return flag
const flag = token.getFlag('token-hud-wildcard', 'default') || '';
return flag;
},
_hookPreTokenCreate () {
_hookPreTokenCreate() {
Hooks.on('preCreateToken', (parent, data, options, userId) => {
const defaultValue = parent?.actor?.token?.flags['token-hud-wildcard'] ? parent.actor.token.flags['token-hud-wildcard'].default : ''

if (defaultValue !== '' && parent?.actor?.token?.randomImg) {
const dimensions = getTokenDimensions(parent, defaultValue)
let updateInfo = { img: defaultValue , ...dimensions }
mergeObject(data, updateInfo)
parent.data.update(updateInfo)
const defaultValue = parent?.actor?.prototypeToken?.flags['token-hud-wildcard'] ? parent.actor.prototypeToken.flags['token-hud-wildcard'].default : '';
if (defaultValue !== '' && parent?.actor?.prototypeToken?.randomImg) {
/*const dimensions = getTokenDimensions(parent, defaultValue);
let updateInfo = { img: defaultValue, ...dimensions };*/
parent.data.update({"texture.src" : defaultValue});
}
})
});
},
render: async (config, html) => {
const defaultImg = await WildcardDefault.getDefaultImg(config.token)
const defaultImg = await WildcardDefault.getDefaultImg(config.token);
if (config.token._id) {
return
return;
}
const imageDataTab = html.find('.tab[data-tab="appearance"],.tab[data-tab="image"]')
const checkBoxWildcard = imageDataTab.find('input[name="randomImg"]')
const configField = await renderTemplate('/modules/token-hud-wildcard/templates/configField.html', { defaultImg, available: checkBoxWildcard[0].checked })
const imageDataTab = html.find('.tab[data-tab="appearance"],.tab[data-tab="image"]');
const checkBoxWildcard = imageDataTab.find('input[name="randomImg"]');
const configField = await renderTemplate('/modules/token-hud-wildcard/templates/configField.html', { defaultImg, available: checkBoxWildcard[0].checked });

imageDataTab.append(configField)
const defaultConfig = imageDataTab.find('.thwildcard-default')
imageDataTab.append(configField);
const defaultConfig = imageDataTab.find('.thwildcard-default');

defaultConfig.find('button').click(event => {
event.preventDefault()
const input = defaultConfig.find('input')[0]
event.preventDefault();
const input = defaultConfig.find('input')[0];

const fp = new FilePicker({ current: input.value, field: input })
fp.browse(defaultImg)
})
const fp = new FilePicker({ current: input.value, field: input });
fp.browse(defaultImg);
});

checkBoxWildcard.click(event => {
if (event.target.checked) {
defaultConfig[0].classList.add('active')
defaultConfig[0].classList.add('active');
} else {
defaultConfig[0].classList.remove('active')
defaultConfig[0].classList.remove('active');
}
})
});
}
}

Hooks.on('renderTokenHUD', async (hud, html, token) => {
const actor = game.actors.get(token.actorId)
const images = await actor?.getTokenImages() ?? []
const actor = game.actors.get(token.actorId);
const images = await actor?.getTokenImages() ?? [];

if (images.length < 2) {
return
return;
}

const imageDisplay = game.settings.get('token-hud-wildcard', 'imageDisplay')
const imageOpacity = game.settings.get('token-hud-wildcard', 'imageOpacity')/100
const imageDisplay = game.settings.get('token-hud-wildcard', 'imageDisplay');
const imageOpacity = game.settings.get('token-hud-wildcard', 'imageOpacity') / 100;
const imagesParsed = images.map(im => {
const split = im.split('/')
var extension = im.split('.')
extension = extension[extension.length - 1]
const img = ['jpg', 'jpeg', 'png', 'svg', 'webp'].includes(extension)
const vid = ['webm', 'mp4', 'm4v'].includes(extension)
return { route: im, name: split[split.length - 1], used: im === token.texture.src, img, vid, type: img || vid }
})

const wildcardDisplay = await renderTemplate('/modules/token-hud-wildcard/templates/hud.html', { imagesParsed, imageDisplay, imageOpacity })
const split = im.split('/');
var extension = im.split('.');
extension = extension[extension.length - 1];
const img = ['jpg', 'jpeg', 'png', 'svg', 'webp'].includes(extension);
const vid = ['webm', 'mp4', 'm4v'].includes(extension);
return { route: im, name: split[split.length - 1], used: im === token.texture.src, img, vid, type: img || vid };
});

const is080 = !isNewerVersion("0.8.0", game.data.version)
const wildcardDisplay = await renderTemplate('/modules/token-hud-wildcard/templates/hud.html', { imagesParsed, imageDisplay, imageOpacity });

html.find('div.right')
.append(wildcardDisplay)
.click((event) => {
let activeButton, clickedButton, tokenButton;
for ( const button of html.find('div.control-icon') ) {
if (button.classList.contains('active')) activeButton = button
if (button === event.target.parentElement) clickedButton = button
if (button.dataset.action === 'thwildcard-selector') tokenButton = button
for (const button of html.find('div.control-icon')) {
if (button.classList.contains('active')) activeButton = button;
if (button === event.target.parentElement) clickedButton = button;
if (button.dataset.action === 'thwildcard-selector') tokenButton = button;
}

if (clickedButton === tokenButton && activeButton !== tokenButton) {
tokenButton.classList.add('active')
tokenButton.classList.add('active');

html.find('.thwildcard-selector-wrap')[0].classList.add('active')
const effectSelector = is080 ? '[data-action="effects"]' : '.effects'
html.find(`.control-icon${effectSelector}`)[0].classList.remove('active')
html.find('.status-effects')[0].classList.remove('active')
html.find('.thwildcard-selector-wrap')[0].classList.add('active');
const effectSelector = '[data-action="effects"]';
html.find(`.control-icon${effectSelector}`)[0].classList.remove('active');
html.find('.status-effects')[0].classList.remove('active');
} else {
tokenButton.classList.remove('active')
tokenButton.classList.remove('active');

html.find('.thwildcard-selector-wrap')[0].classList.remove('active')
html.find('.thwildcard-selector-wrap')[0].classList.remove('active');
}
})
});

const buttons = html.find('.thwildcard-button-select')
const buttons = html.find('.thwildcard-button-select');

buttons.map((button) => {
buttons[button].addEventListener('click', function (event) {
event.preventDefault()
event.stopPropagation()
const controlled = canvas.tokens.controlled
const index = controlled.findIndex(x => x.data._id === token._id)
const tokenToChange = controlled[index]
const updateTarget = is080 ? tokenToChange.document : tokenToChange

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

updateTarget.update(updateInfo)
})
})
})
event.preventDefault();
event.stopPropagation();
const controlled = canvas.tokens.controlled;
const index = controlled.findIndex(x => x.data._id === token._id);
const tokenToChange = controlled[index];
const updateTarget = tokenToChange.document;

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

updateTarget.update(updateInfo);
});
});
});
6 changes: 3 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"author": "Javieros",
"scripts": ["main.js"],
"styles": ["./css/token-hud-wildcard.css"],
"url": "https://github.com/mhilbrunner/FVTT-TokenHUDWildcard",
"manifest": "https://raw.githubusercontent.com/mhilbrunner/FVTT-TokenHUDWildcard/master/module.json",
"download": "https://github.com/mhilbrunner/FVTT-TokenHUDWildcard/archive/1.7.0.zip",
"url": "https://github.com/mhilbrunner/token-hud-wildcard",
"manifest": "https://raw.githubusercontent.com/mhilbrunner/token-hud-wildcard/master/module.json",
"download": "https://github.com/mhilbrunner/token-hud-wildcard/archive/1.7.0.zip",
"languages": [
{
"lang": "en",
Expand Down
26 changes: 0 additions & 26 deletions token-hud-wildcard/module.json

This file was deleted.

0 comments on commit 0bce996

Please sign in to comment.