Add extra features to the Temple Enricher and fix the mousewheel issues with the Template Manager

This commit is contained in:
Chris Ryan 2025-09-04 12:38:16 +10:00
parent 0bd423ef52
commit 8febcafa83
2 changed files with 63 additions and 18 deletions

View file

@ -57,7 +57,10 @@ export default class DhTemplateManager {
* @param {wheel Event} event
*/
#onMouseWheel(event) {
if (!event.shiftKey) return;
if (!this.#activePreview) {
return;
}
if (!event.shiftKey && !event.ctrlKey) return;
event.stopPropagation();
event.preventDefault();
const { moveTime, object } = this.#activePreview;
@ -66,8 +69,10 @@ export default class DhTemplateManager {
if (now - (moveTime || 0) <= 16) return;
this.#activePreview.moveTime = now;
const multiplier = event.shiftKey ? 0.2 : 0.1;
object.document.updateSource({
direction: object.document.direction + event.deltaY * 0.2
direction: object.document.direction + event.deltaY * multiplier
});
object.renderFlags.set({ refresh: true });
}
@ -77,12 +82,13 @@ export default class DhTemplateManager {
* @param {contextmenu Event} event
*/
#cancelTemplate(event) {
const { mousemove, mousedown, contextmenu } = this.#activePreview.events;
const { mousemove, mousedown, contextmenu, wheel } = this.#activePreview.events;
canvas.templates._onDragLeftCancel(event);
canvas.stage.off('mousemove', mousemove);
canvas.stage.off('mousedown', mousedown);
canvas.app.view.removeEventListener('contextmenu', contextmenu);
canvas.app.view.removeEventListener('wheel', wheel);
}
/**
@ -91,9 +97,9 @@ export default class DhTemplateManager {
*/
#confirmTemplate(event) {
event.stopPropagation();
this.#cancelTemplate(event);
canvas.scene.createEmbeddedDocuments('MeasuredTemplate', [this.#activePreview.document.toObject()]);
this.#cancelTemplate(event);
this.#activePreview = undefined;
}
}

View file

@ -3,6 +3,8 @@ export default function DhTemplateEnricher(match, _options) {
let type = null,
range = null,
angle = CONFIG.MeasuredTemplate.defaults.angle,
direction = 0,
inline = false;
parts.forEach(part => {
@ -16,14 +18,24 @@ export default function DhTemplateEnricher(match, _options) {
type = matchedType;
break;
case 'range':
const matchedRange = Object.values(CONFIG.DH.GENERAL.templateRanges).find(
x => x.id.toLowerCase() === split[1] || x.short === split[1]
);
range = matchedRange?.id;
if (Number.isNaN(Number(split[1]))) {
const matchedRange = Object.values(CONFIG.DH.GENERAL.templateRanges).find(
x => x.id.toLowerCase() === split[1] || x.short === split[1]
);
range = matchedRange?.id;
} else {
range = split[1];
}
break;
case 'inline':
inline = true;
break;
case 'angle':
angle = split[1];
break;
case 'direction':
direction = split[1];
break;
}
}
});
@ -32,10 +44,32 @@ export default function DhTemplateEnricher(match, _options) {
const label = game.i18n.localize(`DAGGERHEART.CONFIG.TemplateTypes.${type}`);
const rangeDisplay = Number.isNaN(Number(range)) ? game.i18n.localize(`DAGGERHEART.CONFIG.Range.${range}.name`) : range;
let angleDisplay = '';
if (angle != CONFIG.MeasuredTemplate.defaults.angle) {
angleDisplay = 'angle:' + angle;
}
let directionDisplay = '';
if (direction != 0) {
directionDisplay = 'direction:' + direction;
}
let extraDisplay = '';
if (angleDisplay != '' && directionDisplay != '') {
extraDisplay = ' (' + angleDisplay + '|' + directionDisplay + ')';
} else if (angleDisplay != '') {
extraDisplay = ' (' + angleDisplay + ')';
} else if (directionDisplay != '') {
extraDisplay = ' (' + directionDisplay + ')';
}
const templateElement = document.createElement('span');
templateElement.innerHTML = `
<button type="button" class="measured-template-button${inline ? ' inline' : ''}" data-type="${type}" data-range="${range}">
${label} - ${game.i18n.localize(`DAGGERHEART.CONFIG.Range.${range}.name`)}
<button type="button" class="measured-template-button${inline ? ' inline' : ''}"
data-type="${type}" data-range="${range}" data-angle="${angle}" data-direction="${direction}">
${label} - ${rangeDisplay}${extraDisplay}
</button>
`;
@ -45,21 +79,25 @@ export default function DhTemplateEnricher(match, _options) {
export const renderMeasuredTemplate = async event => {
const button = event.currentTarget,
type = button.dataset.type,
range = button.dataset.range;
range = button.dataset.range,
angle = button.dataset.angle,
direction = button.dataset.direction;
if (!type || !range || !game.canvas.scene) return;
const usedType = type === 'inFront' ? 'cone' : type === 'emanation' ? 'circle' : type;
const angle =
const usedAngle =
type === CONST.MEASURED_TEMPLATE_TYPES.CONE
? CONFIG.MeasuredTemplate.defaults.angle
? (angle ?? CONFIG.MeasuredTemplate.defaults.angle)
: type === CONFIG.DH.GENERAL.templateTypes.INFRONT
? '180'
: undefined;
const baseDistance = game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.variantRules).rangeMeasurement[
range
];
let baseDistance = range;
if (Number.isNaN(Number(range))) {
baseDistance =
game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.variantRules).rangeMeasurement[range];
}
const distance = type === CONFIG.DH.GENERAL.templateTypes.EMANATION ? baseDistance + 2.5 : baseDistance;
const { width, height } = game.canvas.scene.dimensions;
@ -69,7 +107,8 @@ export const renderMeasuredTemplate = async event => {
t: usedType,
distance: distance,
width: type === CONST.MEASURED_TEMPLATE_TYPES.RAY ? 5 : undefined,
angle: angle
angle: usedAngle,
direction: direction
};
CONFIG.ux.TemplateManager.createPreview(data);