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.

162 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. #ifndef __JUCER_PAINTELEMENTRECTANGLE_JUCEHEADER__
  18. #define __JUCER_PAINTELEMENTRECTANGLE_JUCEHEADER__
  19. #include "jucer_ColouredElement.h"
  20. //==============================================================================
  21. class PaintElementRectangle : public ColouredElement
  22. {
  23. public:
  24. PaintElementRectangle (PaintRoutine* pr)
  25. : ColouredElement (pr, "Rectangle", true, false)
  26. {
  27. }
  28. Rectangle<int> getCurrentBounds (const Rectangle<int>& parentArea) const
  29. {
  30. return PaintElement::getCurrentBounds (parentArea); // bypass the ColouredElement implementation
  31. }
  32. void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& parentArea, const bool undoable)
  33. {
  34. PaintElement::setCurrentBounds (newBounds, parentArea, undoable); // bypass the ColouredElement implementation
  35. }
  36. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  37. {
  38. Component tempParentComp;
  39. tempParentComp.setBounds (parentArea);
  40. fillType.setFillType (g, getDocument(), parentArea);
  41. const Rectangle<int> r (position.getRectangle (parentArea, layout));
  42. g.fillRect (r);
  43. if (isStrokePresent)
  44. {
  45. strokeType.fill.setFillType (g, getDocument(), parentArea);
  46. g.drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  47. roundToInt (getStrokeType().stroke.getStrokeThickness()));
  48. }
  49. }
  50. void getEditableProperties (Array <PropertyComponent*>& props)
  51. {
  52. ColouredElement::getEditableProperties (props);
  53. props.add (new ShapeToPathProperty (this));
  54. }
  55. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  56. {
  57. if (! fillType.isInvisible())
  58. {
  59. String x, y, w, h, s;
  60. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  61. fillType.fillInGeneratedCode (code, paintMethodCode);
  62. s << "g.fillRect (" << x << ", " << y << ", " << w << ", " << h << ");\n\n";
  63. paintMethodCode += s;
  64. }
  65. if (isStrokePresent && ! strokeType.isInvisible())
  66. {
  67. String x, y, w, h, s;
  68. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  69. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  70. s << "g.drawRect (" << x << ", " << y << ", " << w << ", " << h << ", "
  71. << roundToInt (strokeType.stroke.getStrokeThickness()) << ");\n\n";
  72. paintMethodCode += s;
  73. }
  74. }
  75. static const char* getTagName() noexcept { return "RECT"; }
  76. XmlElement* createXml() const
  77. {
  78. XmlElement* e = new XmlElement (getTagName());
  79. position.applyToXml (*e);
  80. addColourAttributes (e);
  81. return e;
  82. }
  83. bool loadFromXml (const XmlElement& xml)
  84. {
  85. if (xml.hasTagName (getTagName()))
  86. {
  87. position.restoreFromXml (xml, position);
  88. loadColourAttributes (xml);
  89. return true;
  90. }
  91. jassertfalse;
  92. return false;
  93. }
  94. void convertToPath()
  95. {
  96. Path path;
  97. path.addRectangle (getCurrentAbsoluteBounds());
  98. convertToNewPathElement (path);
  99. }
  100. private:
  101. class ShapeToPathProperty : public ButtonPropertyComponent
  102. {
  103. public:
  104. ShapeToPathProperty (PaintElementRectangle* 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. PaintElementRectangle* const element;
  119. };
  120. };
  121. #endif // __JUCER_PAINTELEMENTRECTANGLE_JUCEHEADER__