From 121719be691f088896f2379c152252de0f35d666 Mon Sep 17 00:00:00 2001 From: jules Date: Wed, 4 Apr 2018 16:43:17 +0100 Subject: [PATCH] Added new method Rectangle::toNearestIntEdges(), and used this to improve the Grid layout snapping --- modules/juce_graphics/geometry/juce_Rectangle.h | 17 ++++++++++++++--- modules/juce_gui_basics/layout/juce_Grid.cpp | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/modules/juce_graphics/geometry/juce_Rectangle.h b/modules/juce_graphics/geometry/juce_Rectangle.h index 5792948ddd..ba581757b6 100644 --- a/modules/juce_graphics/geometry/juce_Rectangle.h +++ b/modules/juce_graphics/geometry/juce_Rectangle.h @@ -794,7 +794,7 @@ public: */ Rectangle transformedBy (const AffineTransform& transform) const noexcept { - typedef typename TypeHelpers::SmallestFloatType::type FloatType; + using FloatType = typename TypeHelpers::SmallestFloatType::type; auto x1 = static_cast (pos.x), y1 = static_cast (pos.y); auto x2 = static_cast (pos.x + w), y2 = static_cast (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 getSmallestIntegerContainer() const noexcept { @@ -829,7 +829,7 @@ public: /** Casts this rectangle to a Rectangle. 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 toNearestInt() const noexcept { @@ -837,6 +837,17 @@ public: roundToInt (w), roundToInt (h) }; } + /** Casts this rectangle to a Rectangle. + 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 toNearestIntEdges() const noexcept + { + return Rectangle::leftTopRightBottom (roundToInt (pos.x), roundToInt (pos.y), + roundToInt (getRight()), roundToInt (getBottom())); + } + /** Casts this rectangle to a Rectangle. @see getSmallestIntegerContainer */ diff --git a/modules/juce_gui_basics/layout/juce_Grid.cpp b/modules/juce_gui_basics/layout/juce_Grid.cpp index 6bd3123bdd..721441cf26 100644 --- a/modules/juce_gui_basics/layout/juce_Grid.cpp +++ b/modules/juce_gui_basics/layout/juce_Grid.cpp @@ -1017,7 +1017,7 @@ void Grid::performLayout (juce::Rectangle targetArea) + targetArea.toFloat().getPosition(); if (auto* c = item->associatedComponent) - c->setBounds (item->currentBounds.toNearestInt()); + c->setBounds (item->currentBounds.toNearestIntEdges()); } }