Do some more cleanup

This commit is contained in:
Johannes Loher 2021-06-29 20:40:54 +02:00
parent 9f39c0856b
commit e48b045a9d
10 changed files with 118 additions and 92 deletions

View file

@ -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."
}

View file

@ -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);

View file

@ -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();
}

View file

@ -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);
},
});
}

34
src/module/hooks/init.js Normal file
View file

@ -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');
}

View file

@ -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());
}
}

View file

@ -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();
}
}

View file

@ -4,7 +4,7 @@
'use strict';
const loggingContext = 'DDV';
const loggingContext = 'Darkness Dependent Vision';
const loggingSeparator = '|';
/**

View file

@ -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];
}

View file

@ -7,7 +7,7 @@ SPDX-License-Identifier: MIT
<form class="{{cssClasses}}" autocomplete="off">
<div class="form-group">
<label>{{ localize 'DDV.DimVisionDarknessRange' }}</label>
<label>{{ localize 'DarknessDependentVision.DimVisionDarknessRange' }}</label>
<div class="form-fields">
<label for="flags.darkness-dependent-vision.dimVisionDarknessMin">{{ localize "Between" }}</label>
<input type="number" name="flags.darkness-dependent-vision.dimVisionDarknessMin"
@ -18,11 +18,11 @@ SPDX-License-Identifier: MIT
value="{{object.flags.darkness-dependent-vision.dimVisionDarknessMax}}" min="0" max="1" step="any"
placeholder="1" />
</div>
<p class="hint">{{ localize "DDV.DimVisionDarknessRangeHint" }}</p>
<p class="hint">{{ localize "DarknessDependentVision.DimVisionDarknessRangeHint" }}</p>
</div>
<div class="form-group">
<label>{{ localize 'DDV.BrightVisionDarknessRange' }}</label>
<label>{{ localize 'DarknessDependentVision.BrightVisionDarknessRange' }}</label>
<div class="form-fields">
<label for="flags.darkness-dependent-vision.brightVisionDarknessMin">{{ localize "Between" }}</label>
<input type="number" name="flags.darkness-dependent-vision.brightVisionDarknessMin"
@ -33,7 +33,7 @@ SPDX-License-Identifier: MIT
value="{{object.flags.darkness-dependent-vision.brightVisionDarknessMax}}" min="0" max="1" step="any"
placeholder="1" />
</div>
<p class="hint">{{ localize "DDV.BrightVisionDarknessRangeHint" }}</p>
<p class="hint">{{ localize "DarknessDependentVision.BrightVisionDarknessRangeHint" }}</p>
</div>
<button type="submit" name="submit" value="1"><i class="far fa-save"></i> {{localize "TOKEN.Update"}}</button>