UNPKG

@spearwolf/twopoint5d

Version:

a library to create 2.5d realtime graphics and pixelart with three.js

55 lines 1.98 kB
export class InputControlBase { #listeners = []; #findListenerIndex = (host, eventName, callback, passive = true) => { return this.#listeners.findIndex((listener) => listener[0] === host && listener[1] === eventName && listener[2] === callback && listener[3] === passive); }; #active = true; addEventListener(host, eventName, callback, passive = true) { if (this.#findListenerIndex(host, eventName, callback, passive) === -1) { this.#listeners.push([host, eventName, callback, passive]); if (this.#active) { host.addEventListener(eventName, callback, { passive }); } } } removeEventListener(host, eventName, callback, passive = true) { const index = this.#findListenerIndex(host, eventName, callback, passive); if (index >= 0) { if (this.#active) { const [host, eventName, callback] = this.#listeners[index]; host.removeEventListener(eventName, callback); } this.#listeners.splice(index, 1); } } get isActive() { return this.#active && this.#listeners.length > 0; } set isActive(active) { if (!this.#active && active) { this.subscribe(); } else if (this.#active && !active) { this.unsubscribe(); } } subscribe() { if (!this.#active) { this.#listeners.forEach(([host, eventName, callback, passive]) => { host.addEventListener(eventName, callback, { passive }); }); this.#active = true; } } unsubscribe() { this.#listeners.forEach(([host, eventName, callback]) => { host.removeEventListener(eventName, callback); }); this.#active = false; } destroyAllListeners() { this.unsubscribe(); this.#listeners.length = 0; } } //# sourceMappingURL=InputControlBase.js.map