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.

178 lines
5.3KB

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