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
17KB

  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. #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. ScopedPointer<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. ScopedPointer<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. ScopedPointer<PaintElement> deleter (e);
  125. ScopedPointer<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 = 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. ScopedPointer<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.createDocument ("", false, false));
  245. }
  246. void PaintRoutine::paste()
  247. {
  248. XmlDocument clip (SystemClipboard::getTextFromClipboard());
  249. ScopedPointer<XmlElement> doc (clip.getDocumentElement());
  250. if (doc != nullptr && doc->hasTagName (clipboardXmlTag))
  251. {
  252. selectedElements.deselectAll();
  253. selectedPoints.deselectAll();
  254. forEachXmlChildElement (*doc, e)
  255. if (PaintElement* newElement = addElementFromXml (*e, -1, true))
  256. selectedElements.addToSelection (newElement);
  257. }
  258. }
  259. void PaintRoutine::deleteSelected()
  260. {
  261. const SelectedItemSet<PaintElement*> temp1 (selectedElements);
  262. const SelectedItemSet<PathPoint*> temp2 (selectedPoints);
  263. if (temp2.getNumSelected() > 0)
  264. {
  265. selectedPoints.deselectAll();
  266. selectedPoints.changed (true); // synchronous message to get rid of any property components
  267. // if any points are selected, just delete them, and not the element, which may
  268. // also be selected..
  269. for (int i = temp2.getNumSelected(); --i >= 0;)
  270. temp2.getSelectedItem (i)->deleteFromPath();
  271. changed();
  272. }
  273. else if (temp1.getNumSelected() > 0)
  274. {
  275. selectedElements.deselectAll();
  276. selectedElements.changed (true);
  277. for (int i = temp1.getNumSelected(); --i >= 0;)
  278. removeElement (temp1.getSelectedItem (i), true);
  279. changed();
  280. }
  281. }
  282. void PaintRoutine::selectAll()
  283. {
  284. if (selectedPoints.getNumSelected() > 0)
  285. {
  286. if (const PaintElementPath* path = selectedPoints.getSelectedItem (0)->owner)
  287. for (int i = 0; i < path->getNumPoints(); ++i)
  288. selectedPoints.addToSelection (path->getPoint (i));
  289. }
  290. else
  291. {
  292. for (int i = 0; i < elements.size(); ++i)
  293. selectedElements.addToSelection (elements.getUnchecked (i));
  294. }
  295. }
  296. void PaintRoutine::selectedToFront()
  297. {
  298. const SelectedItemSet<PaintElement*> temp (selectedElements);
  299. for (int i = temp.getNumSelected(); --i >= 0;)
  300. elementToFront (temp.getSelectedItem(i), true);
  301. }
  302. void PaintRoutine::selectedToBack()
  303. {
  304. const SelectedItemSet<PaintElement*> temp (selectedElements);
  305. for (int i = 0; i < temp.getNumSelected(); ++i)
  306. elementToBack (temp.getSelectedItem(i), true);
  307. }
  308. void PaintRoutine::groupSelected()
  309. {
  310. PaintElementGroup::groupSelected (this);
  311. }
  312. void PaintRoutine::ungroupSelected()
  313. {
  314. const SelectedItemSet<PaintElement*> temp (selectedElements);
  315. for (int i = 0; i < temp.getNumSelected(); ++i)
  316. if (PaintElementGroup* const pg = dynamic_cast<PaintElementGroup*> (temp.getSelectedItem (i)))
  317. pg->ungroup (true);
  318. }
  319. void PaintRoutine::bringLostItemsBackOnScreen (const Rectangle<int>& parentArea)
  320. {
  321. for (auto* c : elements)
  322. {
  323. auto r = c->getCurrentBounds (parentArea);
  324. if (! r.intersects (parentArea))
  325. {
  326. r.setPosition (parentArea.getCentreX(), parentArea.getCentreY());
  327. c->setCurrentBounds (r, parentArea, true);
  328. }
  329. }
  330. }
  331. void PaintRoutine::startDragging (const Rectangle<int>& parentArea)
  332. {
  333. for (auto* c : elements)
  334. {
  335. auto r = c->getCurrentBounds (parentArea);
  336. c->getProperties().set ("xDragStart", r.getX());
  337. c->getProperties().set ("yDragStart", r.getY());
  338. }
  339. getDocument()->beginTransaction();
  340. }
  341. void PaintRoutine::dragSelectedComps (int dx, int dy, const Rectangle<int>& parentArea)
  342. {
  343. getDocument()->getUndoManager().undoCurrentTransactionOnly();
  344. if (document != nullptr && selectedElements.getNumSelected() > 1)
  345. {
  346. dx = document->snapPosition (dx);
  347. dy = document->snapPosition (dy);
  348. }
  349. for (int i = 0; i < selectedElements.getNumSelected(); ++i)
  350. {
  351. PaintElement* const c = selectedElements.getSelectedItem (i);
  352. const int startX = c->getProperties() ["xDragStart"];
  353. const int startY = c->getProperties() ["yDragStart"];
  354. Rectangle<int> r (c->getCurrentBounds (parentArea));
  355. if (document != nullptr && selectedElements.getNumSelected() == 1)
  356. {
  357. r.setPosition (document->snapPosition (startX + dx),
  358. document->snapPosition (startY + dy));
  359. }
  360. else
  361. {
  362. r.setPosition (startX + dx,
  363. startY + dy);
  364. }
  365. c->setCurrentBounds (r, parentArea, true);
  366. }
  367. changed();
  368. }
  369. void PaintRoutine::endDragging()
  370. {
  371. getDocument()->beginTransaction();
  372. }
  373. //==============================================================================
  374. void PaintRoutine::fillWithBackground (Graphics& g, const bool drawOpaqueBackground)
  375. {
  376. if ((! backgroundColour.isOpaque()) && drawOpaqueBackground)
  377. {
  378. g.fillCheckerBoard (Rectangle<int> (0, 0, g.getClipBounds().getRight(), g.getClipBounds().getBottom()),
  379. 50, 50,
  380. Colour (0xffdddddd).overlaidWith (backgroundColour),
  381. Colour (0xffffffff).overlaidWith (backgroundColour));
  382. }
  383. else
  384. {
  385. g.fillAll (backgroundColour);
  386. }
  387. }
  388. void PaintRoutine::drawElements (Graphics& g, const Rectangle<int>& relativeTo)
  389. {
  390. Component temp;
  391. temp.setBounds (relativeTo);
  392. for (auto* e : elements)
  393. e->draw (g, getDocument()->getComponentLayout(), relativeTo);
  394. }
  395. //==============================================================================
  396. void PaintRoutine::dropImageAt (const File& f, int x, int y)
  397. {
  398. ScopedPointer<Drawable> d (Drawable::createFromImageFile (f));
  399. if (d != nullptr)
  400. {
  401. auto bounds = d->getDrawableBounds();
  402. d = nullptr;
  403. auto* newElement = addNewElement (ObjectTypes::createNewImageElement (this), -1, true);
  404. if (auto* pei = dynamic_cast<PaintElementImage*> (newElement))
  405. {
  406. String resourceName (getDocument()->getResources().findUniqueName (f.getFileName()));
  407. if (auto* existingResource = getDocument()->getResources().getResourceForFile (f))
  408. {
  409. resourceName = existingResource->name;
  410. }
  411. else
  412. {
  413. MemoryBlock data;
  414. f.loadFileAsData (data);
  415. getDocument()->getResources().add (resourceName, f.getFullPathName(), data);
  416. }
  417. pei->setResource (resourceName, true);
  418. const int imageW = (int) (bounds.getRight() + 0.999f);
  419. const int imageH = (int) (bounds.getBottom() + 0.999f);
  420. RelativePositionedRectangle pr;
  421. pr.rect.setX (x - imageW / 2);
  422. pr.rect.setY (y - imageH / 2);
  423. pr.rect.setWidth (imageW);
  424. pr.rect.setHeight (imageH);
  425. pei->setPosition (pr, true);
  426. getSelectedElements().selectOnly (pei);
  427. }
  428. }
  429. }
  430. //==============================================================================
  431. const char* PaintRoutine::xmlTagName = "BACKGROUND";
  432. XmlElement* PaintRoutine::createXml() const
  433. {
  434. auto* xml = new XmlElement (xmlTagName);
  435. xml->setAttribute ("backgroundColour", backgroundColour.toString());
  436. for (auto* e : elements)
  437. xml->addChildElement (e->createXml());
  438. return xml;
  439. }
  440. bool PaintRoutine::loadFromXml (const XmlElement& xml)
  441. {
  442. if (xml.hasTagName (xmlTagName))
  443. {
  444. backgroundColour = Colour::fromString (xml.getStringAttribute ("backgroundColour", Colours::white.toString()));
  445. clear();
  446. forEachXmlChildElement (xml, e)
  447. if (auto* newElement = ObjectTypes::createElementForXml (e, this))
  448. elements.add (newElement);
  449. return true;
  450. }
  451. return false;
  452. }
  453. void PaintRoutine::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) const
  454. {
  455. if (! backgroundColour.isTransparent())
  456. paintMethodCode << "g.fillAll (" << CodeHelpers::colourToCode (backgroundColour) << ");\n\n";
  457. for (auto* e : elements)
  458. e->fillInGeneratedCode (code, paintMethodCode);
  459. }
  460. void PaintRoutine::applyCustomPaintSnippets (StringArray& snippets)
  461. {
  462. for (auto* e : elements)
  463. e->applyCustomPaintSnippets (snippets);
  464. }