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.

152 lines
4.3KB

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