Browse Source

Math: Add ExpandToPixelGrid

Michael Ragazzon 5 years ago
parent
commit
90e4c1a82b
2 changed files with 17 additions and 6 deletions
  1. 4 0
      Include/RmlUi/Core/Math.h
  2. 13 6
      Source/Core/Math.cpp

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

@@ -175,6 +175,10 @@ RMLUICORE_API void SnapToPixelGrid(float& x, float& width);
 /// @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.
 RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size);
+/// 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] size The size, which is rounded such that the right and bottom edges are rounded up.
+RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size);
 
 /// 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.

+ 13 - 6
Source/Core/Math.cpp

@@ -166,16 +166,23 @@ RMLUICORE_API int RealToInteger(float value)
 
 RMLUICORE_API void SnapToPixelGrid(float& offset, float& width)
 {
-	const float rounded_offset = Math::RoundFloat(offset);
-	width = Math::RoundFloat(offset + width) - rounded_offset;
-	offset = rounded_offset;
+	const float right_edge = offset + width;
+	offset = Math::RoundFloat(offset);
+	width = Math::RoundFloat(right_edge) - offset;
 }
 
 RMLUICORE_API void SnapToPixelGrid(Vector2f& position, Vector2f& size)
 {
-	const Vector2f rounded_position = position.Round();
-	size = (position + size).Round() - rounded_position;
-	position = rounded_position;
+	const Vector2f bottom_right = position + size;
+	position = position.Round();
+	size = bottom_right.Round() - position;
+}
+
+RMLUICORE_API void ExpandToPixelGrid(Vector2f& position, Vector2f& size)
+{
+	const Vector2f bottom_right = position + size;
+	position = Vector2f(std::floor(position.x), std::floor(position.y));
+	size = Vector2f(std::ceil(bottom_right.x), std::ceil(bottom_right.y)) - position;
 }
 
 // Converts the given number to a power of two, rounding up if necessary.