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.

525 lines
19KB

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