Browse Source

Replace many SetProperty(String, String) with SetProperty(String, Property)

Michael Ragazzon 6 years ago
parent
commit
122bd097bb

+ 44 - 18
Include/Rocket/Core/ComputedValues.h

@@ -35,7 +35,7 @@ namespace Rocket {
 namespace Core {
 
 struct LengthPercentageAuto {
-	enum Type { Length, Percentage, Auto } type = Length;
+	enum Type { Auto, Length, Percentage } type = Length;
 	float value = 0;
 	LengthPercentageAuto() {}
 	LengthPercentageAuto(Type type, float value = 0) : type(type), value(value) {}
@@ -58,6 +58,25 @@ struct NumberAuto {
 namespace Style
 {
 
+using Margin = LengthPercentageAuto;
+using Width = LengthPercentageAuto;
+using Height = LengthPercentageAuto;
+
+using Top = LengthPercentageAuto;
+using Right = LengthPercentageAuto;
+using Bottom = LengthPercentageAuto;
+using Left = LengthPercentageAuto;
+
+using Padding = LengthPercentage;
+
+using MinWidth = LengthPercentage;
+using MaxWidth = LengthPercentage;
+using MinHeight = LengthPercentage;
+using MaxHeight = LengthPercentage;
+
+using PerspectiveOrigin = LengthPercentage;
+using TransformOrigin = LengthPercentage;
+
 
 enum class Display { None, Block, Inline, InlineBlock };
 enum class Position { Static, Relative, Absolute, Fixed };
@@ -72,10 +91,15 @@ struct VerticalAlign {
 	VerticalAlign(float value) : type(Length), value(value) {}
 };
 
-
 enum class Overflow { Visible, Hidden, Auto, Scroll };
 
-enum class Clip { None = -1, Auto = 0, NumberStart = 1}; // Can contain any positive value as number
+struct Clip {
+	// Note, internally Auto is 0 and None is -1, however, the enum must correspond to the keywords in StyleSheetSpec
+	enum Type { Auto, None, Number }; 
+	int number = 0;
+	Clip() {}
+	Clip(Type type, int number = 0) : number(type == Auto ? 0 : (type == None ? -1 : number)) {}
+};
 
 enum class Visibility { Visible, Hidden };
 
@@ -106,34 +130,36 @@ struct LineHeight {
 // A computed value is a value resolved as far as possible :before: updating layout. See CSS specs for details of each property.
 struct ComputedValues
 {
-	LengthPercentageAuto margin_top, margin_right, margin_bottom, margin_left;
-	LengthPercentage padding_top, padding_right, padding_bottom, padding_left;
+	Margin margin_top, margin_right, margin_bottom, margin_left;
+	Padding padding_top, padding_right, padding_bottom, padding_left;
 	float border_top_width = 0, border_right_width = 0, border_bottom_width = 0, border_left_width = 0;
 	Colourb border_top_color{ 255, 255, 255 }, border_right_color{ 255, 255, 255 }, border_bottom_color{ 255, 255, 255 }, border_left_color{ 255, 255, 255 };
 
 	Display display = Display::Inline;
 	Position position = Position::Static;
 
-	LengthPercentageAuto top{ LengthPercentageAuto::Auto };
-	LengthPercentageAuto right{ LengthPercentageAuto::Auto };
-	LengthPercentageAuto bottom{ LengthPercentageAuto::Auto };
-	LengthPercentageAuto left{ LengthPercentageAuto::Auto };
+	Top top{ Top::Auto };
+	Right right{ Right::Auto };
+	Bottom bottom{ Bottom::Auto };
+	Left left{ Left::Auto };
 
 	Float float_ = Float::None;
 	Clear clear = Clear::None;
 
 	NumberAuto z_index = { NumberAuto::Auto };
 
-	LengthPercentageAuto width = { LengthPercentageAuto::Auto };
-	LengthPercentage min_width, max_width{ LengthPercentage::Length, -1.f };
-	LengthPercentageAuto height = { LengthPercentageAuto::Auto };
-	LengthPercentage min_height, max_height{ LengthPercentage::Length, -1.f };
+	Width width = { Width::Auto };
+	MinWidth min_width;
+	MaxWidth max_width{ MaxWidth::Length, -1.f };
+	Height height = { Height::Auto };
+	MinHeight min_height;
+	MaxHeight max_height{ MaxHeight::Length, -1.f };
 
 	LineHeight line_height;
 	VerticalAlign vertical_align;
 
 	Overflow overflow_x = Overflow::Visible, overflow_y = Overflow::Visible;
-	Clip clip = Clip::Auto;
+	Clip clip;
 
 	Visibility visibility = Visibility::Visible;
 
@@ -162,12 +188,12 @@ struct ComputedValues
 	PointerEvents pointer_events = PointerEvents::Auto;
 
 	float perspective = 0;
-	LengthPercentage perspective_origin_x = { LengthPercentage::Percentage, 50.f };
-	LengthPercentage perspective_origin_y = { LengthPercentage::Percentage, 50.f };
+	PerspectiveOrigin perspective_origin_x = { PerspectiveOrigin::Percentage, 50.f };
+	PerspectiveOrigin perspective_origin_y = { PerspectiveOrigin::Percentage, 50.f };
 
 	TransformRef transform;
-	LengthPercentage transform_origin_x = { LengthPercentage::Percentage, 50.f };
-	LengthPercentage transform_origin_y = { LengthPercentage::Percentage, 50.f };
+	TransformOrigin transform_origin_x = { TransformOrigin::Percentage, 50.f };
+	TransformOrigin transform_origin_y = { TransformOrigin::Percentage, 50.f };
 	float transform_origin_z = 0.0f;
 
 	TransitionList transition;

+ 6 - 4
Include/Rocket/Core/Property.h

@@ -87,13 +87,15 @@ public:
 
 	Property();
 	template < typename PropertyType >
-	Property(PropertyType value, Unit unit, int specificity = -1) : value(Variant(value)), unit(unit), specificity(specificity)
+	Property(PropertyType value, Unit unit, int specificity = -1) : value(value), unit(unit), specificity(specificity)
 	{
 		definition = NULL;
 		parser_index = -1;
 
 		source_line_number = 0;
 	}
+	template<typename EnumType, typename = typename std::enable_if< std::is_enum<EnumType>::value, EnumType >::type>
+	Property(EnumType value) : value(static_cast<int>(value)), unit(KEYWORD), specificity(-1) {}
 
 	~Property();	
 
@@ -114,11 +116,11 @@ public:
 	Unit unit;
 	int specificity;
 
-	const PropertyDefinition* definition;
-	int parser_index;
+	const PropertyDefinition* definition = nullptr;
+	int parser_index = -1;
 
 	String source;
-	int source_line_number;
+	int source_line_number = 0;
 };
 
 }

+ 7 - 7
Source/Controls/ElementDataGrid.cpp

@@ -45,24 +45,24 @@ ElementDataGrid::ElementDataGrid(const Rocket::Core::String& tag) : Core::Elemen
 
 	// Create the row for the column headers:
 	header = dynamic_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridheader", attributes));
-	header->SetProperty("display", "block");
+	header->SetProperty("display", Core::Property(Core::Style::Display::Block));
 	header->Initialise(this);
 	AppendChild(header);
 	header->RemoveReference();
 
 	body = Core::Factory::InstanceElement(this, "*", "datagridbody", attributes);
-	body->SetProperty("display", "none");
-	body->SetProperty("width", "auto");
+	body->SetProperty("display", Core::Property(Core::Style::Display::None));
+	body->SetProperty("width", Core::Property(Core::Style::Width::Auto));
 	AppendChild(body);
 	body->RemoveReference();
 
 	body_visible = false;
 
 	root = dynamic_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridroot", attributes));
-	root->SetProperty("display", "none");
+	root->SetProperty("display", Core::Property(Core::Style::Display::None));
 	root->Initialise(this);
 
-	SetProperty("overflow", "auto");
+	SetProperty("overflow", Core::Property(Core::Style::Overflow::Auto));
 
 	new_data_source = "";
 }
@@ -108,7 +108,7 @@ void ElementDataGrid::AddColumn(const Rocket::Core::String& fields, const Rocket
 	// The header elements are added to the header row at the top of the table.
 	if (header_element)
 	{
-		header_element->SetProperty("display", "inline-block");
+		header_element->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
 
 		// Push all the width properties from the column onto the header element.
 		Rocket::Core::String width = header_element->GetAttribute<Rocket::Core::String>("width", "100%");
@@ -239,7 +239,7 @@ void ElementDataGrid::OnUpdate()
 	
 	if (!body_visible && (!any_new_children || root->GetNumLoadedChildren() >= Rocket::Core::Math::RealToInteger(ResolveProperty("min-rows", 0))))
 	{
-		body->SetProperty("display", "block");
+		body->SetProperty("display", Core::Property(Core::Style::Display::Block));
 		body_visible = true;
 	}
 	

+ 6 - 6
Source/Controls/ElementDataGridRow.cpp

@@ -53,8 +53,8 @@ ElementDataGridRow::ElementDataGridRow(const Rocket::Core::String& tag) : Core::
 	dirty_children = false;
 	row_expanded = true;
 
-	SetProperty("white-space", "nowrap");
-	SetProperty("display", Rocket::Core::Property(Rocket::Core::DISPLAY_INLINE_BLOCK, Rocket::Core::Property::KEYWORD));
+	SetProperty("white-space", Core::Property(Core::Style::WhiteSpace::Nowrap));
+	SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
 }
 
 ElementDataGridRow::~ElementDataGridRow()
@@ -85,7 +85,7 @@ void ElementDataGridRow::Initialise(ElementDataGrid* _parent_grid, ElementDataGr
 	{
 		ElementDataGridCell* cell = dynamic_cast< ElementDataGridCell* >(Core::Factory::InstanceElement(this, "#rktctl_datagridcell", "datagridcell", cell_attributes));
 		cell->Initialise(i, header_row->GetChild(i));
-		cell->SetProperty("display", Rocket::Core::Property(Rocket::Core::DISPLAY_INLINE_BLOCK, Rocket::Core::Property::KEYWORD));
+		cell->SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
 		AppendChild(cell);
 		cell->RemoveReference();
 	}
@@ -387,7 +387,7 @@ void ElementDataGridRow::AddChildren(int first_row_added, int num_rows_added)
 
 			if (!row_expanded)
 			{
-				new_row->SetProperty("display", "none");
+				new_row->SetProperty("display", Core::Property(Core::Style::Display::None));
 			}
 		}
 
@@ -671,7 +671,7 @@ void ElementDataGridRow::DirtyRow()
 // Sets this row's child rows to be visible.
 void ElementDataGridRow::Show()
 {
-	SetProperty("display", "inline-block");
+	SetProperty("display", Core::Property(Core::Style::Display::InlineBlock));
 
 	if (row_expanded)
 	{
@@ -685,7 +685,7 @@ void ElementDataGridRow::Show()
 // Sets this row's children to be invisible.
 void ElementDataGridRow::Hide()
 {
-	SetProperty("display", "none");
+	SetProperty("display", Core::Property(Core::Style::Display::None));
 
 	for (size_t i = 0; i < children.size(); i++)
 	{

+ 1 - 1
Source/Controls/ElementFormControl.cpp

@@ -33,7 +33,7 @@ namespace Controls {
 
 ElementFormControl::ElementFormControl(const Rocket::Core::String& tag) : Core::Element(tag)
 {
-	SetProperty("tab-index", Core::Property((int)Core::Style::TabIndex::Auto, Core::Property::KEYWORD));
+	SetProperty("tab-index", Core::Property(Core::Style::TabIndex::Auto));
 }
 
 ElementFormControl::~ElementFormControl()

+ 4 - 3
Source/Controls/WidgetSlider.cpp

@@ -90,7 +90,8 @@ WidgetSlider::~WidgetSlider()
 // Initialises the slider to a given orientation.
 bool WidgetSlider::Initialise()
 {
-	parent->SetProperty("drag", Core::Property((int)Core::Style::Drag::Drag, Core::Property::KEYWORD));
+	Core::Property drag_property = Core::Property((int)Core::Style::Drag::Drag, Core::Property::KEYWORD);
+	parent->SetProperty("drag", drag_property);
 
 	// Create all of our child elements as standard elements, and abort if we can't create them.
 	track = Core::Factory::InstanceElement(parent, "*", "slidertrack", Rocket::Core::XMLAttributes());
@@ -99,8 +100,8 @@ bool WidgetSlider::Initialise()
 
 	arrows[0] = Core::Factory::InstanceElement(parent, "*", "sliderarrowdec", Rocket::Core::XMLAttributes());
 	arrows[1] = Core::Factory::InstanceElement(parent, "*", "sliderarrowinc", Rocket::Core::XMLAttributes());
-	arrows[0]->SetProperty("drag", "drag");
-	arrows[1]->SetProperty("drag", "drag");
+	arrows[0]->SetProperty("drag", drag_property);
+	arrows[1]->SetProperty("drag", drag_property);
 
 	if (track == NULL ||
 		bar == NULL ||

+ 1 - 1
Source/Core/Element.cpp

@@ -1605,7 +1605,7 @@ bool Element::IsClippingEnabled()
 							|| computed.overflow_y != Style::Overflow::Visible;
 		
 		// Get the clipping ignore depth from the clip property
-		clipping_ignore_depth = static_cast<int>(computed.clip);
+		clipping_ignore_depth = computed.clip.number;
 
 		clipping_state_dirty = false;
 	}

+ 4 - 4
Source/Core/ElementDocument.cpp

@@ -55,7 +55,7 @@ ElementDocument::ElementDocument(const String& tag) : Element(tag)
 
 	ForceLocalStackingContext();
 
-	SetProperty(POSITION, "absolute");
+	SetProperty(POSITION, Property(Style::Position::Absolute));
 }
 
 ElementDocument::~ElementDocument()
@@ -150,7 +150,7 @@ void ElementDocument::ProcessHeader(const DocumentHeader* document_header)
 	}
 
 	// Hide this document.
-	SetProperty(VISIBILITY, "hidden");
+	SetProperty(VISIBILITY, Property(Style::Visibility::Hidden));
 }
 
 ElementDocument* ElementDocument::GetOwnerDocument()
@@ -225,7 +225,7 @@ void ElementDocument::Show(int focus_flags)
 	modal = (focus_flags & MODAL) > 0;
 
 	// Set to visible and switch focus if necessary
-	SetProperty(VISIBILITY, "visible");
+	SetProperty(VISIBILITY, Property(Style::Visibility::Visible));
 	if (focus_flags & FOCUS || focus_flags & MODAL)
 	{
 		// If no element could be focused, focus the window
@@ -240,7 +240,7 @@ void ElementDocument::Show(int focus_flags)
 
 void ElementDocument::Hide()
 {
-	SetProperty(VISIBILITY, "hidden");
+	SetProperty(VISIBILITY, Property(Style::Visibility::Hidden));
 	DispatchEvent("hide", Dictionary(), false);
 	
 	if (context)

+ 1 - 1
Source/Core/ElementHandle.cpp

@@ -38,7 +38,7 @@ namespace Core {
 ElementHandle::ElementHandle(const String& tag) : Element(tag), drag_start(0, 0)
 {
 	// Make sure we can be dragged!
-	SetProperty(DRAG, DRAG);
+	SetProperty(DRAG, Property(Style::Drag::Drag));
 
 	move_target = NULL;
 	size_target = NULL;

+ 5 - 5
Source/Core/ElementScroll.cpp

@@ -82,7 +82,7 @@ void ElementScroll::EnableScrollbar(Orientation orientation, float element_width
 	if (!scrollbars[orientation].enabled)
 	{
 		CreateScrollbar(orientation);
-		scrollbars[orientation].element->SetProperty(VISIBILITY, "visible");
+		scrollbars[orientation].element->SetProperty(VISIBILITY, Property(Style::Visibility::Visible));
 		scrollbars[orientation].enabled = true;
 	}
 
@@ -108,7 +108,7 @@ void ElementScroll::DisableScrollbar(Orientation orientation)
 {
 	if (scrollbars[orientation].enabled)
 	{
-		scrollbars[orientation].element->SetProperty(VISIBILITY, "hidden");
+		scrollbars[orientation].element->SetProperty(VISIBILITY, Property(Style::Visibility::Hidden));
 		scrollbars[orientation].enabled = false;
 	}
 }
@@ -218,12 +218,12 @@ void ElementScroll::FormatScrollbars()
 		corner->SetBox(corner_box);
 		corner->SetOffset(containing_block - Vector2f(scrollbars[VERTICAL].size, scrollbars[HORIZONTAL].size), element, true);
 
-		corner->SetProperty(VISIBILITY, "visible");
+		corner->SetProperty(VISIBILITY, Property(Style::Visibility::Visible));
 	}
 	else
 	{
 		if (corner != NULL)
-			corner->SetProperty(VISIBILITY, "hidden");
+			corner->SetProperty(VISIBILITY, Property(Style::Visibility::Hidden));
 	}
 }
 
@@ -250,7 +250,7 @@ bool ElementScroll::CreateScrollbar(Orientation orientation)
 
 	scrollbars[orientation].element = Factory::InstanceElement(element, "*", orientation == VERTICAL ? "scrollbarvertical" : "scrollbarhorizontal", XMLAttributes());
 	scrollbars[orientation].element->AddEventListener("scrollchange", this);
-	scrollbars[orientation].element->SetProperty(CLIP, "1");
+	scrollbars[orientation].element->SetProperty(CLIP, Property(1, Property::NUMBER));
 
 	scrollbars[orientation].widget = new WidgetSliderScroll(scrollbars[orientation].element);
 	scrollbars[orientation].widget->Initialise(orientation == VERTICAL ? WidgetSlider::VERTICAL : WidgetSlider::HORIZONTAL);

+ 3 - 3
Source/Core/ElementStyle.cpp

@@ -1048,11 +1048,11 @@ 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);
+		return Style::Clip(value == CLIP_NONE ? Style::Clip::None : Style::Clip::Auto);
 	else if (property->unit == Property::NUMBER)
-		return (Style::Clip)value;
+		return Style::Clip(Style::Clip::Number, value);
 	ROCKET_ERRORMSG("Invalid clip type");
-	return Style::Clip::Auto;
+	return Style::Clip();
 }
 
 static inline Style::LineHeight ComputeLineHeight(const Property* property, float font_size, float document_font_size, float dp_ratio, float pixels_per_inch)

+ 3 - 3
Source/Debugger/ElementInfo.cpp

@@ -135,15 +135,15 @@ void ElementInfo::ProcessEvent(Core::Event& event)
 				{
 					Core::Element* panel = target_element->GetNextSibling();
 					if (panel->IsVisible())
-						panel->SetProperty("display", "none");
+						panel->SetProperty("display", Core::Property(Core::Style::Display::None));
 					else
-						panel->SetProperty("display", "block");
+						panel->SetProperty("display", Core::Property(Core::Style::Display::Block));
 					event.StopPropagation();
 				}
 				else if (event.GetTargetElement()->GetId() == "close_button")
 				{
 					if (IsVisible())
-						SetProperty("visibility", "hidden");
+						SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 				}
 				// Check if the id is in the form "a %d" or "c %d" - these are the ancestor or child labels.
 				else

+ 5 - 5
Source/Debugger/ElementLog.cpp

@@ -104,7 +104,7 @@ bool ElementLog::Initialise()
 		return false;
 
 	beacon->SetId("rkt-debug-log-beacon");
-	beacon->SetProperty("visibility", "hidden");
+	beacon->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 	beacon->SetInnerRML(beacon_rml);
 
 	// Remove the initial reference on the beacon.
@@ -164,7 +164,7 @@ void ElementLog::AddLogMessage(Core::Log::Type type, const Core::String& message
 			{
 				if (type < current_beacon_level)
 				{
-					beacon->SetProperty("visibility", "visible");
+					beacon->SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
 
 					current_beacon_level = type;
 					Rocket::Core::Element* beacon_button = beacon->GetFirstChild();
@@ -235,15 +235,15 @@ void ElementLog::ProcessEvent(Core::Event& event)
 			if (event.GetTargetElement() == beacon->GetFirstChild())
 			{
 				if (!IsVisible())
-					SetProperty("visibility", "visible");
+					SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
 
-				beacon->SetProperty("visibility", "hidden");
+				beacon->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 				current_beacon_level = Core::Log::LT_MAX;
 			}
 			else if (event.GetTargetElement()->GetId() == "close_button")
 			{
 				if (IsVisible())
-					SetProperty("visibility", "hidden");
+					SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 			}
 			else
 			{

+ 9 - 9
Source/Debugger/Plugin.cpp

@@ -143,9 +143,9 @@ bool Plugin::SetContext(Core::Context* context)
 void Plugin::SetVisible(bool visibility)
 {
 	if (visibility)
-		menu_element->SetProperty("visibility", "visible");
+		menu_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
 	else
-		menu_element->SetProperty("visibility", "hidden");
+		menu_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 }
 
 // Returns the visibility of the debugger.
@@ -261,16 +261,16 @@ void Plugin::ProcessEvent(Core::Event& event)
 		if (event.GetTargetElement()->GetId() == "event-log-button")
 		{
 			if (log_element->IsVisible())
-				log_element->SetProperty("visibility", "hidden");
+				log_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 			else
-				log_element->SetProperty("visibility", "visible");
+				log_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
 		}
 		else if (event.GetTargetElement()->GetId() == "debug-info-button")
 		{
 			if (info_element->IsVisible())
-				info_element->SetProperty("visibility", "hidden");
+				info_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 			else
-				info_element->SetProperty("visibility", "visible");
+				info_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Visible));
 		}
 		else if (event.GetTargetElement()->GetId() == "outlines-button")
 		{
@@ -297,7 +297,7 @@ bool Plugin::LoadMenuElement()
 		return false;
 
 	menu_element->SetId("rkt-debug-menu");
-	menu_element->SetProperty("visibility", "hidden");
+	menu_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 	menu_element->SetInnerRML(menu_rml);
 
 	// Remove our reference on the document.
@@ -339,7 +339,7 @@ bool Plugin::LoadInfoElement()
 	if (info_element == NULL)
 		return false;
 
-	info_element->SetProperty("visibility", "hidden");
+	info_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 
 	if (!info_element->Initialise())
 	{
@@ -360,7 +360,7 @@ bool Plugin::LoadLogElement()
 	if (log_element == NULL)
 		return false;
 
-	log_element->SetProperty("visibility", "hidden");
+	log_element->SetProperty("visibility", Core::Property(Core::Style::Visibility::Hidden));
 
 	if (!log_element->Initialise())
 	{