2017-01-11 23:17:57 -05:00
|
|
|
import Matter, { Vector, Sleeping, World, Constraint, Composite, Runner, Common, Body, Bodies, Events } from 'matter-js'
|
2017-01-12 04:00:52 -05:00
|
|
|
import { last, sortBy, values } from 'lodash'
|
2017-01-05 00:05:18 -05:00
|
|
|
|
|
|
|
import $jit from '../patched/JIT'
|
|
|
|
|
2017-01-11 23:17:57 -05:00
|
|
|
import Create from './Create'
|
2017-01-05 00:05:18 -05:00
|
|
|
import DataModel from './DataModel'
|
2017-01-12 04:00:52 -05:00
|
|
|
import Mouse from './Mouse'
|
2017-01-11 23:17:57 -05:00
|
|
|
import JIT from './JIT'
|
2017-01-05 00:05:18 -05:00
|
|
|
import Visualize from './Visualize'
|
|
|
|
|
|
|
|
const Engine = {
|
2017-01-12 04:00:52 -05:00
|
|
|
focusBody: null,
|
|
|
|
newNodeConstraint: null,
|
|
|
|
newNodeBody: Bodies.circle(Mouse.newNodeCoords.x, Mouse.newNodeCoords.y, 1),
|
2017-01-05 00:05:18 -05:00
|
|
|
init: () => {
|
|
|
|
Engine.engine = Matter.Engine.create()
|
|
|
|
Events.on(Engine.engine, 'afterUpdate', Engine.callUpdate)
|
2017-01-12 04:00:52 -05:00
|
|
|
//Engine.engine.world.gravity.scale = 0
|
|
|
|
Engine.engine.world.gravity.y = 0
|
|
|
|
Engine.engine.world.gravity.x = -1
|
|
|
|
Body.setStatic(Engine.newNodeBody, true)
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
|
|
|
run: init => {
|
2017-01-11 23:17:57 -05:00
|
|
|
if (init) {
|
2017-01-12 04:00:52 -05:00
|
|
|
World.addBody(Engine.engine.world, Engine.newNodeBody)
|
2017-01-11 23:17:57 -05:00
|
|
|
Visualize.mGraph.graph.eachNode(Engine.addNode)
|
|
|
|
DataModel.Synapses.each(s => Engine.addEdge(s.get('edge')))
|
2017-01-12 04:00:52 -05:00
|
|
|
if (Object.keys(Visualize.mGraph.graph.nodes).length) {
|
|
|
|
Engine.setFocusNode(Engine.findFocusNode(Visualize.mGraph.graph.nodes))
|
|
|
|
}
|
2017-01-11 23:17:57 -05:00
|
|
|
}
|
2017-01-05 00:05:18 -05:00
|
|
|
Engine.runner = Matter.Runner.run(Engine.engine)
|
|
|
|
},
|
|
|
|
endActiveMap: () => {
|
|
|
|
Runner.stop(Engine.runner)
|
|
|
|
Matter.Engine.clear(Engine.engine)
|
|
|
|
},
|
2017-01-11 23:17:57 -05:00
|
|
|
setNodePos: (id, x, y) => {
|
2017-01-05 00:05:18 -05:00
|
|
|
const body = Composite.get(Engine.engine.world, id, 'body')
|
|
|
|
Body.setPosition(body, { x, y })
|
2017-01-11 23:17:57 -05:00
|
|
|
Body.setVelocity(body, Vector.create(0, 0))
|
|
|
|
Body.setAngularVelocity(body, 0)
|
|
|
|
Body.setAngle(body, 0)
|
2017-01-05 00:05:18 -05:00
|
|
|
},
|
2017-01-11 23:17:57 -05:00
|
|
|
setNodeSleeping: (id, isSleeping) => {
|
|
|
|
const body = Composite.get(Engine.engine.world, id, 'body')
|
|
|
|
Sleeping.set(body, isSleeping)
|
|
|
|
if (!isSleeping) {
|
|
|
|
Body.setVelocity(body, Vector.create(0, 0))
|
|
|
|
Body.setAngularVelocity(body, 0)
|
|
|
|
Body.setAngle(body, 0)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
addNode: node => {
|
|
|
|
let body = Bodies.circle(node.pos.x, node.pos.y, 100)
|
|
|
|
body.node_id = node.id
|
|
|
|
node.setData('body_id', body.id)
|
2017-01-05 00:05:18 -05:00
|
|
|
World.addBody(Engine.engine.world, body)
|
|
|
|
},
|
2017-01-11 23:17:57 -05:00
|
|
|
removeNode: node => {
|
|
|
|
|
2017-01-12 04:00:52 -05:00
|
|
|
},
|
|
|
|
findFocusNode: nodes => {
|
|
|
|
return last(sortBy(values(nodes), n => new Date(n.getData('topic').get('created_at'))))
|
|
|
|
},
|
|
|
|
setFocusNode: node => {
|
|
|
|
Create.newSynapse.focusNode = node
|
|
|
|
const body = Composite.get(Engine.engine.world, node.getData('body_id'), 'body')
|
|
|
|
Engine.focusBody = body
|
|
|
|
let constraint
|
|
|
|
if (Engine.newNodeConstraint) {
|
|
|
|
Engine.newNodeConstraint.bodyA = body
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
constraint = Constraint.create({
|
|
|
|
bodyA: body,
|
|
|
|
bodyB: Engine.newNodeBody,
|
|
|
|
length: JIT.ForceDirected.graphSettings.levelDistance,
|
|
|
|
stiffness: 0.2
|
|
|
|
})
|
|
|
|
World.addConstraint(Engine.engine.world, constraint)
|
|
|
|
Engine.newNodeConstraint = constraint
|
|
|
|
}
|
2017-01-11 23:17:57 -05:00
|
|
|
},
|
|
|
|
addEdge: edge => {
|
|
|
|
const bodyA = Composite.get(Engine.engine.world, edge.nodeFrom.getData('body_id'), 'body')
|
|
|
|
const bodyB = Composite.get(Engine.engine.world, edge.nodeTo.getData('body_id'), 'body')
|
|
|
|
let constraint = Constraint.create({
|
|
|
|
bodyA,
|
|
|
|
bodyB,
|
|
|
|
length: JIT.ForceDirected.graphSettings.levelDistance,
|
|
|
|
stiffness: 0.2
|
|
|
|
})
|
|
|
|
edge.setData('constraint_id', constraint.id)
|
|
|
|
World.addConstraint(Engine.engine.world, constraint)
|
|
|
|
},
|
|
|
|
removeEdge: synapse => {
|
2017-01-05 00:05:18 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
callUpdate: () => {
|
|
|
|
Engine.engine.world.bodies.forEach(b => {
|
2017-01-11 23:17:57 -05:00
|
|
|
const node = Visualize.mGraph.graph.getNode(b.node_id)
|
2017-01-05 00:05:18 -05:00
|
|
|
const newPos = new $jit.Complex(b.position.x, b.position.y)
|
2017-01-11 23:17:57 -05:00
|
|
|
node && node.setPos(newPos, 'current')
|
2017-01-05 00:05:18 -05:00
|
|
|
})
|
2017-01-12 04:00:52 -05:00
|
|
|
if (Engine.focusBody) Mouse.focusNodeCoords = Engine.focusBody.position
|
2017-01-11 23:17:57 -05:00
|
|
|
Create.newSynapse.updateForm()
|
2017-01-12 04:00:52 -05:00
|
|
|
Create.newTopic.position()
|
2017-01-05 00:05:18 -05:00
|
|
|
Visualize.mGraph.plot()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Engine
|