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.

630 lines
18KB

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