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.

683 lines
22KB

  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. #include "../../Application/jucer_Headers.h"
  14. #include "../../Application/jucer_Application.h"
  15. #include "../jucer_PaintRoutine.h"
  16. #include "../jucer_UtilityFunctions.h"
  17. #include "../UI/jucer_JucerCommandIDs.h"
  18. #include "../UI/jucer_PaintRoutineEditor.h"
  19. #include "../Properties/jucer_PositionPropertyBase.h"
  20. #include "jucer_ElementSiblingComponent.h"
  21. #include "jucer_PaintElementUndoableAction.h"
  22. //==============================================================================
  23. PaintElement::PaintElement (PaintRoutine* owner_,
  24. const String& typeName_)
  25. : borderThickness (4),
  26. owner (owner_),
  27. typeName (typeName_),
  28. selected (false),
  29. dragging (false),
  30. originalAspectRatio (1.0)
  31. {
  32. setRepaintsOnMouseActivity (true);
  33. position.rect.setWidth (100);
  34. position.rect.setHeight (100);
  35. setMinimumOnscreenAmounts (0, 0, 0, 0);
  36. setSizeLimits (borderThickness * 2 + 1, borderThickness * 2 + 1, 8192, 8192);
  37. border.reset (new ResizableBorderComponent (this, this));
  38. addChildComponent (border.get());
  39. border->setBorderThickness (BorderSize<int> (borderThickness));
  40. if (owner != nullptr)
  41. owner->getSelectedElements().addChangeListener (this);
  42. selfChangeListenerList.addChangeListener (this);
  43. siblingComponentsChanged();
  44. }
  45. PaintElement::~PaintElement()
  46. {
  47. siblingComponents.clear();
  48. if (owner != nullptr)
  49. {
  50. owner->getSelectedElements().deselect (this);
  51. owner->getSelectedElements().removeChangeListener (this);
  52. }
  53. }
  54. //==============================================================================
  55. void PaintElement::setInitialBounds (int parentWidth, int parentHeight)
  56. {
  57. RelativePositionedRectangle pr (getPosition());
  58. pr.rect.setX (parentWidth / 4 + Random::getSystemRandom().nextInt (parentWidth / 4) - parentWidth / 8);
  59. pr.rect.setY (parentHeight / 3 + Random::getSystemRandom().nextInt (parentHeight / 4) - parentHeight / 8);
  60. setPosition (pr, false);
  61. }
  62. //==============================================================================
  63. const RelativePositionedRectangle& PaintElement::getPosition() const
  64. {
  65. return position;
  66. }
  67. class PaintElementMoveAction : public PaintElementUndoableAction <PaintElement>
  68. {
  69. public:
  70. PaintElementMoveAction (PaintElement* const element, const RelativePositionedRectangle& newState_)
  71. : PaintElementUndoableAction <PaintElement> (element),
  72. newState (newState_),
  73. oldState (element->getPosition())
  74. {
  75. }
  76. bool perform()
  77. {
  78. showCorrectTab();
  79. getElement()->setPosition (newState, false);
  80. return true;
  81. }
  82. bool undo()
  83. {
  84. showCorrectTab();
  85. getElement()->setPosition (oldState, false);
  86. return true;
  87. }
  88. RelativePositionedRectangle newState, oldState;
  89. };
  90. class ChangePaintElementBoundsAction : public PaintElementUndoableAction <PaintElement>
  91. {
  92. public:
  93. ChangePaintElementBoundsAction (PaintElement* const element, const Rectangle<int>& bounds)
  94. : PaintElementUndoableAction <PaintElement> (element),
  95. newBounds (bounds),
  96. oldBounds (element->getBounds())
  97. {
  98. }
  99. bool perform()
  100. {
  101. showCorrectTab();
  102. getElement()->setBounds (newBounds);
  103. return true;
  104. }
  105. bool undo()
  106. {
  107. showCorrectTab();
  108. getElement()->setBounds (oldBounds);
  109. return true;
  110. }
  111. private:
  112. Rectangle<int> newBounds, oldBounds;
  113. };
  114. class ChangePaintElementBoundsAndPropertiesAction : public PaintElementUndoableAction <PaintElement>
  115. {
  116. public:
  117. ChangePaintElementBoundsAndPropertiesAction (PaintElement* const element, const Rectangle<int>& bounds,
  118. const NamedValueSet& props)
  119. : PaintElementUndoableAction <PaintElement> (element),
  120. newBounds (bounds),
  121. oldBounds (element->getBounds()),
  122. newProps (props),
  123. oldProps (element->getProperties())
  124. {
  125. }
  126. bool perform()
  127. {
  128. showCorrectTab();
  129. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getElement()->getParentComponent()))
  130. getElement()->setCurrentBounds (newBounds, pe->getComponentArea(), false);
  131. getElement()->getProperties() = newProps;
  132. return true;
  133. }
  134. bool undo()
  135. {
  136. showCorrectTab();
  137. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getElement()->getParentComponent()))
  138. getElement()->setCurrentBounds (oldBounds, pe->getComponentArea(), false);
  139. getElement()->getProperties() = oldProps;
  140. return true;
  141. }
  142. private:
  143. Rectangle<int> newBounds, oldBounds;
  144. NamedValueSet newProps, oldProps;
  145. };
  146. void PaintElement::setPosition (const RelativePositionedRectangle& newPosition, const bool undoable)
  147. {
  148. if (position != newPosition)
  149. {
  150. if (undoable)
  151. {
  152. perform (new PaintElementMoveAction (this, newPosition),
  153. "Move " + getTypeName());
  154. }
  155. else
  156. {
  157. position = newPosition;
  158. if (owner != nullptr)
  159. owner->changed();
  160. }
  161. }
  162. }
  163. void PaintElement::setPaintElementBounds (const Rectangle<int>& newBounds, const bool undoable)
  164. {
  165. if (getBounds() != newBounds)
  166. {
  167. if (undoable)
  168. {
  169. perform (new ChangePaintElementBoundsAction (this, newBounds), "Change paint element bounds");
  170. }
  171. else
  172. {
  173. setBounds (newBounds);
  174. changed();
  175. }
  176. }
  177. }
  178. void PaintElement::setPaintElementBoundsAndProperties (PaintElement* elementToPosition, const Rectangle<int>& newBounds,
  179. PaintElement* referenceElement, const bool undoable)
  180. {
  181. auto props = NamedValueSet (elementToPosition->getProperties());
  182. auto rect = elementToPosition->getPosition().rect;
  183. auto referenceElementPosition = referenceElement->getPosition();
  184. auto referenceElementRect = referenceElementPosition.rect;
  185. rect.setModes (referenceElementRect.getAnchorPointX(), referenceElementRect.getPositionModeX(),
  186. referenceElementRect.getAnchorPointY(), referenceElementRect.getPositionModeY(),
  187. referenceElementRect.getWidthMode(), referenceElementRect.getHeightMode(),
  188. elementToPosition->getBounds());
  189. props.set ("pos", rect.toString());
  190. props.set ("relativeToX", String::toHexString (referenceElementPosition.relativeToX));
  191. props.set ("relativeToY", String::toHexString (referenceElementPosition.relativeToY));
  192. props.set ("relativeToW", String::toHexString (referenceElementPosition.relativeToW));
  193. props.set ("relativeToH", String::toHexString (referenceElementPosition.relativeToH));
  194. if (elementToPosition->getBounds() != newBounds || elementToPosition->getProperties() != props)
  195. {
  196. if (undoable)
  197. {
  198. perform (new ChangePaintElementBoundsAndPropertiesAction (elementToPosition, newBounds, props),
  199. "Change paint element bounds");
  200. }
  201. else
  202. {
  203. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (elementToPosition->getParentComponent()))
  204. elementToPosition->setCurrentBounds (newBounds, pe->getComponentArea(), false);
  205. elementToPosition->getProperties() = props;
  206. owner->changed();
  207. }
  208. }
  209. }
  210. //==============================================================================
  211. Rectangle<int> PaintElement::getCurrentBounds (const Rectangle<int>& parentArea) const
  212. {
  213. return position.getRectangle (parentArea, getDocument()->getComponentLayout());
  214. }
  215. void PaintElement::setCurrentBounds (const Rectangle<int>& newBounds,
  216. const Rectangle<int>& parentArea,
  217. const bool undoable)
  218. {
  219. RelativePositionedRectangle pr (position);
  220. pr.updateFrom (newBounds.getX() - parentArea.getX(),
  221. newBounds.getY() - parentArea.getY(),
  222. jmax (1, newBounds.getWidth()),
  223. jmax (1, newBounds.getHeight()),
  224. Rectangle<int> (0, 0, parentArea.getWidth(), parentArea.getHeight()),
  225. getDocument()->getComponentLayout());
  226. setPosition (pr, undoable);
  227. updateBounds (parentArea);
  228. }
  229. void PaintElement::updateBounds (const Rectangle<int>& parentArea)
  230. {
  231. if (! parentArea.isEmpty())
  232. {
  233. setBounds (getCurrentBounds (parentArea)
  234. .expanded (borderThickness,
  235. borderThickness));
  236. for (int i = siblingComponents.size(); --i >= 0;)
  237. siblingComponents.getUnchecked(i)->updatePosition();
  238. }
  239. }
  240. //==============================================================================
  241. class ElementPositionProperty : public PositionPropertyBase
  242. {
  243. public:
  244. ElementPositionProperty (PaintElement* e, const String& name,
  245. ComponentPositionDimension dimension_)
  246. : PositionPropertyBase (e, name, dimension_, true, false,
  247. e->getDocument()->getComponentLayout()),
  248. listener (e),
  249. element (e)
  250. {
  251. listener.setPropertyToRefresh (*this);
  252. }
  253. void setPosition (const RelativePositionedRectangle& newPos)
  254. {
  255. if (element->getOwner()->getSelectedElements().getNumSelected() > 1)
  256. positionOtherSelectedElements (getPosition(), newPos);
  257. listener.owner->setPosition (newPos, true);
  258. }
  259. RelativePositionedRectangle getPosition() const
  260. {
  261. return listener.owner->getPosition();
  262. }
  263. private:
  264. ElementListener<PaintElement> listener;
  265. PaintElement* element;
  266. void positionOtherSelectedElements (const RelativePositionedRectangle& oldPos, const RelativePositionedRectangle& newPos)
  267. {
  268. for (auto* s : element->getOwner()->getSelectedElements())
  269. {
  270. if (s != element)
  271. {
  272. auto currentPos = s->getPosition();
  273. auto diff = 0.0;
  274. if (dimension == ComponentPositionDimension::componentX)
  275. {
  276. diff = newPos.rect.getX() - oldPos.rect.getX();
  277. currentPos.rect.setX (currentPos.rect.getX() + diff);
  278. }
  279. else if (dimension == ComponentPositionDimension::componentY)
  280. {
  281. diff = newPos.rect.getY() - oldPos.rect.getY();
  282. currentPos.rect.setY (currentPos.rect.getY() + diff);
  283. }
  284. else if (dimension == ComponentPositionDimension::componentWidth)
  285. {
  286. diff = newPos.rect.getWidth() - oldPos.rect.getWidth();
  287. currentPos.rect.setWidth (currentPos.rect.getWidth() + diff);
  288. }
  289. else if (dimension == ComponentPositionDimension::componentHeight)
  290. {
  291. diff = newPos.rect.getHeight() - oldPos.rect.getHeight();
  292. currentPos.rect.setHeight (currentPos.rect.getHeight() + diff);
  293. }
  294. s->setPosition (currentPos, true);
  295. }
  296. }
  297. }
  298. };
  299. //==============================================================================
  300. void PaintElement::getEditableProperties (Array <PropertyComponent*>& props, bool multipleSelected)
  301. {
  302. ignoreUnused (multipleSelected);
  303. props.add (new ElementPositionProperty (this, "x", PositionPropertyBase::componentX));
  304. props.add (new ElementPositionProperty (this, "y", PositionPropertyBase::componentY));
  305. props.add (new ElementPositionProperty (this, "width", PositionPropertyBase::componentWidth));
  306. props.add (new ElementPositionProperty (this, "height", PositionPropertyBase::componentHeight));
  307. }
  308. //==============================================================================
  309. JucerDocument* PaintElement::getDocument() const
  310. {
  311. return owner->getDocument();
  312. }
  313. void PaintElement::changed()
  314. {
  315. repaint();
  316. owner->changed();
  317. }
  318. bool PaintElement::perform (UndoableAction* action, const String& actionName)
  319. {
  320. return owner->perform (action, actionName);
  321. }
  322. void PaintElement::parentHierarchyChanged()
  323. {
  324. updateSiblingComps();
  325. }
  326. //==============================================================================
  327. void PaintElement::drawExtraEditorGraphics (Graphics&, const Rectangle<int>& /*relativeTo*/)
  328. {
  329. }
  330. void PaintElement::paint (Graphics& g)
  331. {
  332. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  333. {
  334. auto area = pe->getComponentArea();
  335. g.saveState();
  336. g.setOrigin (area.getPosition() - Component::getPosition());
  337. area.setPosition (0, 0);
  338. g.saveState();
  339. g.reduceClipRegion (0, 0, area.getWidth(), area.getHeight());
  340. draw (g, getDocument()->getComponentLayout(), area);
  341. g.restoreState();
  342. drawExtraEditorGraphics (g, area);
  343. g.restoreState();
  344. if (selected)
  345. {
  346. const BorderSize<int> borderSize (border->getBorderThickness());
  347. auto baseColour = findColour (defaultHighlightColourId);
  348. drawResizableBorder (g, getWidth(), getHeight(), borderSize,
  349. (isMouseOverOrDragging() || border->isMouseOverOrDragging()),
  350. baseColour.withAlpha (owner->getSelectedElements().getSelectedItem (0) == this ? 1.0f : 0.3f));
  351. }
  352. else if (isMouseOverOrDragging())
  353. {
  354. drawMouseOverCorners (g, getWidth(), getHeight());
  355. }
  356. }
  357. }
  358. void PaintElement::resized()
  359. {
  360. border->setBounds (getLocalBounds());
  361. }
  362. void PaintElement::mouseDown (const MouseEvent& e)
  363. {
  364. dragging = false;
  365. if (owner != nullptr)
  366. {
  367. owner->getSelectedPoints().deselectAll();
  368. mouseDownSelectStatus = owner->getSelectedElements().addToSelectionOnMouseDown (this, e.mods);
  369. }
  370. if (e.mods.isPopupMenu())
  371. {
  372. showPopupMenu();
  373. return; // this may be deleted now..
  374. }
  375. }
  376. void PaintElement::mouseDrag (const MouseEvent& e)
  377. {
  378. if (! e.mods.isPopupMenu())
  379. {
  380. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  381. {
  382. auto area = pe->getComponentArea();
  383. if (selected && ! dragging)
  384. {
  385. dragging = e.mouseWasDraggedSinceMouseDown();
  386. if (dragging)
  387. owner->startDragging (area);
  388. }
  389. if (dragging)
  390. owner->dragSelectedComps (e.getDistanceFromDragStartX(),
  391. e.getDistanceFromDragStartY(),
  392. area);
  393. }
  394. }
  395. }
  396. void PaintElement::mouseUp (const MouseEvent& e)
  397. {
  398. if (owner != nullptr)
  399. {
  400. if (dragging)
  401. owner->endDragging();
  402. if (owner != nullptr)
  403. owner->getSelectedElements().addToSelectionOnMouseUp (this, e.mods, dragging, mouseDownSelectStatus);
  404. }
  405. }
  406. void PaintElement::resizeStart()
  407. {
  408. if (getHeight() > 0)
  409. originalAspectRatio = getWidth() / (double) getHeight();
  410. else
  411. originalAspectRatio = 1.0;
  412. }
  413. void PaintElement::resizeEnd()
  414. {
  415. }
  416. void PaintElement::checkBounds (Rectangle<int>& b,
  417. const Rectangle<int>& previousBounds,
  418. const Rectangle<int>& limits,
  419. const bool isStretchingTop,
  420. const bool isStretchingLeft,
  421. const bool isStretchingBottom,
  422. const bool isStretchingRight)
  423. {
  424. if (ModifierKeys::currentModifiers.isShiftDown())
  425. setFixedAspectRatio (originalAspectRatio);
  426. else
  427. setFixedAspectRatio (0.0);
  428. ComponentBoundsConstrainer::checkBounds (b, previousBounds, limits, isStretchingTop, isStretchingLeft, isStretchingBottom, isStretchingRight);
  429. if (auto* document = getDocument())
  430. {
  431. if (document->isSnapActive (true))
  432. {
  433. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  434. {
  435. auto area = pe->getComponentArea();
  436. int x = b.getX();
  437. int y = b.getY();
  438. int w = b.getWidth();
  439. int h = b.getHeight();
  440. x += borderThickness - area.getX();
  441. y += borderThickness - area.getY();
  442. w -= borderThickness * 2;
  443. h -= borderThickness * 2;
  444. int right = x + w;
  445. int bottom = y + h;
  446. if (isStretchingRight)
  447. right = document->snapPosition (right);
  448. if (isStretchingBottom)
  449. bottom = document->snapPosition (bottom);
  450. if (isStretchingLeft)
  451. x = document->snapPosition (x);
  452. if (isStretchingTop)
  453. y = document->snapPosition (y);
  454. w = (right - x) + borderThickness * 2;
  455. h = (bottom - y) + borderThickness * 2;
  456. x -= borderThickness - area.getX();
  457. y -= borderThickness - area.getY();
  458. b = { x, y, w, h };
  459. }
  460. }
  461. }
  462. }
  463. void PaintElement::applyBoundsToComponent (Component&, Rectangle<int> newBounds)
  464. {
  465. if (getBounds() != newBounds)
  466. {
  467. getDocument()->getUndoManager().undoCurrentTransactionOnly();
  468. auto dX = newBounds.getX() - getX();
  469. auto dY = newBounds.getY() - getY();
  470. auto dW = newBounds.getWidth() - getWidth();
  471. auto dH = newBounds.getHeight() - getHeight();
  472. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  473. setCurrentBounds (newBounds.expanded (-borderThickness, -borderThickness),
  474. pe->getComponentArea(), true);
  475. if (owner->getSelectedElements().getNumSelected() > 1)
  476. {
  477. for (auto selectedElement : owner->getSelectedElements())
  478. {
  479. if (selectedElement != this)
  480. {
  481. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (selectedElement->getParentComponent()))
  482. {
  483. Rectangle<int> r { selectedElement->getX() + dX, selectedElement->getY() + dY,
  484. selectedElement->getWidth() + dW, selectedElement->getHeight() + dH };
  485. selectedElement->setCurrentBounds (r.expanded (-borderThickness, -borderThickness),
  486. pe->getComponentArea(), true);
  487. }
  488. }
  489. }
  490. }
  491. }
  492. }
  493. Rectangle<int> PaintElement::getCurrentAbsoluteBounds() const
  494. {
  495. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  496. return position.getRectangle (pe->getComponentArea(), getDocument()->getComponentLayout());
  497. return {};
  498. }
  499. void PaintElement::getCurrentAbsoluteBoundsDouble (double& x, double& y, double& w, double& h) const
  500. {
  501. if (auto* pe = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  502. position.getRectangleDouble (x, y, w, h, pe->getComponentArea(), getDocument()->getComponentLayout());
  503. }
  504. void PaintElement::changeListenerCallback (ChangeBroadcaster*)
  505. {
  506. const bool nowSelected = owner != nullptr && owner->getSelectedElements().isSelected (this);
  507. if (selected != nowSelected)
  508. {
  509. selected = nowSelected;
  510. border->setVisible (nowSelected);
  511. repaint();
  512. selectionChanged (nowSelected);
  513. }
  514. updateSiblingComps();
  515. }
  516. void PaintElement::selectionChanged (const bool /*isSelected*/)
  517. {
  518. }
  519. void PaintElement::createSiblingComponents()
  520. {
  521. }
  522. void PaintElement::siblingComponentsChanged()
  523. {
  524. siblingComponents.clear();
  525. selfChangeListenerList.sendChangeMessage();
  526. }
  527. void PaintElement::updateSiblingComps()
  528. {
  529. if (selected && getParentComponent() != nullptr && owner->getSelectedElements().getNumSelected() == 1)
  530. {
  531. if (siblingComponents.size() == 0)
  532. createSiblingComponents();
  533. for (int i = siblingComponents.size(); --i >= 0;)
  534. siblingComponents.getUnchecked(i)->updatePosition();
  535. }
  536. else
  537. {
  538. siblingComponents.clear();
  539. }
  540. }
  541. void PaintElement::showPopupMenu()
  542. {
  543. auto* commandManager = &ProjucerApplication::getCommandManager();
  544. PopupMenu m;
  545. m.addCommandItem (commandManager, JucerCommandIDs::toFront);
  546. m.addCommandItem (commandManager, JucerCommandIDs::toBack);
  547. m.addSeparator();
  548. if (owner->getSelectedElements().getNumSelected() > 1)
  549. {
  550. m.addCommandItem (commandManager, JucerCommandIDs::alignTop);
  551. m.addCommandItem (commandManager, JucerCommandIDs::alignRight);
  552. m.addCommandItem (commandManager, JucerCommandIDs::alignBottom);
  553. m.addCommandItem (commandManager, JucerCommandIDs::alignLeft);
  554. m.addSeparator();
  555. }
  556. m.addCommandItem (commandManager, StandardApplicationCommandIDs::cut);
  557. m.addCommandItem (commandManager, StandardApplicationCommandIDs::copy);
  558. m.addCommandItem (commandManager, StandardApplicationCommandIDs::paste);
  559. m.addCommandItem (commandManager, StandardApplicationCommandIDs::del);
  560. m.show();
  561. }