Fixing linked knot

This commit is contained in:
barsdeveloper
2022-11-14 20:46:27 +01:00
parent 92e4735d47
commit decef44d02
14 changed files with 142 additions and 68 deletions

View File

@@ -90,7 +90,6 @@ export default class IKeyboardShortcut extends IInput {
document.addEventListener("keydown", this.keyDownHandler)
}
}
}
listenEvents() {

View File

@@ -1,7 +1,11 @@
import Configuration from "../../Configuration"
import IMouseClickDrag from "./IMouseClickDrag"
import LinkElement from "../../element/LinkElement"
/** @typedef {import("../../element/PinElement").default} PinElement */
/**
* @typedef {import("../../element/PinElement").default} PinElement
* @typedef {import("../../template/KnotNodeTemplate").default} KnotNodeTemplate
*/
/** @extends IMouseClickDrag<PinElement> */
export default class MouseCreateLink extends IMouseClickDrag {
@@ -9,15 +13,25 @@ export default class MouseCreateLink extends IMouseClickDrag {
/** @type {NodeListOf<PinElement>} */
#listenedPins
/** @type {PinElement} */
#knotPin = null
#mouseenterHandler =
/** @param {MouseEvent} e */
e => {
if (!this.enteredPin) {
this.linkValid = false
this.enteredPin = /** @type {PinElement} */ (e.target)
const a = this.enteredPin
const b = this.target
if (a.getNodeElement() == b.getNodeElement()) {
const a = this.link.sourcePin ?? this.target // Remember target might have change
const b = this.enteredPin
if (
a.nodeElement.getType() == Configuration.knotNodeTypeName
|| b.nodeElement.getType() == Configuration.knotNodeTypeName
) {
// A knot can be linked to any pin, it doesn't matter the type or input/output direction
this.link.setMessageCorrect()
this.linkValid = true
} else if (a.getNodeElement() == b.getNodeElement()) {
this.link.setMessageSameNode()
} else if (a.isOutput() == b.isOutput()) {
this.link.setMessageDirectionsIncompatible()
@@ -52,6 +66,9 @@ export default class MouseCreateLink extends IMouseClickDrag {
linkValid = false
startDrag(location) {
if (this.target.nodeElement.getType() == Configuration.knotNodeTypeName) {
this.#knotPin = this.target
}
this.link = new LinkElement(this.target, null)
this.blueprint.linksContainerElement.prepend(this.link)
this.link.setMessagePlaceNode()
@@ -77,6 +94,20 @@ export default class MouseCreateLink extends IMouseClickDrag {
pin.removeEventListener("mouseleave", this.#mouseleaveHandler)
})
if (this.enteredPin && this.linkValid) {
if (this.#knotPin) {
const otherPin = this.#knotPin !== this.link.sourcePin ? this.link.sourcePin : this.enteredPin
// Knot pin direction correction
if (this.#knotPin.isInput() && otherPin.isInput() || this.#knotPin.isOutput() && otherPin.isOutput()) {
const oppositePin = this.#knotPin.isInput()
?/** @type {KnotNodeTemplate} */(this.#knotPin.nodeElement.template).outputPin
:/** @type {KnotNodeTemplate} */(this.#knotPin.nodeElement.template).inputPin
if (this.#knotPin === this.link.sourcePin) {
this.link.sourcePin = oppositePin
} else {
this.enteredPin = oppositePin
}
}
}
this.blueprint.addGraphElement(this.link)
this.link.destinationPin = this.enteredPin
this.link.removeMessage()