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_RelativeRectangle.cpp 9.1KB

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