JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_RelativeRectangle.cpp
Go to the documentation of this file.
1 /*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29namespace RelativeRectangleHelpers
30{
31 inline void skipComma (String::CharPointerType& s)
32 {
33 s.incrementToEndOfWhitespace();
34
35 if (*s == ',')
36 ++s;
37 }
38
39 static bool dependsOnSymbolsOtherThanThis (const Expression& e)
40 {
41 if (e.getType() == Expression::operatorType && e.getSymbolOrFunction() == ".")
42 return true;
43
44 if (e.getType() == Expression::symbolType)
45 {
46 switch (RelativeCoordinate::StandardStrings::getTypeOf (e.getSymbolOrFunction()))
47 {
48 case RelativeCoordinate::StandardStrings::x:
49 case RelativeCoordinate::StandardStrings::y:
50 case RelativeCoordinate::StandardStrings::left:
51 case RelativeCoordinate::StandardStrings::right:
52 case RelativeCoordinate::StandardStrings::top:
53 case RelativeCoordinate::StandardStrings::bottom: return false;
54 case RelativeCoordinate::StandardStrings::width:
55 case RelativeCoordinate::StandardStrings::height:
56 case RelativeCoordinate::StandardStrings::parent:
57 case RelativeCoordinate::StandardStrings::unknown:
58
59 default: break;
60 }
61
62 return true;
63 }
64 else
65 {
66 for (int i = e.getNumInputs(); --i >= 0;)
67 if (dependsOnSymbolsOtherThanThis (e.getInput (i)))
68 return true;
69 }
70
71 return false;
72 }
73}
74
75//==============================================================================
79
85
87 : left (rect.getX()),
88 right (Expression::symbol (RelativeCoordinate::Strings::left) + Expression ((double) rect.getWidth())),
89 top (rect.getY()),
90 bottom (Expression::symbol (RelativeCoordinate::Strings::top) + Expression ((double) rect.getHeight()))
91{
92}
93
95{
96 String error;
98 left = RelativeCoordinate (Expression::parse (text, error));
99 RelativeRectangleHelpers::skipComma (text);
100 top = RelativeCoordinate (Expression::parse (text, error));
101 RelativeRectangleHelpers::skipComma (text);
102 right = RelativeCoordinate (Expression::parse (text, error));
103 RelativeRectangleHelpers::skipComma (text);
104 bottom = RelativeCoordinate (Expression::parse (text, error));
105}
106
107bool RelativeRectangle::operator== (const RelativeRectangle& other) const noexcept
108{
109 return left == other.left && top == other.top && right == other.right && bottom == other.bottom;
110}
111
112bool RelativeRectangle::operator!= (const RelativeRectangle& other) const noexcept
113{
114 return ! operator== (other);
115}
116
117//==============================================================================
118// An expression context that can evaluate expressions using "this"
120{
121public:
123
124 Expression getSymbolValue (const String& symbol) const override
125 {
126 switch (RelativeCoordinate::StandardStrings::getTypeOf (symbol))
127 {
128 case RelativeCoordinate::StandardStrings::x:
129 case RelativeCoordinate::StandardStrings::left: return rect.left.getExpression();
130 case RelativeCoordinate::StandardStrings::y:
131 case RelativeCoordinate::StandardStrings::top: return rect.top.getExpression();
132 case RelativeCoordinate::StandardStrings::right: return rect.right.getExpression();
133 case RelativeCoordinate::StandardStrings::bottom: return rect.bottom.getExpression();
134 case RelativeCoordinate::StandardStrings::width:
135 case RelativeCoordinate::StandardStrings::height:
136 case RelativeCoordinate::StandardStrings::parent:
137 case RelativeCoordinate::StandardStrings::unknown:
138 default: break;
139 }
140
141 return Expression::Scope::getSymbolValue (symbol);
142 }
143
144private:
145 const RelativeRectangle& rect;
146
148};
149
151{
152 if (scope == nullptr)
153 {
155 return resolve (&defaultScope);
156 }
157 else
158 {
159 const double l = left.resolve (scope);
160 const double r = right.resolve (scope);
161 const double t = top.resolve (scope);
162 const double b = bottom.resolve (scope);
163
164 return Rectangle<float> ((float) l, (float) t, (float) jmax (0.0, r - l), (float) jmax (0.0, b - t));
165 }
166}
167
169{
170 left.moveToAbsolute (newPos.getX(), scope);
171 right.moveToAbsolute (newPos.getRight(), scope);
172 top.moveToAbsolute (newPos.getY(), scope);
173 bottom.moveToAbsolute (newPos.getBottom(), scope);
174}
175
177{
178 using namespace RelativeRectangleHelpers;
179
180 return dependsOnSymbolsOtherThanThis (left.getExpression())
181 || dependsOnSymbolsOtherThanThis (right.getExpression())
182 || dependsOnSymbolsOtherThanThis (top.getExpression())
183 || dependsOnSymbolsOtherThanThis (bottom.getExpression());
184}
185
187{
188 return left.toString() + ", " + top.toString() + ", " + right.toString() + ", " + bottom.toString();
189}
190
192{
193 left = left.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
194 right = right.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
195 top = top.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
196 bottom = bottom.getExpression().withRenamedSymbol (oldSymbol, newName, scope);
197}
198
199//==============================================================================
201{
202public:
205 rectangle (r)
206 {
207 }
208
209 bool registerCoordinates() override
210 {
211 bool ok = addCoordinate (rectangle.left);
212 ok = addCoordinate (rectangle.right) && ok;
213 ok = addCoordinate (rectangle.top) && ok;
214 ok = addCoordinate (rectangle.bottom) && ok;
215 return ok;
216 }
217
218 bool isUsingRectangle (const RelativeRectangle& other) const noexcept
219 {
220 return rectangle == other;
221 }
222
223 void applyToComponentBounds() override
224 {
225 for (int i = 32; --i >= 0;)
226 {
228 const Rectangle<int> newBounds (rectangle.resolve (&scope).getSmallestIntegerContainer());
229
230 if (newBounds == getComponent().getBounds())
231 return;
232
234 }
235
236 jassertfalse; // Seems to be a recursive reference!
237 }
238
240 {
241 if (newBounds != getComponent().getBounds())
242 {
244 rectangle.moveToAbsolute (newBounds.toFloat(), &scope);
245
246 applyToComponentBounds();
247 }
248 }
249
250private:
251 RelativeRectangle rectangle;
252
254};
255
257{
258 if (isDynamic())
259 {
261
262 if (current == nullptr || ! current->isUsingRectangle (*this))
263 {
265
266 component.setPositioner (p);
267 p->apply();
268 }
269 }
270 else
271 {
272 component.setPositioner (nullptr);
273 component.setBounds (resolve (nullptr).getSmallestIntegerContainer());
274 }
275}
276
277} // namespace juce
Wraps a pointer to a null-terminated UTF-8 character string, and provides various methods to operate ...
Component & getComponent() const noexcept
Returns the component that this positioner controls.
The base class for all JUCE user-interface objects.
void setPositioner(Positioner *newPositioner)
Sets a new Positioner object for this component.
Positioner * getPositioner() const noexcept
Returns the Positioner object that has been set for this component.
void setBounds(int x, int y, int width, int height)
Changes the component's position and size.
When evaluating an Expression object, this class is used to resolve symbols and perform functions tha...
virtual Expression getSymbolValue(const String &symbol) const
Returns the value of a symbol.
A class for dynamically evaluating simple numeric expressions.
Expression withRenamedSymbol(const Symbol &oldSymbol, const String &newName, const Scope &scope) const
Returns a copy of this expression in which all instances of a given symbol have been renamed.
static Expression parse(String::CharPointerType &stringToParse, String &parseError)
Returns an Expression which parses a string from a character pointer, and updates the pointer to indi...
Manages a rectangle and allows geometric operations to be performed on it.
Rectangle< int > getSmallestIntegerContainer() const noexcept
Returns the smallest integer-aligned rectangle that completely contains this one.
Used for resolving a RelativeCoordinate expression in the context of a component.
Base class for Component::Positioners that are based upon relative coordinates.
Expresses a coordinate as a dynamically evaluated expression.
void moveToAbsolute(double absoluteTargetPosition, const Expression::Scope *evaluationScope)
Changes the value of this coord to make it resolve to the specified position.
const Expression & getExpression() const
Returns the expression that defines this coordinate.
String toString() const
Returns a string which represents this coordinate.
double resolve(const Expression::Scope *evaluationScope) const
Calculates the absolute position of this coordinate.
void applyNewBounds(const Rectangle< int > &newBounds) override
Attempts to set the component's position to the given rectangle.
Expression getSymbolValue(const String &symbol) const override
Returns the value of a symbol.
A rectangle stored as a set of RelativeCoordinate values.
RelativeRectangle()
Creates a zero-size rectangle at the origin.
void moveToAbsolute(const Rectangle< float > &newPos, const Expression::Scope *scope)
Changes the values of this rectangle's coordinates to make it resolve to the specified position.
const Rectangle< float > resolve(const Expression::Scope *scope) const
Calculates the absolute position of this rectangle.
void applyToComponent(Component &component) const
Creates and sets an appropriate Component::Positioner object for the given component,...
bool isDynamic() const
Returns true if this rectangle depends on any external symbols for its position.
String toString() const
Returns a string which represents this point.
void renameSymbol(const Expression::Symbol &oldSymbol, const String &newName, const Expression::Scope &scope)
Renames a symbol if it is used by any of the coordinates.
The JUCE String class!
Definition juce_String.h:53
CharPointerType getCharPointer() const noexcept
Returns the character pointer currently being used to store this string.
CharPointer_UTF8 CharPointerType
This is the character encoding type used internally to store the string.
#define JUCE_DECLARE_NON_COPYABLE(className)
This is a shorthand macro for deleting a class's copy constructor and copy assignment operator.
#define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className)
This is a shorthand way of writing both a JUCE_DECLARE_NON_COPYABLE and JUCE_LEAK_DETECTOR macro for ...
#define jassertfalse
This will always cause an assertion failure.
typedef double
JUCE Namespace.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
Represents a symbol that is used in an Expression.