forked from EpicGames/PixelStreamingInfrastructure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingNumber.ts
111 lines (98 loc) · 3.12 KB
/
SettingNumber.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
// Copyright Epic Games, Inc. All Rights Reserved.
import type { NumericParametersIds } from './Config';
import { SettingBase } from './SettingBase';
/**
* A number setting object with a text label. Min and max limit the range of allowed values.
*/
export class SettingNumber<
CustomIds extends string = NumericParametersIds
> extends SettingBase {
_min: number;
_max: number;
id: NumericParametersIds | CustomIds;
onChangeEmit: (changedValue: number) => void;
useUrlParams: boolean;
constructor(
id: NumericParametersIds | CustomIds,
label: string,
description: string,
min: number,
max: number,
defaultNumber: number,
useUrlParams: boolean,
// eslint-disable-next-line @typescript-eslint/no-empty-function
defaultOnChangeListener: (changedValue: unknown, setting: SettingBase) => void = () => { /* Do nothing, to be overridden. */ }
) {
super(id, label, description, defaultNumber, defaultOnChangeListener);
this._min = min;
this._max = max;
// attempt to read the number from the url params
const urlParams = new URLSearchParams(window.location.search);
if (!useUrlParams || !urlParams.has(this.id)) {
this.number = defaultNumber;
} else {
const parsedValue = Number.parseFloat(urlParams.get(this.id));
this.number = Number.isNaN(parsedValue)
? defaultNumber
: parsedValue;
}
this.useUrlParams = useUrlParams;
}
/**
* Persist the setting value in URL.
*/
public updateURLParams(): void {
if (this.useUrlParams) {
// set url params like ?id=number
const urlParams = new URLSearchParams(window.location.search);
urlParams.set(this.id, this.number.toString());
window.history.replaceState(
{},
'',
urlParams.toString() !== ''
? `${location.pathname}?${urlParams}`
: `${location.pathname}`
);
}
}
/**
* Set the number value (will be clamped within range).
*/
public set number(newNumber: number) {
this.value = this.clamp(newNumber);
}
/**
* @returns The number stored.
*/
public get number(): number {
return this.value as number;
}
/**
* Clamps a number between the min and max values (inclusive).
* @param inNumber The number to clamp.
* @returns The clamped number.
*/
public clamp(inNumber: number): number {
return Math.max(Math.min(this._max, inNumber), this._min);
}
/**
* Returns the minimum value
* @returns The minimum value
*/
public get min(): number {
return this._min;
}
/**
* Returns the maximum value
* @returns The maximum value
*/
public get max(): number {
return this._max;
}
/**
* Add a change listener to the number object.
*/
public addOnChangedListener(onChangedFunc: (newNumber: number) => void) {
this.onChange = onChangedFunc;
}
}