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.

413 lines
13KB

  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_ButtonDocument.h"
  21. #include "../jucer_UtilityFunctions.h"
  22. //==============================================================================
  23. static const int normalOff = 0;
  24. static const int overOff = 1;
  25. static const int downOff = 2;
  26. static const int normalOn = 3;
  27. static const int overOn = 4;
  28. static const int downOn = 5;
  29. static const int background = 6;
  30. //==============================================================================
  31. ButtonDocument::ButtonDocument (SourceCodeDocument* c)
  32. : JucerDocument (c)
  33. {
  34. paintStatesEnabled [normalOff] = true;
  35. paintStatesEnabled [overOff] = true;
  36. paintStatesEnabled [downOff] = true;
  37. paintStatesEnabled [normalOn] = false;
  38. paintStatesEnabled [overOn] = false;
  39. paintStatesEnabled [downOn] = false;
  40. paintStatesEnabled [background] = false;
  41. parentClasses = "public Button";
  42. for (int i = 7; --i >= 0;)
  43. {
  44. paintRoutines[i].reset (new PaintRoutine());
  45. paintRoutines[i]->setDocument (this);
  46. paintRoutines[i]->setBackgroundColour (Colours::transparentBlack);
  47. }
  48. }
  49. ButtonDocument::~ButtonDocument()
  50. {
  51. }
  52. static const char* const stateNames[] =
  53. {
  54. "normal", "over", "down",
  55. "normal on", "over on", "down on",
  56. "common background"
  57. };
  58. static int stateNameToIndex (const String& name)
  59. {
  60. for (int i = 7; --i >= 0;)
  61. if (name.equalsIgnoreCase (stateNames[i]))
  62. return i;
  63. jassertfalse;
  64. return normalOff;
  65. }
  66. int ButtonDocument::getNumPaintRoutines() const
  67. {
  68. int n = 0;
  69. for (int i = 7; --i >= 0;)
  70. if (paintStatesEnabled [i])
  71. ++n;
  72. return n;
  73. }
  74. StringArray ButtonDocument::getPaintRoutineNames() const
  75. {
  76. StringArray s;
  77. for (int i = 0; i < 7; ++i)
  78. if (paintStatesEnabled [i])
  79. s.add (stateNames [i]);
  80. return s;
  81. }
  82. PaintRoutine* ButtonDocument::getPaintRoutine (const int index) const
  83. {
  84. int n = 0;
  85. for (int i = 0; i < 7; ++i)
  86. {
  87. if (paintStatesEnabled [i])
  88. {
  89. if (index == n)
  90. return paintRoutines[i].get();
  91. ++n;
  92. }
  93. }
  94. jassertfalse;
  95. return {};
  96. }
  97. void ButtonDocument::setStatePaintRoutineEnabled (const int index, bool b)
  98. {
  99. jassert (index > 0 && index < 7);
  100. if (paintStatesEnabled [index] != b)
  101. {
  102. paintStatesEnabled [index] = b;
  103. changed();
  104. }
  105. }
  106. bool ButtonDocument::isStatePaintRoutineEnabled (const int index) const
  107. {
  108. return paintStatesEnabled [index];
  109. }
  110. int ButtonDocument::chooseBestEnabledPaintRoutine (int paintRoutineWanted) const
  111. {
  112. switch (paintRoutineWanted)
  113. {
  114. case normalOff: return normalOff;
  115. case overOff: return paintStatesEnabled [overOff] ? overOff : normalOff;
  116. case downOff: return paintStatesEnabled [downOff] ? downOff : chooseBestEnabledPaintRoutine (overOff);
  117. case normalOn: return paintStatesEnabled [normalOn] ? normalOn : normalOff;
  118. case overOn: return paintStatesEnabled [overOn] ? overOn : (paintStatesEnabled [normalOn] ? normalOn : chooseBestEnabledPaintRoutine (overOff));
  119. case downOn: return paintStatesEnabled [downOn] ? downOn : ((paintStatesEnabled [overOn] || paintStatesEnabled [normalOn])
  120. ? chooseBestEnabledPaintRoutine (overOn)
  121. : chooseBestEnabledPaintRoutine (downOff));
  122. default: jassertfalse; break;
  123. }
  124. return normalOff;
  125. }
  126. //==============================================================================
  127. String ButtonDocument::getTypeName() const
  128. {
  129. return "Button";
  130. }
  131. JucerDocument* ButtonDocument::createCopy()
  132. {
  133. auto newOne = new ButtonDocument (cpp);
  134. newOne->resources = resources;
  135. newOne->loadFromXml (*createXml());
  136. return newOne;
  137. }
  138. std::unique_ptr<XmlElement> ButtonDocument::createXml() const
  139. {
  140. auto doc = JucerDocument::createXml();
  141. for (int i = 0; i < 7; ++i)
  142. {
  143. auto e = paintRoutines[i]->createXml();
  144. e->setAttribute ("buttonState", stateNames [i]);
  145. e->setAttribute ("enabled", paintStatesEnabled [i]);
  146. doc->addChildElement (e);
  147. }
  148. return doc;
  149. }
  150. bool ButtonDocument::loadFromXml (const XmlElement& xml)
  151. {
  152. if (JucerDocument::loadFromXml (xml))
  153. {
  154. for (int i = 7; --i >= 0;)
  155. paintStatesEnabled [i] = false;
  156. forEachXmlChildElementWithTagName (xml, e, PaintRoutine::xmlTagName)
  157. {
  158. const int stateIndex = stateNameToIndex (e->getStringAttribute ("buttonState"));
  159. paintRoutines [stateIndex]->loadFromXml (*e);
  160. paintStatesEnabled [stateIndex] = e->getBoolAttribute ("enabled", stateIndex < normalOn);
  161. }
  162. changed();
  163. getUndoManager().clearUndoHistory();
  164. return true;
  165. }
  166. return false;
  167. }
  168. void ButtonDocument::getOptionalMethods (StringArray& baseClasses,
  169. StringArray& returnValues,
  170. StringArray& methods,
  171. StringArray& initialContents) const
  172. {
  173. JucerDocument::getOptionalMethods (baseClasses, returnValues, methods, initialContents);
  174. addMethod ("Button", "void", "clicked()", "", baseClasses, returnValues, methods, initialContents);
  175. addMethod ("Button", "void", "buttonStateChanged()", "", baseClasses, returnValues, methods, initialContents);
  176. }
  177. //==============================================================================
  178. class ButtonStatePaintEnabledProperty : public BooleanPropertyComponent,
  179. private ChangeListener
  180. {
  181. public:
  182. ButtonStatePaintEnabledProperty (const String& name, ButtonDocument& doc, const int stateMethod_)
  183. : BooleanPropertyComponent (name, "enabled", "disabled"),
  184. document (doc),
  185. stateMethod (stateMethod_)
  186. {
  187. document.addChangeListener (this);
  188. }
  189. ~ButtonStatePaintEnabledProperty()
  190. {
  191. document.removeChangeListener (this);
  192. }
  193. void setState (bool newState)
  194. {
  195. document.setStatePaintRoutineEnabled (stateMethod, newState);
  196. }
  197. bool getState() const
  198. {
  199. return document.isStatePaintRoutineEnabled (stateMethod);
  200. }
  201. private:
  202. void changeListenerCallback (ChangeBroadcaster*)
  203. {
  204. refresh();
  205. }
  206. ButtonDocument& document;
  207. const int stateMethod;
  208. };
  209. void ButtonDocument::addExtraClassProperties (PropertyPanel& panel)
  210. {
  211. Array <PropertyComponent*> props;
  212. for (int i = 1; i < 7; ++i)
  213. props.add (new ButtonStatePaintEnabledProperty (stateNames[i], *this, i));
  214. panel.addSection ("Button paint routines", props);
  215. }
  216. //==============================================================================
  217. class ButtonTestComponent : public Button
  218. {
  219. public:
  220. ButtonTestComponent (ButtonDocument* const doc, const bool fillBackground)
  221. : Button (String()),
  222. document (doc),
  223. alwaysFillBackground (fillBackground)
  224. {
  225. setClickingTogglesState (true);
  226. }
  227. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  228. {
  229. if (document->paintStatesEnabled [background])
  230. {
  231. document->paintRoutines [background]->fillWithBackground (g, alwaysFillBackground);
  232. document->paintRoutines [background]->drawElements (g, getLocalBounds());
  233. }
  234. const int stateIndex
  235. = getToggleState()
  236. ? (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOn)
  237. : (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOn)
  238. : document->chooseBestEnabledPaintRoutine (normalOn)))
  239. : (isButtonDown ? document->chooseBestEnabledPaintRoutine (downOff)
  240. : (isMouseOverButton ? document->chooseBestEnabledPaintRoutine (overOff)
  241. : normalOff));
  242. document->paintRoutines [stateIndex]->fillWithBackground (g, ! document->paintStatesEnabled [background]);
  243. document->paintRoutines [stateIndex]->drawElements (g, getLocalBounds());
  244. }
  245. private:
  246. ButtonDocument* const document;
  247. const bool alwaysFillBackground;
  248. };
  249. Component* ButtonDocument::createTestComponent (const bool alwaysFillBackground)
  250. {
  251. return new ButtonTestComponent (this, alwaysFillBackground);
  252. }
  253. //==============================================================================
  254. void ButtonDocument::fillInGeneratedCode (GeneratedCode& code) const
  255. {
  256. JucerDocument::fillInGeneratedCode (code);
  257. code.parentClassInitialiser = "Button (" + quotedString (code.componentName, false) + ")";
  258. code.removeCallback ("void", "paint (Graphics& g)");
  259. }
  260. void ButtonDocument::fillInPaintCode (GeneratedCode& code) const
  261. {
  262. jassert (paintStatesEnabled [normalOff]);
  263. String paintCode [7];
  264. for (int i = 0; i < 7; ++i)
  265. if (paintStatesEnabled [i])
  266. paintRoutines[i]->fillInGeneratedCode (code, paintCode [i]);
  267. String& s = code.getCallbackCode ("public Button",
  268. "void",
  269. "paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown)",
  270. false);
  271. int numPaintRoutines = getNumPaintRoutines();
  272. if (paintStatesEnabled [background])
  273. {
  274. s << paintCode [background] << "\n";
  275. --numPaintRoutines;
  276. }
  277. if (numPaintRoutines == 1)
  278. {
  279. s << paintCode [normalOff];
  280. }
  281. else if (numPaintRoutines == downOff && (paintStatesEnabled [overOff] || paintStatesEnabled [downOff] || paintStatesEnabled [normalOn]))
  282. {
  283. if (paintStatesEnabled [normalOn])
  284. {
  285. s << "if (getToggleState())\n{\n "
  286. << CodeHelpers::indent (paintCode [normalOn], 4, false).trimEnd();
  287. }
  288. else if (paintStatesEnabled [overOff])
  289. {
  290. s << "if (isButtonDown || isMouseOverButton)\n{\n "
  291. << CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd();
  292. }
  293. else
  294. {
  295. s << "if (isButtonDown)\n{\n "
  296. << CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd();
  297. }
  298. s << "\n}\nelse\n{\n "
  299. << CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
  300. << "\n}\n";
  301. }
  302. else if (numPaintRoutines == normalOn && paintStatesEnabled [overOff] && paintStatesEnabled [downOff])
  303. {
  304. s << "if (isButtonDown)\n{\n "
  305. << CodeHelpers::indent (paintCode [downOff], 4, false).trimEnd()
  306. << "\n}\nelse if (isMouseOverButton)\n{\n "
  307. << CodeHelpers::indent (paintCode [overOff], 4, false).trimEnd()
  308. << "\n}\nelse\n{\n "
  309. << CodeHelpers::indent (paintCode [normalOff], 4, false).trimEnd()
  310. << "\n}\n";
  311. }
  312. else
  313. {
  314. if (paintStatesEnabled [normalOn] || paintStatesEnabled [overOn] || paintStatesEnabled [downOn])
  315. {
  316. s << "switch (getToggleState() ? (isButtonDown ? "
  317. << chooseBestEnabledPaintRoutine (downOn) << " : (isMouseOverButton ? "
  318. << chooseBestEnabledPaintRoutine (overOn) << " : "
  319. << chooseBestEnabledPaintRoutine (normalOn) << "))\n : (isButtonDown ? "
  320. << chooseBestEnabledPaintRoutine (downOff) << " : (isMouseOverButton ? "
  321. << chooseBestEnabledPaintRoutine (overOff) << " : 0)))\n{\n";
  322. }
  323. else
  324. {
  325. s << "switch (isButtonDown ? " << chooseBestEnabledPaintRoutine (downOff)
  326. << " : (isMouseOverButton ? " << chooseBestEnabledPaintRoutine (overOff)
  327. << " : 0))\n{\n";
  328. }
  329. for (int i = 0; i < 6; ++i)
  330. {
  331. if (paintStatesEnabled [i])
  332. {
  333. s << "case " << i << ":\n {\n "
  334. << CodeHelpers::indent (paintCode [i], 8, false).trimEnd()
  335. << "\n break;\n }\n\n";
  336. }
  337. }
  338. s << "default:\n break;\n}\n";
  339. }
  340. }