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.

249 lines
7.7KB

  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. #pragma once
  18. #include "jucer_ColouredElement.h"
  19. #include "../jucer_UtilityFunctions.h"
  20. //==============================================================================
  21. class PaintElementRoundedRectangle : public ColouredElement
  22. {
  23. public:
  24. PaintElementRoundedRectangle (PaintRoutine* pr)
  25. : ColouredElement (pr, "Rounded Rectangle", true, false)
  26. {
  27. cornerSize = 10.0;
  28. }
  29. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  30. {
  31. double x, y, w, h;
  32. position.getRectangleDouble (x, y, w, h, parentArea, layout);
  33. fillType.setFillType (g, getDocument(), parentArea);
  34. g.fillRoundedRectangle ((float) x, (float) y, (float) w, (float) h, (float) cornerSize);
  35. if (isStrokePresent)
  36. {
  37. strokeType.fill.setFillType (g, getDocument(), parentArea);
  38. g.drawRoundedRectangle ((float) x, (float) y, (float) w, (float) h, (float) cornerSize,
  39. getStrokeType().stroke.getStrokeThickness());
  40. }
  41. }
  42. void getEditableProperties (Array<PropertyComponent*>& props)
  43. {
  44. props.add (new CornerSizeProperty (this));
  45. ColouredElement::getEditableProperties (props);
  46. props.add (new ShapeToPathProperty (this));
  47. }
  48. //==============================================================================
  49. class SetCornerSizeAction : public PaintElementUndoableAction <PaintElementRoundedRectangle>
  50. {
  51. public:
  52. SetCornerSizeAction (PaintElementRoundedRectangle* const element, const double newSize_)
  53. : PaintElementUndoableAction <PaintElementRoundedRectangle> (element),
  54. newSize (newSize_)
  55. {
  56. oldSize = element->getCornerSize();
  57. }
  58. bool perform()
  59. {
  60. showCorrectTab();
  61. getElement()->setCornerSize (newSize, false);
  62. return true;
  63. }
  64. bool undo()
  65. {
  66. showCorrectTab();
  67. getElement()->setCornerSize (oldSize, false);
  68. return true;
  69. }
  70. private:
  71. double newSize, oldSize;
  72. };
  73. void setCornerSize (const double newSize, const bool undoable)
  74. {
  75. if (newSize != cornerSize)
  76. {
  77. if (undoable)
  78. {
  79. perform (new SetCornerSizeAction (this, newSize),
  80. "Change rounded rectangle corner size");
  81. }
  82. else
  83. {
  84. cornerSize = newSize;
  85. changed();
  86. }
  87. }
  88. }
  89. double getCornerSize() const noexcept { return cornerSize; }
  90. //==============================================================================
  91. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  92. {
  93. if (! fillType.isInvisible())
  94. {
  95. String x, y, w, h, s;
  96. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  97. fillType.fillInGeneratedCode (code, paintMethodCode);
  98. s << "g.fillRoundedRectangle ("
  99. << castToFloat (x) << ", "
  100. << castToFloat (y) << ", "
  101. << castToFloat (w) << ", "
  102. << castToFloat (h) << ", "
  103. << CodeHelpers::floatLiteral (cornerSize, 3) << ");\n\n";
  104. paintMethodCode += s;
  105. }
  106. if (isStrokePresent && ! strokeType.isInvisible())
  107. {
  108. String x, y, w, h, s;
  109. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  110. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  111. s << "g.drawRoundedRectangle ("
  112. << castToFloat (x) << ", "
  113. << castToFloat (y) << ", "
  114. << castToFloat (w) << ", "
  115. << castToFloat (h) << ", "
  116. << CodeHelpers::floatLiteral (cornerSize, 3) << ", "
  117. << CodeHelpers::floatLiteral (strokeType.stroke.getStrokeThickness(), 3) << ");\n\n";
  118. paintMethodCode += s;
  119. }
  120. }
  121. static const char* getTagName() noexcept { return "ROUNDRECT"; }
  122. XmlElement* createXml() const
  123. {
  124. XmlElement* const e = new XmlElement (getTagName());
  125. position.applyToXml (*e);
  126. e->setAttribute ("cornerSize", cornerSize);
  127. addColourAttributes (e);
  128. return e;
  129. }
  130. bool loadFromXml (const XmlElement& xml)
  131. {
  132. if (xml.hasTagName (getTagName()))
  133. {
  134. position.restoreFromXml (xml, position);
  135. cornerSize = xml.getDoubleAttribute ("cornerSize", 10.0);
  136. loadColourAttributes (xml);
  137. return true;
  138. }
  139. jassertfalse;
  140. return false;
  141. }
  142. void convertToPath()
  143. {
  144. double x, y, w, h;
  145. getCurrentAbsoluteBoundsDouble (x, y, w, h);
  146. Path path;
  147. path.addRoundedRectangle ((float) x, (float) y, (float) w, (float) h, (float) cornerSize);
  148. convertToNewPathElement (path);
  149. }
  150. private:
  151. double cornerSize;
  152. //==============================================================================
  153. class CornerSizeProperty : public SliderPropertyComponent,
  154. public ChangeListener
  155. {
  156. public:
  157. CornerSizeProperty (PaintElementRoundedRectangle* const owner_)
  158. : SliderPropertyComponent ("corner size", 1.0, 200.0, 0.5, 0.4),
  159. owner (owner_)
  160. {
  161. owner->getDocument()->addChangeListener (this);
  162. }
  163. ~CornerSizeProperty()
  164. {
  165. owner->getDocument()->removeChangeListener (this);
  166. }
  167. void setValue (double newValue)
  168. {
  169. owner->getDocument()->getUndoManager().undoCurrentTransactionOnly();
  170. owner->setCornerSize (newValue, true);
  171. }
  172. double getValue() const { return owner->getCornerSize(); }
  173. void changeListenerCallback (ChangeBroadcaster*) { refresh(); }
  174. private:
  175. PaintElementRoundedRectangle* const owner;
  176. };
  177. //==============================================================================
  178. class ShapeToPathProperty : public ButtonPropertyComponent
  179. {
  180. public:
  181. ShapeToPathProperty (PaintElementRoundedRectangle* const e)
  182. : ButtonPropertyComponent ("path", false),
  183. element (e)
  184. {
  185. }
  186. void buttonClicked()
  187. {
  188. element->convertToPath();
  189. }
  190. String getButtonText() const
  191. {
  192. return "convert to a path";
  193. }
  194. private:
  195. PaintElementRoundedRectangle* const element;
  196. };
  197. };