Simple editor to create custom node network graphs aka ontologies
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.

nodes.pde 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. int nodeCount;
  2. int i_selectedNode = -1;
  3. int padding = textSize/3*2; //Padding between text and surrounding box
  4. int letTolerance = 10; //Widens the clickable areas of in/outlets a bit
  5. void addNode(int x, int y, String title) {
  6. nodes[nodeCount] = new Node(x, y, title);
  7. nodeCount++;
  8. }
  9. void deselect() {
  10. i_selectedNode = -1;
  11. }
  12. void deleteSelectedNode() { //Seperate from class for garbage collector reasons
  13. if (i_selectedNode != -1) {
  14. for (int i = 0; i < linkCount; i++) {
  15. if (links[i].targetNode == i_selectedNode || links[i].parentNode == i_selectedNode) {
  16. links[i].active = false;
  17. }
  18. }
  19. nodes[i_selectedNode].deleted = true;
  20. i_selectedNode = -1;
  21. }
  22. }
  23. class Node {
  24. String title;
  25. int id;
  26. int inlets = 2;
  27. int outlets = 2;
  28. int x, y;
  29. int h = textSize+2*padding;
  30. int w = textSize+2*padding;
  31. PVector dragMouseStartPoint = new PVector(0, 0);
  32. PVector outletCenter = new PVector(0, 0);
  33. PVector inletCenter = new PVector(0, 0);
  34. boolean deleted;
  35. boolean drag;
  36. boolean hover() {
  37. if (mouseX > x-w/2 && mouseX < x+w/2 && mouseY > y-h/2 && mouseY < y+h/2) {
  38. return true;
  39. } else {
  40. return false;
  41. }
  42. }
  43. boolean hoverOutlet() {
  44. if (mouseX > x-(textSize+padding)/2-letTolerance && mouseX < x+(textSize+padding)/2+letTolerance && mouseY > y-padding-textSize/2-(textSize+padding)/2-letTolerance && mouseY < y-padding-textSize/2) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. boolean hoverInlet() {
  51. if (mouseX > x-(textSize+padding)/2-letTolerance && mouseX < x+(textSize+padding)/2+letTolerance && mouseY > y+padding+textSize/2 && mouseY < y+padding+textSize/2+(textSize+padding)/2+letTolerance) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57. Node(int x_, int y_, String title_) {
  58. id = nodeCount;
  59. title = title_;
  60. x = x_;
  61. y = y_;
  62. setVectors();
  63. refreshSize();
  64. }
  65. void setVectors() {
  66. outletCenter.x = x;
  67. outletCenter.y = y-(padding+textSize/2)*1.3;
  68. inletCenter.x = x;
  69. inletCenter.y = y+(padding+textSize/2)*1.3;
  70. }
  71. /**
  72. * Called when changing nodes title or when zooming via mouse-wheel
  73. */
  74. void refreshSize() {
  75. w = int(textWidth(title))+2*padding;
  76. h = textSize+2*padding;
  77. }
  78. /**
  79. * Update-function is really only necessary when dragging the node
  80. */
  81. void update() {
  82. if (drag) {
  83. x -= dragMouseStartPoint.x - mouseX;
  84. dragMouseStartPoint.x = mouseX;
  85. y -= dragMouseStartPoint.y - mouseY;
  86. dragMouseStartPoint.y = mouseY;
  87. setVectors();
  88. }
  89. }
  90. void display() {
  91. /**
  92. * Selected nodes border
  93. */
  94. if (i_selectedNode == id) {
  95. noStroke();
  96. fill(150, 100);
  97. rect(x, y, w+20, h+20, 4);
  98. }
  99. /**
  100. * Inlet & Outlet
  101. */
  102. stroke(255, darkMode? 255 : 0);
  103. strokeWeight(1);
  104. fill(255, 0, 0);
  105. ellipse(x, y-padding-textSize/2, textSize+padding, textSize+padding);
  106. fill(0);
  107. ellipse(x, y+padding+textSize/2, textSize+padding, textSize+padding);
  108. /**
  109. * Box & Title
  110. */
  111. stroke(127);
  112. strokeWeight(2);
  113. fill(50);
  114. rect(x, y, w, h, 4);
  115. fill(255);
  116. text(title, x, y+textSize/3);
  117. }
  118. }