Skip to content
Snippets Groups Projects
ConnectionsLayer.vue 3.67 KiB
Newer Older
<template>
  <div class="connections">
Adam Procter's avatar
Adam Procter committed
    <canvas ref="pixi" id="pixi"></canvas>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import * as PIXI from 'pixi.js'
//var initialMoveTo
Adam Procter's avatar
Adam Procter committed

export default {
  name: 'ConnectionsLayer',
  computed: mapState({
Adam Procter's avatar
Adam Procter committed
    configConnections: (state) => state.configConnections,
  }),
Adam Procter's avatar
Adam Procter committed

  watch: {
    configConnections: {
      deep: true,

      handler() {
        this.drawPixi()
      },
    },
  },

Adam Procter's avatar
Adam Procter committed
  methods: {
    drawPixi() {
Adam Procter's avatar
Adam Procter committed
      var i
Adam Procter's avatar
Adam Procter committed
      this.canvas = this.$refs.pixi
      const stage = this.PIXIApp.stage
Adam Procter's avatar
Adam Procter committed
      let graphics = new PIXI.Graphics()
      //let line = new PIXI.Graphics()

Adam Procter's avatar
Adam Procter committed
      graphics.lineStyle(8, 0xcab6ff)

      // FIXME: removes connections probably need to be own function
      // for (i = 0; i < Object.keys(this.configConnections).length; i++) {
      //   // static circle - needs to be same place as buttons
      //   graphics.lineStyle(0)
      //   graphics.beginFill(0xde3249, 1)
      //   graphics.drawCircle(100, 250, 50)
      //   graphics.endFill()
      //   stage.addChild(graphics)
      // }

Adam Procter's avatar
Adam Procter committed
      for (i = 0; i < Object.keys(this.configConnections).length; i++) {
        //start
        graphics.moveTo(
          this.configConnections[i].x_pos_start,
          this.configConnections[i].y_pos_start
        )
        //end
        graphics.lineTo(
          this.configConnections[i].x_pos_end,
          this.configConnections[i].y_pos_end
        )
      }
Adam Procter's avatar
Adam Procter committed
      for (var j = stage.children.length - 1; j >= 0; j--) {
        stage.removeChild(stage.children[j])
      }
Adam Procter's avatar
Adam Procter committed
      stage.addChild(graphics)
Adam Procter's avatar
Adam Procter committed
    },

    //FIXME : this is a sketch of the final code for connection buttons
    // Eventually can move this all from nodes layers as well (which make sense)
    connectingDraw() {
      var i
      this.canvas = this.$refs.pixi
      const stage = this.PIXIApp.stage
      let circle = new PIXI.Graphics()
      let line = new PIXI.Graphics()

      circle.lineStyle(8, 0xcab6ff)

      for (i = 0; i < Object.keys(this.configConnections).length; i++) {
        // static circle - needs to be same place as buttons
        circle.lineStyle(0)
        circle.beginFill(0xde3249, 1)
        circle.drawCircle(100, 250, 50)
        circle.endFill()
        stage.addChild(circle)
      }

      // connection on move
      var initialMoveTo
      // Opt-in to interactivity
      circle.interactive = true

      // Shows hand cursor
      circle.buttonMode = true

      circle
        .on('pointerdown', onDragStart)
        .on('pointerup', onDragEnd)
        .on('pointerupoutside', onDragEnd)
        .on('pointermove', onDragMove)

      let lines = []

      function onDragStart(event) {
        this.dragging = true

        let mouseX = event.data.global.x
        let mouseY = event.data.global.y

        initialMoveTo = [mouseX, mouseY]

        line.lineStyle(8, 0xcab6ff)
        line.moveTo(mouseX, mouseY)

        lines = [line].concat(lines)

        stage.addChild(line)
      }

      function onDragEnd() {
        this.dragging = false
        stage.removeChild(line)
      }

      function onDragMove(event) {
        if (this.dragging) {
          let mouseX = event.data.global.x
          let mouseY = event.data.global.y
          lines[0].clear()
          lines[0].lineStyle(8, 0xcab6ff)
          lines[0].moveTo(initialMoveTo[0], initialMoveTo[1])
          lines[0].lineTo(mouseX, mouseY)
        }
      }
    },
Adam Procter's avatar
Adam Procter committed
  },
  mounted() {
Adam Procter's avatar
Adam Procter committed
    const canvas = this.$refs.pixi
    this.PIXIApp = new PIXI.Application({
      width: window.innerWidth,
      height: window.innerHeight,
      antialias: true,
      transparent: true,
      view: canvas,
    })
Adam Procter's avatar
Adam Procter committed
    this.drawPixi()
    this.connectingDraw()
Adam Procter's avatar
Adam Procter committed
  },
}
</script>