Browse Source

nodeprops improvements

luis 7 years ago
parent
commit
7af1b063fa

+ 1 - 1
browser/vue-flow/index.html

@@ -2,7 +2,7 @@
 <html lang="en">
   <head>
     <meta charset="utf-8">
-    <title>vue-svg</title>
+    <title>Flow</title>
   </head>
   <body>
     <div id="app"></div>

+ 38 - 27
browser/vue-flow/src/components/flowmanager.vue

@@ -11,7 +11,7 @@
           :key="i"
           v-bind="linkProps(l)"
         />
-        <!-- mousel link-->
+        <!-- mouse link-->
         <flow-link
           v-if="pointerLink.active"
           v-bind="pointerLink"
@@ -21,8 +21,7 @@
           v-for="(n,i) of nodeData.nodes"
           :key="'node' + n.id"
           :id="n.id"
-          :value="n"
-          :transform="nodeTransform(i)"
+          v-bind="nodeProps(n)"
           @nodePointerDown.prevent="nodeDragStart($event,i)"
           @socketPointerDown="socketPointerDown(n.id,...arguments)"
         />
@@ -49,29 +48,44 @@ export default {
   },
   data () {
     return {
-      nodeData: nodeData, // shared?
-      pointerLink: {active: false, x1: 0, y1: 0, x2: 0, y2: 0}
+      nodeData: nodeData,
+      pointerLink: {active: false, x1: 0, y1: 0, x2: 0, y2: 0},
+      hlType: ''
     }
   },
   computed: {
-    nodeTransform () {
-      return (i) => `translate(${this.nodeData.nodes[i].x} ${this.nodeData.nodes[i].y})`
+    nodeProps () {
+      return (n) => {
+        return {
+          transform: `translate(${n.x} ${n.y})`,
+          id: n.id,
+          label: n.label,
+          type: this.nodeData.registry[n.src].type,
+          inputs: this.nodeData.registry[n.src].inputs,
+          output: this.nodeData.registry[n.src].output,
+          match: {}
+        }
+      }
     },
     // computed
     linkProps () {
       return (link) => {
         if (!this.$refs.nodes) return
-        const refFrom = this.$refs.nodes.find(n => n.value.id === link.from)
-        const refTo = this.$refs.nodes.find(n => n.value.id === link.to)
+        // find Index perhaps
+        const nodeFrom = this.nodeData.nodes.find(n => n.id === link.from)
+        const nodeTo = this.nodeData.nodes.find(n => n.id === link.to)
 
-        const fromOutput = refFrom.output(0) // only 1 output
-        const toInput = refTo.input(link.in)
+        const refFrom = this.$refs.nodes.find(n => n.id === link.from)
+        const refTo = this.$refs.nodes.find(n => n.id === link.to)
 
-        const x1 = refFrom.value.x + fromOutput.cx // + inputPos.x
-        const y1 = refFrom.value.y + fromOutput.cy // + inputPos.y
+        const fromOutput = refFrom.outputPos(0) // only 1 output
+        const toInput = refTo.inputPos(link.in)
 
-        const x2 = refTo.value.x + toInput.cx
-        const y2 = refTo.value.y + toInput.cy
+        const x1 = nodeFrom.x + fromOutput.cx // + inputPos.x
+        const y1 = nodeFrom.y + fromOutput.cy // + inputPos.y
+
+        const x2 = nodeTo.x + toInput.cx
+        const y2 = nodeTo.y + toInput.cy
         // console.log('Thing:', x1, y1, x2, y2)
         return {x1, y1, x2, y2}
       }
@@ -85,27 +99,23 @@ export default {
   },
   methods: {
     socketPointerDown (nodeId, e, socket) {
-      const node = this.$refs.nodes.find(n => n.value.id === nodeId)
+      const nodeRef = this.$refs.nodes.find(n => n.id === nodeId)
+      const node = this.nodeData.nodes.find(n => n.id === nodeId)
 
-      const isInput = socket.in != undefined
-      console.log('Socket:', socket)
-      if (isInput) {
-        console.log('Socket is input')
-      }
-      const socketPos = isInput ? node.input(socket.in) : node.output(socket.out)
+      const isInput = socket.in !== undefined
+      const socketPos = isInput ? nodeRef.inputPos(socket.in) : nodeRef.outputPos(socket.out)
 
       const p = this.transformedPoint(e.x, e.y)
-
       if (isInput) {
-        this.pointerLink.x1 = node.value.x + socketPos.cx
-        this.pointerLink.y1 = node.value.y + socketPos.cy
+        this.pointerLink.x1 = node.x + socketPos.cx
+        this.pointerLink.y1 = node.y + socketPos.cy
         this.pointerLink.x2 = p.x
         this.pointerLink.y2 = p.y
       } else {
         this.pointerLink.x1 = p.x
         this.pointerLink.y1 = p.y
-        this.pointerLink.x2 = node.value.x + socketPos.cx
-        this.pointerLink.y2 = node.value.y + socketPos.cy
+        this.pointerLink.x2 = node.x + socketPos.cx
+        this.pointerLink.y2 = node.y + socketPos.cy
       }
 
       this.pointerLink.active = true
@@ -154,6 +164,7 @@ export default {
       document.addEventListener('mousemove', drag)
       document.addEventListener('mouseup', drop)
     },
+
     createSVGPoint (x, y) {
       const p = this.$refs.svg.createSVGPoint()
       p.x = x; p.y = y

+ 31 - 18
browser/vue-flow/src/components/flownode.vue

@@ -11,24 +11,26 @@
     />
     <!-- input -->
     <circle
-      class="flow-node__socket flow-node__inputs"
-      v-for="i in value.inputs"
-      v-bind="input(i-1)"
+      class="flow-node__socket flow-node__socket--inputs"
+      v-for="(inp,i) in inputs"
+      v-bind="inputPos(i)"
+      :class="{'flow-node__socket--match': inp == match.in}"
       :key="'in'+i"
-      @mousedown.stop.prevent="socketPointerDown($event, {in:i-1})"
+      @mousedown.stop.prevent="socketPointerDown($event, {in:i})"
     />
     <!-- output -->
     <circle
-      class="flow-node__socket flow-node__outputs"
+      class="flow-node__socket flow-node__socket--outputs"
       :key="'out'+0"
-      v-bind="output(0)"
+      :class="{ 'flow-node__socket--match': output == match.out}"
+      v-bind="outputPos(0)"
       @mousedown.stop.prevent="socketPointerDown($event, {out:0})"
     />
     <text
       ref="label"
       class="flow-node__label"
       v-bind="labelProps">
-      {{ value.label }}
+      {{ label }}
     </text>
   </g>
 </template>
@@ -38,7 +40,13 @@
 export default {
   name: 'FlowNode',
   props: {
-    'value': {type: Object, default: () => {}}
+    'id': {type: String, required: true},
+    'label': {type: String, default: ''},
+    'type': {type: String, default: ''},
+    'inputs': {type: Array, default: () => []},
+    'output': {type: String, default: ''},
+    'match': {type: Object, default: () => {}}
+    // 'value': {type: Object, default: () => {}}
   },
   data () {
     return {
@@ -50,28 +58,32 @@ export default {
   },
   computed: {
     labelProps () {
+      console.log('match:', this.match)
       return {x: -this.labelRect.width / 2, y: 5}
     },
     bodyProps () {
       const width = this.labelRect.width + 20
       const rect = {x: -width / 2, y: -50, width: width, height: 100}
-      if (this.value.type === 'circle') {
+      if (this.type === 'circle') {
         rect.rx = width
         rect.ry = width
       }
       return rect
     },
-    input () {
+
+    // Do this else where
+    inputPos () {
       return (i) => {
-        if (this.value.inputs === 0) return {}
-        const d = this.bodyProps.height / (this.value.inputs * 2)
+        const ilen = this.inputs.length
+        if (ilen === 0) return {}
+        const d = this.bodyProps.height / (ilen * 2)
         return {
           cx: this.bodyProps.x,
           cy: this.bodyProps.y + d + (i * 2 * d)
         }
       }
     },
-    output () {
+    outputPos () {
       return (i) => {
         const rect = this.bodyProps
         return {cx: rect.x + rect.width, cy: 0, r: 10}
@@ -81,7 +93,7 @@ export default {
   },
   watch: {
     // the only thing now that affects geometry
-    'value.label' () {
+    'label' () {
       this.$nextTick(() => {
         this.labelRect = this.$refs.label.getBBox()
       })
@@ -89,14 +101,14 @@ export default {
   },
   mounted () {
     // After render
-    this.labelRect = this.$refs.label.getBBox()
     this.$nextTick(() => { // after mount we reupdate with new values
+      if (!this.$refs.label) return
+      this.labelRect = this.$refs.label.getBBox()
       this.$forceUpdate()
     })
   },
   methods: {
     socketPointerDown (e, socket) {
-      console.log('Socket click:', e, socket)
       this.$emit('socketPointerDown', e, socket)
     }
 
@@ -122,8 +134,6 @@ export default {
   //stroke-width:4;
   transition: all 1s;
 }
-.flow-node {
-}
 .flow-node.flow-node--dragging {
 }
 .flow-node__socket {
@@ -144,4 +154,7 @@ export default {
   pointer-events:none;
   user-select:none;
 }
+.flownode__socket--match {
+  fill:#0F0;
+}
 </style>

+ 1 - 1
browser/vue-flow/src/components/flowpanzoom.vue

@@ -63,7 +63,7 @@ export default {
     },
     wheel (e) {
       e.preventDefault()
-      const svgRect = this.$el.getBoundingClientRect()
+      const svgRect = this.$refs.transformer.getBoundingClientRect()
       const oX = e.clientX - svgRect.x
       const oY = e.clientY - svgRect.y
       const z = this.zoom - (e.deltaY * 0.001 * this.zoom)

+ 18 - 13
browser/vue-flow/src/components/nodedata.js

@@ -1,20 +1,25 @@
 // Document shared state
-export default {
-  panzoom: {x: 0, y: 0, zoom: 1}, // or doc transform?
-  defaults: {
-    nodeHeight: 100
+export default{
+  panzoom: { x: 0, y: 0, zoom: 1 },
+  defaults: { nodeHeight: 100 },
+  registry: {
+    'MatMul': { inputs: [ '[]float32', '[]float32' ], output: '[]float32' },
+    'Weights': { inputs: [], output: '[]float32' },
+    'Input': { inputs: [], output: '[]float32' },
+    'Activator': { inputs: [ '[]float32' ], output: '[]float32', type: 'circle' },
+    'R': { inputs: [ '[]float32', '[]float32' ], output: 'string ' }
   },
   nodes: [
-    {id: '1', x: 100, y: 100, label: 'Test input random', inputs: 2},
-    {id: '2', x: 100, y: 300, label: 'Sigmoid', inputs: 1, type: 'circle'},
-    {id: '3', x: 300, y: 400, label: 'MatMul', inputs: 2},
-    {id: '4', x: 250, y: 300, label: 'Input', inputs: 0},
-    {id: '5', x: 700, y: 300, label: 'Weights', inputs: 0}
+    { id: '1', x: 976, y: 163, label: 'Test input random', src: 'R' },
+    { id: '2', x: 702, y: 190, label: 'Sigmoid', src: 'Activator' },
+    { id: '3', x: 541, y: 195, label: 'MatMul', src: 'MatMul' },
+    { id: '4', x: 320, y: 249, label: 'Input', src: 'Input' },
+    { id: '5', x: 347, y: 90, label: 'Weights', src: 'Weights' }
   ],
   links: [
-    {from: '4', to: '3', in: 1, link: {}},
-    {from: '5', to: '3', in: 0, link: {}},
-    {from: '3', to: '2', in: 0, link: {}},
-    {from: '2', to: '1', in: 0, link: {}}
+    { from: '4', to: '3', in: 1 },
+    { from: '5', to: '3', in: 0 },
+    { from: '3', to: '2', in: 0 },
+    { from: '2', to: '1', in: 0 }
   ]
 }