mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-17 23:49:02 +01:00
Extracted common logic to token-config-mixin.mjs
This commit is contained in:
parent
595fd48efe
commit
a31ec9b778
4 changed files with 128 additions and 212 deletions
|
|
@ -1,109 +1,15 @@
|
||||||
export default class DhPrototypeTokenConfig extends foundry.applications.sheets.PrototypeTokenConfig {
|
import DHTokenConfigMixin from './token-config-mixin.mjs';
|
||||||
|
import { getActorSizeFromForm } from './token-config-mixin.mjs';
|
||||||
|
|
||||||
|
export default class DhPrototypeTokenConfig extends DHTokenConfigMixin(
|
||||||
|
foundry.applications.sheets.PrototypeTokenConfig
|
||||||
|
) {
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
static DEFAULT_OPTIONS = {
|
static DEFAULT_OPTIONS = {
|
||||||
...super.DEFAULT_OPTIONS,
|
...super.DEFAULT_OPTIONS,
|
||||||
form: { handler: DhPrototypeTokenConfig.#onSubmit }
|
form: { handler: DhPrototypeTokenConfig.#onSubmit }
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @override */
|
|
||||||
static PARTS = {
|
|
||||||
tabs: super.PARTS.tabs,
|
|
||||||
identity: super.PARTS.identity,
|
|
||||||
appearance: {
|
|
||||||
template: 'systems/daggerheart/templates/sheets-settings/token-config/appearance.hbs',
|
|
||||||
scrollable: ['']
|
|
||||||
},
|
|
||||||
vision: super.PARTS.vision,
|
|
||||||
light: super.PARTS.light,
|
|
||||||
resources: super.PARTS.resources,
|
|
||||||
footer: super.PARTS.footer
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
async _prepareResourcesTab() {
|
|
||||||
const token = this.token;
|
|
||||||
const usesTrackableAttributes = !foundry.utils.isEmpty(CONFIG.Actor.trackableAttributes);
|
|
||||||
const attributeSource =
|
|
||||||
this.actor?.system instanceof foundry.abstract.DataModel && usesTrackableAttributes
|
|
||||||
? this.actor?.type
|
|
||||||
: this.actor?.system;
|
|
||||||
const TokenDocument = foundry.utils.getDocumentClass('Token');
|
|
||||||
const attributes = TokenDocument.getTrackedAttributes(attributeSource);
|
|
||||||
return {
|
|
||||||
barAttributes: TokenDocument.getTrackedAttributeChoices(attributes, attributeSource),
|
|
||||||
bar1: token.getBarAttribute?.('bar1'),
|
|
||||||
bar2: token.getBarAttribute?.('bar2'),
|
|
||||||
turnMarkerModes: DhPrototypeTokenConfig.TURN_MARKER_MODES,
|
|
||||||
turnMarkerAnimations: CONFIG.Combat.settings.turnMarkerAnimations
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async _prepareAppearanceTab() {
|
|
||||||
const context = await super._prepareAppearanceTab();
|
|
||||||
context.tokenSizes = CONFIG.DH.ACTOR.tokenSize;
|
|
||||||
context.tokenSize = this.token.actor?.system?.size;
|
|
||||||
context.usesActorSize = this.token.actor?.system?.metadata?.usesSize;
|
|
||||||
context.actorSizeDisable = context.usesActorSize && this.token.actor.system.size !== 'custom';
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
_attachPartListeners(partId, htmlElement, options) {
|
|
||||||
super._attachPartListeners(partId, htmlElement, options);
|
|
||||||
|
|
||||||
switch (partId) {
|
|
||||||
case 'appearance':
|
|
||||||
htmlElement
|
|
||||||
.querySelector('#dhTokenSize')
|
|
||||||
?.addEventListener('change', this.onTokenSizeChange.bind(this));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
_previewChanges(changes) {
|
|
||||||
if (!changes || !this._preview) return;
|
|
||||||
|
|
||||||
const tokenSizeSelect = this.element?.querySelector('#dhTokenSize');
|
|
||||||
if (this.token.actor && tokenSizeSelect && tokenSizeSelect.value !== 'custom') {
|
|
||||||
const tokenSizes = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew).tokenSizes;
|
|
||||||
const tokenSize = tokenSizes[tokenSizeSelect.value];
|
|
||||||
changes.width = tokenSize;
|
|
||||||
changes.height = tokenSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletions = { '-=actorId': null, '-=actorLink': null };
|
|
||||||
const mergeOptions = { inplace: false, performDeletions: true };
|
|
||||||
this._preview.updateSource(mergeObject(changes, deletions, mergeOptions));
|
|
||||||
|
|
||||||
if (this._preview?.object?.destroyed === false) {
|
|
||||||
this._preview.object.initializeSources();
|
|
||||||
this._preview.object.renderFlags.set({ refresh: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async onTokenSizeChange(event) {
|
|
||||||
const value = event.target.value;
|
|
||||||
const tokenSizeDimensions = this.element.querySelector('#tokenSizeDimensions');
|
|
||||||
if (tokenSizeDimensions) {
|
|
||||||
const disabled = value !== 'custom';
|
|
||||||
|
|
||||||
tokenSizeDimensions.dataset.tooltip = disabled
|
|
||||||
? game.i18n.localize('DAGGERHEART.APPLICATIONS.TokenConfig.actorSizeUsed')
|
|
||||||
: '';
|
|
||||||
|
|
||||||
const disabledIcon = tokenSizeDimensions.querySelector('i');
|
|
||||||
if (disabledIcon) {
|
|
||||||
disabledIcon.style.opacity = disabled ? '' : '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
const dimensionsInputs = tokenSizeDimensions.querySelectorAll('.form-fields input');
|
|
||||||
for (const input of dimensionsInputs) {
|
|
||||||
input.disabled = disabled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Process form submission for the sheet
|
* Process form submission for the sheet
|
||||||
* @this {PrototypeTokenConfig}
|
* @this {PrototypeTokenConfig}
|
||||||
|
|
@ -115,12 +21,8 @@ export default class DhPrototypeTokenConfig extends foundry.applications.sheets.
|
||||||
this._processChanges(submitData);
|
this._processChanges(submitData);
|
||||||
const changes = { prototypeToken: submitData };
|
const changes = { prototypeToken: submitData };
|
||||||
|
|
||||||
const tokenSizeSelect = this.element.querySelector('#dhTokenSize');
|
const changedTokenSizeValue = getActorSizeFromForm(this.element, this.actor);
|
||||||
if (tokenSizeSelect && this.actor) {
|
if (changedTokenSizeValue) changes.system = { size: changedTokenSizeValue };
|
||||||
if (tokenSizeSelect.value !== this.actor.system.size) {
|
|
||||||
changes.system = { size: tokenSizeSelect.value };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.actor.validate({ changes, clean: true, fallback: false });
|
this.actor.validate({ changes, clean: true, fallback: false });
|
||||||
await this.actor.update(changes);
|
await this.actor.update(changes);
|
||||||
|
|
|
||||||
114
module/applications/sheets-configs/token-config-mixin.mjs
Normal file
114
module/applications/sheets-configs/token-config-mixin.mjs
Normal file
|
|
@ -0,0 +1,114 @@
|
||||||
|
export default function DHTokenConfigMixin(Base) {
|
||||||
|
class DHTokenConfigBase extends Base {
|
||||||
|
/** @override */
|
||||||
|
static PARTS = {
|
||||||
|
tabs: super.PARTS.tabs,
|
||||||
|
identity: super.PARTS.identity,
|
||||||
|
appearance: {
|
||||||
|
template: 'systems/daggerheart/templates/sheets-settings/token-config/appearance.hbs',
|
||||||
|
scrollable: ['']
|
||||||
|
},
|
||||||
|
vision: super.PARTS.vision,
|
||||||
|
light: super.PARTS.light,
|
||||||
|
resources: super.PARTS.resources,
|
||||||
|
footer: super.PARTS.footer
|
||||||
|
};
|
||||||
|
|
||||||
|
_attachPartListeners(partId, htmlElement, options) {
|
||||||
|
super._attachPartListeners(partId, htmlElement, options);
|
||||||
|
|
||||||
|
switch (partId) {
|
||||||
|
case 'appearance':
|
||||||
|
htmlElement
|
||||||
|
.querySelector('#dhTokenSize')
|
||||||
|
?.addEventListener('change', this.onTokenSizeChange.bind(this));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
async _prepareResourcesTab() {
|
||||||
|
const token = this.token;
|
||||||
|
const usesTrackableAttributes = !foundry.utils.isEmpty(CONFIG.Actor.trackableAttributes);
|
||||||
|
const attributeSource =
|
||||||
|
this.actor?.system instanceof foundry.abstract.DataModel && usesTrackableAttributes
|
||||||
|
? this.actor?.type
|
||||||
|
: this.actor?.system;
|
||||||
|
const TokenDocument = foundry.utils.getDocumentClass('Token');
|
||||||
|
const attributes = TokenDocument.getTrackedAttributes(attributeSource);
|
||||||
|
return {
|
||||||
|
barAttributes: TokenDocument.getTrackedAttributeChoices(attributes, attributeSource),
|
||||||
|
bar1: token.getBarAttribute?.('bar1'),
|
||||||
|
bar2: token.getBarAttribute?.('bar2'),
|
||||||
|
turnMarkerModes: DHTokenConfigBase.TURN_MARKER_MODES,
|
||||||
|
turnMarkerAnimations: CONFIG.Combat.settings.turnMarkerAnimations
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async _prepareAppearanceTab() {
|
||||||
|
const context = await super._prepareAppearanceTab();
|
||||||
|
context.tokenSizes = CONFIG.DH.ACTOR.tokenSize;
|
||||||
|
context.tokenSize = this.actor?.system?.size;
|
||||||
|
context.usesActorSize = this.actor?.system?.metadata?.usesSize;
|
||||||
|
context.actorSizeDisable = context.usesActorSize && this.actor.system.size !== 'custom';
|
||||||
|
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
_previewChanges(changes) {
|
||||||
|
if (!changes || !this._preview) return;
|
||||||
|
|
||||||
|
const tokenSizeSelect = this.element?.querySelector('#dhTokenSize');
|
||||||
|
if (this.actor && tokenSizeSelect && tokenSizeSelect.value !== 'custom') {
|
||||||
|
const tokenSizes = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew).tokenSizes;
|
||||||
|
const tokenSize = tokenSizes[tokenSizeSelect.value];
|
||||||
|
changes.width = tokenSize;
|
||||||
|
changes.height = tokenSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
const deletions = { '-=actorId': null, '-=actorLink': null };
|
||||||
|
const mergeOptions = { inplace: false, performDeletions: true };
|
||||||
|
this._preview.updateSource(mergeObject(changes, deletions, mergeOptions));
|
||||||
|
|
||||||
|
if (this._preview?.object?.destroyed === false) {
|
||||||
|
this._preview.object.initializeSources();
|
||||||
|
this._preview.object.renderFlags.set({ refresh: true });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async onTokenSizeChange(event) {
|
||||||
|
const value = event.target.value;
|
||||||
|
const tokenSizeDimensions = this.element.querySelector('#tokenSizeDimensions');
|
||||||
|
if (tokenSizeDimensions) {
|
||||||
|
const disabled = value !== 'custom';
|
||||||
|
|
||||||
|
tokenSizeDimensions.dataset.tooltip = disabled
|
||||||
|
? game.i18n.localize('DAGGERHEART.APPLICATIONS.TokenConfig.actorSizeUsed')
|
||||||
|
: '';
|
||||||
|
|
||||||
|
const disabledIcon = tokenSizeDimensions.querySelector('i');
|
||||||
|
if (disabledIcon) {
|
||||||
|
disabledIcon.style.opacity = disabled ? '' : '0';
|
||||||
|
}
|
||||||
|
|
||||||
|
const dimensionsInputs = tokenSizeDimensions.querySelectorAll('.form-fields input');
|
||||||
|
for (const input of dimensionsInputs) {
|
||||||
|
input.disabled = disabled;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return DHTokenConfigBase;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getActorSizeFromForm(element, actor) {
|
||||||
|
const tokenSizeSelect = element.querySelector('#dhTokenSize');
|
||||||
|
const isSizeDifferent = tokenSizeSelect?.value !== actor?.system?.size;
|
||||||
|
if (tokenSizeSelect && actor && isSizeDifferent) {
|
||||||
|
return tokenSizeSelect.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
@ -1,110 +1,10 @@
|
||||||
export default class DhTokenConfig extends foundry.applications.sheets.TokenConfig {
|
import DHTokenConfigMixin from './token-config-mixin.mjs';
|
||||||
/** @override */
|
import { getActorSizeFromForm } from './token-config-mixin.mjs';
|
||||||
static PARTS = {
|
|
||||||
tabs: super.PARTS.tabs,
|
|
||||||
identity: super.PARTS.identity,
|
|
||||||
appearance: {
|
|
||||||
template: 'systems/daggerheart/templates/sheets-settings/token-config/appearance.hbs',
|
|
||||||
scrollable: ['']
|
|
||||||
},
|
|
||||||
vision: super.PARTS.vision,
|
|
||||||
light: super.PARTS.light,
|
|
||||||
resources: super.PARTS.resources,
|
|
||||||
footer: super.PARTS.footer
|
|
||||||
};
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
async _prepareResourcesTab() {
|
|
||||||
const token = this.token;
|
|
||||||
const usesTrackableAttributes = !foundry.utils.isEmpty(CONFIG.Actor.trackableAttributes);
|
|
||||||
const attributeSource =
|
|
||||||
this.actor?.system instanceof foundry.abstract.DataModel && usesTrackableAttributes
|
|
||||||
? this.actor?.type
|
|
||||||
: this.actor?.system;
|
|
||||||
const TokenDocument = foundry.utils.getDocumentClass('Token');
|
|
||||||
const attributes = TokenDocument.getTrackedAttributes(attributeSource);
|
|
||||||
return {
|
|
||||||
barAttributes: TokenDocument.getTrackedAttributeChoices(attributes, attributeSource),
|
|
||||||
bar1: token.getBarAttribute?.('bar1'),
|
|
||||||
bar2: token.getBarAttribute?.('bar2'),
|
|
||||||
turnMarkerModes: DhTokenConfig.TURN_MARKER_MODES,
|
|
||||||
turnMarkerAnimations: CONFIG.Combat.settings.turnMarkerAnimations
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
async _prepareAppearanceTab() {
|
|
||||||
const context = await super._prepareAppearanceTab();
|
|
||||||
context.tokenSizes = CONFIG.DH.ACTOR.tokenSize;
|
|
||||||
context.tokenSize = this.document.actor.system.size;
|
|
||||||
context.usesActorSize = this.token.actor?.system?.metadata?.usesSize;
|
|
||||||
context.actorSizeDisable = context.usesActorSize && this.token.actor.system.size !== 'custom';
|
|
||||||
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
_attachPartListeners(partId, htmlElement, options) {
|
|
||||||
super._attachPartListeners(partId, htmlElement, options);
|
|
||||||
|
|
||||||
switch (partId) {
|
|
||||||
case 'appearance':
|
|
||||||
htmlElement
|
|
||||||
.querySelector('#dhTokenSize')
|
|
||||||
?.addEventListener('change', this.onTokenSizeChange.bind(this));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
|
||||||
_previewChanges(changes) {
|
|
||||||
if (!changes || !this._preview) return;
|
|
||||||
|
|
||||||
const tokenSizeSelect = this.element?.querySelector('#dhTokenSize');
|
|
||||||
if (this.token.actor && tokenSizeSelect && tokenSizeSelect.value !== 'custom') {
|
|
||||||
const tokenSizes = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew).tokenSizes;
|
|
||||||
const tokenSize = tokenSizes[tokenSizeSelect.value];
|
|
||||||
changes.width = tokenSize;
|
|
||||||
changes.height = tokenSize;
|
|
||||||
}
|
|
||||||
|
|
||||||
const deletions = { '-=actorId': null, '-=actorLink': null };
|
|
||||||
const mergeOptions = { inplace: false, performDeletions: true };
|
|
||||||
this._preview.updateSource(mergeObject(changes, deletions, mergeOptions));
|
|
||||||
|
|
||||||
if (this._preview?.object?.destroyed === false) {
|
|
||||||
this._preview.object.initializeSources();
|
|
||||||
this._preview.object.renderFlags.set({ refresh: true });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async onTokenSizeChange(event) {
|
|
||||||
const value = event.target.value;
|
|
||||||
const tokenSizeDimensions = this.element.querySelector('#tokenSizeDimensions');
|
|
||||||
if (tokenSizeDimensions) {
|
|
||||||
const disabled = value !== 'custom';
|
|
||||||
|
|
||||||
tokenSizeDimensions.dataset.tooltip = disabled
|
|
||||||
? game.i18n.localize('DAGGERHEART.APPLICATIONS.TokenConfig.actorSizeUsed')
|
|
||||||
: '';
|
|
||||||
|
|
||||||
const disabledIcon = tokenSizeDimensions.querySelector('i');
|
|
||||||
if (disabledIcon) {
|
|
||||||
disabledIcon.style.opacity = disabled ? '' : '0';
|
|
||||||
}
|
|
||||||
|
|
||||||
const dimensionsInputs = tokenSizeDimensions.querySelectorAll('.form-fields input');
|
|
||||||
for (const input of dimensionsInputs) {
|
|
||||||
input.disabled = disabled;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
export default class DhTokenConfig extends DHTokenConfigMixin(foundry.applications.sheets.TokenConfig) {
|
||||||
async _processSubmitData(event, form, submitData, options) {
|
async _processSubmitData(event, form, submitData, options) {
|
||||||
const tokenSizeSelect = this.element.querySelector('#dhTokenSize');
|
const changedTokenSizeValue = getActorSizeFromForm(this.element, this.actor);
|
||||||
if (tokenSizeSelect && this.token.actor) {
|
if (changedTokenSizeValue) this.token.actor.update({ 'system.size': changedTokenSizeValue });
|
||||||
if (tokenSizeSelect.value !== this.token.actor.system.size) {
|
|
||||||
this.token.actor.update({ 'system.size': tokenSizeSelect.value });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super._processSubmitData(event, form, submitData, options);
|
super._processSubmitData(event, form, submitData, options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
{{/if}}
|
{{/if}}
|
||||||
|
Test
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<legend>{{localize "Token Size"}}</legend>
|
<legend>{{localize "Token Size"}}</legend>
|
||||||
{{#if usesActorSize}}
|
{{#if usesActorSize}}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue