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.

278 lines
11KB

  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. #ifndef __JUCETICE_GRAPHNODECOMPONENT_HEADER__
  21. #define __JUCETICE_GRAPHNODECOMPONENT_HEADER__
  22. #include "jucetice_GraphLinkComponent.h"
  23. #include "jucetice_GraphConnectorComponent.h"
  24. #include "jucetice_GraphNodeListener.h"
  25. //==============================================================================
  26. /**
  27. A node in a graph, represented by a graphical component with inputs and
  28. outputs connectors.
  29. */
  30. class GraphNodeComponent : public Component,
  31. public SettableTooltipClient
  32. {
  33. public:
  34. //==============================================================================
  35. /** Constructor */
  36. GraphNodeComponent();
  37. /** Destructor */
  38. ~GraphNodeComponent();
  39. //==============================================================================
  40. /** Get the unique id that identify this node */
  41. int getUniqueID () const { return uniqueID; }
  42. /** Sets the id that identify this node */
  43. void setUniqueID (const int newID) { uniqueID = newID; }
  44. //==============================================================================
  45. /** Return the text displayed */
  46. const String& getText () const { return textToDisplay; }
  47. /** Set the text displayed in the middle of the object */
  48. void setText (const String& newText) { textToDisplay = newText; }
  49. //==============================================================================
  50. /** Return the colour of the node */
  51. const Colour& getNodeColour () const { return nodeColour; }
  52. /** Set the colour of the node */
  53. void setNodeColour (const Colour& newColour) { nodeColour = newColour; }
  54. //==============================================================================
  55. /** Return if the object is locked, so no dragging is available */
  56. bool isLocked () const { return hasBeenLocked; }
  57. /** Set if the object is locked, so no draggaing is available */
  58. void setLocked (const bool lockedState) { hasBeenLocked = lockedState; }
  59. //==============================================================================
  60. /** Return the user data associated with this node */
  61. void* getUserData () const { return userData; }
  62. /** Set the user data associated with this node */
  63. void setUserData (void* newUserData) { userData = newUserData; }
  64. //==============================================================================
  65. /** Get the orientation of the node */
  66. bool isLeftToRight () const { return leftToRight; }
  67. /** Set the orientation of the node */
  68. void setLeftToRight (const bool isLToR) { leftToRight = isLToR; }
  69. //==============================================================================
  70. /** Return the number of input connectors for this node.
  71. Inputs connectors are obviously >= 0.
  72. */
  73. int getInputConnectorCount () const { return graphInputs.size (); }
  74. /** Get an already added input connector.
  75. You should use the connector id, which is tipically the index
  76. of the connector in the inputs array.
  77. */
  78. GraphConnectorComponentInput* getInputConnector (const int connectorID);
  79. /** Add a input connector.
  80. You can specify the id of the new connector you are adding, or
  81. leave it to be calculated automagically.
  82. */
  83. void addInputConnector (const int connectorType = 0);
  84. /** Returns the index of the first occurrence of an input.
  85. Specify a link type to be checked.
  86. */
  87. int getFirstInputOfType (const int connectorType);
  88. /** TODO - document */
  89. int getInputLinksCount () const;
  90. //==============================================================================
  91. /** Return the number of output connectors for this node.
  92. Outputs connectors are obviously >= 0
  93. */
  94. int getOutputConnectorCount () const { return graphOutputs.size (); }
  95. /** Get an already added output connector.
  96. You should use the connector id, which is tipically the index
  97. of the connector in the outputs array.
  98. */
  99. GraphConnectorComponentOutput* getOutputConnector (const int connectorID);
  100. /** Add a output connector.
  101. You can specify the id of the new connector you are adding, or
  102. leave it to be calculated automagically.
  103. */
  104. void addOutputConnector (const int connectorType = 0);
  105. /** Returns the index of the first occurrence of an output.
  106. Specify a link type to be checked.
  107. */
  108. int getFirstOutputOfType (const int connectorType);
  109. /** TODO - document */
  110. int getOutputLinksCount () const;
  111. //==============================================================================
  112. /** Free all connectors */
  113. virtual void deleteConnectors (const bool freeConnectors = true);
  114. //==============================================================================
  115. /** Connect an output port of this to an input port of another node
  116. It will return true if the connection has take place, false
  117. otherwise.
  118. Eventually you can specify to not notify listeners of the changes,
  119. typically if you are restoring node to node connections massivley
  120. and wants to.
  121. */
  122. bool connectToNode (const int sourcePort,
  123. GraphNodeComponent* destinationNode,
  124. const int destinationPort,
  125. const bool notifyListener = true);
  126. //==============================================================================
  127. /** Break all links of this node.
  128. This should break all inputs and outputs links between this and other nodes.
  129. You can optionally set if it have to notify the listener about this changes.
  130. */
  131. void breakAllLinks (const bool notifyListener = true);
  132. /** Break inputs links of this node.
  133. This should break all inputs links between this and other nodes.
  134. You can optionally set if it have to notify the listener about this changes.
  135. */
  136. void breakInputLinks (const bool notifyListener = true);
  137. /** Break output links of this node.
  138. This should break all outputs links between this and other nodes.
  139. You can optionally set if it have to notify the listener about this changes.
  140. */
  141. void breakOutputLinks (const bool notifyListener = true);
  142. //==============================================================================
  143. /** Set the listener that manage this Graph */
  144. void setNodeListener (GraphNodeListener* const listener);
  145. //==============================================================================
  146. /** Dispatching for the listener about popup menu clicked on a connector.
  147. This is typically called from within a connector itself, then dispatched
  148. to the attached listener.
  149. */
  150. void notifyConnectorPopupMenuSelected (GraphConnectorComponent* changedConnector);
  151. /** Dispatching for the listener about popup menu clicked on a connector.
  152. This is typically called from within a connector itself, then dispatched
  153. to the attached listener.
  154. */
  155. void notifyLinkPopupMenuSelected (GraphLinkComponent* changedLink);
  156. /** This is called whenever a node is right clicked */
  157. void notifyLinkConnected (GraphLinkComponent* changedLink);
  158. /** This is called whenever a node is right clicked */
  159. void notifyLinkDisconnected (GraphLinkComponent* changedLink);
  160. /** Dispatch a call to the listener, about a graph change */
  161. void notifyGraphChanged ();
  162. /** Called from a connector when we want to get its colour */
  163. Colour getConnectorColour (GraphConnectorComponent* connector,
  164. const bool isSelected);
  165. //==============================================================================
  166. /** Check if this node is already connected AFTER another one */
  167. bool isAfterNode (GraphNodeComponent* nodeBefore);
  168. /** Check if this node is already connected BEFORE another one */
  169. bool isBeforeNode (GraphNodeComponent* nodeAfter);
  170. //==============================================================================
  171. /** @internal */
  172. void mouseDown (const MouseEvent& e);
  173. /** @internal */
  174. void mouseDrag (const MouseEvent& e);
  175. /** @internal */
  176. void mouseDoubleClick (const MouseEvent& e);
  177. /** @internal */
  178. void paint (Graphics& g);
  179. /** @internal */
  180. void resized();
  181. /** @internal */
  182. void moved();
  183. //==============================================================================
  184. juce_UseDebuggingNewOperator
  185. protected:
  186. //==============================================================================
  187. /** Called when we move around cause we have to update connectors */
  188. void updateConnectors();
  189. Array<GraphConnectorComponentInput*> graphInputs;
  190. Array<GraphConnectorComponentOutput*> graphOutputs;
  191. GraphNodeListener* listener;
  192. void* userData;
  193. int uniqueID;
  194. String textToDisplay;
  195. Colour nodeColour;
  196. bool leftToRight : 1,
  197. hasBeenLocked : 1;
  198. ComponentBoundsConstrainer constrainer;
  199. int originalX, originalY;
  200. int lastX, lastY;
  201. //DropShadower dropShadower;
  202. };
  203. #endif