Browse Source

Round clipping region by snapping rectangle instead of expanding

This way, the size of a clipping region is independent of positioning. This behavior also matches how borders are drawn, thus better coinciding with the padding area which is what we normally want to clip to.
Michael Ragazzon 1 year ago
parent
commit
ee31b5e7e0
3 changed files with 10 additions and 2 deletions
  1. 4 1
      Include/RmlUi/Core/Math.h
  2. 1 1
      Source/Core/ElementUtilities.cpp
  3. 5 0
      Source/Core/Math.cpp

+ 4 - 1
Include/RmlUi/Core/Math.h

@@ -208,12 +208,15 @@ namespace Math {
 	/// @param[inout] position The position, which will use normal rounding.
 	/// @param[inout] position The position, which will use normal rounding.
 	/// @param[inout] size The size, which is rounded such that movement of the right and bottom edges is minimized.
 	/// @param[inout] size The size, which is rounded such that movement of the right and bottom edges is minimized.
 	RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size);
 	RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size);
+	/// Round the rectangle to the pixel grid while minimizing movement of the edges.
+	/// @param[inout] rectangle The rectangle to round.
+	RMLUICORE_API void SnapToPixelGrid(Rectanglef& rectangle);
 	/// Round the position and size of a rectangle to the pixel grid such that it fully covers the original rectangle.
 	/// Round the position and size of a rectangle to the pixel grid such that it fully covers the original rectangle.
 	/// @param[inout] position The position, which will be rounded down.
 	/// @param[inout] position The position, which will be rounded down.
 	/// @param[inout] size The size, which is rounded such that the right and bottom edges are rounded up.
 	/// @param[inout] size The size, which is rounded such that the right and bottom edges are rounded up.
 	RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size);
 	RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size);
 	/// Round the rectangle to the pixel grid such that it fully covers the original rectangle.
 	/// Round the rectangle to the pixel grid such that it fully covers the original rectangle.
-	/// @param[inout] position The rectangle to round.
+	/// @param[inout] rectangle The rectangle to round.
 	RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle);
 	RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle);
 
 
 	/// Converts a number to the nearest power of two, rounding up if necessary.
 	/// Converts a number to the nearest power of two, rounding up if necessary.

+ 1 - 1
Source/Core/ElementUtilities.cpp

@@ -255,7 +255,7 @@ bool ElementUtilities::GetClippingRegion(Element* element, Rectanglei& out_clip_
 
 
 	if (clip_region.Valid())
 	if (clip_region.Valid())
 	{
 	{
-		Math::ExpandToPixelGrid(clip_region);
+		Math::SnapToPixelGrid(clip_region);
 		out_clip_region = Rectanglei(clip_region);
 		out_clip_region = Rectanglei(clip_region);
 	}
 	}
 
 

+ 5 - 0
Source/Core/Math.cpp

@@ -184,6 +184,11 @@ namespace Math {
 		size = bottom_right.Round() - position;
 		size = bottom_right.Round() - position;
 	}
 	}
 
 
+	RMLUICORE_API void SnapToPixelGrid(Rectanglef& rectangle)
+	{
+		rectangle = Rectanglef::FromCorners(rectangle.TopLeft().Round(), rectangle.BottomRight().Round());
+	}
+
 	RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
 	RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
 	{
 	{
 		const Vector2f bottom_right = position + size;
 		const Vector2f bottom_right = position + size;