-
Notifications
You must be signed in to change notification settings - Fork 42
/
settings.js
74 lines (70 loc) · 2.79 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
import { EventDispatcher } from "./utils/EventDispatcher.js";
class Settings extends EventDispatcher {
_getStorage() { return JSON.parse(localStorage.getItem("mcSettings") || "{}"); };
_setStorage(data) { localStorage.setItem("mcSettings", JSON.stringify(data)); };
_changeProp(key, value) {
const storage = this._getStorage();
storage[key] = value;
this._setStorage(storage);
this.dispatchEvent("changedValue", key, value);
};
constructor() {
super();
const storage = this._getStorage();
this._addNumberProp("fov", 30, 110, 60, storage.fov);
this._addNumberProp("mousemoveSensitivity", 60, 800, 200, storage.mousemoveSensitivity);
this._addNumberProp("renderDistance", 1, 32, 4, storage.renderDistance);
this._addNumberProp("homepageBlur", 0, 10, 3.5, storage.homepageBlur);
this._addBoolProp("shade", true, storage.shade);
this._addBoolProp("showDebugOutput", false, storage.showDebugOutput);
};
_addNumberProp(key, min, max, defaultVal, currentVal = defaultVal) {
Object.defineProperty(this, "_" + key, {
value: { type: "number", min, max, defaultVal, currentVal, },
});
Object.defineProperty(this, key, {
enumerable: true,
get: () => this["_" + key].currentVal,
set: (newValue) => {
let o = this["_" + key];
o.currentVal = Math.max(o.min, Math.min(o.max, newValue));
this._changeProp(key, o.currentVal);
},
});
Object.defineProperty(this, key + "Min", {
get: () => this["_" + key].min,
set: (newValue) => o.min = newValue,
});
Object.defineProperty(this, key + "Max", {
get: () => this["_" + key].max,
set: (newValue) => o.max = newValue,
});
Object.defineProperty(this, key + "Default", {
get: () => this["_" + key].defaultVal,
set: (newValue) => o.defaultVal = newValue,
});
};
_addBoolProp(key, defaultVal, currentVal = defaultVal) {
Object.defineProperty(this, "_" + key, {
value: { type: "boolean", defaultVal, currentVal, },
});
Object.defineProperty(this, key, {
enumerable: true,
get: () => this["_" + key].currentVal,
set: (newValue) => {
let o = this["_" + key];
o.currentVal = !!newValue;
this._changeProp(key, o.currentVal);
},
});
Object.defineProperty(this, key + "Default", {
get: () => this["_" + key].defaultVal,
set: (newValue) => o.defaultVal = newValue,
});
};
};
const settings = new Settings();
export {
settings as default,
settings,
};