|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- var editor = {};
- var pi2 = Math.PI / 2;
-
- editor.selection = undefined;
-
- editor.planes = [
- new THREE.Plane(new THREE.Vector3(0, 0, 1), 0),
- new THREE.Plane(new THREE.Vector3(0, 1, 0), 0),
- new THREE.Plane(new THREE.Vector3(1, 0, 0), 0)
- ];
- editor.orientation = 0;
- editor.plane = editor.planes[editor.orientation];
-
- editor.onFreeMove = function() {
- var found = editor.findNodeOnRay(mouse.ray);
- if (editor.selection !== found) {
- editor.selection = found;
- if (found) {
- gui.nodeMessage("Node " + found + " (VOP:" + abj.node[found].vop + ")" +
- "<br/>" + "Click to edit neighbourhood");
- } else {
- gui.hideNodeMessage();
- }
- }
- };
-
- editor.focus = function(node) {
- editor.grid.position.copy(abj.node[node].position);
- gui.controls.target.copy(abj.node[node].position);
- gui.hideNodeMessage();
- editor.selection = node;
- gui.serverMessage("Selected node " + node + ".");
- };
-
- editor.addQubitAtPoint = function(point) {
- if (point === null) {
- return;
- }
- point.round();
-
- // Check for clashes
- for (var node in abj.node) {
- var delta = new THREE.Vector3();
- delta.subVectors(abj.node[node].position, point);
- if (delta.length()<0.1){
- editor.focus(node);
- return;
- }
- }
-
- abj.add_node(abj.order(), { position: point });
- editor.grid.position.copy(point);
- gui.controls.target.copy(point);
- graph.update();
- gui.serverMessage("Created node.");
- };
-
- editor.onClick = function() {
- var found = editor.findNodeOnRay(mouse.ray);
- if (found) {
- editor.focus(found);
- } else {
- var intersection = mouse.ray.intersectPlane(editor.plane);
- if (intersection !== null) {
- editor.addQubitAtPoint(intersection);
- }
- }
- };
-
- editor.prepare = function() {
- mouse.onFreeMove = editor.onFreeMove;
- mouse.onClick = editor.onClick;
- document.addEventListener("keydown", editor.onKey, false);
- editor.makeGrid();
- };
-
- editor.onKey = function(evt) {
- if (evt.keyCode == 32) {
- editor.grid.rotation.x += Math.PI / 2;
- editor.orientation = (editor.orientation + 1) % 3;
- console.log(editor.orientation);
- var m = editor.orientations[editor.orientation];
- editor.plane.applyMatrix4(m);
- console.log(m);
- editor.grid.matrix = m;
- gui.render();
- gui.serverMessage("Rotated into the XY plane or whatever");
- }
- };
-
- editor.makeGrid = function() {
- editor.grid = new THREE.GridHelper(10, 1);
- editor.grid.rotation.x = Math.PI / 2;
- editor.grid.setColors(0xbbbbbb, 0xeeeeee);
- editor.grid.matrixAutoUpdate = true;
- gui.scene.add(editor.grid);
- };
-
- editor.update = function() {};
-
- // Gets a reference to the node nearest to the mouse cursor
- editor.findNodeOnRay = function(ray) {
- for (var n in abj.node) {
- if (ray.distanceSqToPoint(abj.node[n].position) < 0.3) {
- return n;
- }
- }
- return undefined;
- };
|