Various improvements

This commit is contained in:
barsdeveloper
2022-02-13 23:04:07 +01:00
parent 7cf556d89d
commit a0fcc23e31
16 changed files with 355 additions and 92 deletions

View File

@@ -7,8 +7,8 @@ export default class Context {
this.blueprint = blueprint
this.options = options
let self = this
this.blueprintFocusHandler = _ => self.blueprintFocused()
this.blueprintUnfocusHandler = _ => self.blueprintUnfocused()
this.blueprintFocusHandler = _ => self.listenEvents()
this.blueprintUnfocusHandler = _ => self.unlistenEvents()
if (options?.wantsFocusCallback ?? false) {
this.blueprint.addEventListener("blueprint-focus", this.blueprintFocusHandler)
this.blueprint.addEventListener("blueprint-unfocus", this.blueprintUnfocusHandler)
@@ -16,16 +16,16 @@ export default class Context {
}
unlistenDOMElement() {
this.blueprintUnfocused()
this.unlistenEvents()
this.blueprint.removeEventListener("blueprint-focus", this.blueprintFocusHandler)
this.blueprint.removeEventListener("blueprint-unfocus", this.blueprintUnfocusHandler)
}
/* Subclasses will probabily override the following methods */
blueprintFocused() {
listenEvents() {
}
blueprintUnfocused() {
unlistenEvents() {
}
}

View File

@@ -14,11 +14,11 @@ export default class Copy extends Context {
}
}
blueprintFocused() {
listenEvents() {
document.body.addEventListener("copy", this.copyHandler)
}
blueprintUnfocused() {
unlistenEvents() {
document.body.removeEventListener("copy", this.copyHandler)
}

View File

@@ -12,11 +12,11 @@ export default class Paste extends Context {
this.pasteHandle = e => self.pasted(e.clipboardData.getData("Text"))
}
blueprintFocused() {
listenEvents() {
document.body.addEventListener("paste", this.pasteHandle)
}
blueprintUnfocused() {
unlistenEvents() {
document.body.removeEventListener("paste", this.pasteHandle)
}

View File

@@ -74,11 +74,11 @@ export default class KeyboardShortcut extends Context {
return options
}
blueprintFocused() {
listenEvents() {
document.addEventListener("keydown", this.keyDownHandler)
}
blueprintUnfocused() {
unlistenEvents() {
document.removeEventListener("keydown", this.keyDownHandler)
}

View File

@@ -1,3 +1,4 @@
import Configuration from "../../Configuration"
import Pointing from "./Pointing"
/**
@@ -54,6 +55,12 @@ export default class MouseClickDrag extends Pointing {
// Do actual actions
self.startDrag()
self.started = true
const dragEvent = new CustomEvent(Configuration.trackingMouseEventName.begin, {
bubbles: true,
cancelable: true
})
this.target.dispatchEvent(dragEvent)
return true
}
this.mouseMoveHandler = e => {
@@ -62,6 +69,8 @@ export default class MouseClickDrag extends Pointing {
const location = self.locationFromEvent(e)
const movement = [e.movementX, e.movementY]
self.dragTo(location, movement)
self.blueprint.entity.mousePosition = self.locationFromEvent(e)
return true
}
this.mouseUpHandler = e => {
@@ -71,7 +80,14 @@ export default class MouseClickDrag extends Pointing {
movementListenedElement.removeEventListener("mousemove", self.mouseMoveHandler)
document.removeEventListener("mouseup", self.mouseUpHandler)
self.endDrag()
const dragEvent = new CustomEvent(Configuration.trackingMouseEventName.end, {
bubbles: true,
cancelable: true
})
this.target.dispatchEvent(dragEvent)
return true
}
return false
}
this.target.addEventListener("mousedown", this.mouseDownHandler)

View File

@@ -1,3 +1,4 @@
import Configuration from "../../Configuration"
import Pointing from "./Pointing"
export default class MouseTracking extends Pointing {
@@ -6,18 +7,43 @@ export default class MouseTracking extends Pointing {
options.wantsFocusCallback = true
super(target, blueprint, options)
this.trackingStolen = null
let self = this
this.mousemoveHandler = e => {
self.blueprint.entity.mousePosition = self.locationFromEvent(e)
return true
}
this.trackingMouseStolenHandler = e => {
this.trackingStolen = e.target
self.unlistenMouseMove()
return true
}
this.trackingMouseGaveBackHandler = e => {
if (!this.trackingStolen == e.target) {
return false
}
self.listenMouseMove()
return true
}
}
blueprintFocused() {
listenMouseMove() {
this.target.addEventListener("mousemove", this.mousemoveHandler)
}
blueprintUnfocused() {
unlistenMouseMove() {
this.target.removeEventListener("mousemove", this.mousemoveHandler)
}
listenEvents() {
this.listenMouseMove()
this.blueprint.addEventListener(Configuration.trackingMouseEventName.begin, this.trackingMouseStolenHandler)
this.blueprint.addEventListener(Configuration.trackingMouseEventName.end, this.trackingMouseGaveBackHandler)
}
unlistenEvents() {
this.unlistenMouseMove()
this.blueprint.removeEventListener(Configuration.trackingMouseEventName.begin, this.trackingMouseStolenHandler)
this.blueprint.removeEventListener(Configuration.trackingMouseEventName.end, this.trackingMouseGaveBackHandler)
}
}

View File

@@ -27,12 +27,12 @@ export default class MouseWheel extends Pointing {
}
}
blueprintFocused() {
listenEvents() {
this.movementSpace.addEventListener("wheel", this.mouseWheelHandler, false)
this.movementSpace.parentElement?.addEventListener("wheel", this.mouseParentWheelHandler)
}
blueprintUnfocused() {
unlistenEvents() {
this.movementSpace.removeEventListener("wheel", this.mouseWheelHandler, false)
this.movementSpace.parentElement?.removeEventListener("wheel", this.mouseParentWheelHandler)
}

View File

@@ -25,11 +25,11 @@ export default class Unfocus extends Context {
this.blueprint.setFocused(false)
}
blueprintFocused() {
listenEvents() {
document.addEventListener("click", this.clickHandler)
}
blueprintUnfocused() {
unlistenEvents() {
document.removeEventListener("click", this.clickHandler)
}
}