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.

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