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.

189 lines
6.4KB

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