Преглед на файлове

Remove 'offset' from Box.

Michael Ragazzon преди 5 години
родител
ревизия
a638aaf33e

+ 0 - 9
Include/RmlUi/Core/Box.h

@@ -73,9 +73,6 @@ public:
 	Box(Vector2f content);
 	Box(Vector2f content);
 	~Box();
 	~Box();
 
 
-	/// Returns the offset of this box. This will usually be (0, 0).
-	/// @return The box's offset.
-	Vector2f GetOffset() const;
 	/// Returns the top-left position of one of the box's areas, relative to the top-left of the border area. This
 	/// Returns the top-left position of one of the box's areas, relative to the top-left of the border area. This
 	/// means the position of the margin area is likely to be negative.
 	/// means the position of the margin area is likely to be negative.
 	/// @param area[in] The desired area.
 	/// @param area[in] The desired area.
@@ -86,10 +83,6 @@ public:
 	/// @return The size of the requested area.
 	/// @return The size of the requested area.
 	Vector2f GetSize(Area area = Box::CONTENT) const;
 	Vector2f GetSize(Area area = Box::CONTENT) const;
 
 
-	/// Sets the offset of the box, relative usually to the owning element. This should only be set for auxiliary
-	/// boxes of an element.
-	/// @param offset[in] The offset of the box from the primary box.
-	void SetOffset(Vector2f offset);
 	/// Sets the size of the content area.
 	/// Sets the size of the content area.
 	/// @param content[in] The size of the new content area.
 	/// @param content[in] The size of the new content area.
 	void SetContent(Vector2f content);
 	void SetContent(Vector2f content);
@@ -127,8 +120,6 @@ public:
 private:
 private:
 	Vector2f content;
 	Vector2f content;
 	float area_edges[NUM_AREAS][NUM_EDGES];
 	float area_edges[NUM_AREAS][NUM_EDGES];
-
-	Vector2f offset;
 };
 };
 
 
 } // namespace Rml
 } // namespace Rml

+ 10 - 4
Include/RmlUi/Core/Element.h

@@ -147,14 +147,16 @@ public:
 	void SetBox(const Box& box);
 	void SetBox(const Box& box);
 	/// Adds a box to the end of the list describing this element's geometry.
 	/// Adds a box to the end of the list describing this element's geometry.
 	/// @param[in] box The auxiliary box for the element.
 	/// @param[in] box The auxiliary box for the element.
-	void AddBox(const Box& box);
+	/// @param[in] offset The offset of the box relative to the top left border corner of the element.
+	void AddBox(const Box& box, Vector2f offset);
 	/// Returns the main box describing the size of the element.
 	/// Returns the main box describing the size of the element.
 	/// @return The box.
 	/// @return The box.
 	const Box& GetBox();
 	const Box& GetBox();
 	/// Returns one of the boxes describing the size of the element.
 	/// Returns one of the boxes describing the size of the element.
 	/// @param[in] index The index of the desired box, with 0 being the main box. If outside of bounds, the main box will be returned.
 	/// @param[in] index The index of the desired box, with 0 being the main box. If outside of bounds, the main box will be returned.
+	/// @param[out] offset The offset of the box relative to the element's border box.
 	/// @return The requested box.
 	/// @return The requested box.
-	const Box& GetBox(int index);
+	const Box& GetBox(int index, Vector2f& offset);
 	/// Returns the number of boxes making up this element's geometry.
 	/// Returns the number of boxes making up this element's geometry.
 	/// @return the number of boxes making up this element's geometry.
 	/// @return the number of boxes making up this element's geometry.
 	int GetNumBoxes();
 	int GetNumBoxes();
@@ -702,9 +704,13 @@ private:
 	Vector2f scroll_offset;
 	Vector2f scroll_offset;
 
 
 	// The size of the element.
 	// The size of the element.
