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.

156 lines
4.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. //==============================================================================
  20. class PaintElementEllipse : public ColouredElement
  21. {
  22. public:
  23. PaintElementEllipse (PaintRoutine* pr)
  24. : ColouredElement (pr, "Ellipse", true, false)
  25. {
  26. }
  27. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  28. {
  29. fillType.setFillType (g, getDocument(), parentArea);
  30. Rectangle<int> r (position.getRectangle (parentArea, layout));
  31. g.fillEllipse ((float) r.getX(), (float) r.getY(), (float) r.getWidth(), (float) r.getHeight());
  32. if (isStrokePresent)
  33. {
  34. strokeType.fill.setFillType (g, getDocument(), parentArea);
  35. g.drawEllipse ((float) r.getX(), (float) r.getY(), (float) r.getWidth(), (float) r.getHeight(),
  36. getStrokeType().stroke.getStrokeThickness());
  37. }
  38. }
  39. void getEditableProperties (Array<PropertyComponent*>& props)
  40. {
  41. ColouredElement::getEditableProperties (props);
  42. props.add (new ShapeToPathProperty (this));
  43. }
  44. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  45. {
  46. if (! fillType.isInvisible())
  47. {
  48. String x, y, w, h, s;
  49. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  50. fillType.fillInGeneratedCode (code, paintMethodCode);
  51. s << "g.fillEllipse ("
  52. << castToFloat (x) << ", "
  53. << castToFloat (y) << ", "
  54. << castToFloat (w) << ", "
  55. << castToFloat (h) << ");\n\n";
  56. paintMethodCode += s;
  57. }
  58. if (isStrokePresent && ! strokeType.isInvisible())
  59. {
  60. String x, y, w, h, s;
  61. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  62. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  63. s << "g.drawEllipse ("
  64. << castToFloat (x) << ", "
  65. << castToFloat (y) << ", "
  66. << castToFloat (w) << ", "
  67. << castToFloat (h) << ", "
  68. << CodeHelpers::floatLiteral (strokeType.stroke.getStrokeThickness(), 3) << ");\n\n";
  69. paintMethodCode += s;
  70. }
  71. }
  72. static const char* getTagName() noexcept { return "ELLIPSE"; }
  73. XmlElement* createXml() const
  74. {
  75. XmlElement* e = new XmlElement (getTagName());
  76. position.applyToXml (*e);
  77. addColourAttributes (e);
  78. return e;
  79. }
  80. bool loadFromXml (const XmlElement& xml)
  81. {
  82. if (xml.hasTagName (getTagName()))
  83. {
  84. position.restoreFromXml (xml, position);
  85. loadColourAttributes (xml);
  86. return true;
  87. }
  88. jassertfalse;
  89. return false;
  90. }
  91. void convertToPath()
  92. {
  93. double x, y, w, h;
  94. getCurrentAbsoluteBoundsDouble (x, y, w, h);
  95. Path path;
  96. path.addEllipse ((float) x, (float) y, (float) w, (float) h);
  97. convertToNewPathElement (path);
  98. }
  99. private:
  100. //==============================================================================
  101. class ShapeToPathProperty : public ButtonPropertyComponent
  102. {
  103. public:
  104. ShapeToPathProperty (PaintElementEllipse* const e)
  105. : ButtonPropertyComponent ("path", false),
  106. element (e)
  107. {
  108. }
  109. void buttonClicked()
  110. {
  111. element->convertToPath();
  112. }
  113. String getButtonText() const
  114. {
  115. return "convert to a path";
  116. }
  117. private:
  118. PaintElementEllipse* const element;
  119. };
  120. };