Browse Source

Added new method Rectangle::toNearestIntEdges(), and used this to improve the Grid layout snapping

tags/2021-05-28
jules 7 years ago
parent
commit
121719be69
2 changed files with 15 additions and 4 deletions
  1. +14
    -3
      modules/juce_graphics/geometry/juce_Rectangle.h
  2. +1
    -1
      modules/juce_gui_basics/layout/juce_Grid.cpp

+ 14
- 3
modules/juce_graphics/geometry/juce_Rectangle.h View File

@@ -794,7 +794,7 @@ public:
*/
Rectangle transformedBy (const AffineTransform& transform) const noexcept
{
typedef typename TypeHelpers::SmallestFloatType<ValueType>::type FloatType;
using FloatType = typename TypeHelpers::SmallestFloatType<ValueType>::type;
auto x1 = static_cast<FloatType> (pos.x), y1 = static_cast<FloatType> (pos.y);
auto x2 = static_cast<FloatType> (pos.x + w), y2 = static_cast<FloatType> (pos.y);
@@ -816,7 +816,7 @@ public:
/** Returns the smallest integer-aligned rectangle that completely contains this one.
This is only relevant for floating-point rectangles, of course.
@see toFloat(), toNearestInt()
@see toFloat(), toNearestInt(), toNearestIntEdges()
*/
Rectangle<int> getSmallestIntegerContainer() const noexcept
{
@@ -829,7 +829,7 @@ public:
/** Casts this rectangle to a Rectangle<int>.
This uses roundToInt to snap x, y, width and height to the nearest integer (losing precision).
If the rectangle already uses integers, this will simply return a copy.
@see getSmallestIntegerContainer()
@see getSmallestIntegerContainer(), toNearestIntEdges()
*/
Rectangle<int> toNearestInt() const noexcept
{
@@ -837,6 +837,17 @@ public:
roundToInt (w), roundToInt (h) };
}
/** Casts this rectangle to a Rectangle<int>.
This uses roundToInt to snap top, left, right and bottom to the nearest integer (losing precision).
If the rectangle already uses integers, this will simply return a copy.
@see getSmallestIntegerContainer(), toNearestInt()
*/
Rectangle<int> toNearestIntEdges() const noexcept
{
return Rectangle<int>::leftTopRightBottom (roundToInt (pos.x), roundToInt (pos.y),
roundToInt (getRight()), roundToInt (getBottom()));
}
/** Casts this rectangle to a Rectangle<float>.
@see getSmallestIntegerContainer
*/


+ 1
- 1
modules/juce_gui_basics/layout/juce_Grid.cpp View File

@@ -1017,7 +1017,7 @@ void Grid::performLayout (juce::Rectangle<int> targetArea)
+ targetArea.toFloat().getPosition();
if (auto* c = item->associatedComponent)
c->setBounds (item->currentBounds.toNearestInt());
c->setBounds (item->currentBounds.toNearestIntEdges());
}
}


Loading…
Cancel
Save