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.

160 lines
4.7KB

  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
  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)
  34. {
  35. PaintElement::setCurrentBounds (newBounds, parentArea, undoable); // bypass the ColouredElement implementation
  36. }
  37. void draw (Graphics& g, const ComponentLayout* layout, const Rectangle<int>& parentArea)
  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)
  52. {
  53. ColouredElement::getEditableProperties (props);
  54. props.add (new ShapeToPathProperty (this));
  55. }
  56. void fillInGeneratedCode (GeneratedCode& code, String& paintMethodCode)
  57. {
  58. if (! fillType.isInvisible())
  59. {
  60. String x, y, w, h, s;
  61. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  62. fillType.fillInGeneratedCode (code, paintMethodCode);
  63. s << "g.fillRect (" << x << ", " << y << ", " << w << ", " << h << ");\n\n";
  64. paintMethodCode += s;
  65. }
  66. if (isStrokePresent && ! strokeType.isInvisible())
  67. {
  68. String x, y, w, h, s;
  69. positionToCode (position, code.document->getComponentLayout(), x, y, w, h);
  70. strokeType.fill.fillInGeneratedCode (code, paintMethodCode);
  71. s << "g.drawRect (" << x << ", " << y << ", " << w << ", " << h << ", "
  72. << roundToInt (strokeType.stroke.getStrokeThickness()) << ");\n\n";
  73. paintMethodCode += s;
  74. }
  75. }
  76. static const char* getTagName() noexcept { return "RECT"; }
  77. XmlElement* createXml() const
  78. {
  79. XmlElement* e = new XmlElement (getTagName());
  80. position.applyToXml (*e);
  81. addColourAttributes (e);
  82. return e;
  83. }
  84. bool loadFromXml (const XmlElement& xml)
  85. {
  86. if (xml.hasTagName (getTagName()))
  87. {
  88. position.restoreFromXml (xml, position);
  89. loadColourAttributes (xml);
  90. return true;
  91. }
  92. jassertfalse;
  93. return false;
  94. }
  95. void convertToPath()
  96. {
  97. Path path;
  98. path.addRectangle (getCurrentAbsoluteBounds());
  99. convertToNewPathElement (path);
  100. }
  101. private:
  102. class ShapeToPathProperty : public ButtonPropertyComponent
  103. {
  104. public:
  105. ShapeToPathProperty (PaintElementRectangle* const e)
  106. : ButtonPropertyComponent ("path", false),
  107. element (e)
  108. {
  109. }
  110. void buttonClicked()
  111. {
  112. element->convertToPath();
  113. }
  114. String getButtonText() const
  115. {
  116. return "convert to a path";
  117. }
  118. private:
  119. PaintElementRectangle* const element;
  120. };
  121. };