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.

158 lines
4.7KB

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