-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
settings.js
311 lines (293 loc) · 8.93 KB
/
settings.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
307
308
309
310
311
import { CUSTOM_FONT_SIZES } from "./Fonts.js";
import { PolyglotFontSettings } from "./forms/FontSettings.js";
import { PolyglotGeneralSettings } from "./forms/GeneralSettings.js";
import { PolyglotLanguageSettings } from "./forms/LanguageSettings.js";
/**
* Shorthand for game.settings.register.
* Default data: {scope: "world", config: true}
* @function addSetting
* @param {string} key
* @param {object} data
*/
export function addSetting(key, data) {
const commonData = {
name: `POLYGLOT.${key}.title`,
hint: `POLYGLOT.${key}.hint`,
scope: "world",
config: true,
};
game.settings.register("polyglot", key, Object.assign(commonData, data));
}
export function addMenuSetting(key, data) {
const commonData = {
name: `POLYGLOT.${key}.title`,
hint: `POLYGLOT.${key}.hint`,
scope: "world",
config: false,
};
game.settings.register("polyglot", key, Object.assign(commonData, data));
}
export function registerSettings() {
// General Settings Menu
game.settings.registerMenu("polyglot", "GeneralSettings", {
name: "POLYGLOT.GeneralSettings",
label: "POLYGLOT.GeneralSettings",
icon: "fas fa-cogs",
type: PolyglotGeneralSettings,
});
// Font Settings Menu
game.settings.registerMenu("polyglot", "FontSettings", {
name: "POLYGLOT.FontSettings",
label: "POLYGLOT.FontSettings",
icon: "fas fa-font",
type: PolyglotFontSettings,
restricted: true,
});
// Language Settings Menu
game.settings.registerMenu("polyglot", "LanguageSettings", {
name: "POLYGLOT.LanguageSettings",
label: "POLYGLOT.LanguageSettings",
icon: "fas fa-globe",
type: PolyglotLanguageSettings,
restricted: true,
});
addMenuSetting("Alphabets", {
config: false,
default: {},
type: Object,
});
addMenuSetting("Languages", {
config: false,
default: {},
type: Object,
});
// Font Settings
addMenuSetting("RuneRegex", {
default: false,
type: Boolean,
});
addMenuSetting("enableAllFonts", {
name: "POLYGLOT.enableAllFonts.title",
hint: "POLYGLOT.enableAllFonts.hint",
default: false,
type: Boolean,
requiresReload: true,
});
addMenuSetting("exportFonts", {
name: "POLYGLOT.ExportFonts.title",
hint: "POLYGLOT.ExportFonts.hint",
default: false,
type: Boolean,
requiresReload: true,
});
// Journal Settings
addMenuSetting("IgnoreJournalFontSize", {
default: false,
type: Boolean,
});
addMenuSetting("JournalHighlightColor", {
default: "#ffb400",
type: String,
isColor: true,
onChange: (value) => {
document.documentElement.style.setProperty("--polyglot-journal-color", value);
},
});
const hex = hexToRgb(game.settings.get("polyglot", "JournalHighlightColor"));
document.documentElement.style.setProperty("--polyglot-journal-color", Object.values(hex).toString());
addMenuSetting("JournalHighlight", {
default: 25,
range: {
min: 0,
max: 100,
step: 1,
},
type: Number,
onChange: (value) => {
document.documentElement.style.setProperty("--polyglot-journal-opacity", value / 100);
},
});
document.documentElement.style.setProperty(
"--polyglot-journal-opacity",
game.settings.get("polyglot", "JournalHighlight") / 100,
);
// Language Settings
addMenuSetting("replaceLanguages", {
name: "POLYGLOT.ReplaceLanguages.title",
hint: "POLYGLOT.ReplaceLanguages.hint",
default: false,
type: Boolean,
requiresReload: true,
onChange: async () => {
await game.polyglot.languageProvider.getLanguages();
game.polyglot.languageProvider.loadLanguages();
game.polyglot.languageProvider.reloadLanguages();
},
});
addMenuSetting("defaultLanguage", {
name: "POLYGLOT.DefaultLanguage.title",
hint: "POLYGLOT.DefaultLanguage.hint",
default: "",
type: String,
onChange: () => {
game.polyglot.languageProvider.getDefaultLanguage();
},
});
addMenuSetting("customLanguages", {
name: "POLYGLOT.CustomLanguages.title",
hint: "POLYGLOT.CustomLanguages.hint",
default: "",
type: String,
hasTextarea: true,
requiresReload: true,
onChange: () => {
game.polyglot.languageProvider.loadLanguages();
game.polyglot.languageProvider.reloadLanguages();
},
});
addMenuSetting("omniglot", {
name: "POLYGLOT.Omniglot.title",
hint: "POLYGLOT.Omniglot.hint",
default: "",
type: String,
onChange: (value) => (game.polyglot.omniglot = value.trim().replace(/'/g, "_")),
});
addMenuSetting("comprehendLanguages", {
name: "POLYGLOT.ComprehendLanguages.title",
hint: "POLYGLOT.ComprehendLanguages.hint",
default: "",
type: String,
onChange: (value) => (game.polyglot.comprehendLanguages = value.trim().replace(/'/g, "_")),
});
addMenuSetting("truespeech", {
name: "POLYGLOT.Truespeech.title",
hint: "POLYGLOT.Truespeech.hint",
default: "",
type: String,
onChange: (value) => (game.polyglot.truespeech = value.trim().replace(/'/g, "_")),
});
// Chat Settings
addMenuSetting("displayCheckbox", {
name: "POLYGLOT.DisplayCheckbox.title",
hint: "POLYGLOT.DisplayCheckbox.hint",
default: true,
type: Boolean,
requiresReload: true,
});
addMenuSetting("display-translated", {
name: "POLYGLOT.DisplayTranslated.title",
hint: "POLYGLOT.DisplayTranslated.hint",
default: true,
type: Boolean,
});
addMenuSetting("hideTranslation", {
name: "POLYGLOT.HideTranslation.title",
hint: "POLYGLOT.HideTranslation.hint",
default: false,
type: Boolean,
requiresReload: true,
});
// allowOOC
addMenuSetting("runifyGM", {
name: "POLYGLOT.ScrambleGM.title",
hint: "POLYGLOT.ScrambleGM.hint",
default: false,
type: Boolean,
requiresReload: true,
});
// Used Internally
addMenuSetting("CustomFontSizes", {
default: CUSTOM_FONT_SIZES,
type: Object,
});
addMenuSetting("checkbox", {
default: true,
type: Boolean,
scope: "client"
});
Hooks.on("i18nInit", () => {
addMenuSetting("allowOOC", {
name: "POLYGLOT.AllowOOC.title",
hint: "POLYGLOT.AllowOOC.hint",
choices: {
a: game.i18n.localize("POLYGLOT.AllowOOCOptions.a"),
b: game.i18n.localize("POLYGLOT.AllowOOCOptions.b"),
c: game.i18n.localize("POLYGLOT.AllowOOCOptions.c"),
d: game.i18n.localize("POLYGLOT.AllowOOCOptions.d"),
},
default: "b",
type: String,
});
});
}
// Language Provider Settings
export function registerProviderSettings() {
const systemSpecificSettings = game.polyglot.languageProvider.settings;
if (Object.keys(systemSpecificSettings).length) {
for (let [key, data] of Object.entries(systemSpecificSettings)) {
addSetting(key, data);
}
}
}
export async function renderPolyglotGeneralSettingsHandler(settingsConfig, html) {
const JournalHighlightColor = game.settings.get("polyglot", "JournalHighlightColor");
const JournalHighlight = game.settings.get("polyglot", "JournalHighlight");
const JournalHighlightInput = html.find('input[name="JournalHighlight"]');
const JournalHighlightNotes = JournalHighlightInput.parent().children()[3];
if (JournalHighlightNotes) JournalHighlightNotes.classList.add("polyglot-journal-temp");
const hex = hexToRgb(JournalHighlightColor);
document.documentElement.style.setProperty("--polyglot-journal-color-temp", Object.values(hex).toString());
document.documentElement.style.setProperty("--polyglot-journal-opacity-temp", JournalHighlight / 100);
JournalHighlightInput.on("change", (event) => {
document.documentElement.style.setProperty("--polyglot-journal-opacity-temp", event.target.value / 100);
});
const JournalHighlightColorPicker = html.find('input[data-edit="JournalHighlightColor"]');
JournalHighlightColorPicker.on("change", (event) => {
const hex = hexToRgb(event.target.value);
document.documentElement.style.setProperty("--polyglot-journal-color-temp", Object.values(hex).toString());
});
}
export async function renderSettingsConfigHandler(settingsConfig, html) {
if (game.settings.settings.has("polyglot.languageDataPath")) {
const languageDataPath = game.settings.get("polyglot", "languageDataPath");
const languageDataPathInput = html.find('input[name="polyglot.languageDataPath"]');
const LanguageRegexInput = html.find('input[name="polyglot.LanguageRegex"]');
const literacyDataPathInput = html.find('input[name="polyglot.literacyDataPath"]');
if (languageDataPath) disableCheckbox(LanguageRegexInput, true);
else disableCheckbox(literacyDataPathInput, true);
languageDataPathInput.on("change", (event) => {
disableCheckbox(LanguageRegexInput, event.target.value.length);
disableCheckbox(literacyDataPathInput, !event.target.value.length);
});
}
}
export function disableCheckbox(checkbox, boolean) {
checkbox.prop("disabled", boolean);
}
export function getNestedData(data, path) {
if (!RegExp(/^([\w_-]+\.)*([\w_-]+)$/).test(path)) {
return null;
}
const paths = path.split(".");
if (!paths.length) {
return null;
}
let res = data;
for (let i = 0; i < paths.length; i += 1) {
if (res === undefined) {
return null;
}
res = res?.[paths[i]];
}
return res;
}
function hexToRgb(hex) {
let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result
? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16),
}
: null;
}