Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

78 lines
2.4KB

  1. var editor = {};
  2. editor.nearest = undefined;
  3. editor.onFreeMove = function() {
  4. var n = editor.nearestNode(mouse.ray);
  5. if (editor.nearest !== n) {
  6. editor.nearest = n;
  7. if (n) {
  8. gui.nodeMessage("Node " + n + " (VOP:" + abj.vops[n] + ")" +
  9. "<br/>" + "Click to edit neighbourhood");
  10. } else {
  11. gui.hideNodeMessage();
  12. }
  13. }
  14. };
  15. editor.onClick = function() {
  16. var n = editor.nearestNode(mouse.ray);
  17. if (n){
  18. var p = abj.meta[n].position;
  19. editor.grid.position.set(p.x, p.y, p.z);
  20. gui.controls.target.set(p.x, p.y, p.z);
  21. gui.hideNodeMessage();
  22. editor.nearest = undefined;
  23. gui.serverMessage("Selected node " + n + "");
  24. } else {
  25. //TODO: ghastly
  26. var intersection = mouse.ray.intersectPlane(editor.plane);
  27. intersection.x = Math.round(intersection.x, 0);
  28. intersection.y = Math.round(intersection.y, 0);
  29. intersection.z = Math.round(intersection.z, 0);
  30. var newNode = abj.order();
  31. abj.add_node(newNode, {position:intersection});
  32. editor.grid.position.set(intersection.x, intersection.y, intersection.z);
  33. gui.controls.target.set(intersection.x, intersection.y, intersection.z);
  34. graph.update();
  35. gui.serverMessage("Created node " + newNode + " at ");
  36. }
  37. };
  38. editor.prepare = function() {
  39. mouse.onFreeMove = editor.onFreeMove;
  40. mouse.onClick = editor.onClick;
  41. document.addEventListener("keydown", editor.onKey, false);
  42. editor.makeGrid();
  43. };
  44. editor.onKey = function(evt){
  45. if (evt.keyCode==32){
  46. editor.grid.rotation.x += Math.PI/2;
  47. var m = new THREE.Matrix4();
  48. m.makeRotationX(Math.PI/2);
  49. editor.plane.applyMatrix4(m);
  50. gui.render();
  51. gui.serverMessage("Rotated into the XY plane or whatever");
  52. }
  53. };
  54. editor.makeGrid = function() {
  55. editor.grid = new THREE.GridHelper(10, 1);
  56. editor.grid.rotation.x = Math.PI / 2;
  57. editor.grid.setColors(0xbbbbbb, 0xeeeeee);
  58. editor.plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  59. gui.scene.add(editor.grid);
  60. }
  61. // Gets a reference to the node nearest to the mouse cursor
  62. // TODO: get rid of meta{}
  63. editor.nearestNode = function(ray) {
  64. for (var j in abj.meta) {
  65. if (ray.distanceSqToPoint(abj.meta[j].position) < 0.03) {
  66. return j;
  67. }
  68. }
  69. return undefined;
  70. };