From e48b045a9da003c53c7f2380df6d28f3bee02b30 Mon Sep 17 00:00:00 2001 From: Johannes Loher Date: Tue, 29 Jun 2021 20:40:54 +0200 Subject: [PATCH] Do some more cleanup --- src/lang/en.json | 14 ++--- .../darkness-dependent-vision-config.js | 4 +- src/module/hooks.js | 56 +++---------------- .../hooks/getTokenConfigHeaderButtons.js | 26 +++++++++ src/module/hooks/init.js | 34 +++++++++++ src/module/hooks/updateScene.js | 15 +++++ src/module/hooks/updateToken.js | 21 +++++++ src/module/logger.js | 2 +- src/module/wrappers/token-config.js | 30 ---------- .../darkness-dependent-vision-config.hbs | 8 +-- 10 files changed, 118 insertions(+), 92 deletions(-) create mode 100644 src/module/hooks/getTokenConfigHeaderButtons.js create mode 100644 src/module/hooks/init.js create mode 100644 src/module/hooks/updateScene.js create mode 100644 src/module/hooks/updateToken.js delete mode 100644 src/module/wrappers/token-config.js diff --git a/src/lang/en.json b/src/lang/en.json index ca2e74d..e149dce 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -1,9 +1,9 @@ { - "DDV.WarningLackingPermissionToConfigure": "You do not have permission to configure this Token!", - "DDV.Title": "Darkness Dependent Vision Configuration", - "DDV.TokenConfigHeaderButtonLabel": "DDV", - "DDV.DimVisionDarknessRange": "Dim Vision Darkness Range", - "DDV.DimVisionDarknessRangeHint": "You may specify a range of darkness levels during which this token has dim vision.", - "DDV.BrightVisionDarknessRange": "Bright Vision Darkness Range", - "DDV.BrightVisionDarknessRangeHint": "You may specify a range of darkness levels during which this token has bright vision." + "DarknessDependentVision.WarningLackingPermissionToConfigure": "You do not have permission to configure this Token!", + "DarknessDependentVision.Title": "Darkness Dependent Vision Configuration", + "DarknessDependentVision.TokenConfigHeaderButtonLabel": "DDV", + "DarknessDependentVision.DimVisionDarknessRange": "Dim Vision Darkness Range", + "DarknessDependentVision.DimVisionDarknessRangeHint": "You may specify a range of darkness levels during which this token has dim vision.", + "DarknessDependentVision.BrightVisionDarknessRange": "Bright Vision Darkness Range", + "DarknessDependentVision.BrightVisionDarknessRangeHint": "You may specify a range of darkness levels during which this token has bright vision." } diff --git a/src/module/darkness-dependent-vision-config.js b/src/module/darkness-dependent-vision-config.js index 5770690..e770fd2 100644 --- a/src/module/darkness-dependent-vision-config.js +++ b/src/module/darkness-dependent-vision-config.js @@ -51,7 +51,7 @@ export class DarknessDependentVisionConfig extends FormApplication { const name = this.isPrototype ? `[${game.i18n.localize('TOKEN.TitlePrototype')}] ${this.actor.name}` : this.token.name; - return `${name}: ${game.i18n.localize('DDV.Title')}`; + return `${name}: ${game.i18n.localize('DarknessDependentVision.Title')}`; } async getData() { @@ -64,7 +64,7 @@ export class DarknessDependentVisionConfig extends FormApplication { async render(force, options) { const canConfigure = game.user.isGM || this.actor?.isOwner; if (!game.user.can('TOKEN_CONFIGURE') || !canConfigure) { - ui.notifications?.warn(game.i18n.localize('DDV.WarningLackingPermissionToConfigure')); + ui.notifications?.warn(game.i18n.localize('DarknessDependentVision.WarningLackingPermissionToConfigure')); return this; } return super.render(force, options); diff --git a/src/module/hooks.js b/src/module/hooks.js index 296d166..752ec8d 100644 --- a/src/module/hooks.js +++ b/src/module/hooks.js @@ -4,54 +4,14 @@ 'use strict'; -import { packageName } from './config'; -import logger from './logger'; -import { getBrightRadius, getDimRadius, updateSource } from './wrappers/token'; -import { getHeaderButtons } from './wrappers/token-config'; -import { libWrapper } from './shims/libWrapperShim'; +import registerForGetTokenConfigHeaderButtonsHook from './hooks/getTokenConfigHeaderButtons'; +import registerForInitHook from './hooks/init'; +import registerForUpdateSceneHook from './hooks/updateScene'; +import registerForUpdateTokenHook from './hooks/updateToken'; export default function registerForHooks() { - Hooks.once('init', onInit); - Hooks.on('updateScene', onUpdateScene); - Hooks.on('updateToken', onUpdateToken); -} - -function onInit() { - logger.info(`Initializing ${packageName}`); - - const dimRadiusTarget = 'CONFIG.Token.objectClass.prototype.dimRadius'; - try { - libWrapper.register(packageName, dimRadiusTarget, getDimRadius, 'OVERRIDE'); - } catch (e) { - logger.warn(`Failed to override ${dimRadiusTarget}, some things might not work correctly:`, e); - } - - const brightRadiusTarget = 'CONFIG.Token.objectClass.prototype.dimRadius'; - try { - libWrapper.register(packageName, brightRadiusTarget, getBrightRadius, 'OVERRIDE'); - } catch (e) { - logger.warn(`Failed to override ${brightRadiusTarget}, some things might not work correctly:`, e); - } - - libWrapper.register(packageName, 'CONFIG.Token.objectClass.prototype.updateSource', updateSource, 'WRAPPER'); - - libWrapper.register(packageName, 'CONFIG.Token.sheetClass.prototype._getHeaderButtons', getHeaderButtons, 'WRAPPER'); -} - -function onUpdateScene(scene, change) { - if (change.darkness != null) { - scene.getEmbeddedCollection('Token').forEach((tokenDocument) => tokenDocument.object.updateSource()); - } -} - -function onUpdateToken(token, change) { - const shouldUpdateSource = [ - 'dimVisionDarknessMin', - 'dimVisionDarknessMax', - 'brightVisionDarknessMin', - 'brightVisionDarknessMax', - ].some((flagKey) => `flags.darkness-dependent-vision.${flagKey}` in foundry.utils.flattenObject(change)); - if (shouldUpdateSource) { - token.object.updateSource(); - } + registerForInitHook(); + registerForUpdateSceneHook(); + registerForUpdateTokenHook(); + registerForGetTokenConfigHeaderButtonsHook(); } diff --git a/src/module/hooks/getTokenConfigHeaderButtons.js b/src/module/hooks/getTokenConfigHeaderButtons.js new file mode 100644 index 0000000..7d36ef3 --- /dev/null +++ b/src/module/hooks/getTokenConfigHeaderButtons.js @@ -0,0 +1,26 @@ +// SPDX-FileCopyrightText: 2021 Johannes Loher +// +// SPDX-License-Identifier: MIT + +'use strict'; + +import { DarknessDependentVisionConfig } from '../darkness-dependent-vision-config'; + +export default function registerForGetTokenConfigHeaderButtonsHook() { + Hooks.on('getTokenConfigHeaderButtons', onGetTokenConfigHeaderButtons); +} + +/** + * @param {TokenConfig} tokenConfig + * @param {ApplicationHeaderButton[]} buttons + */ +function onGetTokenConfigHeaderButtons(tokenConfig, buttons) { + buttons.unshift({ + label: 'DarknessDependentVision.TokenConfigHeaderButtonLabel', + class: 'configure-darkness-dependent-vision', + icon: 'fas fa-eye', + onclick: async () => { + return new DarknessDependentVisionConfig(tokenConfig.object).render(true); + }, + }); +} diff --git a/src/module/hooks/init.js b/src/module/hooks/init.js new file mode 100644 index 0000000..58ed296 --- /dev/null +++ b/src/module/hooks/init.js @@ -0,0 +1,34 @@ +// SPDX-FileCopyrightText: 2021 Johannes Loher +// +// SPDX-License-Identifier: MIT + +'use strict'; + +import { packageName } from '../config'; +import logger from '../logger'; +import { libWrapper } from '../shims/libWrapperShim'; +import { getBrightRadius, getDimRadius, updateSource } from '../wrappers/token'; + +export default function registerForInitHook() { + Hooks.on('init', onInit); +} + +function onInit() { + logger.info(`Initializing ${packageName}`); + + const dimRadiusTarget = 'Token.prototype.dimRadius'; + try { + libWrapper.register(packageName, dimRadiusTarget, getDimRadius, 'OVERRIDE'); + } catch (e) { + logger.warn(`Failed to override ${dimRadiusTarget}, some things might not work correctly:`, e); + } + + const brightRadiusTarget = 'Token.prototype.dimRadius'; + try { + libWrapper.register(packageName, brightRadiusTarget, getBrightRadius, 'OVERRIDE'); + } catch (e) { + logger.warn(`Failed to override ${brightRadiusTarget}, some things might not work correctly:`, e); + } + + libWrapper.register(packageName, 'Token.prototype.updateSource', updateSource, 'WRAPPER'); +} diff --git a/src/module/hooks/updateScene.js b/src/module/hooks/updateScene.js new file mode 100644 index 0000000..d14b427 --- /dev/null +++ b/src/module/hooks/updateScene.js @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2021 Johannes Loher +// +// SPDX-License-Identifier: MIT + +'use strict'; + +export default function registerForUpdateSceneHook() { + Hooks.on('updateScene', onUpdateScene); +} + +function onUpdateScene(scene, change) { + if (change.darkness != null) { + scene.getEmbeddedCollection('Token').forEach((tokenDocument) => tokenDocument.object.updateSource()); + } +} diff --git a/src/module/hooks/updateToken.js b/src/module/hooks/updateToken.js new file mode 100644 index 0000000..0c4154c --- /dev/null +++ b/src/module/hooks/updateToken.js @@ -0,0 +1,21 @@ +// SPDX-FileCopyrightText: 2021 Johannes Loher +// +// SPDX-License-Identifier: MIT + +'use strict'; + +export default function registerForUpdateTokenHook() { + Hooks.on('updateToken', onUpdateToken); +} + +function onUpdateToken(token, change) { + const shouldUpdateSource = [ + 'dimVisionDarknessMin', + 'dimVisionDarknessMax', + 'brightVisionDarknessMin', + 'brightVisionDarknessMax', + ].some((flagKey) => `flags.darkness-dependent-vision.${flagKey}` in foundry.utils.flattenObject(change)); + if (shouldUpdateSource) { + token.object.updateSource(); + } +} diff --git a/src/module/logger.js b/src/module/logger.js index 71da521..738c5d8 100644 --- a/src/module/logger.js +++ b/src/module/logger.js @@ -4,7 +4,7 @@ 'use strict'; -const loggingContext = 'DDV'; +const loggingContext = 'Darkness Dependent Vision'; const loggingSeparator = '|'; /** diff --git a/src/module/wrappers/token-config.js b/src/module/wrappers/token-config.js deleted file mode 100644 index 661d2e5..0000000 --- a/src/module/wrappers/token-config.js +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2021 Johannes Loher -// -// SPDX-License-Identifier: MIT - -'use strict'; - -import { DarknessDependentVisionConfig } from '../darkness-dependent-vision-config'; - -/** - * Specify the set of config buttons which should appear in the Application header. - * Buttons should be returned as an Array of objects. - * The header buttons which are added to the application can be modified by the getApplicationHeaderButtons hook. - * @typedef {{label: string, class: string, icon: string, onclick: Function|null}} ApplicationHeaderButton - * @param {() => ApplicationHeaderButton[]} wrapped The function that is wrapped by this function and needs to be called next in the chain - * @fires Application#hook:getApplicationHeaderButtons - * @returns {ApplicationHeaderButton[]} - */ -export function getHeaderButtons(wrapped) { - const buttons = wrapped(); - const button = { - label: 'DDV.TokenConfigHeaderButtonLabel', - class: 'configure-darkness-dependent-vision', - icon: 'fas fa-eye', - onclick: async () => { - return new DarknessDependentVisionConfig(this.object).render(true); - }, - }; - - return [button, ...buttons]; -} diff --git a/src/templates/darkness-dependent-vision-config.hbs b/src/templates/darkness-dependent-vision-config.hbs index baee810..8546ae2 100644 --- a/src/templates/darkness-dependent-vision-config.hbs +++ b/src/templates/darkness-dependent-vision-config.hbs @@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT
- +
-

{{ localize "DDV.DimVisionDarknessRangeHint" }}

+

{{ localize "DarknessDependentVision.DimVisionDarknessRangeHint" }}

- +
-

{{ localize "DDV.BrightVisionDarknessRangeHint" }}

+

{{ localize "DarknessDependentVision.BrightVisionDarknessRangeHint" }}