1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Added floating-point overrides for some Component area conversion methods

This commit is contained in:
ed 2020-07-08 17:15:24 +01:00
parent 7ce8cc32e9
commit 60b6afd517
4 changed files with 62 additions and 6 deletions

View file

@ -216,6 +216,22 @@ struct ScalingHelpers
roundToInt ((float) pos.getHeight() * scale)) : pos;
}
static Rectangle<float> unscaledScreenPosToScaled (float scale, Rectangle<float> pos) noexcept
{
return scale != 1.0f ? Rectangle<float> (pos.getX() / scale,
pos.getY() / scale,
pos.getWidth() / scale,
pos.getHeight() / scale) : pos;
}
static Rectangle<float> scaledScreenPosToUnscaled (float scale, Rectangle<float> pos) noexcept
{
return scale != 1.0f ? Rectangle<float> (pos.getX() * scale,
pos.getY() * scale,
pos.getWidth() * scale,
pos.getHeight() * scale) : pos;
}
template <typename PointOrRect>
static PointOrRect unscaledScreenPosToScaled (PointOrRect pos) noexcept
{
@ -1051,13 +1067,15 @@ int Component::getScreenY() const { return getScreenPositi
Point<int> Component::getScreenPosition() const { return localPointToGlobal (Point<int>()); }
Rectangle<int> Component::getScreenBounds() const { return localAreaToGlobal (getLocalBounds()); }
Point<int> Component::getLocalPoint (const Component* source, Point<int> point) const { return ComponentHelpers::convertCoordinate (this, source, point); }
Point<float> Component::getLocalPoint (const Component* source, Point<float> point) const { return ComponentHelpers::convertCoordinate (this, source, point); }
Rectangle<int> Component::getLocalArea (const Component* source, Rectangle<int> area) const { return ComponentHelpers::convertCoordinate (this, source, area); }
Point<int> Component::getLocalPoint (const Component* source, Point<int> point) const { return ComponentHelpers::convertCoordinate (this, source, point); }
Point<float> Component::getLocalPoint (const Component* source, Point<float> point) const { return ComponentHelpers::convertCoordinate (this, source, point); }
Rectangle<int> Component::getLocalArea (const Component* source, Rectangle<int> area) const { return ComponentHelpers::convertCoordinate (this, source, area); }
Rectangle<float> Component::getLocalArea (const Component* source, Rectangle<float> area) const { return ComponentHelpers::convertCoordinate (this, source, area); }
Point<int> Component::localPointToGlobal (Point<int> point) const { return ComponentHelpers::convertCoordinate (nullptr, this, point); }
Point<float> Component::localPointToGlobal (Point<float> point) const { return ComponentHelpers::convertCoordinate (nullptr, this, point); }
Rectangle<int> Component::localAreaToGlobal (Rectangle<int> area) const { return ComponentHelpers::convertCoordinate (nullptr, this, area); }
Point<int> Component::localPointToGlobal (Point<int> point) const { return ComponentHelpers::convertCoordinate (nullptr, this, point); }
Point<float> Component::localPointToGlobal (Point<float> point) const { return ComponentHelpers::convertCoordinate (nullptr, this, point); }
Rectangle<int> Component::localAreaToGlobal (Rectangle<int> area) const { return ComponentHelpers::convertCoordinate (nullptr, this, area); }
Rectangle<float> Component::localAreaToGlobal (Rectangle<float> area) const { return ComponentHelpers::convertCoordinate (nullptr, this, area); }
//==============================================================================
void Component::setBounds (int x, int y, int w, int h)

View file

@ -369,6 +369,19 @@ public:
Rectangle<int> getLocalArea (const Component* sourceComponent,
Rectangle<int> areaRelativeToSourceComponent) const;
/** Converts a rectangle to be relative to this component's coordinate space.
This takes a rectangle that is relative to a different component, and returns its position relative
to this component. If the sourceComponent parameter is null, the source rectangle is assumed to be
a screen coordinate.
If you've used setTransform() to apply one or more transforms to components, then the source rectangle
may not actually be rectangular when converted to the target space, so in that situation this will return
the smallest rectangle that fully contains the transformed area.
*/
Rectangle<float> getLocalArea (const Component* sourceComponent,
Rectangle<float> areaRelativeToSourceComponent) const;
/** Converts a point relative to this component's top-left into a screen coordinate.
@see getLocalPoint, localAreaToGlobal
*/
@ -388,6 +401,15 @@ public:
*/
Rectangle<int> localAreaToGlobal (Rectangle<int> localArea) const;
/** Converts a rectangle from this component's coordinate space to a screen coordinate.
If you've used setTransform() to apply one or more transforms to components, then the source rectangle
may not actually be rectangular when converted to the target space, so in that situation this will return
the smallest rectangle that fully contains the transformed area.
@see getLocalPoint, localPointToGlobal
*/
Rectangle<float> localAreaToGlobal (Rectangle<float> localArea) const;
//==============================================================================
/** Moves the component to a new position.

View file

@ -402,6 +402,16 @@ Rectangle<int> ComponentPeer::globalToLocal (const Rectangle<int>& screenPositio
return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
}
Rectangle<float> ComponentPeer::localToGlobal (const Rectangle<float>& relativePosition)
{
return relativePosition.withPosition (localToGlobal (relativePosition.getPosition()));
}
Rectangle<float> ComponentPeer::globalToLocal (const Rectangle<float>& screenPosition)
{
return screenPosition.withPosition (globalToLocal (screenPosition.getPosition()));
}
Rectangle<int> ComponentPeer::getAreaCoveredBy (Component& subComponent) const
{
return ScalingHelpers::scaledScreenPosToUnscaled

View file

@ -167,6 +167,12 @@ public:
/** Converts a screen area to a position relative to the top-left of this component. */
virtual Rectangle<int> globalToLocal (const Rectangle<int>& screenPosition);
/** Converts a rectangle relative to the top-left of this component to screen coordinates. */
Rectangle<float> localToGlobal (const Rectangle<float>& relativePosition);
/** Converts a screen area to a position relative to the top-left of this component. */
Rectangle<float> globalToLocal (const Rectangle<float>& screenPosition);
/** Returns the area in peer coordinates that is covered by the given sub-comp (which
may be at any depth)
*/