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.

275 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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 "../../Application/jucer_Application.h"
  15. #include "../UI/jucer_JucerCommandIDs.h"
  16. #include "jucer_PaintRoutineEditor.h"
  17. #include "../jucer_ObjectTypes.h"
  18. #include "jucer_JucerDocumentEditor.h"
  19. //==============================================================================
  20. PaintRoutineEditor::PaintRoutineEditor (PaintRoutine& pr, JucerDocument& doc,
  21. JucerDocumentEditor* docHolder)
  22. : graphics (pr),
  23. document (doc),
  24. documentHolder (docHolder),
  25. componentOverlay (nullptr),
  26. componentOverlayOpacity (0.0f)
  27. {
  28. refreshAllElements();
  29. setSize (document.getInitialWidth(),
  30. document.getInitialHeight());
  31. }
  32. PaintRoutineEditor::~PaintRoutineEditor()
  33. {
  34. document.removeChangeListener (this);
  35. removeAllElementComps();
  36. removeChildComponent (&lassoComp);
  37. deleteAllChildren();
  38. }
  39. void PaintRoutineEditor::removeAllElementComps()
  40. {
  41. for (int i = getNumChildComponents(); --i >= 0;)
  42. if (PaintElement* const e = dynamic_cast<PaintElement*> (getChildComponent (i)))
  43. removeChildComponent (e);
  44. }
  45. Rectangle<int> PaintRoutineEditor::getComponentArea() const
  46. {
  47. if (document.isFixedSize())
  48. return Rectangle<int> ((getWidth() - document.getInitialWidth()) / 2,
  49. (getHeight() - document.getInitialHeight()) / 2,
  50. document.getInitialWidth(),
  51. document.getInitialHeight());
  52. return getLocalBounds().reduced (4);
  53. }
  54. //==============================================================================
  55. void PaintRoutineEditor::paint (Graphics& g)
  56. {
  57. const Rectangle<int> clip (getComponentArea());
  58. g.reduceClipRegion (clip);
  59. g.setOrigin (clip.getPosition());
  60. graphics.fillWithBackground (g, true);
  61. grid.draw (g, &graphics);
  62. }
  63. void PaintRoutineEditor::paintOverChildren (Graphics& g)
  64. {
  65. if (componentOverlay.isNull() && document.getComponentOverlayOpacity() > 0.0f)
  66. updateComponentOverlay();
  67. if (componentOverlay.isValid())
  68. {
  69. const Rectangle<int> clip (getComponentArea());
  70. g.drawImageAt (componentOverlay, clip.getX(), clip.getY());
  71. }
  72. }
  73. void PaintRoutineEditor::resized()
  74. {
  75. if (getWidth() > 0 && getHeight() > 0)
  76. {
  77. componentOverlay = Image();
  78. refreshAllElements();
  79. }
  80. }
  81. void PaintRoutineEditor::updateChildBounds()
  82. {
  83. const Rectangle<int> clip (getComponentArea());
  84. for (int i = 0; i < getNumChildComponents(); ++i)
  85. if (PaintElement* const e = dynamic_cast<PaintElement*> (getChildComponent (i)))
  86. e->updateBounds (clip);
  87. }
  88. void PaintRoutineEditor::updateComponentOverlay()
  89. {
  90. if (componentOverlay.isValid())
  91. repaint();
  92. componentOverlay = Image();
  93. componentOverlayOpacity = document.getComponentOverlayOpacity();
  94. if (componentOverlayOpacity > 0.0f)
  95. {
  96. if (documentHolder != nullptr)
  97. componentOverlay = documentHolder->createComponentLayerSnapshot();
  98. if (componentOverlay.isValid())
  99. {
  100. componentOverlay.multiplyAllAlphas (componentOverlayOpacity);
  101. repaint();
  102. }
  103. }
  104. }
  105. void PaintRoutineEditor::visibilityChanged()
  106. {
  107. document.beginTransaction();
  108. if (isVisible())
  109. {
  110. refreshAllElements();
  111. document.addChangeListener (this);
  112. }
  113. else
  114. {
  115. document.removeChangeListener (this);
  116. componentOverlay = Image();
  117. }
  118. }
  119. void PaintRoutineEditor::refreshAllElements()
  120. {
  121. for (int i = getNumChildComponents(); --i >= 0;)
  122. if (auto* e = dynamic_cast<PaintElement*> (getChildComponent (i)))
  123. if (! graphics.containsElement (e))
  124. removeChildComponent (e);
  125. Component* last = nullptr;
  126. for (int i = graphics.getNumElements(); --i >= 0;)
  127. {
  128. auto* e = graphics.getElement (i);
  129. addAndMakeVisible (e);
  130. if (last != nullptr)
  131. e->toBehind (last);
  132. else
  133. e->toFront (false);
  134. last = e;
  135. }
  136. updateChildBounds();
  137. if (grid.updateFromDesign (document))
  138. repaint();
  139. if (currentBackgroundColour != graphics.getBackgroundColour())
  140. {
  141. currentBackgroundColour = graphics.getBackgroundColour();
  142. repaint();
  143. }
  144. if (componentOverlayOpacity != document.getComponentOverlayOpacity())
  145. {
  146. componentOverlay = Image();
  147. componentOverlayOpacity = document.getComponentOverlayOpacity();
  148. repaint();
  149. }
  150. }
  151. void PaintRoutineEditor::changeListenerCallback (ChangeBroadcaster*)
  152. {
  153. refreshAllElements();
  154. }
  155. void PaintRoutineEditor::mouseDown (const MouseEvent& e)
  156. {
  157. if (e.mods.isPopupMenu())
  158. {
  159. ApplicationCommandManager* commandManager = &ProjucerApplication::getCommandManager();
  160. PopupMenu m;
  161. m.addCommandItem (commandManager, JucerCommandIDs::editCompLayout);
  162. m.addCommandItem (commandManager, JucerCommandIDs::editCompGraphics);
  163. m.addSeparator();
  164. for (int i = 0; i < ObjectTypes::numElementTypes; ++i)
  165. m.addCommandItem (commandManager, JucerCommandIDs::newElementBase + i);
  166. m.show();
  167. }
  168. else
  169. {
  170. addChildComponent (lassoComp);
  171. lassoComp.beginLasso (e, this);
  172. }
  173. }
  174. void PaintRoutineEditor::mouseDrag (const MouseEvent& e)
  175. {
  176. lassoComp.toFront (false);
  177. lassoComp.dragLasso (e);
  178. }
  179. void PaintRoutineEditor::mouseUp (const MouseEvent& e)
  180. {
  181. lassoComp.endLasso();
  182. if (! (e.mouseWasDraggedSinceMouseDown() || e.mods.isAnyModifierKeyDown()))
  183. {
  184. graphics.getSelectedElements().deselectAll();
  185. graphics.getSelectedPoints().deselectAll();
  186. }
  187. }
  188. void PaintRoutineEditor::findLassoItemsInArea (Array <PaintElement*>& results, const Rectangle<int>& lasso)
  189. {
  190. for (int i = 0; i < getNumChildComponents(); ++i)
  191. if (PaintElement* const e = dynamic_cast<PaintElement*> (getChildComponent (i)))
  192. if (e->getBounds().expanded (-e->borderThickness).intersects (lasso))
  193. results.add (e);
  194. }
  195. SelectedItemSet <PaintElement*>& PaintRoutineEditor::getLassoSelection()
  196. {
  197. return graphics.getSelectedElements();
  198. }
  199. bool PaintRoutineEditor::isInterestedInFileDrag (const StringArray& files)
  200. {
  201. return File::createFileWithoutCheckingPath (files[0])
  202. .hasFileExtension ("jpg;jpeg;png;gif;svg");
  203. }
  204. void PaintRoutineEditor::filesDropped (const StringArray& filenames, int x, int y)
  205. {
  206. const File f (filenames [0]);
  207. if (f.existsAsFile())
  208. {
  209. std::unique_ptr<Drawable> d (Drawable::createFromImageFile (f));
  210. if (d != nullptr)
  211. {
  212. d.reset();
  213. document.beginTransaction();
  214. graphics.dropImageAt (f,
  215. jlimit (10, getWidth() - 10, x),
  216. jlimit (10, getHeight() - 10, y));
  217. document.beginTransaction();
  218. }
  219. }
  220. }