Browse Source

Compute more properties

Michael Ragazzon 6 years ago
parent
commit
ad9f11a988

+ 2 - 2
Include/Rocket/Core/Element.h

@@ -61,7 +61,7 @@ class PropertyDictionary;
 class RenderInterface;
 class StyleSheet;
 struct ElementMeta;
-namespace RCSS { struct ComputedValues; }
+namespace Style { struct ComputedValues; }
 
 /**
 	A generic element in the DOM tree.
@@ -622,7 +622,7 @@ public:
 	/// @param[in] event The event to process.
 	virtual void ProcessEvent(Event& event);
 
-	const RCSS::ComputedValues& GetComputedValues() const;
+	const Style::ComputedValues& GetComputedValues() const;
 
 protected:
 	void Update();

+ 2 - 2
Include/Rocket/Core/ElementUtilities.h

@@ -40,7 +40,7 @@ class Context;
 class FontFaceHandle;
 class RenderInterface;
 class ViewState;
-namespace RCSS { struct ComputedValues; }
+namespace Style { struct ComputedValues; }
 
 /**
 	Utility functions for dealing with elements.
@@ -82,7 +82,7 @@ public:
 	/// Returns an element's font face.
 	/// @param[in] element The element to determine the font face for.
 	/// @return The element's font face. This will be NULL if no valid RCSS font styles have been set up for this element.
-	static FontFaceHandle* GetFontFaceHandle(const RCSS::ComputedValues& computed_values);
+	static FontFaceHandle* GetFontFaceHandle(const Style::ComputedValues& computed_values);
 	/// Returns an element's density-independent pixel ratio, defined by it's context
 	/// @param[in] element The element to determine the density-independent pixel ratio for.
 	/// @return The density-independent pixel ratio of the context, or 1.0 if no context assigned.

+ 4 - 1
Samples/basic/benchmark/src/main.cpp

@@ -82,7 +82,10 @@ public:
 		  Include chobo flat containers: 65.0  [1696aa5]
 		  Move benchmark to its own sample (no code change, fps increase because of removal of animation elements): 68.0  [2433880]
 		  Keep the element's main sizing box local: 69.0  [cf928b2]
-		  Improved hashing of element definition: 70.0
+		  Improved hashing of element definition: 70.0  [5cb9e1d]
+		  First usage of computed values (font): 74.0  [04dc275]
+		  Computed values, clipping: 77.0
+		  Computed values, background-color, image-color, opacity: 77.0
 		
 		*/
 

+ 4 - 2
Source/Core/DecoratorTiled.cpp

@@ -29,6 +29,7 @@
 #include "precompiled.h"
 #include "DecoratorTiled.h"
 #include "../../Include/Rocket/Core.h"
