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.

224 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. A class used by the LassoComponent to manage the things that it selects.
  24. This allows the LassoComponent to find out which items are within the lasso,
  25. and to change the list of selected items.
  26. @see LassoComponent, SelectedItemSet
  27. */
  28. template <class SelectableItemType>
  29. class LassoSource
  30. {
  31. public:
  32. /** Destructor. */
  33. virtual ~LassoSource() {}
  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. */
  72. template <class SelectableItemType>
  73. class LassoComponent : public Component
  74. {
  75. public:
  76. //==============================================================================
  77. /** Creates a Lasso component. */
  78. LassoComponent() : source (nullptr)
  79. {
  80. }
  81. //==============================================================================
  82. /** Call this in your mouseDown event, to initialise a drag.
  83. Pass in a suitable LassoSource object which the lasso will use to find
  84. the items and change the selection.
  85. After using this method to initialise the lasso, repeatedly call dragLasso()
  86. in your component's mouseDrag callback.
  87. @see dragLasso, endLasso, LassoSource
  88. */
  89. void beginLasso (const MouseEvent& e, LassoSource<SelectableItemType>* lassoSource)
  90. {
  91. jassert (source == nullptr); // this suggests that you didn't call endLasso() after the last drag...
  92. jassert (lassoSource != nullptr); // the source can't be null!
  93. jassert (getParentComponent() != nullptr); // you need to add this to a parent component for it to work!
  94. source = lassoSource;
  95. if (lassoSource != nullptr)
  96. originalSelection = lassoSource->getLassoSelection().getItemArray();
  97. setSize (0, 0);
  98. dragStartPos = e.getMouseDownPosition();
  99. }
  100. /** Call this in your mouseDrag event, to update the lasso's position.
  101. This must be repeatedly calling when the mouse is dragged, after you've
  102. first initialised the lasso with beginLasso().
  103. This method takes into account the modifier keys that are being held down, so
  104. if shift is pressed, then the lassoed items will be added to any that were
  105. previously selected; if ctrl or command is pressed, then they will be xor'ed
  106. with previously selected items.
  107. @see beginLasso, endLasso
  108. */
  109. void dragLasso (const MouseEvent& e)
  110. {
  111. if (source != nullptr)
  112. {
  113. setBounds (Rectangle<int> (dragStartPos, e.getPosition()));
  114. setVisible (true);
  115. Array<SelectableItemType> itemsInLasso;
  116. source->findLassoItemsInArea (itemsInLasso, getBounds());
  117. if (e.mods.isShiftDown())
  118. {
  119. itemsInLasso.removeValuesIn (originalSelection); // to avoid duplicates
  120. itemsInLasso.addArray (originalSelection);
  121. }
  122. else if (e.mods.isCommandDown() || e.mods.isAltDown())
  123. {
  124. Array<SelectableItemType> originalMinusNew (originalSelection);
  125. originalMinusNew.removeValuesIn (itemsInLasso);
  126. itemsInLasso.removeValuesIn (originalSelection);
  127. itemsInLasso.addArray (originalMinusNew);
  128. }
  129. source->getLassoSelection() = SelectedItemSet<SelectableItemType> (itemsInLasso);
  130. }
  131. }
  132. /** Call this in your mouseUp event, after the lasso has been dragged.
  133. @see beginLasso, dragLasso
  134. */
  135. void endLasso()
  136. {
  137. source = nullptr;
  138. originalSelection.clear();
  139. setVisible (false);
  140. }
  141. //==============================================================================
  142. /** A set of colour IDs to use to change the colour of various aspects of the label.
  143. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  144. methods.
  145. Note that you can also use the constants from TextEditor::ColourIds to change the
  146. colour of the text editor that is opened when a label is editable.
  147. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  148. */
  149. enum ColourIds
  150. {
  151. lassoFillColourId = 0x1000440, /**< The colour to fill the lasso rectangle with. */
  152. lassoOutlineColourId = 0x1000441, /**< The colour to draw the outline with. */
  153. };
  154. //==============================================================================
  155. /** @internal */
  156. void paint (Graphics& g) override
  157. {
  158. getLookAndFeel().drawLasso (g, *this);
  159. // this suggests that you've left a lasso comp lying around after the
  160. // mouse drag has finished.. Be careful to call endLasso() when you get a
  161. // mouse-up event.
  162. jassert (isMouseButtonDownAnywhere());
  163. }
  164. /** @internal */
  165. bool hitTest (int, int) override { return false; }
  166. private:
  167. //==============================================================================
  168. Array<SelectableItemType> originalSelection;
  169. LassoSource<SelectableItemType>* source;
  170. Point<int> dragStartPos;
  171. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LassoComponent)
  172. };
  173. } // namespace juce