From 377320181f34c58e59ff9601b0e56b9df2463a56 Mon Sep 17 00:00:00 2001 From: barsdeveloper Date: Tue, 19 Apr 2022 21:13:47 +0200 Subject: [PATCH] Fix input consume --- dist/ueblueprint.js | 30 +++++++++++++++--------------- js/input/mouse/IMouseClickDrag.js | 30 +++++++++++++++--------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/dist/ueblueprint.js b/dist/ueblueprint.js index fb5b97c..6515973 100755 --- a/dist/ueblueprint.js +++ b/dist/ueblueprint.js @@ -1703,24 +1703,24 @@ class IMouseClickDrag extends IPointing { started = false constructor(target, blueprint, options = {}) { + options.clickButton ??= 0; + options.consumeEvent ??= true; + options.exitAnyButton ??= true; + options.looseTarget ??= false; + options.moveEverywhere ??= false; super(target, blueprint, options); - this.clickButton = options?.clickButton ?? 0; - this.exitAnyButton = options?.exitAnyButton ?? true; - this.moveEverywhere = options?.moveEverywhere ?? false; - this.looseTarget = options?.looseTarget ?? false; - this.consumeEvent = options?.consumeEvent ?? true; this.clickedPosition = [0, 0]; - const movementListenedElement = this.moveEverywhere ? document.documentElement : this.movementSpace; + const movementListenedElement = this.options.moveEverywhere ? document.documentElement : this.movementSpace; let self = this; this.#mouseDownHandler = e => { this.blueprint.setFocused(true); switch (e.button) { - case self.clickButton: + case self.options.clickButton: // Either doesn't matter or consider the click only when clicking on the parent, not descandants - if (self.looseTarget || e.target == e.currentTarget) { - if (this.consumeEvent) { + if (self.options.looseTarget || e.target == e.currentTarget) { + if (this.options.consumeEvent) { e.stopImmediatePropagation(); // Captured, don't call anyone else } // Attach the listeners @@ -1731,7 +1731,7 @@ class IMouseClickDrag extends IPointing { } break default: - if (!self.exitAnyButton) { + if (!self.options.exitAnyButton) { self.#mouseUpHandler(e); } break @@ -1739,7 +1739,7 @@ class IMouseClickDrag extends IPointing { }; this.#mouseStartedMovingHandler = e => { - if (this.consumeEvent) { + if (this.options.consumeEvent) { e.stopImmediatePropagation(); // Captured, don't call anyone else } // Delegate from now on to self.#mouseMoveHandler @@ -1754,7 +1754,7 @@ class IMouseClickDrag extends IPointing { }; this.#mouseMoveHandler = e => { - if (this.consumeEvent) { + if (this.options.consumeEvent) { e.stopImmediatePropagation(); // Captured, don't call anyone else } const location = self.locationFromEvent(e); @@ -1766,8 +1766,8 @@ class IMouseClickDrag extends IPointing { }; this.#mouseUpHandler = e => { - if (!self.exitAnyButton || e.button == self.clickButton) { - if (this.consumeEvent) { + if (!self.options.exitAnyButton || e.button == self.options.clickButton) { + if (this.options.consumeEvent) { e.stopImmediatePropagation(); // Captured, don't call anyone else } // Remove the handlers of "mousemove" and "mouseup" @@ -1792,7 +1792,7 @@ class IMouseClickDrag extends IPointing { listenEvents() { this.target.addEventListener("mousedown", this.#mouseDownHandler); - if (this.clickButton == 2) { + if (this.options.clickButton == 2) { this.target.addEventListener("contextmenu", e => e.preventDefault()); } } diff --git a/js/input/mouse/IMouseClickDrag.js b/js/input/mouse/IMouseClickDrag.js index 43bcad5..d38e07e 100644 --- a/js/input/mouse/IMouseClickDrag.js +++ b/js/input/mouse/IMouseClickDrag.js @@ -31,24 +31,24 @@ export default class IMouseClickDrag extends IPointing { started = false constructor(target, blueprint, options = {}) { + options.clickButton ??= 0 + options.consumeEvent ??= true + options.exitAnyButton ??= true + options.looseTarget ??= false + options.moveEverywhere ??= false super(target, blueprint, options) - this.clickButton = options?.clickButton ?? 0 - this.exitAnyButton = options?.exitAnyButton ?? true - this.moveEverywhere = options?.moveEverywhere ?? false - this.looseTarget = options?.looseTarget ?? false - this.consumeEvent = options?.consumeEvent ?? true this.clickedPosition = [0, 0] - const movementListenedElement = this.moveEverywhere ? document.documentElement : this.movementSpace + const movementListenedElement = this.options.moveEverywhere ? document.documentElement : this.movementSpace let self = this this.#mouseDownHandler = e => { this.blueprint.setFocused(true) switch (e.button) { - case self.clickButton: + case self.options.clickButton: // Either doesn't matter or consider the click only when clicking on the parent, not descandants - if (self.looseTarget || e.target == e.currentTarget) { - if (this.consumeEvent) { + if (self.options.looseTarget || e.target == e.currentTarget) { + if (this.options.consumeEvent) { e.stopImmediatePropagation() // Captured, don't call anyone else } // Attach the listeners @@ -59,7 +59,7 @@ export default class IMouseClickDrag extends IPointing { } break default: - if (!self.exitAnyButton) { + if (!self.options.exitAnyButton) { self.#mouseUpHandler(e) } break @@ -67,7 +67,7 @@ export default class IMouseClickDrag extends IPointing { } this.#mouseStartedMovingHandler = e => { - if (this.consumeEvent) { + if (this.options.consumeEvent) { e.stopImmediatePropagation() // Captured, don't call anyone else } // Delegate from now on to self.#mouseMoveHandler @@ -82,7 +82,7 @@ export default class IMouseClickDrag extends IPointing { } this.#mouseMoveHandler = e => { - if (this.consumeEvent) { + if (this.options.consumeEvent) { e.stopImmediatePropagation() // Captured, don't call anyone else } const location = self.locationFromEvent(e) @@ -94,8 +94,8 @@ export default class IMouseClickDrag extends IPointing { } this.#mouseUpHandler = e => { - if (!self.exitAnyButton || e.button == self.clickButton) { - if (this.consumeEvent) { + if (!self.options.exitAnyButton || e.button == self.options.clickButton) { + if (this.options.consumeEvent) { e.stopImmediatePropagation() // Captured, don't call anyone else } // Remove the handlers of "mousemove" and "mouseup" @@ -120,7 +120,7 @@ export default class IMouseClickDrag extends IPointing { listenEvents() { this.target.addEventListener("mousedown", this.#mouseDownHandler) - if (this.clickButton == 2) { + if (this.options.clickButton == 2) { this.target.addEventListener("contextmenu", e => e.preventDefault()) } }