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.2KB

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