Audio plugin host https://kx.studio/carla
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.

juce_DrawableRectangle.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. DrawableRectangle::DrawableRectangle()
  18. {
  19. }
  20. DrawableRectangle::DrawableRectangle (const DrawableRectangle& other)
  21. : DrawableShape (other),
  22. bounds (other.bounds),
  23. cornerSize (other.cornerSize)
  24. {
  25. }
  26. DrawableRectangle::~DrawableRectangle()
  27. {
  28. }
  29. Drawable* DrawableRectangle::createCopy() const
  30. {
  31. return new DrawableRectangle (*this);
  32. }
  33. //==============================================================================
  34. void DrawableRectangle::setRectangle (const RelativeParallelogram& newBounds)
  35. {
  36. if (bounds != newBounds)
  37. {
  38. bounds = newBounds;
  39. rebuildPath();
  40. }
  41. }
  42. void DrawableRectangle::setCornerSize (const RelativePoint& newSize)
  43. {
  44. if (cornerSize != newSize)
  45. {
  46. cornerSize = newSize;
  47. rebuildPath();
  48. }
  49. }
  50. void DrawableRectangle::rebuildPath()
  51. {
  52. if (bounds.isDynamic() || cornerSize.isDynamic())
  53. {
  54. Drawable::Positioner<DrawableRectangle>* const p = new Drawable::Positioner<DrawableRectangle> (*this);
  55. setPositioner (p);
  56. p->apply();
  57. }
  58. else
  59. {
  60. setPositioner (nullptr);
  61. recalculateCoordinates (nullptr);
  62. }
  63. }
  64. bool DrawableRectangle::registerCoordinates (RelativeCoordinatePositionerBase& pos)
  65. {
  66. bool ok = pos.addPoint (bounds.topLeft);
  67. ok = pos.addPoint (bounds.topRight) && ok;
  68. ok = pos.addPoint (bounds.bottomLeft) && ok;
  69. return pos.addPoint (cornerSize) && ok;
  70. }
  71. void DrawableRectangle::recalculateCoordinates (Expression::Scope* scope)
  72. {
  73. Point<float> points[3];
  74. bounds.resolveThreePoints (points, scope);
  75. const float cornerSizeX = (float) cornerSize.x.resolve (scope);
  76. const float cornerSizeY = (float) cornerSize.y.resolve (scope);
  77. const float w = Line<float> (points[0], points[1]).getLength();
  78. const float h = Line<float> (points[0], points[2]).getLength();
  79. Path newPath;
  80. if (cornerSizeX > 0 && cornerSizeY > 0)
  81. newPath.addRoundedRectangle (0, 0, w, h, cornerSizeX, cornerSizeY);
  82. else
  83. newPath.addRectangle (0, 0, w, h);
  84. newPath.applyTransform (AffineTransform::fromTargetPoints (0, 0, points[0].x, points[0].y,
  85. w, 0, points[1].x, points[1].y,
  86. 0, h, points[2].x, points[2].y));
  87. if (path != newPath)
  88. {
  89. path.swapWithPath (newPath);
  90. pathChanged();
  91. }
  92. }
  93. //==============================================================================
  94. const Identifier DrawableRectangle::valueTreeType ("Rectangle");
  95. const Identifier DrawableRectangle::ValueTreeWrapper::topLeft ("topLeft");
  96. const Identifier DrawableRectangle::ValueTreeWrapper::topRight ("topRight");
  97. const Identifier DrawableRectangle::ValueTreeWrapper::bottomLeft ("bottomLeft");
  98. const Identifier DrawableRectangle::ValueTreeWrapper::cornerSize ("cornerSize");
  99. //==============================================================================
  100. DrawableRectangle::ValueTreeWrapper::ValueTreeWrapper (const ValueTree& state_)
  101. : FillAndStrokeState (state_)
  102. {
  103. jassert (state.hasType (valueTreeType));
  104. }
  105. RelativeParallelogram DrawableRectangle::ValueTreeWrapper::getRectangle() const
  106. {
  107. return RelativeParallelogram (state.getProperty (topLeft, "0, 0"),
  108. state.getProperty (topRight, "100, 0"),
  109. state.getProperty (bottomLeft, "0, 100"));
  110. }
  111. void DrawableRectangle::ValueTreeWrapper::setRectangle (const RelativeParallelogram& newBounds, UndoManager* undoManager)
  112. {
  113. state.setProperty (topLeft, newBounds.topLeft.toString(), undoManager);
  114. state.setProperty (topRight, newBounds.topRight.toString(), undoManager);
  115. state.setProperty (bottomLeft, newBounds.bottomLeft.toString(), undoManager);
  116. }
  117. void DrawableRectangle::ValueTreeWrapper::setCornerSize (const RelativePoint& newSize, UndoManager* undoManager)
  118. {
  119. state.setProperty (cornerSize, newSize.toString(), undoManager);
  120. }
  121. RelativePoint DrawableRectangle::ValueTreeWrapper::getCornerSize() const
  122. {
  123. return RelativePoint (state [cornerSize]);
  124. }
  125. Value DrawableRectangle::ValueTreeWrapper::getCornerSizeValue (UndoManager* undoManager)
  126. {
  127. return state.getPropertyAsValue (cornerSize, undoManager);
  128. }
  129. //==============================================================================
  130. void DrawableRectangle::refreshFromValueTree (const ValueTree& tree, ComponentBuilder& builder)
  131. {
  132. ValueTreeWrapper v (tree);
  133. setComponentID (v.getID());
  134. refreshFillTypes (v, builder.getImageProvider());
  135. setStrokeType (v.getStrokeType());
  136. setRectangle (v.getRectangle());
  137. setCornerSize (v.getCornerSize());
  138. }
  139. ValueTree DrawableRectangle::createValueTree (ComponentBuilder::ImageProvider* imageProvider) const
  140. {
  141. ValueTree tree (valueTreeType);
  142. ValueTreeWrapper v (tree);
  143. v.setID (getComponentID());
  144. writeTo (v, imageProvider, nullptr);
  145. v.setRectangle (bounds, nullptr);
  146. v.setCornerSize (cornerSize, nullptr);
  147. return tree;
  148. }