Audio plugin host https://kx.studio/carla
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.

juce_LassoComponent.h 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_LASSOCOMPONENT_H_INCLUDED
  18. #define JUCE_LASSOCOMPONENT_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. A class used by the LassoComponent to manage the things that it selects.
  22. This allows the LassoComponent to find out which items are within the lasso,
  23. and to change the list of selected items.
  24. @see LassoComponent, SelectedItemSet
  25. */
  26. template <class SelectableItemType>
  27. class LassoSource
  28. {
  29. public:
  30. /** Destructor. */
  31. virtual ~LassoSource() {}
  32. /** Returns the set of items that lie within a given lassoable region.
  33. Your implementation of this method must find all the relevent items that lie
  34. within the given rectangle. and add them to the itemsFound array.
  35. The coordinates are relative to the top-left of the lasso component's parent
  36. component. (i.e. they are the same as the size and position of the lasso
  37. component itself).
  38. */
  39. virtual void findLassoItemsInArea (Array <SelectableItemType>& itemsFound,
  40. const Rectangle<int>& area) = 0;
  41. /** Returns the SelectedItemSet that the lasso should update.
  42. This set will be continuously updated by the LassoComponent as it gets
  43. dragged around, so make sure that you've got a ChangeListener attached to
  44. the set so that your UI objects will know when the selection changes and
  45. be able to update themselves appropriately.
  46. */
  47. virtual SelectedItemSet<SelectableItemType>& getLassoSelection() = 0;
  48. };
  49. //==============================================================================
  50. /**
  51. A component that acts as a rectangular selection region, which you drag with
  52. the mouse to select groups of objects (in conjunction with a SelectedItemSet).
  53. To use one of these:
  54. - In your mouseDown or mouseDrag event, add the LassoComponent to your parent
  55. component, and call its beginLasso() method, giving it a
  56. suitable LassoSource object that it can use to find out which items are in
  57. the active area.
  58. - Each time your parent component gets a mouseDrag event, call dragLasso()
  59. to update the lasso's position - it will use its LassoSource to calculate and
  60. update the current selection.
  61. - After the drag has finished and you get a mouseUp callback, you should call
  62. endLasso() to clean up. This will make the lasso component invisible, and you
  63. can remove it from the parent component, or delete it.
  64. The class takes into account the modifier keys that are being held down while
  65. the lasso is being dragged, so if shift is pressed, then any lassoed items will
  66. be added to the original selection; if ctrl or command is pressed, they will be
  67. xor'ed with any previously selected items.
  68. @see LassoSource, SelectedItemSet
  69. */
  70. template <class SelectableItemType>
  71. class LassoComponent : public Component
  72. {
  73. public:
  74. //==============================================================================
  75. /** Creates a Lasso component. */
  76. LassoComponent() : source (nullptr)
  77. {
  78. }
  79. //==============================================================================
  80. /** Call this in your mouseDown event, to initialise a drag.
  81. Pass in a suitable LassoSource object which the lasso will use to find
  82. the items and change the selection.
  83. After using this method to initialise the lasso, repeatedly call dragLasso()
  84. in your component's mouseDrag callback.
  85. @see dragLasso, endLasso, LassoSource
  86. */
  87. void beginLasso (const MouseEvent& e, LassoSource<SelectableItemType>* lassoSource)
  88. {
  89. jassert (source == nullptr); // this suggests that you didn't call endLasso() after the last drag...
  90. jassert (lassoSource != nullptr); // the source can't be null!
  91. jassert (getParentComponent() != nullptr); // you need to add this to a parent component for it to work!
  92. source = lassoSource;
  93. if (lassoSource != nullptr)
  94. originalSelection = lassoSource->getLassoSelection().getItemArray();
  95. setSize (0, 0);
  96. dragStartPos = e.getMouseDownPosition();
  97. }
  98. /** Call this in your mouseDrag event, to update the lasso's position.
  99. This must be repeatedly calling when the mouse is dragged, after you've
  100. first initialised the lasso with beginLasso().
  101. This method takes into account the modifier keys that are being held down, so
  102. if shift is pressed, then the lassoed items will be added to any that were
  103. previously selected; if ctrl or command is pressed, then they will be xor'ed
  104. with previously selected items.
  105. @see beginLasso, endLasso
  106. */
  107. void dragLasso (const MouseEvent& e)
  108. {
  109. if (source != nullptr)
  110. {
  111. setBounds (Rectangle<int> (dragStartPos, e.getPosition()));
  112. setVisible (true);
  113. Array<SelectableItemType> itemsInLasso;
  114. source->findLassoItemsInArea (itemsInLasso, getBounds());
  115. if (e.mods.isShiftDown())
  116. {
  117. itemsInLasso.removeValuesIn (originalSelection); // to avoid duplicates
  118. itemsInLasso.addArray (originalSelection);
  119. }
  120. else if (e.mods.isCommandDown() || e.mods.isAltDown())
  121. {
  122. Array<SelectableItemType> originalMinusNew (originalSelection);
  123. originalMinusNew.removeValuesIn (itemsInLasso);
  124. itemsInLasso.removeValuesIn (originalSelection);
  125. itemsInLasso.addArray (originalMinusNew);
  126. }
  127. source->getLassoSelection() = SelectedItemSet<SelectableItemType> (itemsInLasso);
  128. }
  129. }
  130. /** Call this in your mouseUp event, after the lasso has been dragged.
  131. @see beginLasso, dragLasso
  132. */
  133. void endLasso()
  134. {
  135. source = nullptr;
  136. originalSelection.clear();
  137. setVisible (false);
  138. }
  139. //==============================================================================
  140. /** A set of colour IDs to use to change the colour of various aspects of the label.
  141. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  142. methods.
  143. Note that you can also use the constants from TextEditor::ColourIds to change the
  144. colour of the text editor that is opened when a label is editable.
  145. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  146. */
  147. enum ColourIds
  148. {
  149. lassoFillColourId = 0x1000440, /**< The colour to fill the lasso rectangle with. */
  150. lassoOutlineColourId = 0x1000441, /**< The colour to draw the outline with. */
  151. };
  152. //==============================================================================
  153. /** @internal */
  154. void paint (Graphics& g) override
  155. {
  156. getLookAndFeel().drawLasso (g, *this);
  157. // this suggests that you've left a lasso comp lying around after the
  158. // mouse drag has finished.. Be careful to call endLasso() when you get a
  159. // mouse-up event.
  160. jassert (isMouseButtonDownAnywhere());
  161. }
  162. /** @internal */
  163. bool hitTest (int, int) override { return false; }
  164. private:
  165. //==============================================================================
  166. Array<SelectableItemType> originalSelection;
  167. LassoSource<SelectableItemType>* source;
  168. Point<int> dragStartPos;
  169. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LassoComponent)
  170. };
  171. #endif // JUCE_LASSOCOMPONENT_H_INCLUDED