From 11df9853d0f86a9230ae5bcecbca9f26b956f360 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Sun, 22 Aug 2021 01:50:03 -0500 Subject: [PATCH] Implemented selecting nodes and running immediate actions --- src/components/board/Board.vue | 76 +++++++++------- src/components/board/BoardNode.vue | 137 +++++++++++++++++++++++------ src/data/layers/Main.vue | 80 +++++++++++++++++ src/data/layers/main.ts | 61 +++++++++---- src/game/gameLoop.ts | 8 +- src/game/layers.ts | 29 +++++- src/typings/component.d.ts | 4 +- src/typings/features/board.d.ts | 19 +++- src/typings/player.d.ts | 4 +- src/util/layers.ts | 20 +++-- src/util/proxies.ts | 9 +- src/util/vue.ts | 2 +- 12 files changed, 346 insertions(+), 103 deletions(-) create mode 100644 src/data/layers/Main.vue diff --git a/src/components/board/Board.vue b/src/components/board/Board.vue index b904f8e..78d6ca0 100644 --- a/src/components/board/Board.vue +++ b/src/components/board/Board.vue @@ -6,6 +6,7 @@ ref="stage" @init="onInit" @mousemove="drag" + @mousedown="deselect" @mouseup="() => endDragging(dragging)" @mouseleave="() => endDragging(dragging)" > @@ -18,18 +19,9 @@ :nodeType="board.types[node.type]" :dragging="draggingNode" :dragged="dragged" + :hasDragged="hasDragged" :receivingNode="receivingNode?.id === node.id" - @startDragging="startDragging" - @endDragging="endDragging" - /> - @@ -51,11 +43,13 @@ export default defineComponent({ return { lastMousePosition: { x: 0, y: 0 }, dragged: { x: 0, y: 0 }, - dragging: null + dragging: null, + hasDragged: false } as { lastMousePosition: { x: number; y: number }; dragged: { x: number; y: number }; dragging: string | null; + hasDragged: boolean; }; }, props: { @@ -79,14 +73,15 @@ export default defineComponent({ ]; }, draggingNode() { - return this.dragging - ? player.layers[this.layer].boards[this.id].find(node => node.id === this.dragging) - : null; + return this.dragging ? this.board.nodes.find(node => node.id === this.dragging) : null; }, nodes() { - return player.layers[this.layer].boards[this.id].filter( - node => node !== this.draggingNode - ); + const nodes = this.board.nodes.slice(); + if (this.draggingNode) { + const draggingNode = nodes.splice(nodes.indexOf(this.draggingNode), 1)[0]; + nodes.push(draggingNode); + } + return nodes; }, receivingNode(): BoardNode | null { if (this.draggingNode == null) { @@ -99,6 +94,9 @@ export default defineComponent({ }; let smallestDistance = Number.MAX_VALUE; return this.nodes.reduce((smallest: BoardNode | null, curr: BoardNode) => { + if (curr.id === this.draggingNode!.id) { + return smallest; + } const nodeType = this.board.types[curr.type]; const canAccept = typeof nodeType.canAccept === "boolean" @@ -126,10 +124,14 @@ export default defineComponent({ getZoomLevel(): number { return (this.$refs.stage as any).$panZoomInstance.getTransform().scale; }, - onInit: function(panzoomInstance: any) { + onInit(panzoomInstance: any) { panzoomInstance.setTransformOrigin(null); }, - startDragging(e: MouseEvent, nodeID: string) { + deselect() { + player.layers[this.layer].boards[this.id].selectedNode = null; + player.layers[this.layer].boards[this.id].selectedAction = null; + }, + mouseDown(e: MouseEvent, nodeID: string, draggable: boolean) { if (this.dragging == null) { e.preventDefault(); e.stopPropagation(); @@ -139,33 +141,43 @@ export default defineComponent({ y: e.clientY }; this.dragged = { x: 0, y: 0 }; + this.hasDragged = false; - this.dragging = nodeID; + if (draggable) { + this.dragging = nodeID; + } } + player.layers[this.layer].boards[this.id].selectedNode = null; + player.layers[this.layer].boards[this.id].selectedAction = null; }, drag(e: MouseEvent) { + const zoom = (this.getZoomLevel as () => number)(); + this.dragged = { + x: this.dragged.x + (e.clientX - this.lastMousePosition.x) / zoom, + y: this.dragged.y + (e.clientY - this.lastMousePosition.y) / zoom + }; + this.lastMousePosition = { + x: e.clientX, + y: e.clientY + }; + + if (Math.abs(this.dragged.x) > 10 || Math.abs(this.dragged.y) > 10) { + this.hasDragged = true; + } + if (this.dragging) { e.preventDefault(); e.stopPropagation(); - - const zoom = (this.getZoomLevel as () => number)(); - this.dragged = { - x: this.dragged.x + (e.clientX - this.lastMousePosition.x) / zoom, - y: this.dragged.y + (e.clientY - this.lastMousePosition.y) / zoom - } - this.lastMousePosition = { - x: e.clientX, - y: e.clientY - }; } }, endDragging(nodeID: string | null) { if (this.dragging != null && this.dragging === nodeID) { - const nodes = player.layers[this.layer].boards[this.id]; const draggingNode = this.draggingNode!; const receivingNode = this.receivingNode; draggingNode.position.x += Math.round(this.dragged.x / 25) * 25; draggingNode.position.y += Math.round(this.dragged.y / 25) * 25; + + const nodes = this.board.nodes; nodes.splice(nodes.indexOf(draggingNode), 1); nodes.push(draggingNode); diff --git a/src/components/board/BoardNode.vue b/src/components/board/BoardNode.vue index 93cd9c4..c4d5746 100644 --- a/src/components/board/BoardNode.vue +++ b/src/components/board/BoardNode.vue @@ -1,13 +1,38 @@