metamaps--metamaps/frontend/src/Metamaps/Engine.js

121 lines
3.9 KiB
JavaScript
Raw Normal View History

2017-01-11 23:17:57 -05:00
import Matter, { Vector, Sleeping, World, Constraint, Composite, Runner, Common, Body, Bodies, Events } from 'matter-js'
import { last, sortBy, values } from 'lodash'
2017-01-05 00:05:18 -05:00
import $jit from '../patched/JIT'
import Active from './Active'
2017-01-11 23:17:57 -05:00
import Create from './Create'
2017-01-05 00:05:18 -05:00
import DataModel from './DataModel'
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 = {
focusBody: null,
newNodeConstraint: null,
newNodeBody: Bodies.circle(Mouse.newNodeCoords.x, Mouse.newNodeCoords.y, 1),
init: (serverData) => {
2017-01-05 00:05:18 -05:00
Engine.engine = Matter.Engine.create()
Events.on(Engine.engine, 'afterUpdate', Engine.callUpdate)
if (!serverData.ActiveMapper) Engine.engine.world.gravity.scale = 0
else {
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) {
if (Active.Mapper) 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')))
if (Active.Mapper && 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: () => {
2017-01-12 14:11:01 -05:00
Engine.runner && Runner.stop(Engine.runner)
2017-01-05 00:05:18 -05:00
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 => {
},
findFocusNode: nodes => {
return last(sortBy(values(nodes), n => new Date(n.getData('topic').get('created_at'))))
},
setFocusNode: node => {
if (!Active.Mapper) return
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
})
if (Active.Mapper) {
if (Engine.focusBody) Mouse.focusNodeCoords = Engine.focusBody.position
Create.newSynapse.updateForm()
Create.newTopic.position()
}
2017-01-05 00:05:18 -05:00
Visualize.mGraph.plot()
}
}
export default Engine