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) 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 PaintElementRectangle : public ColouredElement
  21. {
  22. public:
  23. PaintElementRectangle (PaintRoutine* pr)
  24. : ColouredElement (pr, "Rectangle", true, false)
  25. {
  26. }
  27. Rectangle<int> getCurrentBounds (const Rectangle<int>& parentArea) const
  28. {
  29. return PaintElement::getCurrentBounds (parentArea); // bypass the ColouredElement implementation
  30. }
  31. void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& parentArea, const bool undoable)
  32. {
  33. PaintElement::setCurrentBounds (newBounds, parentArea, undoable); // bypass the ColouredElement implementation
  34. }
  35. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  36. {
  37. Component tempParentComp;
  38. tempParentComp.setBounds (parentArea);
  39. fillType.setFillType (g, getDocument(), parentArea);
  40. const Rectangle<int> r (position.getRectangle (parentArea, layout));
  41. g.fillRect (r);
  42. if (isStrokePresent)
  43. {
  44. strokeType.fill.setFillType (g, getDocument(), parentArea);
  45. g.drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  46. roundToInt (getStrokeType().stroke.getStrokeThickness()));
  47. }
  48. }
  49. void getEditableProperties (Array <PropertyComponent*>& props)
  50. {
  51. ColouredElement::getEditableProperties (props);
  52. props.add (new ShapeToPathProperty (this));
  53. }
  54. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  55. {
  56. if (! fillType.isInvisible())
  57. {
  58. String x, y, w, h, s;
  59. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  60. fillType.fillInGeneratedCode (code, paintMethodCode);
  61. s << "g.fillRect (" << x << ", " << y << ", " << w << ", " << h << ");\n\n";
  62. paintMethodCode += s;
  63. }
  64. if (isStrokePresent && ! strokeType.isInvisible())
  65. {
  66. String x, y, w, h, s;
  67. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  68. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  69. s << "g.drawRect (" << x << ", " << y << ", " << w << ", " << h << ", "
  70. << roundToInt (strokeType.stroke.getStrokeThickness()) << ");\n\n";
  71. paintMethodCode += s;
  72. }
  73. }
  74. static const char* getTagName() noexcept { return "RECT"; }
  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. Path path;
  96. path.addRectangle (getCurrentAbsoluteBounds());
  97. convertToNewPathElement (path);
  98. }
  99. private:
  100. class ShapeToPathProperty : public ButtonPropertyComponent
  101. {
  102. public:
  103. ShapeToPathProperty (PaintElementRectangle* const e)
  104. : ButtonPropertyComponent ("path", false),
  105. element (e)
  106. {
  107. }
  108. void buttonClicked()
  109. {
  110. element->convertToPath();
  111. }
  112. String getButtonText() const
  113. {
  114. return "convert to a path";
  115. }
  116. private:
  117. PaintElementRectangle* const element;
  118. };
  119. };