forked from Animated-Java/animated-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
461 lines (411 loc) · 11.9 KB
/
Copy pathsettings.ts
File metadata and controls
461 lines (411 loc) · 11.9 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import { GUIStructure } from './guiStructure'
import { formatStr } from './util/misc'
import { Subscribable } from './util/subscribable'
import { translate } from './util/translation'
import * as events from './events'
import { reducedMotion } from './ui/util/accessability'
export interface IInfoPopup {
type: 'warning' | 'error' | 'info'
title: string
lines: string[]
}
export type SettingID = `${string}${string}:${string}${string}/${string}${string}`
export interface ISettingOptions<V> {
/**
* The id of the setting.
* The id should be in the format of `namespace:interface/setting_name`.
* This should be unique across all plugins and interfaces!
*/
id: SettingID
displayName: string
/**
* A list of paragraphs to display in the description of the setting.
*/
description: string[]
/**
* A string to display below the setting's value in the settings dialog.
*/
subtext?: string
/**
* The default value of the setting.
*/
defaultValue: V
/**
* Whether or not the setting can be reset to its default value.
*/
resettable?: boolean
/**
* A link to the docs page/section for this setting.
*/
docsLink?: string
/**
* A list of settings that this setting depends on.
* If any of the settings in this list update, this setting will also update.
*/
dependsOn?: SettingID[]
}
export type ISettingsObject = Record<string, Setting<any>>
export class Setting<V, R = any> extends Subscribable<R> {
static registeredSettings = new Map<string, Setting<any>>()
id: SettingID
displayName: string
description: string[]
defaultValue: V
resettable?: boolean
docsLink?: string
dependsOn?: SettingID[]
subtext?: string
private _initialized: boolean
private _updating: boolean
protected _value: V
protected lastValue: V
infoPopup?: IInfoPopup
/**
* Creates a new setting
* @param onUpdate runs when the setting's value is updated.
* @param onInit runs when the setting is initialized.
* @param onConfirm runs when the setting's value is confirmed (when closing the dialog).
*/
constructor(
options: ISettingOptions<V>,
public onUpdate?: (setting: R) => void,
public onInit?: (setting: R) => void,
public onConfirm?: (setting: R) => void
) {
super()
this.id = options.id
this.displayName = options.displayName
this.description = options.description
this.defaultValue = options.defaultValue
this.resettable = options.resettable
this.docsLink = options.docsLink
this.dependsOn = options.dependsOn
this.subtext = options.subtext
this._value = this.defaultValue
this.lastValue = this.defaultValue
this._initialized = false
this._updating = false
Setting.registeredSettings.set(this.id, this)
}
get value(): V {
return this._value
}
set value(value: V) {
this._value = value
this._value ??= this.defaultValue
this._onUpdate()
}
set(value: V) {
this.value = value
}
_onInit() {
if (this._initialized) return
console.log('Initializing setting', this.id)
if (this.onInit) this.onInit(this as unknown as R)
if (this.dependsOn) {
for (const id of this.dependsOn) {
const setting = Setting.registeredSettings.get(id)
if (!setting) {
console.warn(
`Setting ${this.id} depends on setting ${id}, but that setting does not exist.`
)
continue
}
setting.subscribe(() => {
this._onUpdate(true)
})
}
}
this._initialized = true
}
_onUpdate(forced = false) {
if (this._updating) return
if (!forced && this.value === this.lastValue) return
// console.log('Updating setting', this.id, this.value)
this._updating = true
this.lastValue = this.value
this.infoPopup = undefined
if (this.onUpdate) this.onUpdate(this as unknown as R)
this.dispatch(this as unknown as R)
this._updating = false
}
verify() {
if (this.onUpdate) this.onUpdate(this as unknown as R)
return this.infoPopup
}
_save(): any {
return this.value
}
_load(value: any) {
this.value = value
}
}
export class CheckboxSetting extends Setting<boolean, CheckboxSetting> {}
export class InlineTextSetting extends Setting<string, InlineTextSetting> {}
export class CodeboxSetting extends Setting<string, CodeboxSetting> {
language: string
constructor(
options: ISettingOptions<string> & { language: string },
onUpdate?: (setting: CodeboxSetting) => void,
onInit?: (setting: CodeboxSetting) => void,
onConfirm?: (setting: CodeboxSetting) => void
) {
super(options, onUpdate, onInit, onConfirm)
this.language = options.language
}
}
export class FolderSetting extends Setting<string, FolderSetting> {}
export class FileSetting extends Setting<string, FileSetting> {}
export class NumberSetting extends Setting<number, NumberSetting> {
min?: number
max?: number
step?: number
snap?: boolean
constructor(
options: ISettingOptions<number> & {
min?: number
max?: number
step?: number
snap?: boolean
},
onUpdate?: (setting: NumberSetting) => void,
onInit?: (setting: NumberSetting) => void,
onConfirm?: (setting: NumberSetting) => void
) {
super(options, onUpdate, onInit, onConfirm)
this.min = options.min
this.max = options.max
this.step = options.step
this.snap = options.snap
}
_onUpdate() {
if (isNaN(this._value)) this._value = this.defaultValue
if (this.step && this.snap) this._value = Math.round(this._value / this.step) * this.step
this._value = Math.min(Math.max(this._value, this.min ?? -Infinity), this.max ?? Infinity)
super._onUpdate()
}
}
export class DoubleNumberSetting extends Setting<[number, number], DoubleNumberSetting> {
min?: number
max?: number
step?: number
snap?: boolean
firstNumberLabel?: string
secondNumberLabel?: string
constructor(
options: ISettingOptions<[number, number]> & {
min?: number
max?: number
step?: number
snap?: boolean
firstNumberLabel?: string
secondNumberLabel?: string
},
onUpdate?: (setting: DoubleNumberSetting) => void,
onInit?: (setting: DoubleNumberSetting) => void,
onConfirm?: (setting: DoubleNumberSetting) => void
) {
super(options, onUpdate, onInit, onConfirm)
this.min = options.min
this.max = options.max
this.step = options.step
this.snap = options.snap
this.firstNumberLabel = options.firstNumberLabel
this.secondNumberLabel = options.secondNumberLabel
}
get numberA() {
return this._value[0]
}
set numberA(value: number) {
this._value[0] = value
this._onUpdate(true)
}
get numberB() {
return this._value[1]
}
set numberB(value: number) {
this._value[1] = value
this._onUpdate(true)
}
_onUpdate(forced = false) {
if (isNaN(this._value[0])) this._value[0] = this.defaultValue[0]
if (isNaN(this._value[1])) this._value[1] = this.defaultValue[1]
if (this.step && this.snap) {
this._value[0] = Math.round(this._value[0] / this.step) * this.step
this._value[1] = Math.round(this._value[1] / this.step) * this.step
}
this._value[0] = Math.min(
Math.max(this._value[0], this.min ?? -Infinity),
this.max ?? Infinity
)
this._value[1] = Math.min(
Math.max(this._value[1], this.min ?? -Infinity),
this.max ?? Infinity
)
super._onUpdate(forced)
}
}
export class DropdownSetting<V = any, K extends number = number> extends Setting<
K,
DropdownSetting<V, K>
> {
options: Array<{ name: string; value: V }>
constructor(
options: ISettingOptions<K> & { options: DropdownSetting<V, K>['options'] },
public onUpdate?: (setting: DropdownSetting<V, K>) => void,
public onInit?: (setting: DropdownSetting<V, K>) => void,
public onConfirm?: (setting: DropdownSetting<V, K>) => void
) {
super(options, onUpdate, onInit, onConfirm)
this.options = options.options
}
get selected(): DropdownSetting<V, K>['options'][any] | undefined {
return this.options[this.value]
}
override _save() {
return this.selected?.value
}
override _load(value: any) {
const index = this.options.findIndex(option => option.value === value) as K
this.value = index >= 0 ? index : this.defaultValue
}
}
export class ImageDropdownSetting extends DropdownSetting<Texture['uuid']> {
constructor(
options: ISettingOptions<number> & { options: ImageDropdownSetting['options'] },
onUpdate?: (setting: ImageDropdownSetting) => void,
onInit?: (setting: ImageDropdownSetting) => void,
onConfirm?: (setting: ImageDropdownSetting) => void
) {
super(
options,
onUpdate as Setting<Texture['uuid']>['onUpdate'],
onInit as Setting<Texture['uuid']>['onInit'],
onConfirm as Setting<Texture['uuid']>['onConfirm']
)
}
getSelectedTexture() {
return Texture.all.find(texture => texture.uuid === this.selected?.value)
}
}
export interface IListItem {
name: string
value: string
}
export class ListBuilderSetting extends Setting<IListItem[]> {
options: IListItem[]
addNewItemMessage: string
constructor(
options: ISettingOptions<IListItem[]> & {
options: ListBuilderSetting['options']
addNewItemMessage: string
},
onUpdate?: (setting: ListBuilderSetting) => void,
onInit?: (setting: ListBuilderSetting) => void,
onConfirm?: (setting: ListBuilderSetting) => void
) {
super(
options,
onUpdate as Setting<string>['onUpdate'],
onInit as Setting<string>['onInit'],
onConfirm as Setting<string>['onConfirm']
)
this.options = options.options
this.addNewItemMessage = options.addNewItemMessage
}
hasItem(item: IListItem) {
return this.value.some(i => i.value === item.value && i.name === item.name)
}
removeItem(item: IListItem) {
this.value = this.value.filter(i => i.value !== item.value && i.name !== item.name)
this._onUpdate(true)
}
addItem(item: IListItem, forced = false) {
if (!forced && this.hasItem(item)) return
this.value.push(item)
this._onUpdate(true)
}
override _save(): any {
return this.value
}
override _load(value: IListItem[]) {
this.value = []
for (const item of value) {
this.addItem(item, true)
}
}
}
export const animatedJavaSettings = {
reduced_motion: new CheckboxSetting(
{
id: 'animated_java:global_settings/reduced_motion',
displayName: translate('animated_java.settings.reduced_motion'),
description: translate('animated_java.settings.reduced_motion.description').split('\n'),
defaultValue: false,
docsLink: '/docs/animated-java/settings#reduced-motion',
},
function onUpdate(setting) {
reducedMotion.set(setting.value)
}
),
minify_output: new CheckboxSetting({
id: 'animated_java:global_settings/minify_output',
displayName: translate('animated_java.settings.minify_output'),
description: translate('animated_java.settings.minify_output.description').split('\n'),
defaultValue: false,
docsLink: '/docs/animated-java/settings#minify-output',
}),
}
export const animatedJavaSettingsStructure: GUIStructure = [
{
type: 'group',
title: translate('animated_java.settings.accessability_options_group'),
openByDefault: true,
children: [
{
type: 'setting',
settingId: animatedJavaSettings.reduced_motion.id,
},
],
},
{
type: 'group',
title: translate('animated_java.settings.resource_pack_group'),
openByDefault: true,
children: [
{
type: 'setting',
settingId: animatedJavaSettings.minify_output.id,
},
],
},
]
export function createInfo(type: 'error' | 'warning' | 'info', info: string, formatObject = {}) {
const lines = formatStr(info, formatObject).split('\n')
return {
type,
title: lines[0],
lines: lines.slice(1),
}
}
export function loadAJSettings() {
let savedSettings = localStorage.getItem('animated_java:settings')
if (!savedSettings) savedSettings = '{}'
const settings = JSON.parse(savedSettings)
for (const [id, setting] of Object.entries(animatedJavaSettings)) {
if (settings[id] !== undefined) {
console.log('Loading setting', id, settings[id])
setting._load(settings[id])
}
}
}
export function saveAJSettings() {
const settings: Record<string, any> = {}
for (const [id, setting] of Object.entries(animatedJavaSettings)) {
settings[id] = setting._save()
}
localStorage.setItem('animated_java:settings', JSON.stringify(settings))
}
events.LOAD_PROJECT.subscribe(() => {
loadAJSettings()
})