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.

520 lines
19KB

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