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.

182 lines
8.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. Expresses a coordinate as a dynamically evaluated expression.
  23. When using relative coordinates to position components, the following symbols are available:
  24. - "left", "right", "top", "bottom" refer to the position of those edges in this component, so
  25. e.g. for a component whose width is always 100, you might set the right edge to the "left + 100".
  26. - "[id].left", "[id].right", "[id].top", "[id].bottom", "[id].width", "[id].height", where [id] is
  27. the identifier of one of this component's siblings. A component's identifier is set with
  28. Component::setComponentID(). So for example if you want your component to always be 50 pixels to the
  29. right of the one called "xyz", you could set your left edge to be "xyz.right + 50".
  30. - Instead of an [id], you can use the name "parent" to refer to this component's parent. Like
  31. any other component, these values are relative to their component's parent, so "parent.right" won't be
  32. very useful for positioning a component because it refers to a position with the parent's parent.. but
  33. "parent.width" can be used for setting positions relative to the parent's size. E.g. to make a 10x10
  34. component which remains 1 pixel away from its parent's bottom-right, you could use
  35. "right - 10, bottom - 10, parent.width - 1, parent.height - 1".
  36. - The name of one of the parent component's markers can also be used as a symbol. For markers to be
  37. used, the parent component must implement its Component::getMarkers() method, and return at least one
  38. valid MarkerList. So if you want your component's top edge to be 10 pixels below the
  39. marker called "foobar", you'd set it to "foobar + 10".
  40. See the Expression class for details about the operators that are supported, but for example
  41. if you wanted to make your component remains centred within its parent with a size of 100, 100,
  42. you could express it as:
  43. @code myComp.setBounds (RelativeBounds ("parent.width / 2 - 50, parent.height / 2 - 50, left + 100, top + 100"));
  44. @endcode
  45. ..or an alternative way to achieve the same thing:
  46. @code myComp.setBounds (RelativeBounds ("right - 100, bottom - 100, parent.width / 2 + 50, parent.height / 2 + 50"));
  47. @endcode
  48. Or if you wanted a 100x100 component whose top edge is lined up to a marker called "topMarker" and
  49. which is positioned 50 pixels to the right of another component called "otherComp", you could write:
  50. @code myComp.setBounds (RelativeBounds ("otherComp.right + 50, topMarker, left + 100, top + 100"));
  51. @endcode
  52. Be careful not to make your coordinate expressions recursive, though, or exceptions and assertions will
  53. be thrown!
  54. @see RelativePoint, RelativeRectangle
  55. @tags{GUI}
  56. */
  57. class JUCE_API RelativeCoordinate
  58. {
  59. public:
  60. //==============================================================================
  61. /** Creates a zero coordinate. */
  62. RelativeCoordinate();
  63. RelativeCoordinate (const Expression& expression);
  64. RelativeCoordinate (const RelativeCoordinate&);
  65. RelativeCoordinate& operator= (const RelativeCoordinate&);
  66. RelativeCoordinate (RelativeCoordinate&&) noexcept;
  67. RelativeCoordinate& operator= (RelativeCoordinate&&) noexcept;
  68. /** Creates an absolute position from the parent origin on either the X or Y axis.
  69. @param absoluteDistanceFromOrigin the distance from the origin
  70. */
  71. RelativeCoordinate (double absoluteDistanceFromOrigin);
  72. /** Recreates a coordinate from a string description.
  73. The string will be parsed by ExpressionParser::parse().
  74. @param stringVersion the expression to use
  75. @see toString
  76. */
  77. RelativeCoordinate (const String& stringVersion);
  78. /** Destructor. */
  79. ~RelativeCoordinate();
  80. bool operator== (const RelativeCoordinate&) const noexcept;
  81. bool operator!= (const RelativeCoordinate&) const noexcept;
  82. //==============================================================================
  83. /** Calculates the absolute position of this coordinate.
  84. You'll need to provide a suitable Expression::Scope for looking up any coordinates that may
  85. be needed to calculate the result.
  86. */
  87. double resolve (const Expression::Scope* evaluationScope) const;
  88. /** Returns true if this coordinate uses the specified coord name at any level in its evaluation.
  89. This will recursively check any coordinates upon which this one depends.
  90. */
  91. bool references (const String& coordName, const Expression::Scope* evaluationScope) const;
  92. /** Returns true if there's a recursive loop when trying to resolve this coordinate's position. */
  93. bool isRecursive (const Expression::Scope* evaluationScope) const;
  94. /** Returns true if this coordinate depends on any other coordinates for its position. */
  95. bool isDynamic() const;
  96. //==============================================================================
  97. /** Changes the value of this coord to make it resolve to the specified position.
  98. Calling this will leave the anchor points unchanged, but will set this coordinate's absolute
  99. or relative position to whatever value is necessary to make its resultant position
  100. match the position that is provided.
  101. */
  102. void moveToAbsolute (double absoluteTargetPosition, const Expression::Scope* evaluationScope);
  103. /** Returns the expression that defines this coordinate. */
  104. const Expression& getExpression() const { return term; }
  105. //==============================================================================
  106. /** Returns a string which represents this coordinate.
  107. For details of the string syntax, see the constructor notes.
  108. */
  109. String toString() const;
  110. //==============================================================================
  111. /** A set of static strings that are commonly used by the RelativeCoordinate class.
  112. As well as avoiding using string literals in your code, using these preset values
  113. has the advantage that all instances of the same string will share the same, reference-counted
  114. String object, so if you have thousands of points which all refer to the same
  115. anchor points, this can save a significant amount of memory allocation.
  116. */
  117. struct Strings
  118. {
  119. static const String parent; /**< "parent" */
  120. static const String left; /**< "left" */
  121. static const String right; /**< "right" */
  122. static const String top; /**< "top" */
  123. static const String bottom; /**< "bottom" */
  124. static const String x; /**< "x" */
  125. static const String y; /**< "y" */
  126. static const String width; /**< "width" */
  127. static const String height; /**< "height" */
  128. };
  129. /** @internal */
  130. struct StandardStrings
  131. {
  132. enum Type
  133. {
  134. left, right, top, bottom,
  135. x, y, width, height,
  136. parent,
  137. unknown
  138. };
  139. static Type getTypeOf (const String& s) noexcept;
  140. };
  141. private:
  142. //==============================================================================
  143. Expression term;
  144. };
  145. } // namespace juce