+#include "RCSS.h"
 
 namespace Rocket {
 namespace Core {
@@ -118,9 +119,10 @@ Vector2f DecoratorTiled::Tile::GetDimensions(Element* element)
 void DecoratorTiled::Tile::GenerateGeometry(std::vector< Vertex >& vertices, std::vector< int >& indices, Element* element, const Vector2f& surface_origin, const Vector2f& surface_dimensions, const Vector2f& tile_dimensions) const
 {
 	RenderInterface* render_interface = element->GetRenderInterface();
-    float opacity = element->GetProperty<float>(OPACITY);
+	const auto& computed = element->GetComputedValues();
 
-    Colourb quad_colour = element->GetProperty<Colourb>(IMAGE_COLOR);
+	float opacity = computed.opacity;
+	Colourb quad_colour = computed.image_color;
 
     // Apply opacity
     quad_colour.alpha = (byte)(opacity * (float)quad_colour.alpha);

+ 13 - 16
Source/Core/Element.cpp

@@ -94,7 +94,7 @@ struct ElementMeta
 	ElementBorder border;
 	ElementDecoration decoration;
 	ElementScroll scroll;
-	RCSS::ComputedValues computed_values;
+	Style::ComputedValues computed_values;
 
 	void* ElementMeta::operator new(size_t size)
 	{
@@ -209,9 +209,12 @@ void Element::Update()
 	style->UpdateDefinition();
 	scroll->Update();
 
-	if(all_properties_dirty || dirty_properties.count("font_family"))
+	UpdateAnimation();
+	AdvanceAnimations();
+
+	if(all_properties_dirty || dirty_properties.size() > 0)
 	{
-		using namespace RCSS;
+		using namespace Style;
 		const ComputedValues* parent_values = (parent ? &parent->GetComputedValues() : nullptr);
 		float ppi = GetRenderInterface()->GetPixelsPerInch();
 		float dp_ratio = 1.0f;
@@ -225,9 +228,6 @@ void Element::Update()
 		style->ComputeValues(element_meta->computed_values, parent_values, document_values, dp_ratio, ppi);
 	}
 
-	UpdateAnimation();
-	AdvanceAnimations();
-
 	UpdateDirtyProperties();
 
 	if (box_dirty)
@@ -1695,18 +1695,15 @@ bool Element::IsClippingEnabled()
 {
 	if (clipping_state_dirty)
 	{
+		const auto& computed = GetComputedValues();
+
 		// Is clipping enabled for this element, yes unless both overlow properties are set to visible
-		clipping_enabled = style->GetProperty(OVERFLOW_X)->Get< int >() != OVERFLOW_VISIBLE 
-							|| style->GetProperty(OVERFLOW_Y)->Get< int >() != OVERFLOW_VISIBLE;
+		clipping_enabled = computed.overflow_x != Style::Overflow::Visible
+							|| computed.overflow_y != Style::Overflow::Visible;
 		
 		// Get the clipping ignore depth from the clip property
-		clipping_ignore_depth = 0;
-		const Property* clip_property = GetProperty(CLIP);
-		if (clip_property->unit == Property::NUMBER)
-			clipping_ignore_depth = clip_property->Get< int >();
-		else if (clip_property->Get< int >() == CLIP_NONE)
-			clipping_ignore_depth = -1;
-		
+		clipping_ignore_depth = static_cast<int>(computed.clip);
+
 		clipping_state_dirty = false;
 	}
 	
@@ -2140,7 +2137,7 @@ void Element::ProcessEvent(Event& event)
 	}
 }
 
-const RCSS::ComputedValues& Element::GetComputedValues() const
+const Style::ComputedValues& Element::GetComputedValues() const
 {
 	return element_meta->computed_values;
 }

+ 5 - 2
Source/Core/ElementBackground.cpp

@@ -31,6 +31,8 @@
 #include "../../Include/Rocket/Core/Element.h"
 #include "../../Include/Rocket/Core/GeometryUtilities.h"
 #include "../../Include/Rocket/Core/Property.h"
+#include "RCSS.h"
+
 
 namespace Rocket {
 namespace Core {
@@ -67,8 +69,9 @@ void ElementBackground::DirtyBackground()
 void ElementBackground::GenerateBackground()
 {
 	// Fetch the new colour for the background. If the colour is transparent, then we don't render any background.
-	Colourb colour = element->GetProperty(BACKGROUND_COLOR)->value.Get< Colourb >();
-	float opacity = element->GetProperty<float>(OPACITY);
+	auto& computed = element->GetComputedValues();
+	Colourb colour = computed.background_color;
+	float opacity = computed.opacity;
 
 	// Apply opacity
 	colour.alpha = (byte)(opacity * (float)colour.alpha);

+ 44 - 6
Source/Core/ElementStyle.cpp

@@ -49,7 +49,7 @@
 namespace Rocket {
 namespace Core {
 
-static const RCSS::ComputedValues DefaultComputedValues;
+static const Style::ComputedValues DefaultComputedValues;
 
 
 ElementStyle::ElementStyle(Element* _element)
@@ -1144,7 +1144,7 @@ static float ComputeAbsoluteLength(const Property& property, float dp_ratio, flo
 
 
 // Resolves one of this element's properties.
-static float ComputeFontsize(const Property& property, const RCSS::ComputedValues& values, const RCSS::ComputedValues* parent_values, const RCSS::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
+static float ComputeFontsize(const Property& property, const Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
 {
 	// The calculated value of the font-size property is inherited, so we need to check if this
 	// is an inherited property. If so, then we return our parent's font size instead.
@@ -1179,8 +1179,20 @@ static float ComputeFontsize(const Property& property, const RCSS::ComputedValue
 }
 
 
+static inline Style::Clip ComputeClip(const Property* property)
+{
+	int value = property->Get<int>();
+	if (property->unit == Property::KEYWORD)
+		return (value == CLIP_NONE ? Style::Clip::None : Style::Clip::Auto);
+	else if (property->unit == Property::NUMBER)
+		return (Style::Clip)value;
+	ROCKET_ERRORMSG("Invalid clip type");
+	return Style::Clip::Auto;
+}
+
 
-void ElementStyle::ComputeValues(RCSS::ComputedValues& values, const RCSS::ComputedValues* parent_values, const RCSS::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
+// Must be called in correct order, from document root to children elements.
+void ElementStyle::ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch)
 {
 	// TODO: Iterate through dirty values, and compute corresponding properties.
 	// Important: Always do font-size first if dirty, because of em-relative values.
@@ -1194,21 +1206,47 @@ void ElementStyle::ComputeValues(RCSS::ComputedValues& values, const RCSS::Compu
 		values.font_family = p->Get<String>();
 	else if (parent_values)
 		values.font_family = parent_values->font_family;
-
+	
 	if (auto p = GetLocalProperty(FONT_CHARSET))
 		values.font_charset = p->Get<String>();
 	else if (parent_values)
 		values.font_charset = parent_values->font_charset;
 
 	if (auto p = GetLocalProperty(FONT_STYLE))
-		values.font_style = (RCSS::FontStyle)p->Get< int >();
+		values.font_style = (Style::FontStyle)p->Get< int >();
 	else if (parent_values)
 		values.font_style = parent_values->font_style;
 
 	if (auto p = GetLocalProperty(FONT_WEIGHT))
-		values.font_weight = (RCSS::FontWeight)p->Get< int >();
+		values.font_weight = (Style::FontWeight)p->Get< int >();
 	else if (parent_values)
 		values.font_weight = parent_values->font_weight;
+
+
+	if (auto p = GetLocalProperty(OVERFLOW_X))
+		values.overflow_x = (Style::Overflow)p->Get< int >();
+
+	if (auto p = GetLocalProperty(OVERFLOW_Y))
+		values.overflow_y = (Style::Overflow)p->Get< int >();
+
+
+	if (auto p = GetLocalProperty(CLIP))
+		values.clip = ComputeClip(p);
+	else if (parent_values)
+		values.clip = parent_values->clip;
+
+
+	if (auto p = GetLocalProperty(OPACITY))
+		values.opacity = p->Get<float>();
+	else if (parent_values)
+		values.opacity = parent_values->opacity;
+
+	if (auto p = GetLocalProperty(BACKGROUND_COLOR))
+		values.background_color = p->Get<Colourb>();
+
+	if (auto p = GetLocalProperty(IMAGE_COLOR))
+		values.image_color = p->Get<Colourb>();
+
 }
 
 }

+ 2 - 2
Source/Core/ElementStyle.h

@@ -34,7 +34,7 @@
 namespace Rocket {
 namespace Core {
 
-namespace RCSS { struct ComputedValues; }
+namespace Style { struct ComputedValues; }
 
 class ElementStyleCache;
 
@@ -216,7 +216,7 @@ public:
 	const Property *GetTransformOriginZ();
 
 
-	void ComputeValues(RCSS::ComputedValues& values, const RCSS::ComputedValues* parent_values, const RCSS::ComputedValues* document_values, float dp_ratio, float pixels_per_inch);
+	void ComputeValues(Style::ComputedValues& values, const Style::ComputedValues* parent_values, const Style::ComputedValues* document_values, float dp_ratio, float pixels_per_inch);
 
 private:
 	// Sets a single property as dirty.

+ 3 - 3
Source/Core/ElementUtilities.cpp

@@ -114,15 +114,15 @@ void ElementUtilities::GetElementsByClassName(ElementList& elements, Element* ro
 }
 
 // Returns the element's font face.
-FontFaceHandle* ElementUtilities::GetFontFaceHandle(const RCSS::ComputedValues& computed_values)
+FontFaceHandle* ElementUtilities::GetFontFaceHandle(const Style::ComputedValues& computed_values)
 {
 	static const String default_charset = "U+0020-007E";
-	// Fetch the new font face.
 
 	const String& charset = (computed_values.font_charset.empty() ? default_charset : computed_values.font_charset);
+	int font_size = (int)computed_values.font_size;
 
 	// TODO Synchronize enums
-	FontFaceHandle* font = FontDatabase::GetFontFaceHandle(computed_values.font_family, charset, (Font::Style)computed_values.font_style, (Font::Weight)computed_values.font_weight, computed_values.font_size);
+	FontFaceHandle* font = FontDatabase::GetFontFaceHandle(computed_values.font_family, charset, (Font::Style)computed_values.font_style, (Font::Weight)computed_values.font_weight, font_size);
 	return font;
 }
 

+ 4 - 6
Source/Core/RCSS.h

@@ -33,7 +33,7 @@ namespace Core {
 
 #include "../../Include/Rocket/Core/Types.h"
 
-namespace RCSS
+namespace Style
 {
 struct LengthPercentageAuto {
 	enum Type { Length, Percentage, Auto } type = Length;
@@ -67,10 +67,7 @@ struct VerticalAlign {
 
 enum class Overflow { Visible, Hidden, Auto, Scroll };
 
-struct Clip {
-	enum Type { Auto, Number, None } type = Auto;
-	float value = 0;
-};
+enum class Clip { None = -1, Auto = 0, NumberStart = 1}; // Can contain any positive value as number
 
 enum class Visibility { Visible, Hidden };
 
@@ -114,7 +111,7 @@ struct ComputedValues
 	VerticalAlign vertical_align;
 
 	Overflow overflow_x = Overflow::Visible, overflow_y = Overflow::Visible;
-	Clip clip;
+	Clip clip = Clip::Auto;
 
 	Visibility visibility = Visibility::Visible;
 
@@ -153,6 +150,7 @@ struct ComputedValues
 };
 }
 
+using ComputedValues = Style::ComputedValues;
 
 }
 }