Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.5KB

  1. var editor = {};
  2. var pi2 = Math.PI / 2;
  3. editor.selection = undefined;
  4. editor.mouseOver = undefined;
  5. editor.orientations = [
  6. new THREE.Euler(pi2, 0, 0),
  7. new THREE.Euler(0, 0, 0),
  8. new THREE.Euler(pi2, 0, pi2),
  9. ];
  10. editor.onFreeMove = function() {
  11. var found = editor.findNodeOnRay(mouse.ray);
  12. if (editor.mouseOver !== found) {
  13. editor.mouseOver = found;
  14. if (found) {
  15. var n = abj.node[found];
  16. var s = "Node " + found + "<br/> ";
  17. for (var i in n) {
  18. if (i!="position"){
  19. s += i + ":" + n[i] + " ";
  20. }
  21. }
  22. s += "";
  23. gui.nodeMessage(s);
  24. } else {
  25. gui.hideNodeMessage();
  26. }
  27. }
  28. };
  29. editor.focus = function(node) {
  30. gui.hideNodeMessage();
  31. editor.selection = node;
  32. //gui.serverMessage("Selected node " + node + ".");
  33. };
  34. editor.addQubitAtPoint = function(point) {
  35. if (point === null) {
  36. return;
  37. }
  38. point.round();
  39. var new_node = Math.floor(point.x) + "," + Math.floor(point.y) + "," + Math.floor(point.z);
  40. websocket.edit({action:"create", name:new_node, position: point});
  41. editor.focus(new_node);
  42. gui.serverMessage("Created node " + new_node +".");
  43. };
  44. editor.onClick = function() {
  45. var found = editor.findNodeOnRay(mouse.ray);
  46. if (found) {
  47. editor.focus(found);
  48. var node=found;
  49. editor.grid.position.copy(abj.node[node].position);
  50. gui.controls.target.copy(abj.node[node].position);
  51. node_name.innerHTML = "Node " + node;
  52. node_data.className = "visible";
  53. node_vop.innerHTML = "VOP: " + abj.node[node].vop;
  54. } else {
  55. var intersection = mouse.ray.intersectPlane(editor.plane);
  56. if (intersection !== null) {
  57. editor.addQubitAtPoint(intersection);
  58. }
  59. }
  60. };
  61. editor.onShiftClick = function() {
  62. var found = editor.findNodeOnRay(mouse.ray);
  63. if (found === undefined){ return; }
  64. if (editor.selection === undefined){ return; }
  65. if (found === editor.selection){ return; }
  66. //abj.act_cz(found, editor.selection);
  67. websocket.edit({action:"cz", start:found, end:editor.selection});
  68. gui.serverMessage("Acted CZ between " + found + " & " + editor.selection + ".");
  69. editor.focus(found);
  70. graph.update();
  71. };
  72. editor.onCtrlClick = function() {
  73. var found = editor.findNodeOnRay(mouse.ray);
  74. if (found === undefined){ return; }
  75. if (editor.selection === undefined){ return; }
  76. editor.focus(found);
  77. websocket.edit({action:"hadamard", node:found});
  78. gui.serverMessage("Acted H on node " + found + ".");
  79. graph.update();
  80. };
  81. editor.prepare = function() {
  82. mouse.onFreeMove = editor.onFreeMove;
  83. mouse.onClick = editor.onClick;
  84. mouse.onShiftClick = editor.onShiftClick;
  85. mouse.onCtrlClick = editor.onCtrlClick;
  86. document.addEventListener("keydown", editor.onKey, false);
  87. editor.makeGrid();
  88. };
  89. editor.onKey = function(evt) {
  90. if (evt.keyCode === 32) {
  91. editor.setOrientation((editor.orientation + 1) % 3);
  92. }
  93. if (evt.keyCode === 46 || evt.keyCode === 68) {
  94. editor.deleteNode();
  95. }
  96. };
  97. editor.setOrientation = function(orientation) {
  98. editor.orientation = orientation;
  99. var rotation = editor.orientations[orientation];
  100. var normal = new THREE.Vector3(0, 1, 0);
  101. normal.applyEuler(rotation);
  102. editor.grid.rotation.copy(rotation);
  103. editor.plane = new THREE.Plane();
  104. editor.plane.setFromNormalAndCoplanarPoint(normal, editor.grid.position);
  105. gui.render();
  106. };
  107. editor.makeGrid = function() {
  108. editor.grid = new THREE.GridHelper(10, 1);
  109. editor.grid.setColors(0xbbbbbb, 0xeeeeee);
  110. editor.setOrientation(0);
  111. gui.scene.add(editor.grid);
  112. };
  113. editor.update = function() {};
  114. // Gets a reference to the node nearest to the mouse cursor
  115. editor.findNodeOnRay = function(ray) {
  116. for (var n in abj.node) {
  117. if (ray.distanceSqToPoint(abj.node[n].position) < 0.012) {
  118. return n;
  119. }
  120. }
  121. return undefined;
  122. };
  123. editor.deleteNode = function() {
  124. if (editor.selection === undefined){ return; }
  125. websocket.edit({action:"delete", node:editor.selection});
  126. graph.update();
  127. gui.serverMessage("Deleted node " + editor.selection + ".");
  128. editor.selection = undefined;
  129. node_data.className = "hidden";
  130. };
  131. editor.localComplementation = function() {
  132. if (editor.selection === undefined){ return; }
  133. abj.local_complementation(editor.selection);
  134. graph.update();
  135. gui.serverMessage("Inverted neighbourhood of " + editor.selection + ".");
  136. };