Skip to content

Commit

Permalink
Make Rectangle member functions return new objects instead of mutatin…
Browse files Browse the repository at this point in the history
…g in-place
  • Loading branch information
mikke89 committed Aug 22, 2024
1 parent 8ca8d30 commit 2cd78d5
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 51 deletions.
4 changes: 1 addition & 3 deletions Backends/RmlUi_Renderer_GL3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
45 changes: 14 additions & 31 deletions Include/RmlUi/Core/Rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,27 @@ class Rectangle {
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; }
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/ElementEffects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]() {
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/ElementUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/FilterBlur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/FilterDropShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions Source/Core/GeometryBoxShadow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 2 additions & 3 deletions Source/Core/RenderInterfaceCompatibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/RenderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Source/Debugger/ElementInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 2cd78d5

Please sign in to comment.