Pārlūkot izejas kodu

Make Rectangle member functions return new objects instead of mutating in-place

Michael Ragazzon 1 gadu atpakaļ
vecāks
revīzija
2cd78d54e9

+ 1 - 3
Backends/RmlUi_Renderer_GL3.cpp

@@ -1437,9 +1437,7 @@ void RenderInterface_GL3::RenderBlur(float sigma, const Gfx::FramebufferData& so
 	// blitting with linear filtering, pixels outside the 'src' region can be blended into the output. On the other
 	// hand, it looks like Nvidia clamps the pixels to the source edge, which is what we really want. Regardless, we
 	// work around the issue with this extra step.
-	Rml::Rectanglei padded_scissor = scissor;
-	padded_scissor.Extend(Rml::Vector2i(1));
-	SetScissor(padded_scissor, true);
+	SetScissor(scissor.Extend(1), true);
 	glClear(GL_COLOR_BUFFER_BIT);
 	SetScissor(scissor, true);
 

+ 14 - 31
Include/RmlUi/Core/Rectangle.h

@@ -67,44 +67,27 @@ public:
 	Type Width() const { return p1.x - p0.x; }
 	Type Height() const { return p1.y - p0.y; }
 
-	void Extend(Type v) { Extend(Vector2Type(v)); }
-	void Extend(Vector2Type v)
-	{
-		p0 -= v;
-		p1 += v;
-	}
-	void ExtendTopLeft(Vector2Type v) { p0 -= v; }
-	void ExtendBottomRight(Vector2Type v) { p1 += v; }
+	Rectangle Extend(Type v) const { return Extend(Vector2Type(v)); }
+	Rectangle Extend(Vector2Type v) const { return Rectangle{p0 - v, p1 + v}; }
+	Rectangle Extend(Vector2Type top_left, Vector2Type bottom_right) const { return Rectangle{p0 - top_left, p1 + bottom_right}; }
 
-	void Translate(Vector2Type v)
-	{
-		p0 += v;
-		p1 += v;
-	}
+	Rectangle Translate(Vector2Type v) const { return Rectangle{p0 + v, p1 + v}; }
 
-	void Join(Vector2Type p)
-	{
-		p0 = Math::Min(p0, p);
-		p1 = Math::Max(p1, p);
-	}
-	void Join(Rectangle other)
-	{
-		p0 = Math::Min(p0, other.p0);
-		p1 = Math::Max(p1, other.p1);
-	}
+	Rectangle Join(Vector2Type p) const { return Rectangle{Math::Min(p0, p), Math::Max(p1, p)}; }
+	Rectangle Join(Rectangle other) const { return Rectangle{Math::Min(p0, other.p0), Math::Max(p1, other.p1)}; }
 
-	void Intersect(Rectangle other)
+	Rectangle Intersect(Rectangle other) const
 	{
 		RMLUI_ASSERT(Valid() && other.Valid());
-		p0 = Math::Max(p0, other.p0);
-		p1 = Math::Max(Math::Min(p1, other.p1), p0);
+		Rectangle result{Math::Max(p0, other.p0), Math::Min(p1, other.p1)};
+		result.p1 = Math::Max(result.p0, result.p1);
+		return result;
 	}
-	void IntersectIfValid(Rectangle other)
+	Rectangle IntersectIfValid(Rectangle other) const
 	{
-		if (!Valid())
-			*this = other;
-		else if (other.Valid())
-			Intersect(other);
+		if (!Valid() || !other.Valid())
+			return *this;
+		return Intersect(other);
 	}
 
 	bool Intersects(Rectangle other) const { return p0.x < other.p1.x && p1.x > other.p0.x && p0.y < other.p1.y && p1.y > other.p0.y; }

+ 1 - 2
Source/Core/ElementEffects.cpp

@@ -254,8 +254,7 @@ void ElementEffects::RenderEffects(RenderStage render_stage)
 
 		Math::ExpandToPixelGrid(filter_region);
 
-		Rectanglei scissor_region = Rectanglei(filter_region);
-		scissor_region.IntersectIfValid(render_manager->GetScissorRegion());
+		Rectanglei scissor_region = Rectanglei(filter_region).IntersectIfValid(render_manager->GetScissorRegion());
 		render_manager->SetScissorRegion(scissor_region);
 	};
 	auto ApplyScissorRegionForBackdrop = [this, &render_manager]() {

+ 4 - 4
Source/Core/ElementUtilities.cpp

@@ -233,8 +233,9 @@ bool ElementUtilities::GetClippingRegion(Element* element, Rectanglei& out_clip_
 				// Shrink the scissor region to the element's client area.
 				Vector2f element_offset = clipping_element->GetAbsoluteOffset(clip_area);
 				Vector2f element_size = clipping_element->GetBox().GetSize(clip_area);
+				Rectanglef element_region = Rectanglef::FromPositionSize(element_offset, element_size);
 
-				clip_region.IntersectIfValid(Rectanglef::FromPositionSize(element_offset, element_size));
+				clip_region = element_region.IntersectIfValid(clip_region);
 			}
 		}
 
