Browse Source

Make 'ratio' properties use integer comparisons to avoid floating point issues.

Michael Ragazzon 4 years ago
parent
commit
390eb6bef5

+ 1 - 0
Include/RmlUi/Core/Property.h

@@ -92,6 +92,7 @@ public:
 		ANIMATION = 1 << 22,        // animation; fetch as < AnimationList >
 		ANIMATION = 1 << 22,        // animation; fetch as < AnimationList >
 		DECORATOR = 1 << 23,        // decorator; fetch as < DecoratorsPtr >
 		DECORATOR = 1 << 23,        // decorator; fetch as < DecoratorsPtr >
 		FONTEFFECT = 1 << 24,       // font-effect; fetch as < FontEffectsPtr >
 		FONTEFFECT = 1 << 24,       // font-effect; fetch as < FontEffectsPtr >
+		RATIO = 1 << 25,            // ratio defined as x/y; fetch as < Vector2f >
 
 
 		LENGTH = PX | DP | PPI_UNIT | EM | REM | VW | VH | X,
 		LENGTH = PX | DP | PPI_UNIT | EM | REM | VW | VH | X,
 		LENGTH_PERCENT = LENGTH | PERCENT,
 		LENGTH_PERCENT = LENGTH | PERCENT,

+ 2 - 2
Source/Core/PropertyParserRatio.cpp

@@ -65,8 +65,8 @@ bool PropertyParserRatio::ParseValue(Property& property, const String& value, co
 		return false;
 		return false;
 	}
 	}
 
 
-	property.value = Variant(first_value / second_value);
-	property.unit = Property::NUMBER;
+	property.value = Variant(Vector2f(first_value, second_value));
+	property.unit = Property::RATIO;
 
 
 	return true;
 	return true;
 }
 }

+ 9 - 4
Source/Core/StyleSheetContainer.cpp

@@ -57,7 +57,8 @@ bool StyleSheetContainer::UpdateCompiledStyleSheet(const Context* context)
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneScoped;
 
 
 	const float dp_ratio = context->GetDensityIndependentPixelRatio();
 	const float dp_ratio = context->GetDensityIndependentPixelRatio();
-	const Vector2f vp_dimensions(context->GetDimensions());
+	const Vector2i vp_dimensions_i(context->GetDimensions());
+	const Vector2f vp_dimensions(vp_dimensions_i);
 
 
 	Vector<int> new_active_media_block_indices;
 	Vector<int> new_active_media_block_indices;
 
 
@@ -70,6 +71,7 @@ bool StyleSheetContainer::UpdateCompiledStyleSheet(const Context* context)
 		for (const auto& property : media_block.properties.GetProperties())
 		for (const auto& property : media_block.properties.GetProperties())
 		{
 		{
 			const MediaQueryId id = static_cast<MediaQueryId>(property.first);
 			const MediaQueryId id = static_cast<MediaQueryId>(property.first);
+			Vector2i ratio;
 
 
 			switch (id)
 			switch (id)
 			{
 			{
@@ -98,15 +100,18 @@ bool StyleSheetContainer::UpdateCompiledStyleSheet(const Context* context)
 					all_match = false;
 					all_match = false;
 				break;
 				break;
 			case MediaQueryId::AspectRatio:
 			case MediaQueryId::AspectRatio:
-				if ((vp_dimensions.x / vp_dimensions.y) != property.second.Get<float>())
+				ratio = Vector2i(property.second.Get<Vector2f>());
+				if (vp_dimensions_i.x * ratio.y != vp_dimensions_i.y * ratio.x)
 					all_match = false;
 					all_match = false;
 				break;
 				break;
 			case MediaQueryId::MinAspectRatio:
 			case MediaQueryId::MinAspectRatio:
-				if ((vp_dimensions.x / vp_dimensions.y) < property.second.Get<float>())
+				ratio = Vector2i(property.second.Get<Vector2f>());
+				if (vp_dimensions_i.x * ratio.y < vp_dimensions_i.y * ratio.x)
 					all_match = false;
 					all_match = false;
 				break;
 				break;
 			case MediaQueryId::MaxAspectRatio:
 			case MediaQueryId::MaxAspectRatio:
-				if ((vp_dimensions.x / vp_dimensions.y) > property.second.Get<float>())
+				ratio = Vector2i(property.second.Get<Vector2f>());
+				if (vp_dimensions_i.x * ratio.y > vp_dimensions_i.y * ratio.x)
 					all_match = false;
 					all_match = false;
 				break;
 				break;
 			case MediaQueryId::Resolution:
 			case MediaQueryId::Resolution: