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.

578 lines
16KB

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