Large mouse events refactoring and cleanup

This commit is contained in:
barsdeveloper
2021-10-04 00:32:22 +02:00
parent dbff7a2c5f
commit fece3da438
17 changed files with 545 additions and 414 deletions

38
js/input/UDrag.js Normal file
View File

@@ -0,0 +1,38 @@
import UMouseClickDrag from "./UMouseClickDrag"
export default class UDrag extends UMouseClickDrag {
constructor(target, blueprint, options) {
super(target, blueprint, options)
this.stepSize = options?.stepSize
this.mousePosition = [0, 0]
}
snapToGrid(posX, posY) {
return [
this.stepSize * Math.round(posX / this.stepSize),
this.stepSize * Math.round(posY / this.stepSize)
]
}
startDrag(e) {
if (!this.stepSize) {
this.stepSize = parseInt(getComputedStyle(this.target).getPropertyValue('--ueb-grid-snap'))
}
// Get the current mouse position
this.mousePosition = this.snapToGrid(e.clientX, e.clientY)
}
dragTo(e) {
let mousePosition = this.snapToGrid(e.clientX, e.clientY)
const d = [mousePosition[0] - this.mousePosition[0], mousePosition[1] - this.mousePosition[1]]
if (d[0] == 0 && d[1] == 0) {
return
}
this.target.addLocation(d)
// Reassign the position of mouse
this.mousePosition = mousePosition
}
}