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.2KB

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