-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom-nameplates.js
306 lines (303 loc) · 10.9 KB
/
custom-nameplates.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
import { libWrapper } from "./shim.js";
export const modName = "Custom Nameplates";
export const mod = "custom-nameplates";
export const DEFAULT_STYLE = {
fontSize: 24,
fontFamily: "Signika",
fontColor: "#FFFFFF",
shadowColor: "#000000",
strokeColor: "#111111",
};
export class StyleDefinition {
constructor(fontSize, fontFamily, fontColor, shadowColor, strokeColor, autoScale = false) {
this.fontSize = fontSize;
this.fontFamily = fontFamily;
this.fontColor = fontColor;
this.shadowColor = shadowColor;
this.strokeColor = strokeColor;
this.autoScale = autoScale;
}
static fromSetting(setting) {
return new StyleDefinition(
setting.fontSize,
setting.fontFamily,
setting.fontColor,
setting.shadowColor,
setting.strokeColor,
setting.autoScale
);
}
toCanvasTextStyle() {
return {
fontSize: this.fontSize + "px",
fontFamily: this.fontFamily,
fill: this.fontColor,
dropShadowColor: this.shadowColor,
stroke: this.strokeColor,
};
}
}
export const DEFAULT_STYLE_DEFINITION = new StyleDefinition(24, "Signika", "#FFFFFF", "#000000", "#111111");
export class CustomNameplates {
constructor(game, canvas, config, mergeObject) {
this.game = game;
this.canvas = canvas;
this.CONFIG = config;
this.mergeObject = mergeObject;
}
loadGlobalStyle() {
const setting = this.game.settings.get(mod, "global-style");
const style = StyleDefinition.fromSetting(setting);
return style;
}
loadLocalStyles() {
const settingBySceneId = this.game.settings.get(mod, "local-styles");
let localStyles = new Map();
for (const sceneId of Object.keys(settingBySceneId)) {
localStyles.set(sceneId, StyleDefinition.fromSetting(settingBySceneId[sceneId]));
}
return localStyles;
}
getLocalStyle(sceneId) {
return this.loadLocalStyles().get(sceneId);
}
async setLocalStyle(sceneId, styleDefinition) {
let localStyles = this.loadLocalStyles();
localStyles.set(sceneId, styleDefinition);
await this.saveLocalStyles(Object.fromEntries(localStyles));
}
async saveLocalStyles(localStyles) {
await game.settings.set("custom-nameplates", "local-styles", localStyles);
}
async saveGlobalStyle(globalStyle) {
await game.settings.set("custom-nameplates", "global-style", globalStyle);
}
async deleteLocalStyle(sceneId) {
let localStyles = this.loadLocalStyles();
localStyles.delete(sceneId);
await this.saveLocalStyles(localStyles);
}
isSceneBeingViewed() {
return this.game.scenes?.viewed;
}
setCanvasStyle() {
const globalStyle = this.loadGlobalStyle();
const localStyles = this.loadLocalStyles();
if (this.isSceneBeingViewed()) {
if (localStyles.has(this.game.scenes.viewed.id)) {
this.setCanvasStyleTo(localStyles.get(this.game.scenes.viewed.id));
} else {
this.setCanvasStyleTo(globalStyle);
}
}
this.updateNameplatesOnCanvas();
}
setCanvasStyleTo(style) {
if (style) {
this.mergeObject(this.CONFIG.canvasTextStyle, style.toCanvasTextStyle());
}
}
updateNameplatesOnCanvas() {
if (this.isSceneBeingViewed()) {
if (this.canvas.tokens) {
for (let token of this.canvas.tokens.placeables) {
if (token.nameplate) {
this.mergeObject(token.nameplate.style, this.CONFIG.canvasTextStyle);
}
}
}
if (this.canvas.templates) {
for (let template of this.canvas.templates.placeables) {
if (template.ruler) {
this.mergeObject(template.ruler.style, this.CONFIG.canvasTextStyle);
}
}
}
if (this.canvas.notes) {
for (let note of this.canvas.notes.placeables) {
if (note.tooltip) {
this.mergeObject(note.tooltip.style, this.CONFIG.canvasTextStyle);
}
}
}
}
}
isAutoScaleEnabledForScene() {
const localStyle = this.loadLocalStyles().get(this.game.scenes.viewed.id);
const localAutoScale = localStyle?.autoScale;
const globalAutoScale = this.loadGlobalStyle().autoScale;
return localAutoScale || (globalAutoScale && !localStyle);
}
checkAutoScale(canvas) {
if (canvas.tokens.preview.children.length > 0 || canvas.templates.preview.children.length > 0) return;
if (this.isSceneBeingViewed()) {
if (this.isAutoScaleEnabledForScene()) {
CustomNameplates._autoScaleTokenNameplates(canvas);
CustomNameplates._autoScaleTemplateNameplates(canvas);
CustomNameplates._autoScaleNotes(canvas);
}
}
}
static _autoScaleTokenNameplates(canvas) {
if (canvas.tokens) {
for (let token of canvas.tokens.placeables) {
if (token.nameplate) {
token.nameplate.scale.set(this._calculateAutoScale(canvas.scene.dimensions.size, canvas.stage.scale.x));
}
}
}
}
static _autoScaleTemplateNameplates(canvas) {
if (document.querySelector('.scene-control.active[data-control="measure"]')) {
if (canvas.templates) {
for (let template of canvas.templates.placeables) {
if (template.ruler) {
template.ruler.scale.set(this._calculateAutoScale(canvas.scene.dimensions.size, canvas.stage.scale.x));
}
}
}
}
}
static _autoScaleNotes(canvas) {
if (canvas.notes) {
for (let note of canvas.notes.placeables) {
note.tooltip.scale.set(this._calculateAutoScale(canvas.scene.dimensions.size, canvas.stage.scale.x));
}
}
}
static _calculateAutoScale(sceneDimensionSize, zoomStage) {
// Taken from Easy Ruler Scale, a mod by Kandashi
// https://github.com/kandashi/easy-ruler-scale
const gs = sceneDimensionSize / 100;
const zs = 1 / zoomStage;
return Math.max(gs * zs, 0.8);
}
}
class NameplateEditConfig extends FormApplication {
static get defaultOptions() {
const options = super.defaultOptions;
options.id = "custom-nameplates-edit";
options.template = "modules/custom-nameplates/templates/nameplate-config.html";
options.width = 350;
return options;
}
get title() {
return "Edit Nameplate Style";
}
async getData(_options) {
let localStyle = game.customNameplates.getLocalStyle(game.scenes.viewed.id);
let hasLocalSettings = localStyle != null;
if (!localStyle) {
localStyle = DEFAULT_STYLE_DEFINITION;
}
return {
globalSettings: game.customNameplates.loadGlobalStyle(),
localSettings: localStyle,
hasLocalSettings: hasLocalSettings,
fontFamilies: FontConfig.getAvailableFontChoices(),
};
}
async _updateObject(_event, formData) {
if (formData.localConfig) {
let localStyle = {
fontFamily: formData.localFontFamily,
fontSize: formData.localFontSize,
fontColor: formData.localFontColor,
shadowColor: formData.localShadowColor,
strokeColor: formData.localStrokeColor,
autoScale: formData.localAutoScaleFont,
};
await game.customNameplates.setLocalStyle(game.scenes.viewed.id, localStyle);
} else {
//Remove local settings (as local settings not enabled)
if (game.customNameplates.isSceneBeingViewed()) {
await game.customNameplates.deleteLocalStyle(game.scenes.viewed.id);
}
}
let globalStyle = {
fontFamily: formData.globalFontFamily,
fontSize: formData.globalFontSize,
fontColor: formData.globalFontColor,
shadowColor: formData.globalShadowColor,
strokeColor: formData.globalStrokeColor,
autoScale: formData.globalAutoScaleFont,
};
await game.customNameplates.saveGlobalStyle(globalStyle);
ui.notifications.notify("Updated nameplate styles. Please refresh for changes to apply");
game.customNameplates.setCanvasStyle();
}
}
async function registerSettings() {
game.customNameplates = new CustomNameplates(game, canvas, CONFIG, foundry.utils.mergeObject);
game.settings.register(mod, "global-style", {
scope: "world",
config: false,
type: Object,
default: DEFAULT_STYLE,
});
/*
* Scene specific config
* use game.scenes.viewed.id as key to style
*/
game.settings.register(mod, "local-styles", {
scope: "world",
config: false,
type: Object,
default: {},
});
game.settings.registerMenu(mod, "settingsMenu", {
name: "Configuration",
label: "Global Settings",
icon: "fas fa-wrench",
type: NameplateEditConfig,
restricted: true,
});
let existing = game.customNameplates.loadGlobalStyle();
if (Object.keys(existing).length < 5) {
await game.customNameplates.setGlobalStyle(DEFAULT_STYLE);
}
game.customNameplates.setCanvasStyle(existing);
registerLibWrapper();
}
function registerLibWrapper() {
//Override token getTextStyle to prevent it from changing
libWrapperRegister(
"Token.prototype._getTextStyle",
function (wrapped, ...args) {
return foundry.utils.mergeObject(wrapped(...args), CONFIG.canvasTextStyle);
},
"WRAPPER"
);
//Mesured Template style change
libWrapperRegister(
"MeasuredTemplate.prototype._refreshRulerText",
function (wrapped, ...args) {
wrapped(...args);
this.ruler.style = foundry.utils.mergeObject(this.ruler.style, CONFIG.canvasTextStyle);
},
"WRAPPER"
);
// Notes change
libWrapperRegister(
"Note.prototype._getTextStyle",
function (wrapped, ...args) {
return foundry.utils.mergeObject(wrapped(...args), CONFIG.canvasTextStyle);
},
"WRAPPER"
);
}
function libWrapperRegister(target, wrapper, type) {
libWrapper.register(mod, target, wrapper, type);
}
Hooks.on("setup", async () => {
await registerSettings();
Hooks.on("canvasInit", () => {
game.customNameplates.setCanvasStyle();
});
Hooks.once("canvasReady", () => {
Hooks.on("canvasPan", (c) => {
game.customNameplates.checkAutoScale(c);
});
});
});