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) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_PaintRoutine.h"
  19. #include "jucer_JucerDocument.h"
  20. #include "jucer_ObjectTypes.h"
  21. #include "paintelements/jucer_PaintElementUndoableAction.h"
  22. #include "paintelements/jucer_PaintElementPath.h"
  23. #include "paintelements/jucer_PaintElementImage.h"
  24. #include "paintelements/jucer_PaintElementGroup.h"
  25. #include "ui/jucer_JucerDocumentEditor.h"
  26. //==============================================================================
  27. PaintRoutine::PaintRoutine()
  28. : document (nullptr),
  29. backgroundColour (Colours::white)
  30. {
  31. clear();
  32. }
  33. PaintRoutine::~PaintRoutine()
  34. {
  35. elements.clear(); // do this explicitly before the scalar destructor because these
  36. // objects will be listeners on this object
  37. }
  38. //==============================================================================
  39. void PaintRoutine::changed()
  40. {
  41. if (document != nullptr)
  42. document->changed();
  43. }
  44. bool PaintRoutine::perform (UndoableAction* action, const String& actionName)
  45. {
  46. if (document != nullptr)
  47. return document->getUndoManager().perform (action, actionName);
  48. ScopedPointer<UndoableAction> deleter (action);
  49. action->perform();
  50. return false;
  51. }
  52. void PaintRoutine::setBackgroundColour (Colour newColour) noexcept
  53. {
  54. backgroundColour = newColour;
  55. changed();
  56. }
  57. void PaintRoutine::clear()
  58. {
  59. if (elements.size() > 0)
  60. {
  61. elements.clear();
  62. changed();
  63. }
  64. }
  65. //==============================================================================
  66. class AddXmlElementAction : public UndoableAction
  67. {
  68. public:
  69. AddXmlElementAction (PaintRoutine& routine_, XmlElement* xml_)
  70. : routine (routine_), xml (xml_)
  71. {
  72. }
  73. bool perform()
  74. {
  75. showCorrectTab();
  76. PaintElement* newElement = routine.addElementFromXml (*xml, -1, false);
  77. jassert (newElement != nullptr);
  78. indexAdded = routine.indexOfElement (newElement);
  79. jassert (indexAdded >= 0);
  80. return indexAdded >= 0;
  81. }
  82. bool undo()
  83. {
  84. showCorrectTab();
  85. routine.removeElement (routine.getElement (indexAdded), false);
  86. return true;
  87. }
  88. int getSizeInUnits() { return 10; }
  89. int indexAdded;
  90. private:
  91. PaintRoutine& routine;
  92. ScopedPointer<XmlElement> xml;
  93. void showCorrectTab() const
  94. {
  95. if (JucerDocumentEditor* const ed = JucerDocumentEditor::getActiveDocumentHolder())
  96. ed->showGraphics (&routine);
  97. }
  98. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AddXmlElementAction)
  99. };
  100. PaintElement* PaintRoutine::addElementFromXml (const XmlElement& xml, const int index, const bool undoable)
  101. {
  102. selectedPoints.deselectAll();
  103. if (undoable)
  104. {
  105. AddXmlElementAction* action = new AddXmlElementAction (*this, new XmlElement (xml));
  106. perform (action, "Add new element");
  107. return elements [action->indexAdded];
  108. }
  109. else
  110. {
  111. if (PaintElement* const newElement = ObjectTypes::createElementForXml (&xml, this))
  112. {
  113. elements.insert (index, newElement);
  114. changed();
  115. return newElement;
  116. }
  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 (int i = 0; i < elements.size(); ++i)
  242. {
  243. PaintElement* const pe = elements.getUnchecked(i);
  244. if (selectedElements.isSelected (pe))
  245. {
  246. XmlElement* const e = pe->createXml();
  247. clip.addChildElement (e);
  248. }
  249. }
  250. SystemClipboard::copyTextToClipboard (clip.createDocument ("", false, false));
  251. }
  252. void PaintRoutine::paste()
  253. {
  254. XmlDocument clip (SystemClipboard::getTextFromClipboard());
  255. ScopedPointer<XmlElement> doc (clip.getDocumentElement());
  256. if (doc != nullptr && doc->hasTagName (clipboardXmlTag))
  257. {
  258. selectedElements.deselectAll();
  259. selectedPoints.deselectAll();
  260. forEachXmlChildElement (*doc, e)
  261. if (PaintElement* newElement = addElementFromXml (*e, -1, true))
  262. selectedElements.addToSelection (newElement);
  263. }
  264. }
  265. void PaintRoutine::deleteSelected()
  266. {
  267. const SelectedItemSet <PaintElement*> temp1 (selectedElements);
  268. const SelectedItemSet <PathPoint*> temp2 (selectedPoints);
  269. if (temp2.getNumSelected() > 0)
  270. {
  271. selectedPoints.deselectAll();
  272. selectedPoints.changed (true); // synchronous message to get rid of any property components
  273. // if any points are selected, just delete them, and not the element, which may
  274. // also be selected..
  275. for (int i = temp2.getNumSelected(); --i >= 0;)
  276. temp2.getSelectedItem (i)->deleteFromPath();
  277. changed();
  278. }
  279. else if (temp1.getNumSelected() > 0)
  280. {
  281. selectedElements.deselectAll();
  282. selectedElements.changed (true);
  283. for (int i = temp1.getNumSelected(); --i >= 0;)
  284. removeElement (temp1.getSelectedItem (i), true);
  285. changed();
  286. }
  287. }
  288. void PaintRoutine::selectAll()
  289. {
  290. if (selectedPoints.getNumSelected() > 0)
  291. {
  292. if (const PaintElementPath* path = selectedPoints.getSelectedItem (0)->owner)
  293. for (int i = 0; i < path->getNumPoints(); ++i)
  294. selectedPoints.addToSelection (path->getPoint (i));
  295. }
  296. else
  297. {
  298. for (int i = 0; i < elements.size(); ++i)
  299. selectedElements.addToSelection (elements.getUnchecked (i));
  300. }
  301. }
  302. void PaintRoutine::selectedToFront()
  303. {
  304. const SelectedItemSet <PaintElement*> temp (selectedElements);
  305. for (int i = temp.getNumSelected(); --i >= 0;)
  306. elementToFront (temp.getSelectedItem(i), true);
  307. }
  308. void PaintRoutine::selectedToBack()
  309. {
  310. const SelectedItemSet <PaintElement*> temp (selectedElements);
  311. for (int i = 0; i < temp.getNumSelected(); ++i)
  312. elementToBack (temp.getSelectedItem(i), true);
  313. }
  314. void PaintRoutine::groupSelected()
  315. {
  316. PaintElementGroup::groupSelected (this);
  317. }
  318. void PaintRoutine::ungroupSelected()
  319. {
  320. const SelectedItemSet <PaintElement*> temp (selectedElements);
  321. for (int i = 0; i < temp.getNumSelected(); ++i)
  322. if (PaintElementGroup* const pg = dynamic_cast <PaintElementGroup*> (temp.getSelectedItem (i)))
  323. pg->ungroup (true);
  324. }
  325. void PaintRoutine::bringLostItemsBackOnScreen (const Rectangle<int>& parentArea)
  326. {
  327. for (int i = 0; i < elements.size(); ++i)
  328. {
  329. PaintElement* const c = elements[i];
  330. Rectangle<int> r (c->getCurrentBounds (parentArea));
  331. if (! r.intersects (parentArea))
  332. {
  333. r.setPosition (parentArea.getCentreX(), parentArea.getCentreY());
  334. c->setCurrentBounds (r, parentArea, true);
  335. }
  336. }
  337. }
  338. void PaintRoutine::startDragging (const Rectangle<int>& parentArea)
  339. {
  340. for (int i = 0; i < elements.size(); ++i)
  341. {
  342. PaintElement* const c = elements[i];
  343. Rectangle<int> r (c->getCurrentBounds (parentArea));
  344. c->getProperties().set ("xDragStart", r.getX());
  345. c->getProperties().set ("yDragStart", r.getY());
  346. }
  347. getDocument()->beginTransaction();
  348. }
  349. void PaintRoutine::dragSelectedComps (int dx, int dy, const Rectangle<int>& parentArea)
  350. {
  351. getDocument()->getUndoManager().undoCurrentTransactionOnly();
  352. if (document != nullptr && selectedElements.getNumSelected() > 1)
  353. {
  354. dx = document->snapPosition (dx);
  355. dy = document->snapPosition (dy);
  356. }
  357. for (int i = 0; i < selectedElements.getNumSelected(); ++i)
  358. {
  359. PaintElement* const c = selectedElements.getSelectedItem (i);
  360. const int startX = c->getProperties() ["xDragStart"];
  361. const int startY = c->getProperties() ["yDragStart"];
  362. Rectangle<int> r (c->getCurrentBounds (parentArea));
  363. if (document != nullptr && selectedElements.getNumSelected() == 1)
  364. {
  365. r.setPosition (document->snapPosition (startX + dx),
  366. document->snapPosition (startY + dy));
  367. }
  368. else
  369. {
  370. r.setPosition (startX + dx,
  371. startY + dy);
  372. }
  373. c->setCurrentBounds (r, parentArea, true);
  374. }
  375. changed();
  376. }
  377. void PaintRoutine::endDragging()
  378. {
  379. getDocument()->beginTransaction();
  380. }
  381. //==============================================================================
  382. void PaintRoutine::fillWithBackground (Graphics& g, const bool drawOpaqueBackground)
  383. {
  384. if ((! backgroundColour.isOpaque()) && drawOpaqueBackground)
  385. {
  386. g.fillCheckerBoard (Rectangle<int> (0, 0, g.getClipBounds().getRight(), g.getClipBounds().getBottom()),
  387. 50, 50,
  388. Colour (0xffdddddd).overlaidWith (backgroundColour),
  389. Colour (0xffffffff).overlaidWith (backgroundColour));
  390. }
  391. else
  392. {
  393. g.fillAll (backgroundColour);
  394. }
  395. }
  396. void PaintRoutine::drawElements (Graphics& g, const Rectangle<int>& relativeTo)
  397. {
  398. Component temp;
  399. temp.setBounds (relativeTo);
  400. for (int i = 0; i < elements.size(); ++i)
  401. elements.getUnchecked (i)->draw (g, getDocument()->getComponentLayout(), relativeTo);
  402. }
  403. //==============================================================================
  404. void PaintRoutine::dropImageAt (const File& f, int x, int y)
  405. {
  406. ScopedPointer<Drawable> d (Drawable::createFromImageFile (f));
  407. if (d != nullptr)
  408. {
  409. Rectangle<float> bounds (d->getDrawableBounds());
  410. d = nullptr;
  411. PaintElement* newElement
  412. = addNewElement (ObjectTypes::createNewImageElement (this), -1, true);
  413. if (PaintElementImage* pei = dynamic_cast <PaintElementImage*> (newElement))
  414. {
  415. String resourceName (getDocument()->getResources().findUniqueName (f.getFileName()));
  416. if (const BinaryResources::BinaryResource* existingResource = getDocument()->getResources().getResourceForFile (f))
  417. {
  418. resourceName = existingResource->name;
  419. }
  420. else
  421. {
  422. MemoryBlock data;
  423. f.loadFileAsData (data);
  424. getDocument()->getResources().add (resourceName, f.getFullPathName(), data);
  425. }
  426. pei->setResource (resourceName, true);
  427. const int imageW = (int) (bounds.getRight() + 0.999f);
  428. const int imageH = (int) (bounds.getBottom() + 0.999f);
  429. RelativePositionedRectangle pr;
  430. pr.rect.setX (x - imageW / 2);
  431. pr.rect.setY (y - imageH / 2);
  432. pr.rect.setWidth (imageW);
  433. pr.rect.setHeight (imageH);
  434. pei->setPosition (pr, true);
  435. getSelectedElements().selectOnly (pei);
  436. }
  437. }
  438. }
  439. //==============================================================================
  440. const char* PaintRoutine::xmlTagName = "BACKGROUND";
  441. XmlElement* PaintRoutine::createXml() const
  442. {
  443. XmlElement* const xml = new XmlElement (xmlTagName);
  444. xml->setAttribute ("backgroundColour", backgroundColour.toString());
  445. for (int i = 0; i < elements.size(); ++i)
  446. xml->addChildElement (elements.getUnchecked (i)->createXml());
  447. return xml;
  448. }
  449. bool PaintRoutine::loadFromXml (const XmlElement& xml)
  450. {
  451. if (xml.hasTagName (xmlTagName))
  452. {
  453. backgroundColour = Colour::fromString (xml.getStringAttribute ("backgroundColour", Colours::white.toString()));
  454. clear();
  455. forEachXmlChildElement (xml, e)
  456. if (PaintElement* const newElement = ObjectTypes::createElementForXml (e, this))
  457. elements.add (newElement);
  458. return true;
  459. }
  460. return false;
  461. }
  462. void PaintRoutine::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) const
  463. {
  464. if (! backgroundColour.isTransparent())
  465. paintMethodCode << "g.fillAll (" << CodeHelpers::colourToCode (backgroundColour) << ");\n\n";
  466. for (int i = 0; i < elements.size(); ++i)
  467. elements[i]->fillInGeneratedCode (code, paintMethodCode);
  468. }