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.

638 lines
19KB

  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. #include "../Application/jucer_Headers.h"
  20. #include "jucer_PaintRoutine.h"
  21. #include "jucer_JucerDocument.h"
  22. #include "jucer_ObjectTypes.h"
  23. #include "PaintElements/jucer_PaintElementUndoableAction.h"
  24. #include "PaintElements/jucer_PaintElementPath.h"
  25. #include "PaintElements/jucer_PaintElementImage.h"
  26. #include "PaintElements/jucer_PaintElementGroup.h"
  27. #include "UI/jucer_JucerDocumentEditor.h"
  28. #include "../Application/jucer_Application.h"
  29. //==============================================================================
  30. PaintRoutine::PaintRoutine()
  31. : document (nullptr),
  32. backgroundColour (ProjucerApplication::getApp().lookAndFeel.findColour (backgroundColourId))
  33. {
  34. clear();
  35. }
  36. PaintRoutine::~PaintRoutine()
  37. {
  38. elements.clear(); // do this explicitly before the scalar destructor because these
  39. // objects will be listeners on this object
  40. }
  41. //==============================================================================
  42. void PaintRoutine::changed()
  43. {
  44. if (document != nullptr)
  45. document->changed();
  46. }
  47. bool PaintRoutine::perform (UndoableAction* action, const String& actionName)
  48. {
  49. if (document != nullptr)
  50. return document->getUndoManager().perform (action, actionName);
  51. std::unique_ptr<UndoableAction> deleter (action);
  52. action->perform();
  53. return false;
  54. }
  55. void PaintRoutine::setBackgroundColour (Colour newColour) noexcept
  56. {
  57. backgroundColour = newColour;
  58. changed();
  59. }
  60. void PaintRoutine::clear()
  61. {
  62. if (elements.size() > 0)
  63. {
  64. elements.clear();
  65. changed();
  66. }
  67. }
  68. //==============================================================================
  69. class AddXmlElementAction : public UndoableAction
  70. {
  71. public:
  72. AddXmlElementAction (PaintRoutine& routine_, XmlElement* xml_)
  73. : routine (routine_), xml (xml_)
  74. {
  75. }
  76. bool perform()
  77. {
  78. showCorrectTab();
  79. PaintElement* newElement = routine.addElementFromXml (*xml, -1, false);
  80. jassert (newElement != nullptr);
  81. indexAdded = routine.indexOfElement (newElement);
  82. jassert (indexAdded >= 0);
  83. return indexAdded >= 0;
  84. }
  85. bool undo()
  86. {
  87. showCorrectTab();
  88. routine.removeElement (routine.getElement (indexAdded), false);
  89. return true;
  90. }
  91. int getSizeInUnits() { return 10; }
  92. int indexAdded;
  93. private:
  94. PaintRoutine& routine;
  95. std::unique_ptr<XmlElement> xml;
  96. void showCorrectTab() const
  97. {
  98. if (JucerDocumentEditor* const ed = JucerDocumentEditor::getActiveDocumentHolder())
  99. ed->showGraphics (&routine);
  100. }
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AddXmlElementAction)
  102. };
  103. PaintElement* PaintRoutine::addElementFromXml (const XmlElement& xml, const int index, const bool undoable)
  104. {
  105. selectedPoints.deselectAll();
  106. if (undoable && document != nullptr)
  107. {
  108. AddXmlElementAction* action = new AddXmlElementAction (*this, new XmlElement (xml));
  109. document->getUndoManager().perform (action, "Add new element");
  110. return elements [action->indexAdded];
  111. }
  112. if (PaintElement* const newElement = ObjectTypes::createElementForXml (&xml, this))
  113. {
  114. elements.insert (index, newElement);
  115. changed();
  116. return newElement;
  117. }
  118. return nullptr;
  119. }
  120. PaintElement* PaintRoutine::addNewElement (PaintElement* e, const int index, const bool undoable)
  121. {
  122. if (e != nullptr)
  123. {
  124. std::unique_ptr<PaintElement> deleter (e);
  125. std::unique_ptr<XmlElement> xml (e->createXml());
  126. e = addElementFromXml (*xml, index, undoable);
  127. }
  128. return e;
  129. }
  130. //==============================================================================
  131. class DeleteElementAction : public PaintElementUndoableAction <PaintElement>
  132. {
  133. public:
  134. DeleteElementAction (PaintElement* const element)
  135. : PaintElementUndoableAction <PaintElement> (element),
  136. oldIndex (-1)
  137. {
  138. xml.reset (element->createXml());
  139. oldIndex = routine.indexOfElement (element);
  140. }
  141. bool perform()
  142. {
  143. showCorrectTab();
  144. routine.removeElement (getElement(), false);
  145. return true;
  146. }
  147. bool undo()
  148. {
  149. PaintElement* newElement = routine.addElementFromXml (*xml, oldIndex, false);
  150. showCorrectTab();
  151. return newElement != nullptr;
  152. }
  153. int getSizeInUnits() { return 10; }
  154. private:
  155. std::unique_ptr<XmlElement> xml;
  156. int oldIndex;
  157. };
  158. void PaintRoutine::removeElement (PaintElement* element, const bool undoable)
  159. {
  160. if (elements.contains (element))
  161. {
  162. if (undoable)
  163. {
  164. perform (new DeleteElementAction (element),
  165. "Delete " + element->getTypeName());
  166. }
  167. else
  168. {
  169. selectedElements.deselect (element);
  170. selectedPoints.deselectAll();
  171. selectedPoints.changed (true);
  172. selectedElements.changed (true);
  173. elements.removeObject (element);
  174. changed();
  175. }
  176. }
  177. }
  178. //==============================================================================
  179. class FrontOrBackElementAction : public PaintElementUndoableAction <PaintElement>
  180. {
  181. public:
  182. FrontOrBackElementAction (PaintElement* const element, int newIndex_)
  183. : PaintElementUndoableAction <PaintElement> (element),
  184. newIndex (newIndex_)
  185. {
  186. oldIndex = routine.indexOfElement (element);
  187. }
  188. bool perform()
  189. {
  190. showCorrectTab();
  191. PaintElement* e = routine.getElement (oldIndex);
  192. routine.moveElementZOrder (oldIndex, newIndex);
  193. newIndex = routine.indexOfElement (e);
  194. return true;
  195. }
  196. bool undo()
  197. {
  198. showCorrectTab();
  199. routine.moveElementZOrder (newIndex, oldIndex);
  200. return true;
  201. }
  202. private:
  203. int newIndex, oldIndex;
  204. };
  205. void PaintRoutine::moveElementZOrder (int oldIndex, int newIndex)
  206. {
  207. jassert (elements [oldIndex] != nullptr);
  208. if (oldIndex != newIndex && elements [oldIndex] != nullptr)
  209. {
  210. elements.move (oldIndex, newIndex);
  211. changed();
  212. }
  213. }
  214. void PaintRoutine::elementToFront (PaintElement* element, const bool undoable)
  215. {
  216. if (element != nullptr && elements.contains (element))
  217. {
  218. if (undoable)
  219. perform (new FrontOrBackElementAction (element, -1), "Move elements to front");
  220. else
  221. moveElementZOrder (elements.indexOf (element), -1);
  222. }
  223. }
  224. void PaintRoutine::elementToBack (PaintElement* element, const bool undoable)
  225. {
  226. if (element != nullptr && elements.contains (element))
  227. {
  228. if (undoable)
  229. perform (new FrontOrBackElementAction (element, 0), "Move elements to back");
  230. else
  231. moveElementZOrder (elements.indexOf (element), 0);
  232. }
  233. }
  234. //==============================================================================
  235. const char* const PaintRoutine::clipboardXmlTag = "PAINTELEMENTS";
  236. void PaintRoutine::copySelectedToClipboard()
  237. {
  238. if (selectedElements.getNumSelected() == 0)
  239. return;
  240. XmlElement clip (clipboardXmlTag);
  241. for (auto* pe : elements)
  242. if (selectedElements.isSelected (pe))
  243. clip.addChildElement (pe->createXml());
  244. SystemClipboard::copyTextToClipboard (clip.toString());
  245. }
  246. void PaintRoutine::paste()
  247. {
  248. if (auto doc = parseXMLIfTagMatches (SystemClipboard::getTextFromClipboard(), clipboardXmlTag))
  249. {
  250. selectedElements.deselectAll();
  251. selectedPoints.deselectAll();
  252. forEachXmlChildElement (*doc, e)
  253. if (PaintElement* newElement = addElementFromXml (*e, -1, true))
  254. selectedElements.addToSelection (newElement);
  255. }
  256. }
  257. void PaintRoutine::deleteSelected()
  258. {
  259. const SelectedItemSet<PaintElement*> temp1 (selectedElements);
  260. const SelectedItemSet<PathPoint*> temp2 (selectedPoints);
  261. if (temp2.getNumSelected() > 0)
  262. {
  263. selectedPoints.deselectAll();
  264. selectedPoints.changed (true); // synchronous message to get rid of any property components
  265. // if any points are selected, just delete them, and not the element, which may
  266. // also be selected..
  267. for (int i = temp2.getNumSelected(); --i >= 0;)
  268. temp2.getSelectedItem (i)->deleteFromPath();
  269. changed();
  270. }
  271. else if (temp1.getNumSelected() > 0)
  272. {
  273. selectedElements.deselectAll();
  274. selectedElements.changed (true);
  275. for (int i = temp1.getNumSelected(); --i >= 0;)
  276. removeElement (temp1.getSelectedItem (i), true);
  277. changed();
  278. }
  279. }
  280. void PaintRoutine::selectAll()
  281. {
  282. if (selectedPoints.getNumSelected() > 0)
  283. {
  284. if (const PaintElementPath* path = selectedPoints.getSelectedItem (0)->owner)
  285. for (int i = 0; i < path->getNumPoints(); ++i)
  286. selectedPoints.addToSelection (path->getPoint (i));
  287. }
  288. else
  289. {
  290. for (int i = 0; i < elements.size(); ++i)
  291. selectedElements.addToSelection (elements.getUnchecked (i));
  292. }
  293. }
  294. void PaintRoutine::selectedToFront()
  295. {
  296. const SelectedItemSet<PaintElement*> temp (selectedElements);
  297. for (int i = temp.getNumSelected(); --i >= 0;)
  298. elementToFront (temp.getSelectedItem(i), true);
  299. }
  300. void PaintRoutine::selectedToBack()
  301. {
  302. const SelectedItemSet<PaintElement*> temp (selectedElements);
  303. for (int i = 0; i < temp.getNumSelected(); ++i)
  304. elementToBack (temp.getSelectedItem(i), true);
  305. }
  306. void PaintRoutine::alignTop()
  307. {
  308. if (selectedElements.getNumSelected() > 1)
  309. {
  310. auto* main = selectedElements.getSelectedItem (0);
  311. auto yPos = main->getY();
  312. for (auto* other : selectedElements)
  313. {
  314. if (other != main)
  315. other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (other->getX(),
  316. yPos), main, true);
  317. }
  318. }
  319. }
  320. void PaintRoutine::alignRight()
  321. {
  322. if (selectedElements.getNumSelected() > 1)
  323. {
  324. auto* main = selectedElements.getSelectedItem (0);
  325. auto rightPos = main->getRight();
  326. for (auto* other : selectedElements)
  327. {
  328. if (other != main)
  329. other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (rightPos - other->getWidth(),
  330. other->getY()), main, true);
  331. }
  332. }
  333. }
  334. void PaintRoutine::alignBottom()
  335. {
  336. if (selectedElements.getNumSelected() > 1)
  337. {
  338. auto* main = selectedElements.getSelectedItem (0);
  339. auto bottomPos = main->getBottom();
  340. for (auto* other : selectedElements)
  341. {
  342. if (other != main)
  343. other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (other->getX(),
  344. bottomPos - other->getHeight()), main, true);
  345. }
  346. }
  347. }
  348. void PaintRoutine::alignLeft()
  349. {
  350. if (selectedElements.getNumSelected() > 1)
  351. {
  352. auto* main = selectedElements.getSelectedItem (0);
  353. auto xPos = main->getX();
  354. for (auto* other : selectedElements)
  355. {
  356. if (other != main)
  357. other->setPaintElementBoundsAndProperties (other, other->getBounds().withPosition (xPos,
  358. other->getY()), main, true);
  359. }
  360. }
  361. }
  362. void PaintRoutine::groupSelected()
  363. {
  364. PaintElementGroup::groupSelected (this);
  365. }
  366. void PaintRoutine::ungroupSelected()
  367. {
  368. const SelectedItemSet<PaintElement*> temp (selectedElements);
  369. for (int i = 0; i < temp.getNumSelected(); ++i)
  370. if (PaintElementGroup* const pg = dynamic_cast<PaintElementGroup*> (temp.getSelectedItem (i)))
  371. pg->ungroup (true);
  372. }
  373. void PaintRoutine::bringLostItemsBackOnScreen (const Rectangle<int>& parentArea)
  374. {
  375. for (auto* c : elements)
  376. {
  377. auto r = c->getCurrentBounds (parentArea);
  378. if (! r.intersects (parentArea))
  379. {
  380. r.setPosition (parentArea.getCentreX(), parentArea.getCentreY());
  381. c->setCurrentBounds (r, parentArea, true);
  382. }
  383. }
  384. }
  385. void PaintRoutine::startDragging (const Rectangle<int>& parentArea)
  386. {
  387. for (auto* c : elements)
  388. {
  389. auto r = c->getCurrentBounds (parentArea);
  390. c->getProperties().set ("xDragStart", r.getX());
  391. c->getProperties().set ("yDragStart", r.getY());
  392. }
  393. getDocument()->beginTransaction();
  394. }
  395. void PaintRoutine::dragSelectedComps (int dx, int dy, const Rectangle<int>& parentArea)
  396. {
  397. getDocument()->getUndoManager().undoCurrentTransactionOnly();
  398. if (document != nullptr && selectedElements.getNumSelected() > 1)
  399. {
  400. dx = document->snapPosition (dx);
  401. dy = document->snapPosition (dy);
  402. }
  403. for (int i = 0; i < selectedElements.getNumSelected(); ++i)
  404. {
  405. PaintElement* const c = selectedElements.getSelectedItem (i);
  406. const int startX = c->getProperties() ["xDragStart"];
  407. const int startY = c->getProperties() ["yDragStart"];
  408. Rectangle<int> r (c->getCurrentBounds (parentArea));
  409. if (document != nullptr && selectedElements.getNumSelected() == 1)
  410. {
  411. r.setPosition (document->snapPosition (startX + dx),
  412. document->snapPosition (startY + dy));
  413. }
  414. else
  415. {
  416. r.setPosition (startX + dx,
  417. startY + dy);
  418. }
  419. c->setCurrentBounds (r, parentArea, true);
  420. }
  421. changed();
  422. }
  423. void PaintRoutine::endDragging()
  424. {
  425. getDocument()->beginTransaction();
  426. }
  427. //==============================================================================
  428. void PaintRoutine::fillWithBackground (Graphics& g, const bool drawOpaqueBackground)
  429. {
  430. if ((! backgroundColour.isOpaque()) && drawOpaqueBackground)
  431. {
  432. g.fillCheckerBoard (Rectangle<float> ((float) g.getClipBounds().getRight(),
  433. (float) g.getClipBounds().getBottom()),
  434. 50.0f, 50.0f,
  435. Colour (0xffdddddd).overlaidWith (backgroundColour),
  436. Colour (0xffffffff).overlaidWith (backgroundColour));
  437. }
  438. else
  439. {
  440. g.fillAll (backgroundColour);
  441. }
  442. }
  443. void PaintRoutine::drawElements (Graphics& g, const Rectangle<int>& relativeTo)
  444. {
  445. Component temp;
  446. temp.setBounds (relativeTo);
  447. for (auto* e : elements)
  448. e->draw (g, getDocument()->getComponentLayout(), relativeTo);
  449. }
  450. //==============================================================================
  451. void PaintRoutine::dropImageAt (const File& f, int x, int y)
  452. {
  453. std::unique_ptr<Drawable> d (Drawable::createFromImageFile (f));
  454. if (d != nullptr)
  455. {
  456. auto bounds = d->getDrawableBounds();
  457. d.reset();
  458. auto* newElement = addNewElement (ObjectTypes::createNewImageElement (this), -1, true);
  459. if (auto* pei = dynamic_cast<PaintElementImage*> (newElement))
  460. {
  461. String resourceName (getDocument()->getResources().findUniqueName (f.getFileName()));
  462. if (auto* existingResource = getDocument()->getResources().getResourceForFile (f))
  463. {
  464. resourceName = existingResource->name;
  465. }
  466. else
  467. {
  468. MemoryBlock data;
  469. f.loadFileAsData (data);
  470. getDocument()->getResources().add (resourceName, f.getFullPathName(), data);
  471. }
  472. pei->setResource (resourceName, true);
  473. const int imageW = (int) (bounds.getRight() + 0.999f);
  474. const int imageH = (int) (bounds.getBottom() + 0.999f);
  475. RelativePositionedRectangle pr;
  476. pr.rect.setX (x - imageW / 2);
  477. pr.rect.setY (y - imageH / 2);
  478. pr.rect.setWidth (imageW);
  479. pr.rect.setHeight (imageH);
  480. pei->setPosition (pr, true);
  481. getSelectedElements().selectOnly (pei);
  482. }
  483. }
  484. }
  485. //==============================================================================
  486. const char* PaintRoutine::xmlTagName = "BACKGROUND";
  487. XmlElement* PaintRoutine::createXml() const
  488. {
  489. auto* xml = new XmlElement (xmlTagName);
  490. xml->setAttribute ("backgroundColour", backgroundColour.toString());
  491. for (auto* e : elements)
  492. xml->addChildElement (e->createXml());
  493. return xml;
  494. }
  495. bool PaintRoutine::loadFromXml (const XmlElement& xml)
  496. {
  497. if (xml.hasTagName (xmlTagName))
  498. {
  499. backgroundColour = Colour::fromString (xml.getStringAttribute ("backgroundColour", Colours::white.toString()));
  500. clear();
  501. forEachXmlChildElement (xml, e)
  502. if (auto* newElement = ObjectTypes::createElementForXml (e, this))
  503. elements.add (newElement);
  504. return true;
  505. }
  506. return false;
  507. }
  508. void PaintRoutine::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) const
  509. {
  510. if (! backgroundColour.isTransparent())
  511. paintMethodCode << "g.fillAll (" << CodeHelpers::colourToCode (backgroundColour) << ");\n\n";
  512. for (auto* e : elements)
  513. e->fillInGeneratedCode (code, paintMethodCode);
  514. }
  515. void PaintRoutine::applyCustomPaintSnippets (StringArray& snippets)
  516. {
  517. for (auto* e : elements)
  518. e->applyCustomPaintSnippets (snippets);
  519. }