All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.0 KiB

  1. import { insertArtSelectButton } from '../../applications/artSelect.js';
  2. import { FEATURE_CONTROL, TVA_CONFIG } from '../settings.js';
  3. import { SEARCH_TYPE, updateTokenImage } from '../utils.js';
  4. import { registerHook, unregisterHook } from './hooks.js';
  5. const feature_id = 'Wildcards';
  6. export function registerWildcardHooks() {
  7. if (!FEATURE_CONTROL[feature_id]) {
  8. ['renderTokenConfig', 'preCreateToken'].forEach((name) => unregisterHook(feature_id, name));
  9. return;
  10. }
  11. // Insert default random image field
  12. registerHook(feature_id, 'renderTokenConfig', _renderTokenConfig);
  13. // Set Default Wildcard images if needed
  14. registerHook(feature_id, 'preCreateToken', _preCreateToken);
  15. }
  16. async function _renderTokenConfig(config, html) {
  17. const checkboxRandomize = html.find('input[name="randomImg"]');
  18. if (checkboxRandomize.length && !html.find('.token-variants-proto').length) {
  19. const defaultImg =
  20. config.actor?.prototypeToken?.flags['token-variants']?.['randomImgDefault'] ||
  21. config.actor?.prototypeToken?.flags['token-hud-wildcard']?.['default'] ||
  22. '';
  23. const field = await renderTemplate('/modules/token-variants/templates/protoTokenElement.html', {
  24. defaultImg,
  25. disableHUDButton: config.object?.getFlag('token-variants', 'disableHUDButton'),
  26. });
  27. checkboxRandomize.closest('.form-group').after(field);
  28. const tvaFieldset = html.find('.token-variants-proto');
  29. tvaFieldset.find('button').click((event) => {
  30. event.preventDefault();
  31. const input = tvaFieldset.find('input');
  32. new FilePicker({ current: input.val(), field: input[0] }).browse(defaultImg);
  33. });
  34. insertArtSelectButton(tvaFieldset, 'flags.token-variants.randomImgDefault', {
  35. search: config.object.name,
  36. searchType: SEARCH_TYPE.TOKEN,
  37. });
  38. // Hide/Show Default Img Form Group
  39. const rdmImgFormGroup = tvaFieldset.find('.imagevideo').closest('.form-group');
  40. const showHideGroup = function (checked) {
  41. if (checked) {
  42. rdmImgFormGroup.show();
  43. } else {
  44. rdmImgFormGroup.hide();
  45. }
  46. config.setPosition();
  47. };
  48. checkboxRandomize.on('click', (event) => showHideGroup(event.target.checked));
  49. showHideGroup(checkboxRandomize.is(':checked'));
  50. }
  51. }
  52. function _preCreateToken(tokenDocument, data, options, userId) {
  53. if (game.user.id !== userId) return;
  54. const update = {};
  55. if (tokenDocument.actor?.prototypeToken?.randomImg) {
  56. const defaultImg =
  57. tokenDocument.actor?.prototypeToken?.flags['token-variants']?.['randomImgDefault'] ||
  58. tokenDocument.actor?.prototypeToken?.flags['token-hud-wildcard']?.['default'] ||
  59. '';
  60. if (defaultImg) update['texture.src'] = defaultImg;
  61. }
  62. if (TVA_CONFIG.imgNameContainsDimensions || TVA_CONFIG.imgNameContainsFADimensions) {
  63. updateTokenImage(update['texture.src'] ?? tokenDocument.texture.src, {
  64. token: tokenDocument,
  65. update,
  66. });
  67. }
  68. if (!isEmpty(update)) tokenDocument.updateSource(update);
  69. }