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.

175 lines
8.0KB

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