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.

179 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCETICE project - Copyright 2009 by Lucio Asnaghi.
  4. JUCETICE is based around the JUCE library - "Jules' Utility Class Extensions"
  5. Copyright 2007 by Julian Storer.
  6. ------------------------------------------------------------------------------
  7. JUCE and JUCETICE can be redistributed and/or modified under the terms of
  8. the GNU General Public License, as published by the Free Software Foundation;
  9. either version 2 of the License, or (at your option) any later version.
  10. JUCE and JUCETICE are distributed in the hope that they will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with JUCE and JUCETICE; if not, visit www.gnu.org/licenses or write to
  16. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  17. Boston, MA 02111-1307 USA
  18. ==============================================================================
  19. */
  20. BEGIN_JUCE_NAMESPACE
  21. #include "jucetice_GraphNodeComponent.h"
  22. #include "jucetice_GraphConnectorComponent.h"
  23. //==============================================================================
  24. GraphLinkComponent::GraphLinkComponent (const bool leftToRight_,
  25. const int lineThickness_,
  26. const float tension_)
  27. : from (0),
  28. to (0),
  29. startPointX (0),
  30. startPointY (0),
  31. endPointX (0),
  32. endPointY (0),
  33. leftToRight (leftToRight_),
  34. lineThickness (lineThickness_),
  35. tension (tension_)
  36. {
  37. setInterceptsMouseClicks (true, false);
  38. setRepaintsOnMouseActivity (true);
  39. }
  40. GraphLinkComponent::~GraphLinkComponent()
  41. {
  42. if (to) to->removeLink (this);
  43. if (from) from->removeLink (this);
  44. }
  45. //==============================================================================
  46. void GraphLinkComponent::setStartPoint (const int x, const int y)
  47. {
  48. startPointX = x;
  49. startPointY = y;
  50. updateBounds();
  51. }
  52. void GraphLinkComponent::setEndPoint (const int x, const int y)
  53. {
  54. endPointX = x;
  55. endPointY = y;
  56. updateBounds();
  57. }
  58. //==============================================================================
  59. void GraphLinkComponent::setTension (const float newTension)
  60. {
  61. tension = jmin (jmax (newTension, 0.0f), 1.0f);
  62. }
  63. //==============================================================================
  64. void GraphLinkComponent::mouseUp (const MouseEvent& e)
  65. {
  66. if (e.mods.isRightButtonDown())
  67. {
  68. GraphNodeComponent* node = (to ? to->getParentGraphComponent ()
  69. : (from ? from->getParentGraphComponent ()
  70. : 0));
  71. if (node)
  72. node->notifyLinkPopupMenuSelected (this);
  73. }
  74. }
  75. //==============================================================================
  76. void GraphLinkComponent::mouseDrag (const MouseEvent& e)
  77. {
  78. if (e.mods.isLeftButtonDown())
  79. {
  80. setTension (tension + e.getDistanceFromDragStartY () / 512.0f);
  81. updateBounds();
  82. repaint ();
  83. }
  84. }
  85. //==============================================================================
  86. void GraphLinkComponent::paint (Graphics& g)
  87. {
  88. Colour wireColour = Colours::black;
  89. if ((to != 0 && from != 0))
  90. {
  91. GraphNodeComponent* node = to->getParentGraphComponent ();
  92. wireColour = node->getConnectorColour (to, isMouseOverOrDragging());
  93. }
  94. else
  95. {
  96. if (to)
  97. {
  98. GraphNodeComponent* node = to->getParentGraphComponent ();
  99. wireColour = node->getConnectorColour (to, true);
  100. }
  101. else if (from)
  102. {
  103. GraphNodeComponent* node = from->getParentGraphComponent ();
  104. wireColour = node->getConnectorColour (from, true);
  105. }
  106. }
  107. g.setColour (wireColour);
  108. g.strokePath (path, PathStrokeType (lineThickness));
  109. }
  110. bool GraphLinkComponent::hitTest (int x, int y)
  111. {
  112. Line<float> l (x, y, x + lineThickness, y + lineThickness);
  113. return path.intersectsLine (l);
  114. }
  115. //==============================================================================
  116. void GraphLinkComponent::updateBounds()
  117. {
  118. int min_x = jmin (endPointX, startPointX),
  119. min_y = jmin (endPointY, startPointY),
  120. max_x = jmax (endPointX, startPointX),
  121. max_y = jmax (endPointY, startPointY);
  122. setBounds ((int) (min_x - lineThickness),
  123. (int) (min_y - lineThickness),
  124. (int) ((max_x - min_x) + (lineThickness * 2)),
  125. (int) ((max_y - min_y) + (lineThickness * 2)));
  126. // recalculate path
  127. float x1 = (float) (startPointX - getX());
  128. float y1 = (float) (startPointY - getY());
  129. float x2 = (float) (endPointX - getX());
  130. float y2 = (float) (endPointY - getY());
  131. // recalculate bounds
  132. path.clear ();
  133. path.startNewSubPath (x1, y1);
  134. if (leftToRight)
  135. {
  136. float cx = fabs ((x2 - x1));
  137. path.cubicTo (cx * tension, y1,
  138. cx * (1.0f - tension), y2,
  139. x2, y2);
  140. }
  141. else
  142. {
  143. float cx = fabs ((y2 - y1));
  144. path.cubicTo (x1, cx * tension,
  145. x2, cx * (1.0f - tension),
  146. x2, y2);
  147. }
  148. }
  149. END_JUCE_NAMESPACE