Browse Source

ResolveLength to ResolveNumericProperty, see #55. Angles now require units of deg or rad.

Michael Ragazzon 6 years ago
parent
commit
b4c4c48297

+ 9 - 7
Include/RmlUi/Core/Element.h

@@ -222,15 +222,17 @@ public:
 	/// @return The local properties for this element, or nullptr if no properties defined
 	const PropertyMap& GetLocalStyleProperties();
 
-	/// Resolves a property with units of number, length, or percentage to a length in 'px' units.
-	/// @param[in] name The property to resolve the value for.
+	/// Resolves a property with units of number, percentage, length, or angle to their canonical unit (unit-less, 'px', or 'rad').
+	/// Numbers and percentages are scaled by the base value and returned.
+	/// @param[in] property The property to resolve the value for.
 	/// @param[in] base_value The value that is scaled by the number or percentage value, if applicable.
-	/// @return The resolved value in 'px' unit, or zero if it could not be resolved.
-	float ResolveLength(const Property *property, float base_value);
-	/// Resolve a property by its name to length in 'px' units. The specified property should be a number, length or percentage.
+	/// @return The resolved value in their canonical unit, or zero if it could not be resolved.
+	float ResolveNumericProperty(const Property *property, float base_value);
+	/// Resolves a property with units of number, percentage, length, or angle to their canonical unit (unit-less, 'px', or 'rad').
+	/// Numbers and percentages are scaled according to the relative target of the property definition.
 	/// @param[in] name The property to resolve the value for.
-	/// @return The resolved value in 'px' unit, or zero if it could not be resolved.
-	float ResolveLength(const String& property_name);
+	/// @return The resolved value in their canonical unit, or zero if it could not be resolved.
+	float ResolveNumericProperty(const String& property_name);
 
 	/// Returns the size of the containing block. Often percentages are scaled relative to this.
 	Vector2f GetContainingBlock();

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

@@ -93,7 +93,7 @@ public:
 		LENGTH_PERCENT = LENGTH | PERCENT,
 		NUMBER_LENGTH_PERCENT = NUMBER | LENGTH | PERCENT,
 		ABSOLUTE_LENGTH = PX | DP | PPI_UNIT,
-		ANGLE = NUMBER | DEG | RAD
+		ANGLE = DEG | RAD
 	};
 
 	Property();

+ 1 - 1
Samples/basic/transform/src/main.cpp

@@ -78,7 +78,7 @@ public:
 			std::stringstream s;
 			if (perspective > 0)
 				s << "perspective(" << perspective << "px) ";
-			s << "rotate3d(0.0, 1.0, 0.0, " << degrees << ")";
+			s << "rotate3d(0.0, 1.0, 0.0, " << degrees << "deg)";
 			document->SetProperty("transform", s.str().c_str());
 		}
 	}

+ 19 - 0
Source/Core/ComputeProperty.cpp

@@ -148,6 +148,25 @@ float ComputeAbsoluteLength(const Property& property, float dp_ratio)
 	return 0.0f;
 }
 
