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.

265 lines
9.1KB

  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. namespace RelativeRectangleHelpers
  20. {
  21. inline void skipComma (String::CharPointerType& s)
  22. {
  23. s = s.findEndOfWhitespace();
  24. if (*s == ',')
  25. ++s;
  26. }
  27. static bool dependsOnSymbolsOtherThanThis (const Expression& e)
  28. {
  29. if (e.getType() == Expression::operatorType && e.getSymbolOrFunction() == ".")
  30. return true;
  31. if (e.getType() == Expression::symbolType)
  32. {
  33. switch (RelativeCoordinate::StandardStrings::getTypeOf (e.getSymbolOrFunction()))
  34. {
  35. case RelativeCoordinate::StandardStrings::x:
  36. case RelativeCoordinate::StandardStrings::y:
  37. case RelativeCoordinate::StandardStrings::left:
  38. case RelativeCoordinate::StandardStrings::right:
  39. case RelativeCoordinate::StandardStrings::top:
  40. case RelativeCoordinate::StandardStrings::bottom: return false;
  41. default: break;
  42. }
  43. return true;
  44. }
  45. else
  46. {
  47. for (int i = e.getNumInputs(); --i >= 0;)
  48. if (dependsOnSymbolsOtherThanThis (e.getInput(i)))
  49. return true;
  50. }
  51. return false;
  52. }
  53. }
  54. //==============================================================================
  55. RelativeRectangle::RelativeRectangle()
  56. {
  57. }
  58. RelativeRectangle::RelativeRectangle (const RelativeCoordinate& left_, const RelativeCoordinate& right_,
  59. const RelativeCoordinate& top_, const RelativeCoordinate& bottom_)
  60. : left (left_), right (right_), top (top_), bottom (bottom_)
  61. {
  62. }
  63. RelativeRectangle::RelativeRectangle (const Rectangle<float>& rect)
  64. : left (rect.getX()),
  65. right (Expression::symbol (RelativeCoordinate::Strings::left) + Expression ((double) rect.getWidth())),
  66. top (rect.getY()),
  67. bottom (Expression::symbol (RelativeCoordinate::Strings::top) + Expression ((double) rect.getHeight()))
  68. {
  69. }
  70. RelativeRectangle::RelativeRectangle (const String& s)
  71. {
  72. String error;
  73. String::CharPointerType text (s.getCharPointer());
  74. left = RelativeCoordinate (Expression::parse (text, error));
  75. RelativeRectangleHelpers::skipComma (text);
  76. top = RelativeCoordinate (Expression::parse (text, error));
  77. RelativeRectangleHelpers::skipComma (text);
  78. right = RelativeCoordinate (Expression::parse (text, error));
  79. RelativeRectangleHelpers::skipComma (text);
  80. bottom = RelativeCoordinate (Expression::parse (text, error));
  81. }
  82. bool RelativeRectangle::operator== (const RelativeRectangle& other) const noexcept
  83. {
  84. return left == other.left && top == other.top && right == other.right && bottom == other.bottom;
  85. }
  86. bool RelativeRectangle::operator!= (const RelativeRectangle& other) const noexcept
  87. {
  88. return ! operator== (other);
  89. }
  90. //==============================================================================
  91. // An expression context that can evaluate expressions using "this"
  92. class RelativeRectangleLocalScope : public Expression::Scope
  93. {
  94. public:
  95. RelativeRectangleLocalScope (const RelativeRectangle& rect_) : rect (rect_) {}
  96. Expression getSymbolValue (const String& symbol) const
  97. {
  98. switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
  99. {
  100. case RelativeCoordinate::StandardStrings::x:
  101. case RelativeCoordinate::StandardStrings::left: return rect.left.getExpression();
  102. case RelativeCoordinate::StandardStrings::y:
  103. case RelativeCoordinate::StandardStrings::top: return rect.top.getExpression();
  104. case RelativeCoordinate::StandardStrings::right: return rect.right.getExpression();
  105. case RelativeCoordinate::StandardStrings::bottom: return rect.bottom.getExpression();
  106. default: break;
  107. }
  108. return Expression::Scope::getSymbolValue (symbol);
  109. }
  110. private:
  111. const RelativeRectangle& rect;
  112. JUCE_DECLARE_NON_COPYABLE (RelativeRectangleLocalScope)
  113. };
  114. const Rectangle<float> RelativeRectangle::resolve (const Expression::Scope* scope) const
  115. {
  116. if (scope == nullptr)
  117. {
  118. RelativeRectangleLocalScope defaultScope (*this);
  119. return resolve (&defaultScope);
  120. }
  121. else
  122. {
  123. const double l = left.resolve (scope);
  124. const double r = right.resolve (scope);
  125. const double t = top.resolve (scope);
  126. const double b = bottom.resolve (scope);
  127. return Rectangle<float> ((float) l, (float) t, (float) jmax (0.0, r - l), (float) jmax (0.0, b - t));
  128. }
  129. }
  130. void RelativeRectangle::moveToAbsolute (const Rectangle<float>& newPos, const Expression::Scope* scope)
  131. {
  132. left.moveToAbsolute (newPos.getX(), scope);
  133. right.moveToAbsolute (newPos.getRight(), scope);
  134. top.moveToAbsolute (newPos.getY(), scope);
  135. bottom.moveToAbsolute (newPos.getBottom(), scope);
  136. }
  137. bool RelativeRectangle::isDynamic() const
  138. {
  139. using namespace RelativeRectangleHelpers;
  140. return dependsOnSymbolsOtherThanThis (left.getExpression())
  141. || dependsOnSymbolsOtherThanThis (right.getExpression())
  142. || dependsOnSymbolsOtherThanThis (top.getExpression())
  143. || dependsOnSymbolsOtherThanThis (bottom.getExpression());
  144. }
  145. String RelativeRectangle::toString() const
  146. {
  147. return left.toString() + ", " + top.toString() + ", " + right.toString() + ", " + bottom.toString();
  148. }
  149. void RelativeRectangle::renameSymbol (const Expression::Symbol& oldSymbol, const String& newName, const Expression::Scope& scope)
  150. {
  151. left = left.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
  152. right = right.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
  153. top = top.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
  154. bottom = bottom.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
  155. }
  156. //==============================================================================
  157. class RelativeRectangleComponentPositioner : public RelativeCoordinatePositionerBase
  158. {
  159. public:
  160. RelativeRectangleComponentPositioner (Component& comp, const RelativeRectangle& r)
  161. : RelativeCoordinatePositionerBase (comp),
  162. rectangle (r)
  163. {
  164. }
  165. bool registerCoordinates() override
  166. {
  167. bool ok = addCoordinate (rectangle.left);
  168. ok = addCoordinate (rectangle.right) && ok;
  169. ok = addCoordinate (rectangle.top) && ok;
  170. ok = addCoordinate (rectangle.bottom) && ok;
  171. return ok;
  172. }
  173. bool isUsingRectangle (const RelativeRectangle& other) const noexcept
  174. {
  175. return rectangle == other;
  176. }
  177. void applyToComponentBounds() override
  178. {
  179. for (int i = 32; --i >= 0;)
  180. {
  181. ComponentScope scope (getComponent());
  182. const Rectangle<int> newBounds (rectangle.resolve (&scope).getSmallestIntegerContainer());
  183. if (newBounds == getComponent().getBounds())
  184. return;
  185. getComponent().setBounds (newBounds);
  186. }
  187. jassertfalse; // Seems to be a recursive reference!
  188. }
  189. void applyNewBounds (const Rectangle<int>& newBounds) override
  190. {
  191. if (newBounds != getComponent().getBounds())
  192. {
  193. ComponentScope scope (getComponent());
  194. rectangle.moveToAbsolute (newBounds.toFloat(), &scope);
  195. applyToComponentBounds();
  196. }
  197. }
  198. private:
  199. RelativeRectangle rectangle;
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (RelativeRectangleComponentPositioner)
  201. };
  202. void RelativeRectangle::applyToComponent (Component& component) const
  203. {
  204. if (isDynamic())
  205. {
  206. RelativeRectangleComponentPositioner* current = dynamic_cast<RelativeRectangleComponentPositioner*> (component.getPositioner());
  207. if (current == nullptr || ! current->isUsingRectangle (*this))
  208. {
  209. RelativeRectangleComponentPositioner* p = new RelativeRectangleComponentPositioner (component, *this);
  210. component.setPositioner (p);
  211. p->apply();
  212. }
  213. }
  214. else
  215. {
  216. component.setPositioner (nullptr);
  217. component.setBounds (resolve (nullptr).getSmallestIntegerContainer());
  218. }
  219. }