-
Notifications
You must be signed in to change notification settings - Fork 38
/
HuesSettings.ts
341 lines (318 loc) · 10.1 KB
/
HuesSettings.ts
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import "../css/hues-settings.css";
import EventListener from "./EventListener";
import SettingsUI from "./HuesSettings.svelte";
import type HuesWindow from "./HuesWindow";
/* If you're modifying settings for your hues, DON'T EDIT THIS
- Go to the HTML and edit the `defaults` object instead!
*/
const defaultSettings: SettingsData = {
// Location relative to root - where do the audio/zip workers live
// This is required because Web Workers need an absolute path
workersPath: "lib/workers/",
// List of respacks to load
respacks: [],
// If true, the query string (?foo=bar&baz=boz) will be parsed for settings
parseQueryString: true,
// ONLY USED FOR QUERY STRINGS this will be prepended to any respacks
// passed in as a ?packs=query
respackPath: "respacks/",
// Debugging var, for loading zips or not
load: true,
// Debug, play first song automatically?
autoplay: true,
// If true, defaults passed in initialiser override locally saved
overwriteLocal: false,
// If set, will attempt to play the named song first
firstSong: null,
// If set, will attempt to set the named image first
firstImage: null,
// set to false to never change images
fullAuto: true,
// The remote respack listing JSON endpoint
// NOTE: Any packs referenced need CORS enabled or loads fail
packsURL: "https://cdn.0x40hu.es/getRespacks.php",
// If set, will disable the remote resources menu. For custom pages.
disableRemoteResources: false,
// You will rarely want to change this. Enables/disables the Hues Window.
enableWindow: true,
// Whether to show the Hues Window on page load
showWindow: false,
// What tab will be displayed first in the Hues Window
firstWindow: "INFO",
// Preloader customisation
preloadPrefix: "0x",
preloadBase: 16,
preloadMax: 0x40,
preloadTitle: "",
// Info customisation
huesName: "0x40 Hues of JS, v%VERSION%",
huesDesc: `0x40 Hues has some music and a few images, and the
music plays and the images change.
This is such a fine idea, like wowzers.
Som- many like it!`,
// If unset, uses <body>, otherwise sets which element to turn hues-y
root: null,
// If set, keyboard shortcuts are ignored
disableKeyboard: false,
// UI accessible config
smartAlign: "on",
blurAmount: "medium",
blurDecay: "fast",
blurQuality: "medium",
currentUI: "modern",
colourSet: "normal",
blendMode: "hard-light",
bgColour: "transparent",
blackoutUI: "off",
invertStyle: "everything",
playBuildups: "on",
visualiser: "off",
shuffleImages: "on",
autoSong: "off",
autoSongDelay: 5, // loops or minutes depending on autoSong value
autoSongShuffle: "on",
autoSongFadeout: "on",
trippyMode: "off",
volume: 0.7,
skipPreloader: "off",
};
// for the UI accessible config only
const settingsOptions = {
smartAlign: {
name: "Smart Align images",
options: ["off", "on"],
},
blurAmount: {
name: "Blur amount",
options: ["low", "medium", "high"],
},
blurDecay: {
name: "Blur decay",
options: ["slow", "medium", "fast", "faster!"],
},
blurQuality: {
name: "Blur quality",
options: ["low", "medium", "high", "extreme"],
},
visualiser: {
name: "Spectrum analyser",
options: ["off", "on"],
},
currentUI: {
name: "UI style",
options: ["retro", "v4.20", "modern", "xmas", "hlwn", "mini"],
},
colourSet: {
name: "Colour set",
options: ["normal", "pastel", "v4.20"],
},
blendMode: {
name: "Blend mode",
options: ["hard-light", "screen", "multiply"],
},
bgColour: {
name: "Render backdrop",
options: ["white", "black", "transparent"],
},
blackoutUI: {
name: "Blackout affects UI",
options: ["off", "on"],
},
invertStyle: {
name: "Invert affects",
options: ["everything", "image"],
},
playBuildups: {
name: "Play buildups",
options: ["off", "once", "on"],
},
autoSong: {
name: "AutoSong",
options: ["off", "loop", "time"],
},
autoSongShuffle: {
name: "AutoSong shuffle",
options: ["off", "on"],
},
autoSongFadeout: {
name: "AutoSong fade out",
options: ["off", "on"],
},
trippyMode: {
name: "Trippy Mode",
options: ["off", "on"],
},
shuffleImages: {
name: "Shuffle images",
options: ["off", "on"],
},
skipPreloader: {
name: "Skip preloader warning",
options: ["off", "on"],
},
} as const; // this magic little thing lets us use "options" as a tuple type!
export type SettingsData = {
workersPath: string;
respacks: string[];
parseQueryString: boolean;
respackPath: string;
load: boolean;
autoplay: boolean;
overwriteLocal: boolean;
firstSong: string | null;
firstImage: string | null;
fullAuto: boolean;
packsURL: string;
disableRemoteResources: boolean;
enableWindow: boolean;
showWindow: boolean;
firstWindow: string;
preloadPrefix: string;
preloadBase: number;
preloadMax: number;
preloadTitle: string;
huesName: string;
huesDesc: string;
root: HTMLElement | string | null;
disableKeyboard: boolean;
// UI accessible config
smartAlign: (typeof settingsOptions.smartAlign.options)[number];
blurAmount: (typeof settingsOptions.blurAmount.options)[number];
blurDecay: (typeof settingsOptions.blurDecay.options)[number];
blurQuality: (typeof settingsOptions.blurQuality.options)[number];
currentUI: (typeof settingsOptions.currentUI.options)[number];
colourSet: (typeof settingsOptions.colourSet.options)[number];
blendMode: (typeof settingsOptions.blendMode.options)[number];
bgColour: (typeof settingsOptions.bgColour.options)[number];
blackoutUI: (typeof settingsOptions.blackoutUI.options)[number];
invertStyle: (typeof settingsOptions.invertStyle.options)[number];
playBuildups: (typeof settingsOptions.playBuildups.options)[number];
visualiser: (typeof settingsOptions.visualiser.options)[number];
shuffleImages: (typeof settingsOptions.shuffleImages.options)[number];
autoSong: (typeof settingsOptions.autoSong.options)[number];
autoSongShuffle: (typeof settingsOptions.autoSongShuffle.options)[number];
autoSongFadeout: (typeof settingsOptions.autoSongFadeout.options)[number];
trippyMode: (typeof settingsOptions.trippyMode.options)[number];
skipPreloader: (typeof settingsOptions.skipPreloader.options)[number];
autoSongDelay: number;
volume: number;
};
type SettingsEvents = {
// Called when settings are updated
updated: () => void;
};
export interface HuesSettings extends SettingsData {}
export class HuesSettings extends EventListener<SettingsEvents> {
ephemerals: Partial<SettingsData>;
ui?: SettingsUI;
constructor(defaults: Partial<SettingsData>) {
super();
let settingsVersion = "1";
if (localStorage.settingsVersion != settingsVersion) {
localStorage.clear();
localStorage.settingsVersion = settingsVersion;
}
this.ephemerals = {};
for (let _attr in defaultSettings) {
let attr = _attr as keyof SettingsData;
Object.defineProperty(this, attr, {
set: this.makeSetter(attr as keyof SettingsData),
get: this.makeGetter(attr as keyof SettingsData),
});
// this is too tricky for typescript and/or my brain, so just be
// lazy with the <any>
if (defaults[attr] !== undefined) {
if (defaults.overwriteLocal) {
(<any>this)[attr] = defaults[attr];
} else {
(<any>this.ephemerals)[attr] = defaults[attr];
}
}
}
if (this.parseQueryString) {
let querySettings = this.getQuerySettings();
for (let _attr in defaultSettings) {
let attr = _attr as keyof SettingsData;
// query string overrides, finally
if (querySettings[attr] !== undefined && attr != "respacks") {
(<any>this.ephemerals)[attr] = querySettings[attr];
}
}
this.respacks = this.respacks.concat(querySettings.respacks!);
}
}
getQuerySettings() {
let results: Partial<SettingsData> = {};
results.respacks = [];
let query = window.location.search.substring(1);
let vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split("=");
let val: string | boolean = decodeURIComponent(pair[1]);
if (pair[0] == "packs" || pair[0] == "respacks") {
let packs = val.split(",");
for (let j = 0; j < packs.length; j++) {
results.respacks.push(this.respackPath + packs[j]);
}
} else if (pair[0] == "song") {
// alias for firstSong
results.firstSong = val;
} else {
// since we can set ephemeral variables this way
if (val === "true" || val === "false") val = val == "true";
(<any>results)[pair[0]] = val;
}
}
return results;
}
initUI(huesWin: HuesWindow) {
let uiTab = huesWin.addTab("OPTIONS");
this.ui = new SettingsUI({
target: uiTab,
props: {
settings: this,
schema: settingsOptions,
},
});
this.ui.$on("update", () => {
this.callEventListeners("updated");
});
}
makeGetter(setting: keyof SettingsData) {
return () => {
if (defaultSettings.hasOwnProperty(setting)) {
if (this.ephemerals[setting] !== undefined)
return this.ephemerals[setting];
else if (localStorage[setting] !== undefined)
return localStorage[setting];
else return defaultSettings[setting];
} else {
console.log("WARNING: Attempted to fetch invalid setting:", setting);
return null;
}
};
}
// Set a named index to its named value, returns false if name doesn't exist
makeSetter<S extends keyof SettingsData>(setting: S) {
return (value: SettingsData[S]) => {
if (this.isEphemeral(setting)) {
this.ephemerals[setting] = value;
} else {
let opt = (<any>settingsOptions)[setting];
if (!opt || opt.options.indexOf(value) == -1) {
console.error(value, "is not a valid value for", setting);
return false;
}
localStorage[setting] = value;
this.ephemerals[setting] = undefined;
}
if (this.ui) {
this.ui.$set({ settings: this });
}
return true;
};
}
isEphemeral(setting: keyof SettingsData) {
return !settingsOptions.hasOwnProperty(setting);
}
}