Converting actual settings to new Settings module.

This commit is contained in:
Adsooi 2024-10-10 23:28:25 +02:00
parent d1ac70a946
commit 52f859349a
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
8 changed files with 176 additions and 75 deletions

View file

@ -53,7 +53,7 @@ export class BaseEventEmitter {
* @param {string} eventType - Name of the event to listen to. Throws an error if this object does not emit this kind of event.
* @param {function(BaseEvent)} eventListener - The function to be called back when the event is emitted.
*/
addEventListener(eventType, eventListener) {
on(eventType, eventListener) {
if(!this.constructor.emits.includes(eventType)) {
const className = this.constructor.name
const eventTypes = this.constructor.emits.join(", ")
@ -70,14 +70,13 @@ export class BaseEventEmitter {
* @param {function(BaseEvent)} eventListener - The function previously registered as a listener.
* @returns {boolean} True if the listener was removed, false if it was not found.
*/
removeEventListener(eventType, eventListener) {
off(eventType, eventListener) {
if(!this.constructor.emits.includes(eventType)) {
const className = this.constructor.name
const eventTypes = this.constructor.emits.join(", ")
throw new Error(`Cannot listen to unknown event ${eventType} in class ${className}. ${className} only emits: ${eventTypes}`)
}
return this.#listeners[eventType].delete(eventListener)
}
/**

View file

@ -62,7 +62,7 @@ class IOAPI extends Module {
// Add extension if necessary
if(["lpf"].indexOf(filename.split(".")[filename.split(".").length - 1]) === -1)
filename += ".lpf"
this.saveFilename = filename
Settings.set("saveFilename", filename, false)
let objs = {}
for(let objType in Objects.currentObjects) {
objs[objType] = []

View file

@ -48,6 +48,8 @@ class ChangedEvent extends BaseEvent {
class SettingsAPI extends Module {
static emits = ["changed"]
#nonConfigurable = ["saveFilename"]
#properties = new Map([
["saveFilename", ""],
["xzoom", 100],
@ -75,16 +77,18 @@ class SettingsAPI extends Module {
super.initialize({ helper })
// Initialize default values.
for(const key of this.#properties.keys()) {
switch(typeof this.#properties.get(key)) {
case "boolean":
this.set(key, helper.getSettingBool("default_graph."+key), false)
break
case "number":
this.set(key, helper.getSettingInt("default_graph."+key), false)
break
case "string":
this.set(key, helper.getSetting("default_graph."+key), false)
break
if(!this.#nonConfigurable.includes(key)) {
switch(typeof this.#properties.get(key)) {
case "boolean":
this.set(key, helper.getSettingBool("default_graph."+key), false)
break
case "number":
this.set(key, helper.getSettingInt("default_graph."+key), false)
break
case "string":
this.set(key, helper.getSetting("default_graph."+key), false)
break
}
}
}
}
@ -101,8 +105,9 @@ class SettingsAPI extends Module {
throw new Error(`Property ${property} is not a setting.`)
}
const oldValue = this.#properties.get(property)
console.debug("Setting", property, "from", oldValue, "to", value, `(${typeof value}, ${byUser})`)
const propType = typeof oldValue
if(byUser)
console.debug("Setting", property, "from", oldValue, "to", value, `(${typeof value}, ${byUser})`)
if(propType !== typeof value)
throw new Error(`Value of ${property} must be a ${propType} (${typeof value} provided).`)
this.#properties.set(property, value)

View file

@ -28,7 +28,7 @@ const XZOOM = new NumberSetting(
const YZOOM = new NumberSetting(
qsTranslate("Settings", "Y Zoom"),
"default_graph.xzoom",
"default_graph.yzoom",
"yzoom",
0.1
)