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.

154 lines
4.4KB

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