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.

196 lines
6.6KB

  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. ResizableBorderComponent::Zone::Zone() noexcept {}
  21. ResizableBorderComponent::Zone::Zone (int zoneFlags) noexcept : zone (zoneFlags) {}
  22. ResizableBorderComponent::Zone::Zone (const ResizableBorderComponent::Zone& other) noexcept : zone (other.zone) {}
  23. ResizableBorderComponent::Zone& ResizableBorderComponent::Zone::operator= (const ResizableBorderComponent::Zone& other) noexcept
  24. {
  25. zone = other.zone;
  26. return *this;
  27. }
  28. bool ResizableBorderComponent::Zone::operator== (const ResizableBorderComponent::Zone& other) const noexcept { return zone == other.zone; }
  29. bool ResizableBorderComponent::Zone::operator!= (const ResizableBorderComponent::Zone& other) const noexcept { return zone != other.zone; }
  30. ResizableBorderComponent::Zone ResizableBorderComponent::Zone::fromPositionOnBorder (Rectangle<int> totalSize,
  31. BorderSize<int> border,
  32. Point<int> position)
  33. {
  34. int z = 0;
  35. if (totalSize.contains (position)
  36. && ! border.subtractedFrom (totalSize).contains (position))
  37. {
  38. auto minW = jmax (totalSize.getWidth() / 10, jmin (10, totalSize.getWidth() / 3));
  39. if (position.x < jmax (border.getLeft(), minW) && border.getLeft() > 0)
  40. z |= left;
  41. else if (position.x >= totalSize.getWidth() - jmax (border.getRight(), minW) && border.getRight() > 0)
  42. z |= right;
  43. auto minH = jmax (totalSize.getHeight() / 10, jmin (10, totalSize.getHeight() / 3));
  44. if (position.y < jmax (border.getTop(), minH) && border.getTop() > 0)
  45. z |= top;
  46. else if (position.y >= totalSize.getHeight() - jmax (border.getBottom(), minH) && border.getBottom() > 0)
  47. z |= bottom;
  48. }
  49. return Zone (z);
  50. }
  51. MouseCursor ResizableBorderComponent::Zone::getMouseCursor() const noexcept
  52. {
  53. auto mc = MouseCursor::NormalCursor;
  54. switch (zone)
  55. {
  56. case (left | top): mc = MouseCursor::TopLeftCornerResizeCursor; break;
  57. case top: mc = MouseCursor::TopEdgeResizeCursor; break;
  58. case (right | top): mc = MouseCursor::TopRightCornerResizeCursor; break;
  59. case left: mc = MouseCursor::LeftEdgeResizeCursor; break;
  60. case right: mc = MouseCursor::RightEdgeResizeCursor; break;
  61. case (left | bottom): mc = MouseCursor::BottomLeftCornerResizeCursor; break;
  62. case bottom: mc = MouseCursor::BottomEdgeResizeCursor; break;
  63. case (right | bottom): mc = MouseCursor::BottomRightCornerResizeCursor; break;
  64. default: break;
  65. }
  66. return mc;
  67. }
  68. //==============================================================================
  69. ResizableBorderComponent::ResizableBorderComponent (Component* componentToResize,
  70. ComponentBoundsConstrainer* boundsConstrainer)
  71. : component (componentToResize),
  72. constrainer (boundsConstrainer),
  73. borderSize (5)
  74. {
  75. }
  76. ResizableBorderComponent::~ResizableBorderComponent() = default;
  77. //==============================================================================
  78. void ResizableBorderComponent::paint (Graphics& g)
  79. {
  80. getLookAndFeel().drawResizableFrame (g, getWidth(), getHeight(), borderSize);
  81. }
  82. void ResizableBorderComponent::mouseEnter (const MouseEvent& e)
  83. {
  84. updateMouseZone (e);
  85. }
  86. void ResizableBorderComponent::mouseMove (const MouseEvent& e)
  87. {
  88. updateMouseZone (e);
  89. }
  90. void ResizableBorderComponent::mouseDown (const MouseEvent& e)
  91. {
  92. if (component == nullptr)
  93. {
  94. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  95. return;
  96. }
  97. updateMouseZone (e);
  98. originalBounds = component->getBounds();
  99. if (constrainer != nullptr)
  100. constrainer->resizeStart();
  101. }
  102. void ResizableBorderComponent::mouseDrag (const MouseEvent& e)
  103. {
  104. if (component == nullptr)
  105. {
  106. jassertfalse; // You've deleted the component that this resizer was supposed to be using!
  107. return;
  108. }
  109. auto newBounds = mouseZone.resizeRectangleBy (originalBounds, e.getOffsetFromDragStart());
  110. if (constrainer != nullptr)
  111. {
  112. constrainer->setBoundsForComponent (component, newBounds,
  113. mouseZone.isDraggingTopEdge(),
  114. mouseZone.isDraggingLeftEdge(),
  115. mouseZone.isDraggingBottomEdge(),
  116. mouseZone.isDraggingRightEdge());
  117. }
  118. else
  119. {
  120. if (auto* p = component->getPositioner())
  121. p->applyNewBounds (newBounds);
  122. else
  123. component->setBounds (newBounds);
  124. }
  125. }
  126. void ResizableBorderComponent::mouseUp (const MouseEvent&)
  127. {
  128. if (constrainer != nullptr)
  129. constrainer->resizeEnd();
  130. }
  131. bool ResizableBorderComponent::hitTest (int x, int y)
  132. {
  133. return ! borderSize.subtractedFrom (getLocalBounds()).contains (x, y);
  134. }
  135. void ResizableBorderComponent::setBorderThickness (BorderSize<int> newBorderSize)
  136. {
  137. if (borderSize != newBorderSize)
  138. {
  139. borderSize = newBorderSize;
  140. repaint();
  141. }
  142. }
  143. BorderSize<int> ResizableBorderComponent::getBorderThickness() const
  144. {
  145. return borderSize;
  146. }
  147. void ResizableBorderComponent::updateMouseZone (const MouseEvent& e)
  148. {
  149. auto newZone = Zone::fromPositionOnBorder (getLocalBounds(), borderSize, e.getPosition());
  150. if (mouseZone != newZone)
  151. {
  152. mouseZone = newZone;
  153. setMouseCursor (newZone.getMouseCursor());
  154. }
  155. }
  156. } // namespace juce