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.

186 lines
5.6KB

  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 PaintElementRectangle : public ColouredElement
  23. {
  24. public:
  25. PaintElementRectangle (PaintRoutine* pr)
  26. : ColouredElement (pr, "Rectangle", true, false)
  27. {
  28. }
  29. Rectangle<int> getCurrentBounds (const Rectangle<int>& parentArea) const override
  30. {
  31. return PaintElement::getCurrentBounds (parentArea); // bypass the ColouredElement implementation
  32. }
  33. void setCurrentBounds (const Rectangle<int>& newBounds, const Rectangle<int>& parentArea, const bool undoable) override
  34. {
  35. PaintElement::setCurrentBounds (newBounds, parentArea, undoable); // bypass the ColouredElement implementation
  36. }
  37. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea) override
  38. {
  39. Component tempParentComp;
  40. tempParentComp.setBounds (parentArea);
  41. fillType.setFillType (g, getDocument(), parentArea);
  42. const Rectangle<int> r (position.getRectangle (parentArea, layout));
  43. g.fillRect (r);
  44. if (isStrokePresent)
  45. {
  46. strokeType.fill.setFillType (g, getDocument(), parentArea);
  47. g.drawRect (r.getX(), r.getY(), r.getWidth(), r.getHeight(),
  48. roundToInt (getStrokeType().stroke.getStrokeThickness()));
  49. }
  50. }
  51. void getEditableProperties (Array <PropertyComponent*>& props, bool multipleSelected) override
  52. {
  53. ColouredElement::getEditableProperties (props, multipleSelected);
  54. props.add (new ShapeToPathProperty (this));
  55. }
  56. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode) override
  57. {
  58. if (fillType.isInvisible() && (strokeType.isInvisible() || ! isStrokePresent))
  59. return;
  60. String x, y, w, h, s;
  61. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  62. s << "{\n"
  63. << " int x = " << x << ", y = " << y << ", width = " << w << ", height = " << h << ";\n";
  64. if (! fillType.isInvisible())
  65. s << " " << fillType.generateVariablesCode ("fill");
  66. if (isStrokePresent && ! strokeType.isInvisible())
  67. s << " " << strokeType.fill.generateVariablesCode ("stroke");
  68. s << " //[UserPaintCustomArguments] Customize the painting arguments here..\n"
  69. << customPaintCode
  70. << " //[/UserPaintCustomArguments]\n";
  71. if (! fillType.isInvisible())
  72. {
  73. s << " ";
  74. fillType.fillInGeneratedCode ("fill", position, code, s);
  75. s << " g.fillRect (x, y, width, height);\n";
  76. }
  77. if (isStrokePresent && ! strokeType.isInvisible())
  78. {
  79. s << " ";
  80. strokeType.fill.fillInGeneratedCode ("stroke", position, code, s);
  81. s << " g.drawRect (x, y, width, height, " << roundToInt (strokeType.stroke.getStrokeThickness()) << ");\n\n";
  82. }
  83. s << "}\n\n";
  84. paintMethodCode += s;
  85. }
  86. void applyCustomPaintSnippets (StringArray& snippets) override
  87. {
  88. customPaintCode.clear();
  89. if (! snippets.isEmpty() && (! fillType.isInvisible() || (isStrokePresent && ! strokeType.isInvisible())))
  90. {
  91. customPaintCode = snippets[0];
  92. snippets.remove (0);
  93. }
  94. }
  95. static const char* getTagName() noexcept { return "RECT"; }
  96. XmlElement* createXml() const override
  97. {
  98. XmlElement* e = new XmlElement (getTagName());
  99. position.applyToXml (*e);
  100. addColourAttributes (e);
  101. return e;
  102. }
  103. bool loadFromXml (const XmlElement& xml) override
  104. {
  105. if (xml.hasTagName (getTagName()))
  106. {
  107. position.restoreFromXml (xml, position);
  108. loadColourAttributes (xml);
  109. return true;
  110. }
  111. jassertfalse;
  112. return false;
  113. }
  114. void convertToPath()
  115. {
  116. Path path;
  117. path.addRectangle (getCurrentAbsoluteBounds());
  118. convertToNewPathElement (path);
  119. }
  120. private:
  121. String customPaintCode;
  122. class ShapeToPathProperty : public ButtonPropertyComponent
  123. {
  124. public:
  125. ShapeToPathProperty (PaintElementRectangle* const e)
  126. : ButtonPropertyComponent ("path", false),
  127. element (e)
  128. {
  129. }
  130. void buttonClicked()
  131. {
  132. element->convertToPath();
  133. }
  134. String getButtonText() const
  135. {
  136. return "convert to a path";
  137. }
  138. private:
  139. PaintElementRectangle* const element;
  140. };
  141. };