The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

272 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. #include "../../Application/jucer_Headers.h"
  14. #include "jucer_ComponentLayoutEditor.h"
  15. #include "../jucer_UtilityFunctions.h"
  16. //==============================================================================
  17. ComponentOverlayComponent::ComponentOverlayComponent (Component* const target_,
  18. ComponentLayout& layout_)
  19. : target (target_),
  20. borderThickness (4),
  21. layout (layout_),
  22. selected (false),
  23. dragging (false),
  24. originalAspectRatio (1.0)
  25. {
  26. setMinimumOnscreenAmounts (0, 0, 0, 0);
  27. setSizeLimits (borderThickness * 2 + 2, borderThickness * 2 + 2, 8192, 8192);
  28. border.reset (new ResizableBorderComponent (this, this));
  29. addChildComponent (border.get());
  30. border->setBorderThickness (BorderSize<int> (borderThickness));
  31. target->addComponentListener (this);
  32. changeListenerCallback (nullptr);
  33. layout.getSelectedSet().addChangeListener (this);
  34. setRepaintsOnMouseActivity (true);
  35. border->setRepaintsOnMouseActivity (true);
  36. }
  37. ComponentOverlayComponent::~ComponentOverlayComponent()
  38. {
  39. layout.getSelectedSet().removeChangeListener (this);
  40. if (target != nullptr)
  41. target->removeComponentListener (this);
  42. }
  43. void ComponentOverlayComponent::changeListenerCallback (ChangeBroadcaster*)
  44. {
  45. const bool nowSelected = layout.getSelectedSet().isSelected (target);
  46. if (selected != nowSelected)
  47. {
  48. selected = nowSelected;
  49. border->setVisible (nowSelected);
  50. repaint();
  51. }
  52. }
  53. void ComponentOverlayComponent::paint (Graphics& g)
  54. {
  55. jassert (target != nullptr);
  56. border->setColour (backgroundColourId, Colours::transparentBlack);
  57. if (selected)
  58. {
  59. auto selectedItems = layout.getSelectedSet();
  60. auto baseColour = findColour (defaultHighlightColourId);
  61. const BorderSize<int> borderSize (border->getBorderThickness());
  62. drawResizableBorder (g, getWidth(), getHeight(), borderSize,
  63. (isMouseOverOrDragging() || border->isMouseOverOrDragging()),
  64. baseColour.withAlpha (selectedItems.getSelectedItem (0) == target ? 1.0f : 0.3f));
  65. }
  66. else if (isMouseOverOrDragging())
  67. {
  68. drawMouseOverCorners (g, getWidth(), getHeight());
  69. }
  70. }
  71. void ComponentOverlayComponent::resized()
  72. {
  73. jassert (target != nullptr);
  74. border->setBounds (getLocalBounds());
  75. }
  76. void ComponentOverlayComponent::mouseDown (const MouseEvent& e)
  77. {
  78. dragging = false;
  79. mouseDownSelectStatus = layout.getSelectedSet().addToSelectionOnMouseDown (target, e.mods);
  80. if (e.mods.isPopupMenu())
  81. {
  82. showPopupMenu();
  83. return; // this may be deleted now..
  84. }
  85. }
  86. void ComponentOverlayComponent::mouseDrag (const MouseEvent& e)
  87. {
  88. if (! e.mods.isPopupMenu())
  89. {
  90. if (selected && ! dragging)
  91. {
  92. dragging = e.mouseWasDraggedSinceMouseDown();
  93. if (dragging)
  94. layout.startDragging();
  95. }
  96. if (dragging)
  97. {
  98. layout.dragSelectedComps (e.getDistanceFromDragStartX(),
  99. e.getDistanceFromDragStartY());
  100. }
  101. }
  102. }
  103. void ComponentOverlayComponent::mouseUp (const MouseEvent& e)
  104. {
  105. if (dragging)
  106. layout.endDragging();
  107. layout.getSelectedSet().addToSelectionOnMouseUp (target, e.mods, dragging, mouseDownSelectStatus);
  108. }
  109. void ComponentOverlayComponent::componentMovedOrResized (Component&, bool /*wasMoved*/, bool /*wasResized*/)
  110. {
  111. updateBoundsToMatchTarget();
  112. }
  113. void ComponentOverlayComponent::updateBoundsToMatchTarget()
  114. {
  115. if (Component* const parent = target->getParentComponent())
  116. {
  117. const int dx = parent->getX();
  118. const int dy = parent->getY();
  119. setBounds (dx + target->getX() - borderThickness,
  120. dy + target->getY() - borderThickness,
  121. target->getWidth() + borderThickness * 2,
  122. target->getHeight() + borderThickness * 2);
  123. }
  124. if (border->isMouseButtonDown())
  125. layout.changed();
  126. }
  127. void ComponentOverlayComponent::resizeStart()
  128. {
  129. if (getHeight() > 0)
  130. originalAspectRatio = getWidth() / (double) getHeight();
  131. else
  132. originalAspectRatio = 1.0;
  133. layout.getDocument()->beginTransaction ("Resize components");
  134. }
  135. void ComponentOverlayComponent::resizeEnd()
  136. {
  137. layout.getDocument()->beginTransaction();
  138. }
  139. void ComponentOverlayComponent::checkBounds (Rectangle<int>& b,
  140. const Rectangle<int>& previousBounds,
  141. const Rectangle<int>& limits,
  142. const bool isStretchingTop,
  143. const bool isStretchingLeft,
  144. const bool isStretchingBottom,
  145. const bool isStretchingRight)
  146. {
  147. if (ModifierKeys::currentModifiers.isShiftDown())
  148. setFixedAspectRatio (originalAspectRatio);
  149. else
  150. setFixedAspectRatio (0.0);
  151. ComponentBoundsConstrainer::checkBounds (b, previousBounds, limits, isStretchingTop, isStretchingLeft, isStretchingBottom, isStretchingRight);
  152. if (layout.getDocument()->isSnapActive (true))
  153. {
  154. if (Component* const parent = target->getParentComponent())
  155. {
  156. const int dx = parent->getX();
  157. const int dy = parent->getY();
  158. int x = b.getX();
  159. int y = b.getY();
  160. int w = b.getWidth();
  161. int h = b.getHeight();
  162. x += borderThickness - dx;
  163. y += borderThickness - dy;
  164. w -= borderThickness * 2;
  165. h -= borderThickness * 2;
  166. int right = x + w;
  167. int bottom = y + h;
  168. if (isStretchingRight)
  169. right = layout.getDocument()->snapPosition (right);
  170. if (isStretchingBottom)
  171. bottom = layout.getDocument()->snapPosition (bottom);
  172. if (isStretchingLeft)
  173. x = layout.getDocument()->snapPosition (x);
  174. if (isStretchingTop)
  175. y = layout.getDocument()->snapPosition (y);
  176. w = (right - x) + borderThickness * 2;
  177. h = (bottom - y) + borderThickness * 2;
  178. x -= borderThickness - dx;
  179. y -= borderThickness - dy;
  180. b = Rectangle<int> (x, y, w, h);
  181. }
  182. }
  183. }
  184. void ComponentOverlayComponent::applyBoundsToComponent (Component& component, Rectangle<int> b)
  185. {
  186. if (component.getBounds() != b)
  187. {
  188. layout.getDocument()->getUndoManager().undoCurrentTransactionOnly();
  189. auto dX = b.getX() - component.getX();
  190. auto dY = b.getY() - component.getY();
  191. auto dW = b.getWidth() - component.getWidth();
  192. auto dH = b.getHeight() - component.getHeight();
  193. component.setBounds (b);
  194. if (auto* parent = target->getParentComponent())
  195. target->setBounds (b.getX() + borderThickness - parent->getX(),
  196. b.getY() + borderThickness - parent->getY(),
  197. b.getWidth() - borderThickness * 2,
  198. b.getHeight() - borderThickness * 2);
  199. layout.updateStoredComponentPosition (target, true);
  200. if (layout.getSelectedSet().getNumSelected() > 1)
  201. {
  202. for (auto s : layout.getSelectedSet())
  203. {
  204. if (s != target)
  205. {
  206. s->setBounds (s->getX() + dX, s->getY() + dY, s->getWidth() + dW, s->getHeight() + dH);
  207. layout.updateStoredComponentPosition (s, true);
  208. }
  209. }
  210. }
  211. }
  212. }
  213. void ComponentOverlayComponent::showPopupMenu()
  214. {
  215. ComponentTypeHandler::getHandlerFor (*target)->showPopupMenu (target, layout);
  216. }