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.

272 lines
8.7 KiB

  1. import { showArtSelect } from '../../token-variants.mjs';
  2. import { doRandomSearch, doSyncSearch } from '../search.js';
  3. import { FEATURE_CONTROL, TVA_CONFIG } from '../settings.js';
  4. import { keyPressed, nameForgeRandomize, SEARCH_TYPE, updateActorImage, updateTokenImage } from '../utils.js';
  5. import { registerHook, unregisterHook } from './hooks.js';
  6. const feature_id = 'PopUpAndRandomize';
  7. export function registerPopRandomizeHooks() {
  8. if (FEATURE_CONTROL[feature_id]) {
  9. registerHook(feature_id, 'createActor', _createActor);
  10. registerHook(feature_id, 'createToken', _createToken);
  11. } else {
  12. ['createActor', 'createToken'].forEach((name) => unregisterHook(feature_id, name));
  13. }
  14. }
  15. async function _createToken(token, options, userId) {
  16. if (userId && game.user.id != userId) return;
  17. // Check if random search is enabled and if so perform it
  18. const actorRandSettings = game.actors.get(token.actorId)?.getFlag('token-variants', 'randomizerSettings');
  19. const randSettings = mergeObject(TVA_CONFIG.randomizer, actorRandSettings ?? {}, {
  20. inplace: false,
  21. recursive: false,
  22. });
  23. let vDown = keyPressed('v');
  24. const flagTarget = token.actor ? game.actors.get(token.actor.id) : token.document ?? token;
  25. const popupFlag = flagTarget.getFlag('token-variants', 'popups');
  26. if ((vDown && randSettings.tokenCopyPaste) || (!vDown && randSettings.tokenCreate)) {
  27. let performRandomSearch = true;
  28. if (!actorRandSettings) {
  29. if (randSettings.representedActorDisable && token.actor) performRandomSearch = false;
  30. if (randSettings.linkedActorDisable && token.actorLink) performRandomSearch = false;
  31. if (_disableRandomSearchForType(randSettings, token.actor)) performRandomSearch = false;
  32. } else {
  33. performRandomSearch = Boolean(actorRandSettings);
  34. }
  35. if (performRandomSearch) {
  36. // Randomize Token Name if need be
  37. const randomName = await nameForgeRandomize(randSettings);
  38. if (randomName) {
  39. token.update({ name: randomName });
  40. }
  41. const img = await doRandomSearch(token.name, {
  42. searchType: SEARCH_TYPE.TOKEN,
  43. actor: token.actor,
  44. randomizerOptions: randSettings,
  45. });
  46. if (img) {
  47. await updateTokenImage(img[0], {
  48. token: token,
  49. actor: token.actor,
  50. imgName: img[1],
  51. });
  52. }
  53. if (!img) return;
  54. if (randSettings.diffImages) {
  55. let imgPortrait;
  56. if (randSettings.syncImages) {
  57. imgPortrait = await doSyncSearch(token.name, img[1], {
  58. actor: token.actor,
  59. searchType: SEARCH_TYPE.PORTRAIT,
  60. randomizerOptions: randSettings,
  61. });
  62. } else {
  63. imgPortrait = await doRandomSearch(token.name, {
  64. searchType: SEARCH_TYPE.PORTRAIT,
  65. actor: token.actor,
  66. randomizerOptions: randSettings,
  67. });
  68. }
  69. if (imgPortrait) {
  70. await updateActorImage(token.actor, imgPortrait[0]);
  71. }
  72. } else if (randSettings.tokenToPortrait) {
  73. await updateActorImage(token.actor, img[0]);
  74. }
  75. return;
  76. }
  77. if (popupFlag == null && !randSettings.popupOnDisable) {
  78. return;
  79. }
  80. } else if (randSettings.tokenCreate || randSettings.tokenCopyPaste) {
  81. return;
  82. }
  83. // Check if pop-up is enabled and if so open it
  84. if (!TVA_CONFIG.permissions.popups[game.user.role]) {
  85. return;
  86. }
  87. let dirKeyDown = keyPressed('popupOverride');
  88. if (vDown && TVA_CONFIG.popup.disableAutoPopupOnTokenCopyPaste) {
  89. return;
  90. }
  91. if (!dirKeyDown || (dirKeyDown && vDown)) {
  92. if (TVA_CONFIG.popup.disableAutoPopupOnTokenCreate && !vDown) {
  93. return;
  94. } else if (popupFlag == null && _disablePopupForType(token.actor)) {
  95. return;
  96. } else if (popupFlag != null && !popupFlag) {
  97. return;
  98. }
  99. }
  100. showArtSelect(token.name, {
  101. callback: async function (imgSrc, imgName) {
  102. if (TVA_CONFIG.popup.twoPopups) {
  103. await updateActorImage(token.actor, imgSrc);
  104. _twoPopupPrompt(token.actor, imgSrc, imgName, token);
  105. } else {
  106. updateTokenImage(imgSrc, {
  107. actor: token.actor,
  108. imgName: imgName,
  109. token: token,
  110. });
  111. }
  112. },
  113. searchType: TVA_CONFIG.popup.twoPopups ? SEARCH_TYPE.PORTRAIT : SEARCH_TYPE.TOKEN,
  114. object: token,
  115. preventClose: TVA_CONFIG.popup.twoPopups && TVA_CONFIG.popup.twoPopupsNoDialog,
  116. });
  117. }
  118. async function _createActor(actor, options, userId) {
  119. if (userId && game.user.id != userId) return;
  120. // Check if random search is enabled and if so perform it
  121. const randSettings = TVA_CONFIG.randomizer;
  122. if (randSettings.actorCreate) {
  123. let performRandomSearch = true;
  124. if (randSettings.linkedActorDisable && actor.prototypeToken.actorLink) performRandomSearch = false;
  125. if (_disableRandomSearchForType(randSettings, actor)) performRandomSearch = false;
  126. if (performRandomSearch) {
  127. const img = await doRandomSearch(actor.name, {
  128. searchType: SEARCH_TYPE.PORTRAIT,
  129. actor: actor,
  130. });
  131. if (img) {
  132. await updateActorImage(actor, img[0]);
  133. }
  134. if (!img) return;
  135. if (randSettings.diffImages) {
  136. let imgToken;
  137. if (randSettings.syncImages) {
  138. imgToken = await doSyncSearch(actor.name, img[1], { actor: actor });
  139. } else {
  140. imgToken = await doRandomSearch(actor.name, {
  141. searchType: SEARCH_TYPE.TOKEN,
  142. actor: actor,
  143. });
  144. }
  145. if (imgToken) {
  146. await updateTokenImage(imgToken[0], { actor: actor, imgName: imgToken[1] });
  147. }
  148. }
  149. return;
  150. }
  151. if (!randSettings.popupOnDisable) {
  152. return;
  153. }
  154. }
  155. // Check if pop-up is enabled and if so open it
  156. if (!TVA_CONFIG.permissions.popups[game.user.role]) {
  157. return;
  158. }
  159. if (TVA_CONFIG.popup.disableAutoPopupOnActorCreate && !keyPressed('popupOverride')) {
  160. return;
  161. } else if (_disablePopupForType(actor)) {
  162. return;
  163. }
  164. showArtSelect(actor.name, {
  165. callback: async function (imgSrc, name) {
  166. const actTokens = actor.getActiveTokens();
  167. const token = actTokens.length === 1 ? actTokens[0] : null;
  168. await updateActorImage(actor, imgSrc);
  169. if (TVA_CONFIG.popup.twoPopups) _twoPopupPrompt(actor, imgSrc, name, token);
  170. else {
  171. updateTokenImage(imgSrc, {
  172. actor: actor,
  173. imgName: name,
  174. token: token,
  175. });
  176. }
  177. },
  178. searchType: TVA_CONFIG.popup.twoPopups ? SEARCH_TYPE.PORTRAIT : SEARCH_TYPE.PORTRAIT_AND_TOKEN,
  179. object: actor,
  180. preventClose: TVA_CONFIG.popup.twoPopups && TVA_CONFIG.popup.twoPopupsNoDialog,
  181. });
  182. }
  183. function _disableRandomSearchForType(randSettings, actor) {
  184. if (!actor) return false;
  185. return randSettings[`${actor.type}Disable`] ?? false;
  186. }
  187. function _disablePopupForType(actor) {
  188. if (!actor) return false;
  189. return TVA_CONFIG.popup[`${actor.type}Disable`] ?? false;
  190. }
  191. function _twoPopupPrompt(actor, imgSrc, imgName, token) {
  192. if (TVA_CONFIG.popup.twoPopups && TVA_CONFIG.popup.twoPopupsNoDialog) {
  193. showArtSelect((token ?? actor.prototypeToken).name, {
  194. callback: (imgSrc, name) =>
  195. updateTokenImage(imgSrc, {
  196. actor: actor,
  197. imgName: name,
  198. token: token,
  199. }),
  200. searchType: SEARCH_TYPE.TOKEN,
  201. object: token ? token : actor,
  202. force: true,
  203. });
  204. } else if (TVA_CONFIG.popup.twoPopups) {
  205. let d = new Dialog({
  206. title: 'Portrait -> Token',
  207. content: `<p>${game.i18n.localize('token-variants.windows.art-select.apply-same-art')}</p>`,
  208. buttons: {
  209. one: {
  210. icon: '<i class="fas fa-check"></i>',
  211. callback: () => {
  212. updateTokenImage(imgSrc, {
  213. actor: actor,
  214. imgName: imgName,
  215. token: token,
  216. });
  217. const artSelects = Object.values(ui.windows).filter((app) => app instanceof ArtSelect);
  218. for (const app of artSelects) {
  219. app.close();
  220. }
  221. },
  222. },
  223. two: {
  224. icon: '<i class="fas fa-times"></i>',
  225. callback: () => {
  226. showArtSelect((token ?? actor.prototypeToken).name, {
  227. callback: (imgSrc, name) =>
  228. updateTokenImage(imgSrc, {
  229. actor: actor,
  230. imgName: name,
  231. token: token,
  232. }),
  233. searchType: SEARCH_TYPE.TOKEN,
  234. object: token ? token : actor,
  235. force: true,
  236. });
  237. },
  238. },
  239. },
  240. default: 'one',
  241. });
  242. d.render(true);
  243. }
  244. }