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.

351 lines
15 KiB

1 year ago
1 year ago
  1. import { registerWrappers } from "./patches.js";
  2. import { getWallBounds,getSceneSettings,migrateData,getTokenLOSheight } from "./utils.js";
  3. import { WallHeightToolTip } from './tooltip.js';
  4. import { MODULE_SCOPE, TOP_KEY, BOTTOM_KEY, ENABLE_ADVANCED_VISION_KEY, ENABLE_ADVANCED_MOVEMENT_KEY } from "./const.js";
  5. const MODULE_ID = 'wall-height';
  6. Object.defineProperty(Token.prototype, "losHeight", {
  7. get: function myProperty() {
  8. return getTokenLOSheight(this);
  9. },
  10. });
  11. Hooks.once("init",()=>{
  12. registerWrappers();
  13. registerSettings();
  14. if(game.settings.get(MODULE_ID,'enableTooltip')){
  15. Hooks.on("renderHeadsUpDisplay", (app, html, data) => {
  16. canvas.hud.wallHeight?.close();
  17. html.find("#wall-height-tooltip").remove();
  18. html.append('<template id="wall-height-tooltip"></template>');
  19. canvas.hud.wallHeight = new WallHeightToolTip();
  20. });
  21. }
  22. WallHeight.cacheSettings();
  23. });
  24. Hooks.once("ready", ()=>{
  25. if(!game.user.isGM) return;
  26. if(game.settings.get(MODULE_ID, 'migrateOnStartup')) WallHeight.migrateAll();
  27. if(game.settings.get(MODULE_ID, 'migrateTokenHeight')) {
  28. WallHeight.migrateTokenHeight();
  29. game.settings.set(MODULE_ID, 'migrateTokenHeight',false)
  30. }
  31. })
  32. Hooks.on("hoverWall",(wall, hovered)=>{
  33. if (!canvas.hud?.wallHeight || canvas.walls._chain) return;
  34. const {advancedVision} = getSceneSettings(canvas.scene);
  35. if(advancedVision!=null && !advancedVision)
  36. return;
  37. if (hovered) {
  38. canvas.hud.wallHeight.bind(wall);
  39. } else {
  40. canvas.hud.wallHeight.clear();
  41. }
  42. });
  43. Hooks.on("renderSceneControls", () => {
  44. if (canvas.hud?.wallHeight) canvas.hud.wallHeight.clear();
  45. });
  46. Hooks.on("deleteWall", () => {
  47. if (canvas.hud?.wallHeight) canvas.hud.wallHeight.clear();
  48. });
  49. Hooks.on("createWall", () => {
  50. if (canvas.hud?.wallHeight) canvas.hud.wallHeight.clear();
  51. });
  52. Hooks.on("updateWall", () => {
  53. if (canvas.hud?.wallHeight) canvas.hud.wallHeight.clear();
  54. });
  55. function registerSettings() {
  56. game.settings.register(MODULE_ID, 'enableTooltip', {
  57. name: game.i18n.localize(`${MODULE_SCOPE}.settings.enableTooltip.name`),
  58. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.enableTooltip.hint`),
  59. scope: 'world',
  60. config: true,
  61. type: Boolean,
  62. default: false
  63. });
  64. game.settings.register(MODULE_ID, 'enableWallText', {
  65. name: game.i18n.localize(`${MODULE_SCOPE}.settings.enableWallText.name`),
  66. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.enableWallText.hint`),
  67. scope: 'world',
  68. config: true,
  69. type: Boolean,
  70. default: true,
  71. onChange: () => {
  72. WallHeight.cacheSettings();
  73. },
  74. });
  75. game.settings.register(MODULE_ID, "blockSightMovement", {
  76. name: game.i18n.localize(`${MODULE_SCOPE}.settings.blockSightMovement.name`),
  77. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.blockSightMovement.hint`),
  78. scope: "world",
  79. config: true,
  80. type: Boolean,
  81. default: true,
  82. onChange: () => {
  83. WallHeight.cacheSettings();
  84. },
  85. });
  86. game.settings.register(MODULE_ID, "autoLOSHeight", {
  87. name: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.name`),
  88. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.autoLOSHeight.hint`),
  89. scope: "world",
  90. config: true,
  91. type: Boolean,
  92. default: true,
  93. onChange: () => {
  94. WallHeight.cacheSettings();
  95. },
  96. });
  97. game.settings.register(MODULE_ID, "defaultLosHeight", {
  98. name: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.name`),
  99. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.defaultLosHeight.hint`),
  100. scope: "world",
  101. config: true,
  102. type: Number,
  103. default: 6,
  104. onChange: () => {
  105. WallHeight.cacheSettings();
  106. },
  107. });
  108. game.settings.register(MODULE_ID, 'globalAdvancedLighting', {
  109. name: game.i18n.localize(`${MODULE_SCOPE}.settings.globalAdvancedLighting.name`),
  110. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.globalAdvancedLighting.hint`),
  111. scope: 'world',
  112. config: true,
  113. type: Boolean,
  114. default: true,
  115. });
  116. game.settings.register(MODULE_ID, 'migrateOnStartup', {
  117. name: game.i18n.localize(`${MODULE_SCOPE}.settings.migrateOnStartup.name`),
  118. hint: game.i18n.localize(`${MODULE_SCOPE}.settings.migrateOnStartup.hint`),
  119. scope: 'world',
  120. config: true,
  121. type: Boolean,
  122. default: false
  123. });
  124. game.settings.register(MODULE_ID, 'migrateTokenHeight', {
  125. scope: 'world',
  126. config: false,
  127. type: Boolean,
  128. default: false
  129. });
  130. }
  131. Hooks.on("renderWallConfig", (app, html, data) => {
  132. const {advancedVision} = getSceneSettings(canvas.scene);
  133. if(!advancedVision) return;
  134. let { top, bottom } = getWallBounds(app.object);
  135. top = parseFloat(top);
  136. bottom = parseFloat(bottom);
  137. const topLabel = game.i18n.localize(`${MODULE_SCOPE}.WallHeightTopLabel`);
  138. const bottomLabel = game.i18n.localize(`${MODULE_SCOPE}.WallHeightBottomLabel`);
  139. const moduleLabel = game.i18n.localize(`${MODULE_SCOPE}.ModuleLabel`);
  140. html.find(`.door-options`).after(`
  141. <fieldset>
  142. <legend>${moduleLabel}</legend>
  143. <div class="form-group">
  144. <label>${topLabel}</label>
  145. <input name="flags.${MODULE_SCOPE}.${TOP_KEY}" type="number" step="any" value="${Number.isFinite(top) ? top : ""}" placeholder="Infinity">
  146. </div>
  147. <div class="form-group">
  148. <label>${bottomLabel}</label>
  149. <input name="flags.${MODULE_SCOPE}.${BOTTOM_KEY}" type="number" step="any" value="${Number.isFinite(bottom) ? bottom : ""}" placeholder="-Infinity">
  150. </div>
  151. </legend>
  152. </fieldset>
  153. `);
  154. app.setPosition({ height: "auto" });
  155. });
  156. Hooks.on("renderAmbientLightConfig", (app, html, data) => {
  157. const {advancedVision} = getSceneSettings(canvas.scene);
  158. if(!advancedVision) return;
  159. const label = game.i18n.localize(`${MODULE_SCOPE}.advancedLightingLabel`);
  160. const notes = game.i18n.localize(`${MODULE_SCOPE}.advancedLightingNotes`);
  161. const rangeTop = game.i18n.localize(`${MODULE_SCOPE}.levelsRangeTop`);
  162. const rangeBottom = game.i18n.localize(`${MODULE_SCOPE}.levelsRangeBottom`);
  163. const distance = (app.object.parent?.grid.units ?? game.system.grid.units) || game.i18n.localize(`${MODULE_SCOPE}.distance`);
  164. const checked = app.object.getFlag(MODULE_SCOPE, "advancedLighting") ? "checked" : "";
  165. const globalAdvancedLighting = game.settings.get(MODULE_ID, 'globalAdvancedLighting');
  166. const warnEnabledGlobally = `<p class="hint" style="color: red;">${game.i18n.localize(`${MODULE_SCOPE}.ALGlobal`)}</p>`;
  167. const hint = globalAdvancedLighting ? warnEnabledGlobally : ""
  168. const _injectHTML = `<div class="form-group">
  169. <label>${label}</label>
  170. <input type="checkbox" name="flags.${MODULE_SCOPE}.advancedLighting" ${checked} ${globalAdvancedLighting ? "disabled" : ""}>
  171. ${hint}
  172. <p class="hint">${notes}</p>
  173. </div>`
  174. html.find(`input[name="walls"]`).closest(".form-group").after(_injectHTML);
  175. app.setPosition({ height: "auto" });
  176. if(WallHeight.isLevels) return
  177. const bottom = app.object.flags?.levels?.rangeBottom;
  178. const top = app.object.flags?.levels?.rangeTop;
  179. const elevationHtml = `
  180. <div class="form-group">
  181. <label>${rangeTop} <span class="units">(${distance})</span></label>
  182. <div class="form-fields">
  183. <input name="flags.levels.rangeTop" type="number" step="any" value="${Number.isFinite(top) ? top : ""}" placeholder="Infinity">
  184. </div>
  185. </div>
  186. <div class="form-group">
  187. <label>${rangeBottom} <span class="units">(${distance})</span></label>
  188. <div class="form-fields">
  189. <input name="flags.levels.rangeBottom" type="number" step="any" value="${Number.isFinite(bottom) ? bottom : ""}" placeholder="-Infinity">
  190. </div>
  191. </div>
  192. `
  193. html.find(`input[name="config.dim"]`).closest(".form-group").after(elevationHtml);
  194. app.setPosition({ height: "auto" });
  195. })
  196. Hooks.on("renderAmbientSoundConfig", (app, html, data) => {
  197. const {advancedVision} = getSceneSettings(canvas.scene);
  198. if(!advancedVision) return;
  199. const label = game.i18n.localize(`${MODULE_SCOPE}.advancedLightingLabel`);
  200. const notes = game.i18n.localize(`${MODULE_SCOPE}.advancedLightingNotes`);
  201. const checked = app.object.getFlag(MODULE_SCOPE, "advancedLighting") ? "checked" : "";
  202. const rangeTop = game.i18n.localize(`${MODULE_SCOPE}.levelsRangeTop`);
  203. const rangeBottom = game.i18n.localize(`${MODULE_SCOPE}.levelsRangeBottom`);
  204. const distance = (canvas.scene.grid.units ?? game.system?.grid?.units) || game.i18n.localize(`${MODULE_SCOPE}.distance`);
  205. const globalAdvancedLighting = game.settings.get(MODULE_ID, 'globalAdvancedLighting');
  206. const warnEnabledGlobally = `<p class="hint" style="color: red;">${game.i18n.localize(`${MODULE_SCOPE}.ALGlobal`)}</p>`;
  207. const hint = globalAdvancedLighting ? warnEnabledGlobally : ""
  208. const _injectHTML = `<div class="form-group">
  209. <label>${label}</label>
  210. <input type="checkbox" name="flags.${MODULE_SCOPE}.advancedLighting" ${checked} ${globalAdvancedLighting ? "disabled" : ""}>
  211. ${hint}
  212. <p class="hint">${notes}</p>
  213. </div>`
  214. html.find(`input[name="walls"]`).closest(".form-group").after(_injectHTML);
  215. app.setPosition({ height: "auto" });
  216. if(WallHeight.isLevels) return
  217. const bottom = app.object.flags?.levels?.rangeBottom;
  218. const top = app.object.flags?.levels?.rangeTop;
  219. const elevationHtml = `
  220. <div class="form-group">
  221. <label>${rangeTop} <span class="units">(${distance})</span></label>
  222. <div class="form-fields">
  223. <input name="flags.levels.rangeTop" type="number" step="any" value="${Number.isFinite(top) ? top : ""}" placeholder="Infinity">
  224. </div>
  225. </div>
  226. <div class="form-group">
  227. <label>${rangeBottom} <span class="units">(${distance})</span></label>
  228. <div class="form-fields">
  229. <input name="flags.levels.rangeBottom" type="number" step="any" value="${Number.isFinite(bottom) ? bottom : ""}" placeholder="-Infinity">
  230. </div>
  231. </div>
  232. `
  233. html.find(`input[name="radius"]`).closest(".form-group").after(elevationHtml);
  234. app.setPosition({ height: "auto" });
  235. })
  236. Hooks.on("renderTokenConfig", (app, html, data) => {
  237. const tokenHeight = app.token.getFlag(MODULE_SCOPE, "tokenHeight") || 0;
  238. const label = game.i18n.localize(`${MODULE_SCOPE}.tokenHeightLabel`);
  239. const losHeight = app.object?.object?.losHeight ?? 0;
  240. const height = losHeight - app.token.elevation;
  241. const hint = game.i18n.localize(`${MODULE_SCOPE}.tokenHeightHint`).replace("{{height}}", height).replace("{{losHeight}}", losHeight);
  242. const distance = (canvas.scene.grid.units ?? game.system?.grid?.units) || game.i18n.localize(`${MODULE_SCOPE}.distance`);
  243. let newHtml = `
  244. <div class="form-group slim">
  245. <label>${label} <span class="units">(${distance})</span></label>
  246. <div class="form-fields">
  247. <input type="number" step="any" name="flags.${MODULE_SCOPE}.tokenHeight" placeholder="units" value="${tokenHeight}">
  248. </div>
  249. ${app.object?.object?.losHeight ? `<p class="hint">${hint}</p>` : ""}
  250. </div>
  251. `;
  252. html.find('input[name="lockRotation"]').closest(".form-group").before(newHtml);
  253. app.setPosition({ height: "auto" });
  254. });
  255. Hooks.on("renderSceneConfig", (app, html, data) => {
  256. const {advancedVision} = getSceneSettings(app.object);
  257. const enableVisionKeyLabel = game.i18n.localize(`${MODULE_SCOPE}.AdvancedVisionLabel`);
  258. const moduleLabel = game.i18n.localize(`${MODULE_SCOPE}.ModuleLabel`);
  259. html.find(`input[name="globalLightThreshold"]`).closest(".form-group").after(`
  260. <fieldset>
  261. <legend>${moduleLabel}</legend>
  262. <div class="form-group">
  263. <li class="flexrow">
  264. <label>${enableVisionKeyLabel}</label>
  265. <input name="flags.${MODULE_SCOPE}.${ENABLE_ADVANCED_VISION_KEY}" type="checkbox" data-dtype="Boolean" `+ ((advancedVision || advancedVision==null)?`checked`:``)+`>
  266. </li>
  267. </div>
  268. </fieldset>`
  269. );
  270. app.setPosition({ height: "auto" });
  271. });
  272. Handlebars.registerHelper('if_null', function(a, opts) {
  273. if (a == null) {
  274. return opts.fn(this);
  275. } else {
  276. return opts.inverse(this);
  277. }
  278. });
  279. // First time message
  280. Hooks.once("ready", () => {
  281. if(game.modules.get("levels-3d-preview")?.active) return;
  282. // Module title
  283. const MODULE_TITLE = game.modules.get(MODULE_ID).title;
  284. const FALLBACK_MESSAGE_TITLE = "Welcome to Wall Height";
  285. const FALLBACK_MESSAGE = `<large>
  286. <p><strong>I (theripper93) am now taking over the development of Wall Height, be sure to stop by my <a href="https://theripper93.com/">Discord</a> for help and support from the wonderful community as well as many resources</strong></p>
  287. <p>Thanks to all the patreons supporting the development of my modules making continued updates possible!</p>
  288. <p>If you want to support the development of the module you can do so here : <a href="https://www.patreon.com/theripper93">Patreon</a> </p></large>
  289. <p><strong>Patreons</strong> get also access to <strong>15+ premium modules</strong></p>
  290. <p>Want even more verticality? Go Full 3D</p>
  291. <h1>3D Canvas</h1>
  292. <iframe width="385" height="225" src="https://www.youtube.com/embed/rziXLJEfxqI" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  293. <p>Check 3D Canvas and all my other <strong>15+ premium modules <a href="https://theripper93.com/">Here</a></strong></p>`;
  294. // Settings key used for the "Don't remind me again" setting
  295. const DONT_REMIND_AGAIN_KEY = "popup-dont-remind-again";
  296. // Dialog code
  297. game.settings.register(MODULE_ID, DONT_REMIND_AGAIN_KEY, {
  298. name: "",
  299. default: false,
  300. type: Boolean,
  301. scope: "world",
  302. config: false,
  303. });
  304. if (game.user.isGM && !game.settings.get(MODULE_ID, DONT_REMIND_AGAIN_KEY)) {
  305. new Dialog({
  306. title: FALLBACK_MESSAGE_TITLE,
  307. content: FALLBACK_MESSAGE,
  308. buttons: {
  309. dont_remind: {
  310. icon: '<i class="fas fa-times"></i>',
  311. label: "Don't remind me again",
  312. callback: () => game.settings.set(MODULE_ID, DONT_REMIND_AGAIN_KEY, true),
  313. },
  314. },
  315. default: "dont_remind",
  316. }).render(true);
  317. }
  318. });