-	using BoxList = Vector< Box >;
+	struct PositionedBox {
+		Box box;
+		Vector2f offset;
+	};
+	using PositionedBoxList = Vector< PositionedBox >;
 	Box main_box;
 	Box main_box;
-	BoxList additional_boxes;
+	PositionedBoxList additional_boxes;
 
 
 	// And of the element's internal content.
 	// And of the element's internal content.
 	Vector2f content_offset;
 	Vector2f content_offset;

+ 2 - 1
Include/RmlUi/Core/GeometryUtilities.h

@@ -80,10 +80,11 @@ public:
 	/// Vertex positions are relative to the border-box, vertex texture coordinates are default initialized.
 	/// Vertex positions are relative to the border-box, vertex texture coordinates are default initialized.
 	/// @param[out] geometry The geometry to append the newly created vertices and indices into.
 	/// @param[out] geometry The geometry to append the newly created vertices and indices into.
 	/// @param[in] box The box which determines the background and border geometry.
 	/// @param[in] box The box which determines the background and border geometry.
+	/// @param[in] offset Offset the position of the generated vertices.
 	/// @param[in] border_radius The border radius in pixel units in the following order: top-left, top-right, bottom-right, bottom-left.
 	/// @param[in] border_radius The border radius in pixel units in the following order: top-left, top-right, bottom-right, bottom-left.
 	/// @param[in] background_colour The colour applied to the background, set alpha to zero to not generate the background.
 	/// @param[in] background_colour The colour applied to the background, set alpha to zero to not generate the background.
 	/// @param[in] border_colours Pointer to a four-element array of border colors in top-right-bottom-left order, or nullptr to not generate borders.
 	/// @param[in] border_colours Pointer to a four-element array of border colors in top-right-bottom-left order, or nullptr to not generate borders.
-	static void GenerateBackgroundBorder(Geometry* geometry, const Box& box, Vector4f border_radius, Colourb background_colour, const Colourb* border_colours = nullptr);
+	static void GenerateBackgroundBorder(Geometry* geometry, const Box& box, Vector2f offset, Vector4f border_radius, Colourb background_colour, const Colourb* border_colours = nullptr);
 
 
 private:
 private:
 	GeometryUtilities();
 	GeometryUtilities();

+ 3 - 15
Source/Core/Box.cpp

