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.

405 lines
13KB

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