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.

225 lines
8.4KB

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