+float ComputeAngle(const Property& property)
+{
+	float value = property.value.Get<float>();
+
+	switch (property.unit)
+	{
+	case Property::NUMBER:
+	case Property::RAD:
+		return value;
+
+	case Property::DEG:
+		return Math::DegreesToRadians(value);
+	default:
+		break;
+	}
+
+	return 0.0f;
+}
+
 float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio)
 {
 	// The calculated value of the font-size property is inherited, so we need to check if this

+ 2 - 0
Source/Core/ComputeProperty.h

@@ -38,6 +38,8 @@ float ComputeLength(const Property* property, float font_size, float document_fo
 
 float ComputeAbsoluteLength(const Property& property, float dp_ratio);
 
+float ComputeAngle(const Property& property);
+
 float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio);
 
 Style::Clip ComputeClip(const Property* property);

+ 4 - 10
Source/Core/DecoratorNinePatch.cpp

@@ -102,10 +102,10 @@ DecoratorDataHandle DecoratorNinePatch::GenerateElementData(Element* element) co
 	{
 		const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
 		float lengths[4]; // top, right, bottom, left
-		lengths[0] = element->ResolveLength(&(*edges)[0], dp_ratio * (surface_pos[1].y - surface_pos[0].y));
-		lengths[1] = element->ResolveLength(&(*edges)[1], dp_ratio * (surface_pos[3].x - surface_pos[2].x));
-		lengths[2] = element->ResolveLength(&(*edges)[2], dp_ratio * (surface_pos[3].y - surface_pos[2].y));
-		lengths[3] = element->ResolveLength(&(*edges)[3], dp_ratio * (surface_pos[1].x - surface_pos[0].x));
+		lengths[0] = element->ResolveNumericProperty(&(*edges)[0], dp_ratio * (surface_pos[1].y - surface_pos[0].y));
+		lengths[1] = element->ResolveNumericProperty(&(*edges)[1], dp_ratio * (surface_pos[3].x - surface_pos[2].x));
+		lengths[2] = element->ResolveNumericProperty(&(*edges)[2], dp_ratio * (surface_pos[3].y - surface_pos[2].y));
+		lengths[3] = element->ResolveNumericProperty(&(*edges)[3], dp_ratio * (surface_pos[1].x - surface_pos[0].x));
 
 		surface_pos[1].y = lengths[0];
 		surface_pos[2].x = surface_dimensions.x - lengths[1];
@@ -210,12 +210,6 @@ SharedPtr<Decorator> DecoratorNinePatchInstancer::InstanceDecorator(const String
 		{
 			edges_set = true;
 		}
-		// Change numbers to percent because we cannot resolve this later unless we make some changes to the resolve procedures.
-		if (edges[i].unit == Property::NUMBER)
-		{
-			edges[i].value = edges[i].value.Get(0.0f) * 100.f;
-			edges[i].unit = Property::PERCENT;
-		}
 	}
 
 	const Sprite* sprite_outer = nullptr;

+ 1 - 1
Source/Core/DecoratorTiled.h

@@ -70,7 +70,7 @@ public:
 		CONTAIN,    // Tile is stretched to boundaries, keeping aspect ratio fixed, 'letter-boxed'.
 		COVER,      // Tile is stretched to cover the boundaries, keeping aspect ratio fixed.
 		SCALE_NONE, // Tile is never scaled.
-		SCALE_DOWN, // Tile acts like 'center' if smaller than boundaries, or like 'contain' otherwise.
+		SCALE_DOWN, // Tile acts like 'scale-none' if smaller than boundaries, or like 'contain' otherwise.
 	};
 
 	/**

+ 6 - 3
Source/Core/Element.cpp

@@ -642,17 +642,20 @@ const PropertyMap& Element::GetLocalStyleProperties()
 	return style->GetLocalStyleProperties();
 }
 
-float Element::ResolveLength(const Property *property, float base_value)
+float Element::ResolveNumericProperty(const Property *property, float base_value)
 {
-	return style->ResolveLength(property, base_value);
+	return style->ResolveNumericProperty(property, base_value);
 }
 
-float Element::ResolveLength(const String& property_name)
+float Element::ResolveNumericProperty(const String& property_name)
 {
 	auto property = style->GetProperty(StyleSheetSpecification::GetPropertyId(property_name));
 	if (!property)
 		return 0.0f;
 
+	if (property->unit & Property::ANGLE)
+		return ComputeAngle(*property);
+
 	RelativeTarget relative_target = RelativeTarget::None;
 	if (property->definition)
 		relative_target = property->definition->GetRelativeTarget();

+ 5 - 3
Source/Core/ElementStyle.cpp

@@ -342,15 +342,17 @@ const PropertyMap& ElementStyle::GetLocalStyleProperties() const
 	return inline_properties.GetProperties();
 }
 
-float ElementStyle::ResolveLength(const Property* property, float base_value) const
+float ElementStyle::ResolveNumericProperty(const Property* property, float base_value) const
 {
-	if (!property || !(property->unit & Property::NUMBER_LENGTH_PERCENT))
+	if (!property || !(property->unit & (Property::NUMBER_LENGTH_PERCENT | Property::ANGLE)))
 		return 0.0f;
 
 	if (property->unit & Property::NUMBER)
 		return property->Get<float>() * base_value;
 	else if (property->unit & Property::PERCENT)
 		return property->Get<float>() * base_value * 0.01f;
+	else if (property->unit & Property::ANGLE)
+		return ComputeAngle(*property);
 
 	const float dp_ratio = ElementUtilities::GetDensityIndependentPixelRatio(element);
 	const float font_size = element->GetComputedValues().font_size;
@@ -383,7 +385,7 @@ float ElementStyle::ResolveLength(const Property* property, RelativeTarget relat
 	switch (relative_target)
 	{
 	case RelativeTarget::None:
-		base_value = 0.0f;
+		base_value = 1.0f;
 		break;
 	case RelativeTarget::ContainingBlockWidth:
 		base_value = element->GetContainingBlock().x;

+ 3 - 3
Source/Core/ElementStyle.h

@@ -103,11 +103,11 @@ public:
 	/// Returns the local style properties, excluding any properties from local class.
 	const PropertyMap& GetLocalStyleProperties() const;
 
-	/// Resolves a property with units of number, length, or percentage to a length in 'px' units.
+	/// Resolves a property with units of number, percentage, length, or angle to their canonical unit (unit-less, 'px', or 'rad').
 	/// @param[in] property The property to resolve the value for.
 	/// @param[in] base_value The value that is scaled by the number or percentage value, if applicable.
-	/// @return The resolved value in 'px' unit, or zero if it could not be resolved.
-	float ResolveLength(const Property* property, float base_value) const;
+	/// @return The resolved value in their canonical unit, or zero if it could not be resolved.
+	float ResolveNumericProperty(const Property* property, float base_value) const;
 	/// Resolves a property with units of number, length, or percentage to a length in 'px' units.
 	/// Numbers and percentages are resolved by scaling the size of the specified target.
 	float ResolveLength(const Property* property, RelativeTarget relative_target) const;

+ 1 - 1
Source/Core/Math.cpp

@@ -112,7 +112,7 @@ RMLUICORE_API float DegreesToRadians(float angle)
 	return angle * (RMLUI_PI / 180.0f);
 }
 
-// Normalises and angle in radians
+// Normalises an angle in radians
 RMLUICORE_API float NormaliseAngle(float angle)
 {
 	return fmodf(angle, RMLUI_PI * 2.0f);

+ 1 - 1
Source/Core/TransformPrimitive.cpp

@@ -91,7 +91,7 @@ float NumericValue::ResolveLengthPercentage(Element& e, float base) const noexce
 	Property prop;
 	prop.value = Variant(number);
 	prop.unit = unit;
-	return e.ResolveLength(&prop, base);
+	return e.ResolveNumericProperty(&prop, base);
 }
 
 float NumericValue::ResolveWidth(Element& e) const noexcept

+ 3 - 2
readme.md

@@ -388,6 +388,7 @@ Breaking changes since RmlUi v2.0.
 - The Controls::DataGrid "min-rows" property has been removed.
 - Removed RenderInterface::GetPixelsPerInch, instead the pixels per inch value has been fixed to 96 PPI, as per CSS specs. To achieve a scalable user interface, instead use the 'dp' unit.
 - Removed 'top' and 'bottom' from z-index property.
+- Anglees need to be declared in either 'deg' or 'rad', unit-less numbers do not work.
 - See changes to the declaration of decorators and font-effects above.
 - See changes to the render interface regarding transforms above.
 - The focus flag in `ElementDocument::Show` has been changed, with a new enum name and new options, see above.
@@ -448,7 +449,7 @@ Use `perspective`, `perspective-origin`, `transform` and `transform-origin` in R
 ```CSS
 perspective: 1000px;
 perspective-origin: 20px 50%;
-transform: rotateX(10) skew(-10, 15) translateZ(100px);
+transform: rotateX(10deg) skew(-10deg, 15deg) translateZ(100px);
 transform-origin: left top 0;
 ```
 
@@ -478,7 +479,7 @@ skewY,        angle1
 skew,         angle2
 ```
 
-Angles are in degrees by default.
+Angles take units of 'deg' or 'rad'.