@@ -32,13 +32,13 @@
 namespace Rml {
 namespace Rml {
 
 
 // Initialises a zero-sized box.
 // Initialises a zero-sized box.
-Box::Box() : content(0, 0), offset(0, 0)
+Box::Box() : content(0, 0)
 {
 {
 	memset(area_edges, 0, sizeof(area_edges));
 	memset(area_edges, 0, sizeof(area_edges));
 }
 }
 
 
 // Initialises a box with a default content area and no padding, borders and margins.
 // Initialises a box with a default content area and no padding, borders and margins.
-Box::Box(Vector2f content) : content(content), offset(0, 0)
+Box::Box(Vector2f content) : content(content)
 {
 {
 	memset(area_edges, 0, sizeof(area_edges));
 	memset(area_edges, 0, sizeof(area_edges));
 }
 }
@@ -47,16 +47,10 @@ Box::~Box()
 {
 {
 }
 }
 
 
-// Returns the offset of this box. This will usually be (0, 0).
-Vector2f Box::GetOffset() const
-{
-	return offset;
-}
-
 // Returns the top-left position of one of the areas.
 // Returns the top-left position of one of the areas.
 Vector2f Box::GetPosition(Area area) const
 Vector2f Box::GetPosition(Area area) const
 {
 {
-	Vector2f area_position(offset.x - area_edges[MARGIN][LEFT], offset.y - area_edges[MARGIN][TOP]);
+	Vector2f area_position(-area_edges[MARGIN][LEFT], -area_edges[MARGIN][TOP]);
 	for (int i = 0; i < area; i++)
 	for (int i = 0; i < area; i++)
 	{
 	{
 		area_position.x += area_edges[i][LEFT];
 		area_position.x += area_edges[i][LEFT];
@@ -79,12 +73,6 @@ Vector2f Box::GetSize(Area area) const
 	return area_size;
 	return area_size;
 }
 }
 
 
-// Sets the offset of the box, relative usually to the owning element.
-void Box::SetOffset(Vector2f _offset)
-{
-	offset = _offset;
-}
-
 // Sets the size of the content area.
 // Sets the size of the content area.
 void Box::SetContent(Vector2f _content)
 void Box::SetContent(Vector2f _content)
 {
 {

+ 1 - 1
Source/Core/DecoratorGradient.cpp

@@ -78,7 +78,7 @@ DecoratorDataHandle DecoratorGradient::GenerateElementData(Element* element) con
 		computed.border_bottom_right_radius,
 		computed.border_bottom_right_radius,
 		computed.border_bottom_left_radius,
 		computed.border_bottom_left_radius,
 	};
 	};
-	GeometryUtilities::GenerateBackgroundBorder(geometry, element->GetBox(), border_radius, Colourb());
+	GeometryUtilities::GenerateBackgroundBorder(geometry, element->GetBox(), Vector2f(0), border_radius, Colourb());
 
 
 	// Apply opacity
 	// Apply opacity
 	Colourb colour_start = start;
 	Colourb colour_start = start;

+ 14 - 12
Source/Core/Element.cpp

@@ -504,9 +504,9 @@ void Element::SetBox(const Box& box)
 }
 }
 
 
 // Adds a box to the end of the list describing this element's geometry.
 // Adds a box to the end of the list describing this element's geometry.
-void Element::AddBox(const Box& box)
+void Element::AddBox(const Box& box, Vector2f offset)
 {
 {
-	additional_boxes.push_back(box);
+	additional_boxes.emplace_back(PositionedBox{ box, offset });
 
 
 	OnResize();
 	OnResize();
 
 
@@ -522,16 +522,20 @@ const Box& Element::GetBox()
 }
 }
 
 
 // Returns one of the boxes describing the size of the element.
 // Returns one of the boxes describing the size of the element.
-const Box& Element::GetBox(int index)
+const Box& Element::GetBox(int index, Vector2f& offset)
 {
 {
+	offset = Vector2f(0);
+
 	if (index < 1)
 	if (index < 1)
 		return main_box;
 		return main_box;
 	
 	
-	int additional_box_index = index - 1;
+	const int additional_box_index = index - 1;
 	if (additional_box_index >= (int)additional_boxes.size())
 	if (additional_box_index >= (int)additional_boxes.size())
 		return main_box;
 		return main_box;
 
 
-	return additional_boxes[additional_box_index];
+	offset = additional_boxes[additional_box_index].offset;
+
+	return additional_boxes[additional_box_index].box;
 }
 }
 
 
 // Returns the number of boxes making up this element's geometry.
 // Returns the number of boxes making up this element's geometry.
@@ -561,10 +565,11 @@ bool Element::IsPointWithinElement(const Vector2f& point)
 
 
 	for (int i = 0; i < GetNumBoxes(); ++i)
 	for (int i = 0; i < GetNumBoxes(); ++i)
 	{
 	{
-		const Box& box = GetBox(i);
+		Vector2f box_offset;
+		const Box& box = GetBox(i, box_offset);
 
 
-		Vector2f box_position = position + box.GetOffset();
-		Vector2f box_dimensions = box.GetSize(Box::BORDER);
+		const Vector2f box_position = position + box_offset;
+		const Vector2f box_dimensions = box.GetSize(Box::BORDER);
 		if (point.x >= box_position.x &&
 		if (point.x >= box_position.x &&
 			point.x <= (box_position.x + box_dimensions.x) &&
 			point.x <= (box_position.x + box_dimensions.x) &&
 			point.y >= box_position.y &&
 			point.y >= box_position.y &&
@@ -1246,10 +1251,7 @@ void Element::ScrollIntoView(bool align_with_top)
 {
 {
 	Vector2f size(0, 0);
 	Vector2f size(0, 0);
 	if (!align_with_top)
 	if (!align_with_top)
-	{
-		size.y = main_box.GetOffset().y +
-			main_box.GetSize(Box::BORDER).y;
-	}
+		size.y = main_box.GetSize(Box::BORDER).y;
 
 
 	Element* scroll_parent = parent;
 	Element* scroll_parent = parent;
 	while (scroll_parent != nullptr)
 	while (scroll_parent != nullptr)

+ 3 - 1
Source/Core/ElementBackgroundBorder.cpp

@@ -96,7 +96,9 @@ void ElementBackgroundBorder::GenerateGeometry(Element* element)
 
 
 	for (int i = 0; i < element->GetNumBoxes(); i++)
 	for (int i = 0; i < element->GetNumBoxes(); i++)
 	{
 	{
-		GeometryUtilities::GenerateBackgroundBorder(&geometry, element->GetBox(i), radii, background_color, border_colors);
+		Vector2f offset;
+		const Box& box = element->GetBox(i, offset);
+		GeometryUtilities::GenerateBackgroundBorder(&geometry, box, offset, radii, background_color, border_colors);
 	}
 	}
 
 
 	geometry.Release();
 	geometry.Release();

+ 2 - 2
Source/Core/GeometryBackgroundBorder.cpp

@@ -37,7 +37,7 @@ namespace Rml {
 GeometryBackgroundBorder::GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices) : vertices(vertices), indices(indices)
 GeometryBackgroundBorder::GeometryBackgroundBorder(Vector<Vertex>& vertices, Vector<int>& indices) : vertices(vertices), indices(indices)
 {}
 {}
 
 
-void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, const Colourb background_color, const Colourb* border_colors)
+void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, const Vector2f offset, const Colourb background_color, const Colourb* border_colors)
 {
 {
 	using Edge = Box::Edge;
 	using Edge = Box::Edge;
 
 
@@ -67,7 +67,7 @@ void GeometryBackgroundBorder::Draw(Vector<Vertex>& vertices, Vector<int>& indic
 
 
 	// -- Find the corner positions --
 	// -- Find the corner positions --
 
 
-	const Vector2f border_position = box.GetPosition(Box::BORDER).Round();
+	const Vector2f border_position = (offset + box.GetPosition(Box::BORDER)).Round();
 	const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
 	const Vector2f padding_position = border_position + Vector2f(border_widths[Edge::LEFT], border_widths[Edge::TOP]);
 	const Vector2f border_size = padding_size + Vector2f(border_widths[Edge::LEFT] + border_widths[Edge::RIGHT], border_widths[Edge::TOP] + border_widths[Edge::BOTTOM]);
 	const Vector2f border_size = padding_size + Vector2f(border_widths[Edge::LEFT] + border_widths[Edge::RIGHT], border_widths[Edge::TOP] + border_widths[Edge::BOTTOM]);
 
 

+ 2 - 1
Source/Core/GeometryBackgroundBorder.h

@@ -53,9 +53,10 @@ public:
 	/// @param[out] indices Destination vector for generated indices.
 	/// @param[out] indices Destination vector for generated indices.
 	/// @param[in] radii The radius of each corner.
 	/// @param[in] radii The radius of each corner.
 	/// @param[in] box The box used for positioning and sizing of the background and borders.
 	/// @param[in] box The box used for positioning and sizing of the background and borders.
+	/// @param[in] offset Offset the position of the generated vertices.
 	/// @param[in] background_color Color of the background, set alpha to zero to not generate a background.
 	/// @param[in] background_color Color of the background, set alpha to zero to not generate a background.
 	/// @param[in] border_colors Pointer to a four-element array of border colors in top-right-bottom-left order, or nullptr to not generate borders.
 	/// @param[in] border_colors Pointer to a four-element array of border colors in top-right-bottom-left order, or nullptr to not generate borders.
-	static void Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, Colourb background_color, const Colourb* border_colors);
+	static void Draw(Vector<Vertex>& vertices, Vector<int>& indices, CornerSizes radii, const Box& box, Vector2f offset, Colourb background_color, const Colourb* border_colors);
 
 
 private:
 private:
 	enum Corner { TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT };
 	enum Corner { TOP_LEFT, TOP_RIGHT, BOTTOM_RIGHT, BOTTOM_LEFT };

+ 2 - 2
Source/Core/GeometryUtilities.cpp

@@ -108,13 +108,13 @@ void GeometryUtilities::GenerateLine(FontFaceHandle font_face_handle, Geometry*
 									);
 									);
 }
 }
 
 
-void GeometryUtilities::GenerateBackgroundBorder(Geometry* geometry, const Box& box, Vector4f border_radius, Colourb background_colour, const Colourb* border_colours)
+void GeometryUtilities::GenerateBackgroundBorder(Geometry* geometry, const Box& box, Vector2f offset, Vector4f border_radius, Colourb background_colour, const Colourb* border_colours)
 {
 {
 	Vector<Vertex>& vertices = geometry->GetVertices();
 	Vector<Vertex>& vertices = geometry->GetVertices();
 	Vector<int>& indices = geometry->GetIndices();
 	Vector<int>& indices = geometry->GetIndices();
 
 
 	CornerSizes corner_sizes{ border_radius.x, border_radius.y, border_radius.z, border_radius.w };
 	CornerSizes corner_sizes{ border_radius.x, border_radius.y, border_radius.z, border_radius.w };
-	GeometryBackgroundBorder::Draw(vertices, indices, corner_sizes, box, background_colour, border_colours);
+	GeometryBackgroundBorder::Draw(vertices, indices, corner_sizes, box, offset, background_colour, border_colours);
 }
 }
 
 
 } // namespace Rml
 } // namespace Rml

+ 2 - 2
Source/Core/LayoutInlineBox.cpp

@@ -345,8 +345,8 @@ void LayoutInlineBox::SizeElement(bool split)
 	// The elements of a chained box have already had their positions set by the first link.
 	// The elements of a chained box have already had their positions set by the first link.
 	if (chained)
 	if (chained)
 	{
 	{
-		element_box.SetOffset((line->GetPosition() + position) - element->GetRelativeOffset(Box::BORDER));
-		element->AddBox(element_box);
+		const Vector2f box_offset = (line->GetPosition() + position) - element->GetRelativeOffset(Box::BORDER);
+		element->AddBox(element_box, box_offset);
 
 
 		if (chain != nullptr)
 		if (chain != nullptr)
 			element->OnLayout();
 			element->OnLayout();

+ 3 - 2
Source/Debugger/DebuggerPlugin.cpp

@@ -185,9 +185,10 @@ void DebuggerPlugin::Render()
 					ElementUtilities::ApplyTransform(*element);
 					ElementUtilities::ApplyTransform(*element);
 					for (int j = 0; j < element->GetNumBoxes(); ++j)
 					for (int j = 0; j < element->GetNumBoxes(); ++j)
 					{
 					{
-						const Box& box = element->GetBox(j);
+						Vector2f box_offset;
+						const Box& box = element->GetBox(j, box_offset);
 						Geometry::RenderOutline(
 						Geometry::RenderOutline(
-							element->GetAbsoluteOffset(Box::BORDER) + box.GetPosition(Box::BORDER), 
+							element->GetAbsoluteOffset(Box::BORDER) + box_offset + box.GetPosition(Box::BORDER),
 							box.GetSize(Box::BORDER), 
 							box.GetSize(Box::BORDER), 
 							Colourb(255, 0, 0, 128), 
 							Colourb(255, 0, 0, 128), 
 							1
 							1

+ 10 - 7
Source/Debugger/ElementInfo.cpp

@@ -138,11 +138,12 @@ void ElementInfo::RenderHoverElement()
 		for (int i = 0; i < hover_element->GetNumBoxes(); i++)
 		for (int i = 0; i < hover_element->GetNumBoxes(); i++)
 		{
 		{
 			// Render the content area.
 			// Render the content area.
-			const Box element_box = hover_element->GetBox(i);
+			Vector2f box_offset;
+			const Box& element_box = hover_element->GetBox(i, box_offset);
 			Vector2f size = element_box.GetSize(Box::BORDER);
 			Vector2f size = element_box.GetSize(Box::BORDER);
 			size = Vector2f(std::max(size.x, 2.0f), std::max(size.y, 2.0f));
 			size = Vector2f(std::max(size.x, 2.0f), std::max(size.y, 2.0f));
 			Geometry::RenderOutline(
 			Geometry::RenderOutline(
-				hover_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::BORDER), 
+				hover_element->GetAbsoluteOffset(Box::BORDER) + box_offset + element_box.GetPosition(Box::BORDER),
 				size,
 				size,
 				Colourb(255, 0, 0, 255), 
 				Colourb(255, 0, 0, 255), 
 				1
 				1
@@ -159,19 +160,21 @@ void ElementInfo::RenderSourceElement()
 
 
 		for (int i = 0; i < source_element->GetNumBoxes(); i++)
 		for (int i = 0; i < source_element->GetNumBoxes(); i++)
 		{
 		{
-			const Box element_box = source_element->GetBox(i);
+			Vector2f box_offset;
+			const Box element_box = source_element->GetBox(i, box_offset);
+			const Vector2f border_offset = box_offset + source_element->GetAbsoluteOffset(Box::BORDER);
 
 
 			// Content area:
 			// Content area:
-			Geometry::RenderBox(source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::CONTENT), element_box.GetSize(), Colourb(158, 214, 237, 128));
+			Geometry::RenderBox(border_offset + element_box.GetPosition(Box::CONTENT), element_box.GetSize(), Colourb(158, 214, 237, 128));
 
 
 			// Padding area:
 			// Padding area:
-			Geometry::RenderBox(source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::PADDING), element_box.GetSize(Box::PADDING), source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::CONTENT), element_box.GetSize(), Colourb(135, 122, 214, 128));
+			Geometry::RenderBox(border_offset + element_box.GetPosition(Box::PADDING), element_box.GetSize(Box::PADDING), border_offset + element_box.GetPosition(Box::CONTENT), element_box.GetSize(), Colourb(135, 122, 214, 128));
 
 
 			// Border area:
 			// Border area:
-			Geometry::RenderBox(source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::BORDER), element_box.GetSize(Box::BORDER), source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::PADDING), element_box.GetSize(Box::PADDING), Colourb(133, 133, 133, 128));
+			Geometry::RenderBox(border_offset + element_box.GetPosition(Box::BORDER), element_box.GetSize(Box::BORDER), border_offset + element_box.GetPosition(Box::PADDING), element_box.GetSize(Box::PADDING), Colourb(133, 133, 133, 128));
 
 
 			// Border area:
 			// Border area:
-			Geometry::RenderBox(source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::MARGIN), element_box.GetSize(Box::MARGIN), source_element->GetAbsoluteOffset(Box::BORDER) + element_box.GetPosition(Box::BORDER), element_box.GetSize(Box::BORDER), Colourb(240, 255, 131, 128));
+			Geometry::RenderBox(border_offset + element_box.GetPosition(Box::MARGIN), element_box.GetSize(Box::MARGIN), border_offset + element_box.GetPosition(Box::BORDER), element_box.GetSize(Box::BORDER), Colourb(240, 255, 131, 128));
 		}
 		}
 	}
 	}
 }
 }