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.

536 lines
19KB

  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. #pragma once
  20. //==============================================================================
  21. class ImageButtonHandler : public ButtonHandler
  22. {
  23. public:
  24. enum ImageRole
  25. {
  26. normalImage = 0,
  27. overImage = 1,
  28. downImage = 2
  29. };
  30. //==============================================================================
  31. ImageButtonHandler()
  32. : ButtonHandler ("Image Button", "ImageButton", typeid (ImageButton), 150, 24)
  33. {
  34. }
  35. Component* createNewComponent (JucerDocument*) override
  36. {
  37. return new ImageButton ("new button");
  38. }
  39. void getEditableProperties (Component* component, JucerDocument& document,
  40. Array<PropertyComponent*>& props, bool multipleSelected) override
  41. {
  42. ButtonHandler::getEditableProperties (component, document, props, multipleSelected);
  43. if (multipleSelected)
  44. return;
  45. addColourProperties (component, document, props);
  46. if (auto* ib = dynamic_cast<ImageButton*> (component))
  47. {
  48. auto& layout = *document.getComponentLayout();
  49. props.add (new ImageButtonProportionProperty (layout, ib));
  50. props.add (new ImageButtonResourceProperty (layout, ib, normalImage, "normal image"));
  51. props.add (new ImageButtonOpacityProperty (layout, ib, "opacity", normalImage));
  52. props.add (new ImageButtonColourProperty (layout, ib, "overlay col.", normalImage));
  53. props.add (new ImageButtonResourceProperty (layout, ib, overImage, "over image"));
  54. props.add (new ImageButtonOpacityProperty (layout, ib, "opacity", overImage));
  55. props.add (new ImageButtonColourProperty (layout, ib, "overlay col.", overImage));
  56. props.add (new ImageButtonResourceProperty (layout, ib, downImage, "down image"));
  57. props.add (new ImageButtonOpacityProperty (layout, ib, "opacity", downImage));
  58. props.add (new ImageButtonColourProperty (layout, ib, "overlay col.", downImage));
  59. }
  60. }
  61. XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout) override
  62. {
  63. XmlElement* e = ButtonHandler::createXmlFor (comp, layout);
  64. ImageButton* const ib = (ImageButton*) comp;
  65. e->setAttribute ("keepProportions", doesImageKeepProportions (ib));
  66. e->setAttribute ("resourceNormal", getImageResource (ib, normalImage));
  67. e->setAttribute ("opacityNormal", getImageOpacity (ib, normalImage));
  68. e->setAttribute ("colourNormal", getImageColour (ib, normalImage).toString());
  69. e->setAttribute ("resourceOver", getImageResource (ib, overImage));
  70. e->setAttribute ("opacityOver", getImageOpacity (ib, overImage));
  71. e->setAttribute ("colourOver", getImageColour (ib, overImage).toString());
  72. e->setAttribute ("resourceDown", getImageResource (ib, downImage));
  73. e->setAttribute ("opacityDown", getImageOpacity (ib, downImage));
  74. e->setAttribute ("colourDown", getImageColour (ib, downImage).toString());
  75. return e;
  76. }
  77. bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout) override
  78. {
  79. if (! ButtonHandler::restoreFromXml (xml, comp, layout))
  80. return false;
  81. ImageButton* const ib = (ImageButton*) comp;
  82. ComponentLayout& l = const_cast<ComponentLayout&> (*layout);
  83. setImageKeepProportions (l, ib, xml.getBoolAttribute ("keepProportions", true), false);
  84. setImageResource (l, ib, normalImage, xml.getStringAttribute ("resourceNormal", String()), false);
  85. setImageOpacity (l, ib, normalImage, (float) xml.getDoubleAttribute ("opacityNormal", 1.0f), false);
  86. setImageColour (l, ib, normalImage, Colour::fromString (xml.getStringAttribute ("colourNormal", "0")), false);
  87. setImageResource (l, ib, overImage, xml.getStringAttribute ("resourceOver", String()), false);
  88. setImageOpacity (l, ib, overImage, (float) xml.getDoubleAttribute ("opacityOver", 1.0f), false);
  89. setImageColour (l, ib, overImage, Colour::fromString (xml.getStringAttribute ("colourOver", "0")), false);
  90. setImageResource (l, ib, downImage, xml.getStringAttribute ("resourceDown", String()), false);
  91. setImageOpacity (l, ib, downImage, (float) xml.getDoubleAttribute ("opacityDown", 1.0f), false);
  92. setImageColour (l, ib, downImage, Colour::fromString (xml.getStringAttribute ("colourDown", "0")), false);
  93. return true;
  94. }
  95. void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName) override
  96. {
  97. ButtonHandler::fillInCreationCode (code, component, memberVariableName);
  98. ImageButton* const ib = dynamic_cast<ImageButton*> (component);
  99. String s;
  100. s << getColourIntialisationCode (component, memberVariableName)
  101. << '\n';
  102. const String indent (String::repeatedString (" ", memberVariableName.length() + 13));
  103. s << memberVariableName << "->setImages (false, true, "
  104. << CodeHelpers::boolLiteral (doesImageKeepProportions (ib)) << ",\n"
  105. << indent
  106. << getImageCreationCode (ib, normalImage) << ", "
  107. << CodeHelpers::floatLiteral (getImageOpacity (ib, normalImage), 3) << ", "
  108. << CodeHelpers::colourToCode (getImageColour (ib, normalImage)) << ",\n"
  109. << indent
  110. << getImageCreationCode (ib, overImage) << ", "
  111. << CodeHelpers::floatLiteral (getImageOpacity (ib, overImage), 3) << ", "
  112. << CodeHelpers::colourToCode (getImageColour (ib, overImage)) << ",\n"
  113. << indent
  114. << getImageCreationCode (ib, downImage) << ", "
  115. << CodeHelpers::floatLiteral (getImageOpacity (ib, downImage), 3) << ", "
  116. << CodeHelpers::colourToCode (getImageColour (ib, downImage))
  117. << ");\n";
  118. code.constructorCode += s;
  119. }
  120. static String getImageCreationCode (ImageButton* ib, const ImageRole role)
  121. {
  122. const String resName (getImageResource (ib, role));
  123. if (resName.isEmpty())
  124. return "Image()";
  125. return "ImageCache::getFromMemory (" + resName + ", " + resName + "Size)";
  126. }
  127. //==============================================================================
  128. class ImageButtonResourceProperty : public ImageResourceProperty <ImageButton>
  129. {
  130. public:
  131. ImageButtonResourceProperty (ComponentLayout& layout_, ImageButton* const owner_, const ImageRole role_, const String& name)
  132. : ImageResourceProperty <ImageButton> (*layout_.getDocument(), owner_, name, true),
  133. role (role_),
  134. layout (layout_)
  135. {
  136. }
  137. void setResource (const String& newName)
  138. {
  139. setImageResource (layout, element, role, newName, true);
  140. }
  141. String getResource() const
  142. {
  143. return getImageResource (element, role);
  144. }
  145. private:
  146. const ImageRole role;
  147. ComponentLayout& layout;
  148. };
  149. class SetImageResourceAction : public ComponentUndoableAction <ImageButton>
  150. {
  151. public:
  152. SetImageResourceAction (ImageButton* const button,
  153. ComponentLayout& layout_,
  154. const ImageRole role_,
  155. const String& newResource_)
  156. : ComponentUndoableAction <ImageButton> (button, layout_),
  157. newResource (newResource_),
  158. role (role_),
  159. layout (layout_)
  160. {
  161. oldResource = ImageButtonHandler::getImageResource (button, role_);
  162. }
  163. bool perform()
  164. {
  165. showCorrectTab();
  166. ImageButtonHandler::setImageResource (layout, getComponent(), role, newResource, false);
  167. return true;
  168. }
  169. bool undo()
  170. {
  171. showCorrectTab();
  172. ImageButtonHandler::setImageResource (layout, getComponent(), role, oldResource, false);
  173. return true;
  174. }
  175. private:
  176. String newResource, oldResource;
  177. const ImageRole role;
  178. ComponentLayout& layout;
  179. };
  180. //==============================================================================
  181. static void setImageResource (ComponentLayout& layout, ImageButton* button, const ImageRole role, const String& newName, const bool undoable)
  182. {
  183. jassert (role < 3);
  184. if (role < 3 && getImageResource (button, role) != newName)
  185. {
  186. if (undoable)
  187. {
  188. layout.getDocument()->perform (new SetImageResourceAction (button, layout, role, newName),
  189. "Change image resource");
  190. }
  191. else
  192. {
  193. button->getProperties().set ("resource" + String ((int) role), newName);
  194. updateButtonImages (*layout.getDocument(), button);
  195. layout.changed();
  196. }
  197. }
  198. }
  199. static String getImageResource (ImageButton* button, const ImageRole role)
  200. {
  201. jassert (role < 3);
  202. return button->getProperties() ["resource" + String ((int) role)].toString();
  203. }
  204. //==============================================================================
  205. class SetImageKeepsPropAction : public ComponentUndoableAction <ImageButton>
  206. {
  207. public:
  208. SetImageKeepsPropAction (ImageButton* const button,
  209. ComponentLayout& layout_,
  210. const bool newState_)
  211. : ComponentUndoableAction <ImageButton> (button, layout_),
  212. newState (newState_),
  213. layout (layout_)
  214. {
  215. oldState = ImageButtonHandler::doesImageKeepProportions (button);
  216. }
  217. bool perform()
  218. {
  219. showCorrectTab();
  220. ImageButtonHandler::setImageKeepProportions (layout, getComponent(), newState, false);
  221. return true;
  222. }
  223. bool undo()
  224. {
  225. showCorrectTab();
  226. ImageButtonHandler::setImageKeepProportions (layout, getComponent(), oldState, false);
  227. return true;
  228. }
  229. private:
  230. bool newState, oldState;
  231. ComponentLayout& layout;
  232. };
  233. static bool doesImageKeepProportions (ImageButton* button)
  234. {
  235. return button->getProperties().getWithDefault ("keepImageProp", true);
  236. }
  237. static void setImageKeepProportions (ComponentLayout& layout, ImageButton* button, const bool newState, const bool undoable)
  238. {
  239. if (undoable)
  240. {
  241. layout.perform (new SetImageKeepsPropAction (button, layout, newState), "change imagebutton proportion mode");
  242. }
  243. else
  244. {
  245. button->getProperties().set ("keepImageProp", newState);
  246. updateButtonImages (*layout.getDocument(), button);
  247. layout.changed();
  248. }
  249. }
  250. class ImageButtonProportionProperty : public ComponentBooleanProperty <ImageButton>
  251. {
  252. public:
  253. ImageButtonProportionProperty (ComponentLayout& layout_, ImageButton* const owner_)
  254. : ComponentBooleanProperty <ImageButton> ("proportional", "maintain image proportions", "scale to fit",
  255. owner_, *layout_.getDocument()),
  256. layout (layout_)
  257. {
  258. }
  259. void setState (bool newState)
  260. {
  261. setImageKeepProportions (layout, component, newState, true);
  262. }
  263. bool getState() const
  264. {
  265. return doesImageKeepProportions (component);
  266. }
  267. private:
  268. ComponentLayout& layout;
  269. };
  270. //==============================================================================
  271. class SetImageOpacityAction : public ComponentUndoableAction <ImageButton>
  272. {
  273. public:
  274. SetImageOpacityAction (ImageButton* const button,
  275. ComponentLayout& layout_,
  276. const ImageRole role_,
  277. const float newState_)
  278. : ComponentUndoableAction <ImageButton> (button, layout_),
  279. role (role_),
  280. newState (newState_),
  281. layout (layout_)
  282. {
  283. oldState = ImageButtonHandler::getImageOpacity (button, role_);
  284. }
  285. bool perform()
  286. {
  287. showCorrectTab();
  288. ImageButtonHandler::setImageOpacity (layout, getComponent(), role, newState, false);
  289. return true;
  290. }
  291. bool undo()
  292. {
  293. showCorrectTab();
  294. ImageButtonHandler::setImageOpacity (layout, getComponent(), role, oldState, false);
  295. return true;
  296. }
  297. private:
  298. const ImageRole role;
  299. float newState, oldState;
  300. ComponentLayout& layout;
  301. };
  302. static float getImageOpacity (ImageButton* button, const ImageRole role)
  303. {
  304. return (float) button->getProperties().getWithDefault ("imageOpacity" + String ((int) role), 1.0f);
  305. }
  306. static void setImageOpacity (ComponentLayout& layout, ImageButton* button, const ImageRole role, const float opacity, const bool undoable)
  307. {
  308. if (undoable)
  309. {
  310. layout.perform (new SetImageOpacityAction (button, layout, role, opacity), "change imagebutton opacity");
  311. }
  312. else
  313. {
  314. button->getProperties().set ("imageOpacity" + String ((int) role), opacity);
  315. updateButtonImages (*layout.getDocument(), button);
  316. layout.changed();
  317. }
  318. }
  319. class ImageButtonOpacityProperty : public SliderPropertyComponent
  320. {
  321. public:
  322. ImageButtonOpacityProperty (ComponentLayout& layout_, ImageButton* const owner_,
  323. const String& name, const ImageRole role_)
  324. : SliderPropertyComponent (name, 0.0, 1.0, 0.0),
  325. owner (owner_),
  326. layout (layout_),
  327. role (role_)
  328. {
  329. }
  330. void setValue (double newValue)
  331. {
  332. setImageOpacity (layout, owner, role, (float) newValue, true);
  333. }
  334. double getValue() const
  335. {
  336. return getImageOpacity (owner, role);
  337. }
  338. private:
  339. ImageButton* const owner;
  340. ComponentLayout& layout;
  341. const ImageRole role;
  342. };
  343. //==============================================================================
  344. class SetImageColourAction : public ComponentUndoableAction <ImageButton>
  345. {
  346. public:
  347. SetImageColourAction (ImageButton* const button,
  348. ComponentLayout& layout_,
  349. const ImageRole role_,
  350. Colour newState_)
  351. : ComponentUndoableAction<ImageButton> (button, layout_),
  352. role (role_),
  353. newState (newState_),
  354. layout (layout_)
  355. {
  356. oldState = ImageButtonHandler::getImageColour (button, role_);
  357. }
  358. bool perform()
  359. {
  360. showCorrectTab();
  361. ImageButtonHandler::setImageColour (layout, getComponent(), role, newState, false);
  362. return true;
  363. }
  364. bool undo()
  365. {
  366. showCorrectTab();
  367. ImageButtonHandler::setImageColour (layout, getComponent(), role, oldState, false);
  368. return true;
  369. }
  370. private:
  371. const ImageRole role;
  372. Colour newState, oldState;
  373. ComponentLayout& layout;
  374. };
  375. static Colour getImageColour (ImageButton* button, const ImageRole role)
  376. {
  377. return Colour::fromString (button->getProperties().getWithDefault ("imageColour" + String ((int) role), "0").toString());
  378. }
  379. static void setImageColour (ComponentLayout& layout, ImageButton* button,
  380. const ImageRole role, Colour colour, const bool undoable)
  381. {
  382. if (undoable)
  383. {
  384. layout.perform (new SetImageColourAction (button, layout, role, colour), "change imagebutton colour");
  385. }
  386. else
  387. {
  388. button->getProperties().set ("imageColour" + String ((int) role), colour.toString());
  389. updateButtonImages (*layout.getDocument(), button);
  390. layout.changed();
  391. }
  392. }
  393. class ImageButtonColourProperty : public JucerColourPropertyComponent,
  394. public ChangeListener
  395. {
  396. public:
  397. ImageButtonColourProperty (ComponentLayout& layout_, ImageButton* const owner_,
  398. const String& name, const ImageRole role_)
  399. : JucerColourPropertyComponent (name, false),
  400. owner (owner_),
  401. layout (layout_),
  402. role (role_)
  403. {
  404. layout_.getDocument()->addChangeListener (this);
  405. }
  406. ~ImageButtonColourProperty()
  407. {
  408. layout.getDocument()->removeChangeListener (this);
  409. }
  410. void setColour (Colour newColour)
  411. {
  412. setImageColour (layout, owner, role, newColour, true);
  413. }
  414. Colour getColour() const
  415. {
  416. return getImageColour (owner, role);
  417. }
  418. void resetToDefault() {}
  419. void changeListenerCallback (ChangeBroadcaster*)
  420. {
  421. refresh();
  422. }
  423. private:
  424. ImageButton* const owner;
  425. ComponentLayout& layout;
  426. const ImageRole role;
  427. };
  428. //==============================================================================
  429. static void updateButtonImages (JucerDocument& document, ImageButton* const ib)
  430. {
  431. Image norm = document.getResources().getImageFromCache (getImageResource (ib, normalImage));
  432. Image over = document.getResources().getImageFromCache (getImageResource (ib, overImage));
  433. Image down = document.getResources().getImageFromCache (getImageResource (ib, downImage));
  434. ib->setImages (false, true, doesImageKeepProportions (ib),
  435. norm,
  436. getImageOpacity (ib, normalImage),
  437. getImageColour (ib, normalImage),
  438. over,
  439. getImageOpacity (ib, overImage),
  440. getImageColour (ib, overImage),
  441. down,
  442. getImageOpacity (ib, downImage),
  443. getImageColour (ib, downImage));
  444. }
  445. };