Input system further simplified

This commit is contained in:
barsdeveloper
2021-10-04 21:35:01 +02:00
parent 5963836947
commit 0563663dfc
5 changed files with 69 additions and 102 deletions

View File

@@ -1,6 +1,6 @@
import Utility from "../Utility"
import UPointing from "./UPointing"
export default class UMouseWheel {
export default class UMouseWheel extends UPointing {
/**
*
@@ -9,32 +9,14 @@ export default class UMouseWheel {
* @param {Object} options
*/
constructor(target, blueprint, options) {
this.target = target
this.blueprint = blueprint
super(target, blueprint, options)
this.looseTarget = options?.looseTarget ?? true
this.movementSpace = this.blueprint?.getGridDOMElement() ?? document
let self = this
this.mouseWheelHandler = function (e) {
e.preventDefault()
if (!self.looseTarget && e.target != e.currentTarget) {
return
}
let scaleCorrection = 1 / Utility.getScale(self.target)
let offset = [0, 0]
if (self.looseTarget) {
/*
* Compensating for having used the mouse wheel over a descendant of the target (the element listened for the 'wheel' event).
* We are interested to get the location relative to the listened target, not the descendant target that caused the event.
*/
const targetOffset = e.target.getBoundingClientRect()
const currentTargetOffset = e.currentTarget.getBoundingClientRect()
offset = [
e.offsetX + targetOffset.x * scaleCorrection - currentTargetOffset.x * scaleCorrection,
e.offsetY + targetOffset.y * scaleCorrection - currentTargetOffset.y * scaleCorrection
]
} // TODO else branch
self.wheel(Math.sign(e.deltaY), offset)
const location = self.getLocation(e)
self.wheel(Math.sign(e.deltaY), location)
}
this.movementSpace.addEventListener('wheel', this.mouseWheelHandler, false)
@@ -43,7 +25,7 @@ export default class UMouseWheel {
}
/* Subclasses will override the following method */
wheel(location, variation) {
wheel(variation, location) {
}
}