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.

430 lines
14KB

  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 "../../Application/jucer_Headers.h"
  20. #include "jucer_PaintElementImage.h"
  21. PaintElementImage::PaintElementImage (PaintRoutine* pr)
  22. : PaintElement (pr, "Image"),
  23. opacity (1.0),
  24. mode (stretched)
  25. {
  26. }
  27. PaintElementImage::~PaintElementImage() {}
  28. const Drawable* PaintElementImage::getDrawable()
  29. {
  30. if (JucerDocument* const document = getDocument())
  31. return document->getResources().getDrawable (resourceName);
  32. return nullptr;
  33. }
  34. void PaintElementImage::draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  35. {
  36. const Rectangle<int> r (position.getRectangle (parentArea, layout));
  37. if (const Drawable* const image = getDrawable())
  38. {
  39. image->drawWithin (g, r.toFloat(),
  40. mode == stretched ? RectanglePlacement::stretchToFit
  41. : (mode == proportionalReducingOnly ? (RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize)
  42. : RectanglePlacement::centred),
  43. (float) opacity);
  44. }
  45. else
  46. {
  47. g.setColour (Colours::grey.withAlpha (0.5f));
  48. g.fillRect (r);
  49. g.setColour (Colours::black);
  50. g.drawText ("(image missing)",
  51. r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  52. Justification::centred, true);
  53. }
  54. }
  55. //==============================================================================
  56. void PaintElementImage::getEditableProperties (Array <PropertyComponent*>& props, bool multipleSelected)
  57. {
  58. PaintElement::getEditableProperties (props, multipleSelected);
  59. props.add (new ImageElementResourceProperty (this));
  60. props.add (new StretchModeProperty (this));
  61. props.add (new OpacityProperty (this));
  62. props.add (new ResetSizeProperty (this));
  63. }
  64. void PaintElementImage::fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  65. {
  66. if (opacity > 0)
  67. {
  68. String x, y, w, h, r;
  69. positionToCode (position, getDocument()->getComponentLayout(), x, y, w, h);
  70. r << "{\n"
  71. << " int x = " << x << ", y = " << y << ", width = " << w << ", height = " << h << ";\n"
  72. << " //[UserPaintCustomArguments] Customize the painting arguments here..\n"
  73. << customPaintCode
  74. << " //[/UserPaintCustomArguments]\n";
  75. if (dynamic_cast<const DrawableImage*> (getDrawable()))
  76. {
  77. const String imageVariable ("cachedImage_" + resourceName.replace ("::", "_") + "_" + String (code.getUniqueSuffix()));
  78. code.addImageResourceLoader (imageVariable, resourceName);
  79. if (opacity >= 254.0 / 255.0)
  80. r << " g.setColour (Colours::black);\n";
  81. else
  82. r << " g.setColour (Colours::black.withAlpha (" << CodeHelpers::floatLiteral (opacity, 3) << "));\n";
  83. if (mode == stretched)
  84. {
  85. r << " g.drawImage (" << imageVariable << ",\n"
  86. << " x, y, width, height,\n"
  87. << " 0, 0, " << imageVariable << ".getWidth(), " << imageVariable << ".getHeight());\n";
  88. }
  89. else
  90. {
  91. r << " g.drawImageWithin (" << imageVariable << ",\n"
  92. << " x, y, width, height,\n"
  93. << " ";
  94. if (mode == proportionalReducingOnly)
  95. r << "RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize";
  96. else
  97. r << "RectanglePlacement::centred";
  98. r << ",\n"
  99. << " false);\n";
  100. }
  101. }
  102. else
  103. {
  104. if (resourceName.isNotEmpty())
  105. {
  106. const String imageVariable ("drawable" + String (code.getUniqueSuffix()));
  107. code.privateMemberDeclarations
  108. << "std::unique_ptr<Drawable> " << imageVariable << ";\n";
  109. code.constructorCode
  110. << imageVariable << " = Drawable::createFromImageData ("
  111. << resourceName << ", " << resourceName << "Size);\n";
  112. code.destructorCode
  113. << imageVariable << " = nullptr;\n";
  114. if (opacity >= 254.0 / 255.0)
  115. r << " g.setColour (Colours::black);\n";
  116. else
  117. r << " g.setColour (Colours::black.withAlpha (" << CodeHelpers::floatLiteral (opacity, 3) << "));\n";
  118. r << " jassert (" << imageVariable << " != 0);\n"
  119. << " if (" << imageVariable << " != 0)\n"
  120. << " " << imageVariable << "->drawWithin (g, Rectangle<float> (x, y, width, height),\n"
  121. << " " << String::repeatedString (" ", imageVariable.length() + 18)
  122. << (mode == stretched ? "RectanglePlacement::stretchToFit"
  123. : (mode == proportionalReducingOnly ? "RectanglePlacement::centred | RectanglePlacement::onlyReduceInSize"
  124. : "RectanglePlacement::centred"))
  125. << ", " << CodeHelpers::floatLiteral (opacity, 3) << ");\n";
  126. }
  127. }
  128. r << "}\n\n";
  129. paintMethodCode += r;
  130. }
  131. }
  132. void PaintElementImage::applyCustomPaintSnippets (StringArray& snippets)
  133. {
  134. customPaintCode.clear();
  135. if (! snippets.isEmpty() && opacity > 0)
  136. {
  137. customPaintCode = snippets[0];
  138. snippets.remove (0);
  139. }
  140. }
  141. //==============================================================================
  142. PaintElementImage::SetResourceAction::SetResourceAction (PaintElementImage* const element, const String& newResource_)
  143. : PaintElementUndoableAction <PaintElementImage> (element),
  144. newResource (newResource_)
  145. {
  146. oldResource = element->getResource();
  147. }
  148. bool PaintElementImage::SetResourceAction::perform()
  149. {
  150. showCorrectTab();
  151. getElement()->setResource (newResource, false);
  152. return true;
  153. }
  154. bool PaintElementImage::SetResourceAction::undo()
  155. {
  156. showCorrectTab();
  157. getElement()->setResource (oldResource, false);
  158. return true;
  159. }
  160. void PaintElementImage::setResource (const String& newName, const bool undoable)
  161. {
  162. if (resourceName != newName)
  163. {
  164. if (undoable)
  165. {
  166. perform (new SetResourceAction (this, newName),
  167. "Change image resource");
  168. }
  169. else
  170. {
  171. resourceName = newName;
  172. changed();
  173. }
  174. }
  175. repaint();
  176. }
  177. String PaintElementImage::getResource() const
  178. {
  179. return resourceName;
  180. }
  181. //==============================================================================
  182. PaintElementImage::SetOpacityAction::SetOpacityAction (PaintElementImage* const element, const double newOpacity_)
  183. : PaintElementUndoableAction <PaintElementImage> (element),
  184. newOpacity (newOpacity_)
  185. {
  186. oldOpacity = element->getOpacity();
  187. }
  188. bool PaintElementImage::SetOpacityAction::perform()
  189. {
  190. showCorrectTab();
  191. getElement()->setOpacity (newOpacity, false);
  192. return true;
  193. }
  194. bool PaintElementImage::SetOpacityAction::undo()
  195. {
  196. showCorrectTab();
  197. getElement()->setOpacity (oldOpacity, false);
  198. return true;
  199. }
  200. void PaintElementImage::setOpacity (double newOpacity, const bool undoable)
  201. {
  202. newOpacity = jlimit (0.0, 1.0, newOpacity);
  203. if (opacity != newOpacity)
  204. {
  205. if (undoable)
  206. {
  207. perform (new SetOpacityAction (this, newOpacity),
  208. "Change image opacity");
  209. }
  210. else
  211. {
  212. opacity = newOpacity;
  213. changed();
  214. }
  215. }
  216. }
  217. double PaintElementImage::getOpacity() const noexcept { return opacity; }
  218. //==============================================================================
  219. const char* PaintElementImage::getTagName() noexcept { return "IMAGE"; }
  220. void PaintElementImage::resetToImageSize()
  221. {
  222. if (const Drawable* const image = getDrawable())
  223. {
  224. if (PaintRoutineEditor* ed = dynamic_cast<PaintRoutineEditor*> (getParentComponent()))
  225. {
  226. const Rectangle<int> parentArea (ed->getComponentArea());
  227. Rectangle<int> r (getCurrentBounds (parentArea));
  228. Rectangle<float> b (image->getDrawableBounds());
  229. r.setSize ((int) (b.getWidth() + 0.999f),
  230. (int) (b.getHeight() + 0.999f));
  231. setCurrentBounds (r, parentArea, true);
  232. }
  233. }
  234. }
  235. //==============================================================================
  236. PaintElementImage::SetStretchModeAction::SetStretchModeAction (PaintElementImage* const element, const StretchMode newValue_)
  237. : PaintElementUndoableAction <PaintElementImage> (element),
  238. newValue (newValue_)
  239. {
  240. oldValue = element->getStretchMode();
  241. }
  242. bool PaintElementImage::SetStretchModeAction::perform()
  243. {
  244. showCorrectTab();
  245. getElement()->setStretchMode (newValue, false);
  246. return true;
  247. }
  248. bool PaintElementImage::SetStretchModeAction::undo()
  249. {
  250. showCorrectTab();
  251. getElement()->setStretchMode (oldValue, false);
  252. return true;
  253. }
  254. PaintElementImage::StretchMode PaintElementImage::getStretchMode() const noexcept { return mode; }
  255. void PaintElementImage::setStretchMode (const StretchMode newMode, const bool undoable)
  256. {
  257. if (mode != newMode)
  258. {
  259. if (undoable)
  260. {
  261. perform (new SetStretchModeAction (this, newMode),
  262. "Change image mode");
  263. }
  264. else
  265. {
  266. mode = newMode;
  267. changed();
  268. }
  269. }
  270. }
  271. //==============================================================================
  272. XmlElement* PaintElementImage::createXml() const
  273. {
  274. XmlElement* e = new XmlElement (getTagName());
  275. position.applyToXml (*e);
  276. e->setAttribute ("resource", resourceName);
  277. e->setAttribute ("opacity", opacity);
  278. e->setAttribute ("mode", (int) mode);
  279. return e;
  280. }
  281. bool PaintElementImage::loadFromXml (const XmlElement& xml)
  282. {
  283. if (xml.hasTagName (getTagName()))
  284. {
  285. position.restoreFromXml (xml, position);
  286. resourceName = xml.getStringAttribute ("resource", String());
  287. opacity = xml.getDoubleAttribute ("opacity", 1.0);
  288. mode = (StretchMode) xml.getIntAttribute ("mode", (int) stretched);
  289. repaint();
  290. return true;
  291. }
  292. jassertfalse;
  293. return false;
  294. }
  295. //==============================================================================
  296. PaintElementImage::ImageElementResourceProperty::ImageElementResourceProperty (PaintElementImage* const e)
  297. : ImageResourceProperty <PaintElementImage> (e, "image source")
  298. {
  299. }
  300. void PaintElementImage::ImageElementResourceProperty::setResource (const String& newName)
  301. {
  302. if (element != nullptr)
  303. element->setResource (newName, true);
  304. }
  305. String PaintElementImage::ImageElementResourceProperty::getResource() const
  306. {
  307. if (element != nullptr)
  308. return element->getResource();
  309. return {};
  310. }
  311. //==============================================================================
  312. PaintElementImage::OpacityProperty::OpacityProperty (PaintElementImage* const e)
  313. : SliderPropertyComponent ("opacity", 0.0, 1.0, 0.001),
  314. listener (e)
  315. {
  316. listener.setPropertyToRefresh (*this);
  317. }
  318. void PaintElementImage::OpacityProperty::setValue (double newValue)
  319. {
  320. listener.owner->getDocument()->getUndoManager().undoCurrentTransactionOnly();
  321. listener.owner->setOpacity (newValue, true);
  322. }
  323. double PaintElementImage::OpacityProperty::getValue() const
  324. {
  325. return listener.owner->getOpacity();
  326. }
  327. PaintElementImage::StretchModeProperty::StretchModeProperty (PaintElementImage* const e)
  328. : ChoicePropertyComponent ("stretch mode"),
  329. listener (e)
  330. {
  331. listener.setPropertyToRefresh (*this);
  332. choices.add ("Stretched to fit");
  333. choices.add ("Maintain aspect ratio");
  334. choices.add ("Maintain aspect ratio, only reduce in size");
  335. }
  336. void PaintElementImage::StretchModeProperty::setIndex (int newIndex)
  337. {
  338. listener.owner->setStretchMode ((StretchMode) newIndex, true);
  339. }
  340. int PaintElementImage::StretchModeProperty::getIndex() const
  341. {
  342. return (int) listener.owner->getStretchMode();
  343. }
  344. PaintElementImage::ResetSizeProperty::ResetSizeProperty (PaintElementImage* const e)
  345. : ButtonPropertyComponent ("reset", false),
  346. element (e)
  347. {
  348. }
  349. void PaintElementImage::ResetSizeProperty::buttonClicked()
  350. {
  351. element->resetToImageSize();
  352. }
  353. String PaintElementImage::ResetSizeProperty::getButtonText() const { return "reset to image size"; }