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.

167 lines
5.1KB

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