Skip to content

Commit

Permalink
Support multiple change event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Jun 16, 2024
1 parent 4d50010 commit 60cb090
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/js/stendhal/ui/dialog/settings/SoundTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ export class SoundTab extends AbstractSettingsTab {
// TODO: add DOM element creation to `SettingsDialog.createCheckBox`
const chkSound = new SettingsComponent("chk_sound", "Enable sound");
chkSound.setValue(soundEnabled);
chkSound.onchange = (evt: Event) => {
chkSound.addListener((evt: Event) => {
soundEnabled = chkSound.getValue() as boolean;
config.set("sound", soundEnabled);
sound.onStateChanged();
this.setSlidersEnabled(soundEnabled);
};
});
chkSound.addTo(col1);

const layers = [
Expand All @@ -61,9 +61,9 @@ export class SoundTab extends AbstractSettingsTab {
const label = group[1];
const slider = new SliderComponent("setting-vol-" + layer, label, 0, 100);
slider.setValue(sound.getVolume(layer) * 100);
slider.onchange = function(evt: Event) {
slider.addListener(function(evt: Event) {
sound.setVolume(layer, slider.getValue() / 100);
}
});
slider.addTo(col1);
this.sliders.push(slider);
}
Expand Down
12 changes: 6 additions & 6 deletions src/js/stendhal/ui/dialog/settings/VisualsTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,37 +56,37 @@ export class VisualsTab extends AbstractSettingsTab {
const chkAnimate = new SettingsComponent("chk_animate", "Animate");
chkAnimate.setValue(animate);
chkAnimate.setEnabled(indicateActivity);
chkAnimate.onchange = (evt: Event) => {
chkAnimate.addListener((evt: Event) => {
animate = chkAnimate.getValue() as boolean;
config.set("activity-indicator.animate", animate);
StandardMessages.changeNeedsRefresh();
parent.refresh();
}
});

const chkActivityInd = new SettingsComponent("chk_activityindicator", "Object activity indicator");
chkActivityInd.setTooltip("Display an indictor over certain interactive objects and corpses"
+ " that aren't empty");
chkActivityInd.setValue(indicateActivity);
chkActivityInd.onchange = (evt: Event) => {
chkActivityInd.addListener((evt: Event) => {
indicateActivity = chkActivityInd.getValue() as boolean;
config.set("activity-indicator", indicateActivity);
chkAnimate.setEnabled(indicateActivity);
StandardMessages.changeNeedsRefresh();
parent.refresh();
};
});
chkActivityInd.addTo(col1);
chkAnimate.addTo(col1);
chkAnimate.componentElement.classList.add("indented");

const chkParallax = new SettingsComponent("chk_parallax", "Parallax scrolling backgrounds");
chkParallax.setTooltip("Parallax scrolling enabled", "Parallax scrolling disabled");
chkParallax.setValue(config.getBoolean("effect.parallax"));
chkParallax.onchange = (evt: Event) => {
chkParallax.addListener((evt: Event) => {
const enabled = chkParallax.getValue() as boolean;
config.set("effect.parallax", enabled);
StandardMessages.changeNeedsRefresh();
parent.refresh();
}
});
chkParallax.addTo(col1);
}
}
51 changes: 48 additions & 3 deletions src/js/stendhal/ui/toolkit/WidgetComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export abstract class WidgetComponent extends ComponentBase {
/** Setting type. */
protected readonly type: WidgetType;
/** Called when the settings state or value changes. */
public onchange?: Function;
private changeListeners: Function[];

private initialized = false;

Expand All @@ -39,6 +39,7 @@ export abstract class WidgetComponent extends ComponentBase {
constructor(type: WidgetType) {
super();
this.type = type;
this.changeListeners = [];
}

/**
Expand All @@ -56,13 +57,57 @@ export abstract class WidgetComponent extends ComponentBase {

// listen for changes to component element
this.componentElement.addEventListener("change", (evt: Event) => {
if (this.onchange) {
this.onchange(evt);
for (const listener of this.changeListeners) {
listener(evt);
}
this.refresh();
});
}

/**
* Adds listener for change event.
*
* @param {Function} listener
* Function to call when change event occurs.
* @return {Function}
*/
addListener(listener: Function): Function {
this.changeListeners.push(listener);
return listener;
}

/**
* Adds listener for change event.
*
* @param {number} idx
* Insertion index in list of listeners.
* @param {Function} listener
* Function to call when change event occurs.
* @return {Function}
*/
insertListener(idx: number, listener: Function): Function {
this.changeListeners.splice(idx, 0, listener);
return listener;
}

/**
* Removes change event listener.
*
* @param {number|Function} listener
* Listener function or index.
*/
removeListener(listener: number|Function) {
let idx: number;
if (typeof(listener) === "number") {
idx = listener as number;
} else {
idx = this.changeListeners.indexOf(listener as Function);
}
if (idx > -1 && idx < this.changeListeners.length) {
this.changeListeners.splice(idx, 1);
}
}

/**
* Adds as child to DOM element.
*
Expand Down

0 comments on commit 60cb090

Please sign in to comment.