Browse Source

Rectangle: Add getters for all corners and math rounding procedure

Michael Ragazzon 2 years ago
parent
commit
607a8ce60f
3 changed files with 15 additions and 0 deletions
  1. 6 0
      Include/RmlUi/Core/Math.h
  2. 2 0
      Include/RmlUi/Core/Rectangle.h
  3. 7 0
      Source/Core/Math.cpp

+ 6 - 0
Include/RmlUi/Core/Math.h

@@ -42,6 +42,9 @@ template <typename Type>
 class Vector2;
 using Vector2f = Vector2<float>;
 using Vector2i = Vector2<int>;
+template <typename Type>
+class Rectangle;
+using Rectanglef = Rectangle<float>;
 
 namespace Math {
 
@@ -208,6 +211,9 @@ namespace Math {
 	/// @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.
 	RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size);
+	/// Round the rectangle to the pixel grid such that it fully covers the original rectangle.
+	/// @param[inout] position The rectangle to round.
+	RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle);
 
 	/// Converts a number to the nearest power of two, rounding up if necessary.
 	/// @param[in] value The value to convert to a power-of-two.

+ 2 - 0
Include/RmlUi/Core/Rectangle.h

@@ -54,7 +54,9 @@ public:
 	Vector2Type Size() const { return p1 - p0; }
 
 	Vector2Type TopLeft() const { return p0; }
+	Vector2Type TopRight() const { return {p1.x, p0.y}; }
 	Vector2Type BottomRight() const { return p1; }
+	Vector2Type BottomLeft() const { return {p0.x, p1.y}; }
 
 	Vector2Type Center() const { return (p0 + p1) / Type(2); }
 

+ 7 - 0
Source/Core/Math.cpp

@@ -188,6 +188,13 @@ namespace Math {
 		size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
 	}
 
+	RMLUICORE_API void ExpandToPixelGrid(Rectanglef& rectangle)
+	{
+		const Vector2f top_left = {std::floor(rectangle.Left()), std::floor(rectangle.Top())};
+		const Vector2f bottom_right = {std::ceil(rectangle.Right()), std::ceil(rectangle.Bottom())};
+		rectangle = Rectanglef::FromCorners(top_left, bottom_right);
+	}
+
 	RMLUICORE_API int ToPowerOfTwo(int number)
 	{
 		// Check if the number is already a power of two.