@@ -316,8 +317,7 @@ bool ElementUtilities::GetBoundingBox(Rectanglef& out_rectangle, Element* elemen
 
 	// Element bounds in non-transformed space.
 	Rectanglef bounds = Rectanglef::FromPositionSize(element->GetAbsoluteOffset(box_area), element->GetBox().GetSize(box_area));
-	bounds.ExtendTopLeft(shadow_extent_top_left);
-	bounds.ExtendBottomRight(shadow_extent_bottom_right);
+	bounds = bounds.Extend(shadow_extent_top_left, shadow_extent_bottom_right);
 
 	const TransformState* transform_state = element->GetTransformState();
 	const Matrix4f* transform = (transform_state ? transform_state->GetTransform() : nullptr);
@@ -366,7 +366,7 @@ bool ElementUtilities::GetBoundingBox(Rectanglef& out_rectangle, Element* elemen
 	// Find the rectangle covering the projected corners.
 	out_rectangle = Rectanglef::FromPosition(corners[0]);
 	for (int i = 1; i < num_corners; i++)
-		out_rectangle.Join(corners[i]);
+		out_rectangle = out_rectangle.Join(corners[i]);
 
 	return true;
 }

+ 1 - 1
Source/Core/FilterBlur.cpp

@@ -51,7 +51,7 @@ void FilterBlur::ExtendInkOverflow(Element* element, Rectanglef& scissor_region)
 {
 	const float sigma = element->ResolveLength(sigma_value);
 	const float blur_extent = 3.0f * Math::Max(sigma, 1.f);
-	scissor_region.Extend(blur_extent);
+	scissor_region = scissor_region.Extend(blur_extent);
 }
 
 FilterBlurInstancer::FilterBlurInstancer()

+ 2 - 2
Source/Core/FilterDropShadow.cpp

@@ -68,8 +68,8 @@ void FilterDropShadow::ExtendInkOverflow(Element* element, Rectanglef& scissor_r
 	};
 
 	const float blur_extent = 3.f * sigma;
-	scissor_region.ExtendTopLeft(Math::Max(-offset, Vector2f(0.f)) + Vector2f(blur_extent));
-	scissor_region.ExtendBottomRight(Math::Max(offset, Vector2f(0.f)) + Vector2f(blur_extent));
+	scissor_region =
+		scissor_region.Extend(Math::Max(-offset, Vector2f(0.f)) + Vector2f(blur_extent), Math::Max(offset, Vector2f(0.f)) + Vector2f(blur_extent));
 }
 
 FilterDropShadowInstancer::FilterDropShadowInstancer()

+ 2 - 3
Source/Core/GeometryBoxShadow.cpp

@@ -77,11 +77,10 @@ void GeometryBoxShadow::Generate(Geometry& out_shadow_geometry, CallbackTexture&
 		{
 			Vector2f offset;
 			const Box& box = element->GetBox(i, offset);
-			texture_region.Join(Rectanglef::FromPositionSize(offset, box.GetSize(BoxArea::Border)));
+			texture_region = texture_region.Join(Rectanglef::FromPositionSize(offset, box.GetSize(BoxArea::Border)));
 		}
 
-		texture_region.ExtendTopLeft(-extend_min);
-		texture_region.ExtendBottomRight(extend_max);
+		texture_region = texture_region.Extend(-extend_min, extend_max);
 		Math::ExpandToPixelGrid(texture_region);
 
 		element_offset_in_texture = -texture_region.TopLeft();

+ 2 - 3
Source/Core/RenderInterfaceCompatibility.cpp

@@ -171,10 +171,9 @@ void RenderInterfaceAdapter::RenderToClipMask(ClipMaskOperation operation, Compi
 
 	Rectanglef rectangle = Rectanglef::FromPosition(geometry->vertices[0].position);
 	for (const Vertex& vertex : geometry->vertices)
-		rectangle.Join(vertex.position);
-	rectangle.Translate(translation);
+		rectangle = rectangle.Join(vertex.position);
 
-	const Rectanglei scissor = Rectanglei(rectangle);
+	const Rectanglei scissor = Rectanglei(rectangle.Translate(translation));
 	legacy.SetScissorRegion(scissor.Left(), scissor.Top(), scissor.Width(), scissor.Height());
 }
 

+ 1 - 1
Source/Core/RenderManager.cpp

@@ -125,7 +125,7 @@ void RenderManager::SetScissorRegion(Rectanglei new_region)
 
 	if (new_scissor_enable)
 	{
-		new_region.Intersect(Rectanglei::FromSize(viewport_dimensions));
+		new_region = new_region.Intersect(Rectanglei::FromSize(viewport_dimensions));
 
 		if (new_region != state.scissor_region)
 			render_interface->SetScissorRegion(new_region);

+ 1 - 1
Source/Debugger/ElementInfo.cpp

@@ -184,7 +184,7 @@ void ElementInfo::RenderSourceElement()
 			Rectanglef bounding_box;
 			if (ElementUtilities::GetBoundingBox(bounding_box, source_element, BoxArea::Auto))
 			{
-				bounding_box.Extend(1.f);
+				bounding_box = bounding_box.Extend(1.f);
 				Math::ExpandToPixelGrid(bounding_box);
 				Geometry::RenderOutline(bounding_box.Position(), bounding_box.Size(), Colourb(255, 255, 255, 200), 1.f);
 			}