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_RelativeCoordinate.h 8.4KB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #ifndef JUCE_RELATIVECOORDINATE_H_INCLUDED
  18. #define JUCE_RELATIVECOORDINATE_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. Expresses a coordinate as a dynamically evaluated expression.
  22. When using relative coordinates to position components, the following symbols are available:
  23. - "left", "right", "top", "bottom" refer to the position of those edges in this component, so
  24. e.g. for a component whose width is always 100, you might set the right edge to the "left + 100".
  25. - "[id].left", "[id].right", "[id].top", "[id].bottom", "[id].width", "[id].height", where [id] is
  26. the identifier of one of this component's siblings. A component's identifier is set with
  27. Component::setComponentID(). So for example if you want your component to always be 50 pixels to the
  28. right of the one called "xyz", you could set your left edge to be "xyz.right + 50".
  29. - Instead of an [id], you can use the name "parent" to refer to this component's parent. Like
  30. any other component, these values are relative to their component's parent, so "parent.right" won't be
  31. very useful for positioning a component because it refers to a position with the parent's parent.. but
  32. "parent.width" can be used for setting positions relative to the parent's size. E.g. to make a 10x10
  33. component which remains 1 pixel away from its parent's bottom-right, you could use
  34. "right - 10, bottom - 10, parent.width - 1, parent.height - 1".
  35. - The name of one of the parent component's markers can also be used as a symbol. For markers to be
  36. used, the parent component must implement its Component::getMarkers() method, and return at least one
  37. valid MarkerList. So if you want your component's top edge to be 10 pixels below the
  38. marker called "foobar", you'd set it to "foobar + 10".
  39. See the Expression class for details about the operators that are supported, but for example
  40. if you wanted to make your component remains centred within its parent with a size of 100, 100,
  41. you could express it as:
  42. @code myComp.setBounds (RelativeBounds ("parent.width / 2 - 50, parent.height / 2 - 50, left + 100, top + 100"));
  43. @endcode
  44. ..or an alternative way to achieve the same thing:
  45. @code myComp.setBounds (RelativeBounds ("right - 100, bottom - 100, parent.width / 2 + 50, parent.height / 2 + 50"));
  46. @endcode
  47. Or if you wanted a 100x100 component whose top edge is lined up to a marker called "topMarker" and
  48. which is positioned 50 pixels to the right of another component called "otherComp", you could write:
  49. @code myComp.setBounds (RelativeBounds ("otherComp.right + 50, topMarker, left + 100, top + 100"));
  50. @endcode
  51. Be careful not to make your coordinate expressions recursive, though, or exceptions and assertions will
  52. be thrown!
  53. @see RelativePoint, RelativeRectangle
  54. */
  55. class JUCE_API RelativeCoordinate
  56. {
  57. public:
  58. //==============================================================================
  59. /** Creates a zero coordinate. */
  60. RelativeCoordinate();
  61. RelativeCoordinate (const Expression& expression);
  62. RelativeCoordinate (const RelativeCoordinate&);
  63. RelativeCoordinate& operator= (const RelativeCoordinate&);
  64. #if JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS
  65. RelativeCoordinate (RelativeCoordinate&&) noexcept;
  66. RelativeCoordinate& operator= (RelativeCoordinate&&) noexcept;
  67. #endif
  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. struct StandardStrings
  130. {
  131. enum Type
  132. {
  133. left, right, top, bottom,
  134. x, y, width, height,
  135. parent,
  136. unknown
  137. };
  138. static Type getTypeOf (const String& s) noexcept;
  139. };
  140. private:
  141. //==============================================================================
  142. Expression term;
  143. };
  144. #endif // JUCE_RELATIVECOORDINATE_H_INCLUDED