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.
 
 
 

113 lines
1.9 KiB

  1. var generator = {
  2. nodes: [],
  3. init: function () {
  4. this.actions.addNode.call(this, null, {
  5. x: 100,
  6. y: 100
  7. });
  8. renderer.center(this.nodes[0]);
  9. },
  10. onClick: function (button, x, y) {
  11. var node = this.findNode(x, y);
  12. if (!node)
  13. return;
  14. if (button == 0)
  15. this.actions.addNode.call(this, node);
  16. else if (button == 1)
  17. this.actions.rotateNode.call(this, node);
  18. else if (button == 2)
  19. this.actions.extendNode.call(this, node);
  20. renderer.makeDirty();
  21. },
  22. findNode: function (x, y, nodes) {
  23. nodes = nodes || this.nodes;
  24. for (var i = 0; i < nodes.length; i++) {
  25. var n = nodes[i];
  26. if (!(
  27. (n.pos.x > x) ||
  28. (n.pos.y > y) ||
  29. (n.pos.x + constants.blockSize <= x) |
  30. (n.pos.y + constants.blockSize <= y)
  31. ))
  32. return n;
  33. else {
  34. var f = this.findNode(x, y, n.children);
  35. if (f)
  36. return f;
  37. }
  38. }
  39. },
  40. actions: {
  41. addNode: function (parent, options = {}) {
  42. var nodes = this.nodes;
  43. if (parent)
  44. options.angle = constants.defaultAngle;
  45. var node = tplNode.build({
  46. id: nodes.length,
  47. angle: options.angle,
  48. x: options.x,
  49. y: options.y,
  50. parent: parent,
  51. distance: constants.defaultDistance
  52. });
  53. if (parent)
  54. parent.children.push(node);
  55. else
  56. nodes.push(node);
  57. },
  58. rotateNode: function (node) {
  59. var newAngle = node.angle - constants.defaultAngleInc;
  60. node.parent.children.forEach(n => (n.angle = newAngle));
  61. console.log(node.parent);
  62. },
  63. extendNode: function (node) {
  64. node.distance += constants.defaultDistanceInc;
  65. }
  66. }
  67. };
  68. var tplNode = {
  69. id: 0,
  70. children: [],
  71. pos: {
  72. x: 0,
  73. y: 0
  74. },
  75. build: function (options) {
  76. var res = $.extend(true, {
  77. parent: options.parent
  78. }, this, {
  79. id: this.id++,
  80. pos: {
  81. x: options.x,
  82. y: options.y
  83. },
  84. distance: options.distance,
  85. angle: options.angle
  86. });
  87. delete res.build;
  88. return res;
  89. }
  90. };
  91. $(function () {
  92. renderer.init();
  93. generator.init();
  94. })