瀏覽代碼

Extend Box API and clean up.

Michael Ragazzon 5 年之前
父節點
當前提交
3d688b6d66
共有 3 個文件被更改,包括 39 次插入10 次删除
  1. 17 4
      Include/RmlUi/Core/Box.h
  2. 20 4
      Source/Core/Box.cpp
  3. 2 2
      Source/Core/LayoutDetails.cpp

+ 17 - 4
Include/RmlUi/Core/Box.h

@@ -61,15 +61,21 @@ public:
 		NUM_EDGES = 4
 	};
 
+	enum Direction
+	{
+		VERTICAL = 0,
+		HORIZONTAL = 1
+	};
+
 	/// Initialises a zero-sized box.
 	Box();
 	/// Initialises a box with a default content area and no padding, borders and margins.
-	Box(const Vector2f& content);
+	Box(Vector2f content);
 	~Box();
 
 	/// Returns the offset of this box. This will usually be (0, 0).
 	/// @return The box's offset.
-	const Vector2f& GetOffset() const;
+	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
 	/// means the position of the margin area is likely to be negative.
 	/// @param area[in] The desired area.
@@ -83,10 +89,10 @@ public:
 	/// 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(const Vector2f& offset);
+	void SetOffset(Vector2f offset);
 	/// Sets the size of the content area.
 	/// @param content[in] The size of the new content area.
-	void SetContent(const Vector2f& content);
+	void SetContent(Vector2f content);
 	/// Sets the size of one of the edges of one of the box's outer areas.
 	/// @param area[in] The area to change.
 	/// @param edge[in] The area edge to change.
@@ -104,6 +110,13 @@ public:
 	/// @return The cumulative size of the edge.
 	float GetCumulativeEdge(Area area, Edge edge) const;
 
+	/// Returns the size along a single direction of the given 'area', including all inner areas up-to and including 'area_end'.
+	/// @example GetSizeAcross(HORIZONTAL, BORDER, PADDING) returns the total width of the horizontal borders and paddings.
+	/// @param direction The desired direction.
+	/// @param area The widest area to include.
+	/// @param area_end The last area to include, anything inside this is excluded.
+	float GetSizeAcross(Direction direction, Area area, Area area_end = Area::CONTENT) const;
+
 	/// Compares the size of the content area and the other area edges.
 	/// @return True if the boxes represent the same area.
 	bool operator==(const Box& rhs) const;

+ 20 - 4
Source/Core/Box.cpp

@@ -38,7 +38,7 @@ Box::Box() : content(0, 0), offset(0, 0)
 }
 
 // Initialises a box with a default content area and no padding, borders and margins.
-Box::Box(const Vector2f& content) : content(content), offset(0, 0)
+Box::Box(Vector2f content) : content(content), offset(0, 0)
 {
 	memset(area_edges, 0, sizeof(area_edges));
 }
@@ -48,7 +48,7 @@ Box::~Box()
 }
 
 // Returns the offset of this box. This will usually be (0, 0).
-const Vector2f& Box::GetOffset() const
+Vector2f Box::GetOffset() const
 {
 	return offset;
 }
@@ -80,13 +80,13 @@ Vector2f Box::GetSize(Area area) const
 }
 
 // Sets the offset of the box, relative usually to the owning element.
-void Box::SetOffset(const Vector2f& _offset)
+void Box::SetOffset(Vector2f _offset)
 {
 	offset = _offset;
 }
 
 // Sets the size of the content area.
-void Box::SetContent(const Vector2f& _content)
+void Box::SetContent(Vector2f _content)
 {
 	content = _content;
 }
@@ -114,6 +114,22 @@ float Box::GetCumulativeEdge(Area area, Edge edge) const
 	return size;
 }
 
+float Box::GetSizeAcross(Direction direction, Area area, Area area_end) const
+{
+	static_assert(HORIZONTAL == 1 && VERTICAL == 0, "");
+	RMLUI_ASSERT(area <= area_end && direction <= 1);
+
+	float size = 0.0f;
+
+	if (area_end == CONTENT)
+		size = (direction == HORIZONTAL ? content.x : content.y);
+	
+	for (int i = area; i <= area_end && i < CONTENT; i++)
+		size += (area_edges[i][TOP + direction] + area_edges[i][BOTTOM + direction]);
+
+	return size;
+}
+
 // Compares the size of the content area and the other area edges.
 bool Box::operator==(const Box& rhs) const
 {

+ 2 - 2
Source/Core/LayoutDetails.cpp

@@ -38,12 +38,12 @@ namespace Rml {
 
 static inline float BorderWidthToContentWidth(float border_width, const Box& box)
 {
-	const float border_padding_edges_width = box.GetEdge(Box::BORDER, Box::LEFT) + box.GetEdge(Box::BORDER, Box::RIGHT) + box.GetEdge(Box::PADDING, Box::LEFT) + box.GetEdge(Box::PADDING, Box::RIGHT);
+	const float border_padding_edges_width = box.GetSizeAcross(Box::HORIZONTAL, Box::BORDER, Box::PADDING);
 	return Math::Max(0.0f, border_width - border_padding_edges_width);
 }
 static inline float BorderHeightToContentHeight(float border_height, const Box& box)
 {
-	const float border_padding_edges_height = box.GetEdge(Box::BORDER, Box::TOP) + box.GetEdge(Box::BORDER, Box::BOTTOM) + box.GetEdge(Box::PADDING, Box::TOP) + box.GetEdge(Box::PADDING, Box::BOTTOM);
+	const float border_padding_edges_height = box.GetSizeAcross(Box::VERTICAL, Box::BORDER, Box::PADDING);
 	return Math::Max(0.0f, border_height - border_padding_edges_height);
 }