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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. //==============================================================================
  22. /**
  23. Expresses a coordinate as a dynamically evaluated expression.
  24. When using relative coordinates to position components, the following symbols are available:
  25. - "left", "right", "top", "bottom" refer to the position of those edges in this component, so
  26. e.g. for a component whose width is always 100, you might set the right edge to the "left + 100".
  27. - "[id].left", "[id].right", "[id].top", "[id].bottom", "[id].width", "[id].height", where [id] is
  28. the identifier of one of this component's siblings. A component's identifier is set with
  29. Component::setComponentID(). So for example if you want your component to always be 50 pixels to the
  30. right of the one called "xyz", you could set your left edge to be "xyz.right + 50".
  31. - Instead of an [id], you can use the name "parent" to refer to this component's parent. Like
  32. any other component, these values are relative to their component's parent, so "parent.right" won't be
  33. very useful for positioning a component because it refers to a position with the parent's parent.. but
  34. "parent.width" can be used for setting positions relative to the parent's size. E.g. to make a 10x10
  35. component which remains 1 pixel away from its parent's bottom-right, you could use
  36. "right - 10, bottom - 10, parent.width - 1, parent.height - 1".
  37. - The name of one of the parent component's markers can also be used as a symbol. For markers to be
  38. used, the parent component must implement its Component::getMarkers() method, and return at least one
  39. valid MarkerList. So if you want your component's top edge to be 10 pixels below the
  40. marker called "foobar", you'd set it to "foobar + 10".
  41. See the Expression class for details about the operators that are supported, but for example
  42. if you wanted to make your component remains centred within its parent with a size of 100, 100,
  43. you could express it as:
  44. @code myComp.setBounds (RelativeBounds ("parent.width / 2 - 50, parent.height / 2 - 50, left + 100, top + 100"));
  45. @endcode
  46. ..or an alternative way to achieve the same thing:
  47. @code myComp.setBounds (RelativeBounds ("right - 100, bottom - 100, parent.width / 2 + 50, parent.height / 2 + 50"));
  48. @endcode
  49. Or if you wanted a 100x100 component whose top edge is lined up to a marker called "topMarker" and
  50. which is positioned 50 pixels to the right of another component called "otherComp", you could write:
  51. @code myComp.setBounds (RelativeBounds ("otherComp.right + 50, topMarker, left + 100, top + 100"));
  52. @endcode
  53. Be careful not to make your coordinate expressions recursive, though, or exceptions and assertions will
  54. be thrown!
  55. @see RelativePoint, RelativeRectangle
  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. 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. } // namespace juce