1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:17:35 +00:00

LibGfx+Everywhere: Change Gfx::Rect to be endpoint exclusive

Previously, calling `.right()` on a `Gfx::Rect` would return the last
column's coordinate still inside the rectangle, or `left + width - 1`.
This is called 'endpoint inclusive' and does not make a lot of sense for
`Gfx::Rect<float>` where a rectangle of width 5 at position (0, 0) would
return 4 as its right side. This same problem exists for `.bottom()`.

This changes `Gfx::Rect` to be endpoint exclusive, which gives us the
nice property that `width = right - left` and `height = bottom - top`.
It enables us to treat `Gfx::Rect<int>` and `Gfx::Rect<float>` exactly
the same.

All users of `Gfx::Rect` have been updated accordingly.
This commit is contained in:
Jelle Raaijmakers 2023-05-22 00:41:18 +02:00 committed by Andreas Kling
parent b7f4363791
commit f391ccfe53
88 changed files with 524 additions and 518 deletions

View file

@ -690,7 +690,7 @@ ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode sca
auto layer_rect = layer->relative_rect().to_type<float>();
auto scaled_top_left = layer_rect.top_left().scaled(scale_x, scale_y).to_rounded<int>();
auto scaled_bottom_right = layer_rect.bottom_right().translated(1).scaled(scale_x, scale_y).to_rounded<int>();
auto scaled_bottom_right = layer_rect.bottom_right().scaled(scale_x, scale_y).to_rounded<int>();
auto scaled_layer_rect = Gfx::IntRect::from_two_points(scaled_top_left, scaled_bottom_right);
TRY(new_layer->scale(scaled_layer_rect, scaling_mode, Layer::NotifyClients::No));

View file

@ -172,15 +172,15 @@ void ImageEditor::paint_event(GUI::PaintEvent& event)
auto event_image_rect = enclosing_int_rect(frame_to_content_rect(event.rect())).inflated(1, 1);
auto image_rect = m_image->rect().inflated(1, 1).intersected(event_image_rect);
for (auto i = image_rect.left(); i < image_rect.right(); i++) {
for (auto i = image_rect.left(); i < image_rect.right() - 1; i++) {
auto start_point = content_to_frame_position({ i, image_rect.top() }).to_type<int>();
auto end_point = content_to_frame_position({ i, image_rect.bottom() }).to_type<int>();
auto end_point = content_to_frame_position({ i, image_rect.bottom() - 1 }).to_type<int>();
painter.draw_line(start_point, end_point, Color::LightGray);
}
for (auto i = image_rect.top(); i < image_rect.bottom(); i++) {
for (auto i = image_rect.top(); i < image_rect.bottom() - 1; i++) {
auto start_point = content_to_frame_position({ image_rect.left(), i }).to_type<int>();
auto end_point = content_to_frame_position({ image_rect.right(), i }).to_type<int>();
auto end_point = content_to_frame_position({ image_rect.right() - 1, i }).to_type<int>();
painter.draw_line(start_point, end_point, Color::LightGray);
}
}
@ -822,19 +822,19 @@ void ImageEditor::paint_selection(Gfx::Painter& painter)
void ImageEditor::draw_marching_ants(Gfx::Painter& painter, Gfx::IntRect const& rect) const
{
// Top line
for (int x = rect.left(); x <= rect.right(); ++x)
for (int x = rect.left(); x < rect.right(); ++x)
draw_marching_ants_pixel(painter, x, rect.top());
// Right line
for (int y = rect.top() + 1; y <= rect.bottom(); ++y)
draw_marching_ants_pixel(painter, rect.right(), y);
for (int y = rect.top() + 1; y < rect.bottom(); ++y)
draw_marching_ants_pixel(painter, rect.right() - 1, y);
// Bottom line
for (int x = rect.right() - 1; x >= rect.left(); --x)
draw_marching_ants_pixel(painter, x, rect.bottom());
for (int x = rect.right() - 2; x >= rect.left(); --x)
draw_marching_ants_pixel(painter, x, rect.bottom() - 1);
// Left line
for (int y = rect.bottom() - 1; y > rect.top(); --y)
for (int y = rect.bottom() - 2; y > rect.top(); --y)
draw_marching_ants_pixel(painter, rect.left(), y);
}
@ -849,18 +849,17 @@ void ImageEditor::draw_marching_ants(Gfx::Painter& painter, Mask const& mask) co
rect.inflate(step * 2, step * 2); // prevent borders from having visible ants if the selection extends beyond it
// Scan the image horizontally to find vertical borders
for (int y = rect.top(); y <= rect.bottom(); y += step) {
for (int y = rect.top(); y < rect.bottom(); y += step) {
bool previous_selected = false;
for (int x = rect.left(); x <= rect.right(); x += step) {
for (int x = rect.left(); x < rect.right(); x += step) {
bool this_selected = mask.get(x, y) > 0;
if (this_selected != previous_selected) {
Gfx::IntRect image_pixel { x, y, 1, 1 };
auto pixel = content_to_frame_rect(image_pixel).to_type<int>();
auto end = max(pixel.top(), pixel.bottom()); // for when the zoom is < 100%
auto end = max(pixel.top() + 1, pixel.bottom()); // for when the zoom is < 100%
for (int pixel_y = pixel.top(); pixel_y <= end; pixel_y++) {
for (int pixel_y = pixel.top(); pixel_y < end; pixel_y++) {
draw_marching_ants_pixel(painter, pixel.left(), pixel_y);
}
}
@ -870,20 +869,19 @@ void ImageEditor::draw_marching_ants(Gfx::Painter& painter, Mask const& mask) co
}
// Scan the image vertically to find horizontal borders
for (int x = rect.left(); x <= rect.right(); x += step) {
for (int x = rect.left(); x < rect.right(); x += step) {
bool previous_selected = false;
for (int y = rect.top(); y <= rect.bottom(); y += step) {
for (int y = rect.top(); y < rect.bottom(); y += step) {
bool this_selected = mask.get(x, y) > 0;
if (this_selected != previous_selected) {
Gfx::IntRect image_pixel { x, y, 1, 1 };
auto pixel = content_to_frame_rect(image_pixel).to_type<int>();
auto end = max(pixel.left(), pixel.right()); // for when the zoom is < 100%
auto end = max(pixel.left() + 1, pixel.right()); // for when the zoom is < 100%
for (int pixel_x = pixel.left(); pixel_x <= end; pixel_x++) {
for (int pixel_x = pixel.left(); pixel_x < end; pixel_x++)
draw_marching_ants_pixel(painter, pixel_x, pixel.top());
}
}
previous_selected = this_selected;

View file

@ -133,9 +133,8 @@ Gfx::Bitmap& Layer::get_scratch_edited_bitmap()
RefPtr<Gfx::Bitmap> Layer::copy_bitmap(Selection const& selection) const
{
if (selection.is_empty()) {
if (selection.is_empty())
return {};
}
auto selection_rect = selection.bounding_rect();
auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, selection_rect.size());
@ -144,9 +143,8 @@ RefPtr<Gfx::Bitmap> Layer::copy_bitmap(Selection const& selection) const
auto result = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
VERIFY(result->has_alpha_channel());
for (int y = selection_rect.top(); y <= selection_rect.bottom(); y++) {
for (int x = selection_rect.left(); x <= selection_rect.right(); x++) {
for (int y = selection_rect.top(); y < selection_rect.bottom(); y++) {
for (int x = selection_rect.left(); x < selection_rect.right(); x++) {
Gfx::IntPoint image_point { x, y };
auto layer_point = image_point - m_location;
auto result_point = image_point - selection_rect.top_left();
@ -180,9 +178,8 @@ void Layer::erase_selection(Selection const& selection)
for (int x = translated_to_layer_space.left(); x < translated_to_layer_space.left() + translated_to_layer_space.width(); ++x) {
// Selection is still in pre-translated coordinates, account for this by adding the layer's relative location
if (content_bitmap().rect().contains(x, y) && selection.is_selected(x + location().x(), y + location().y())) {
if (content_bitmap().rect().contains(x, y) && selection.is_selected(x + location().x(), y + location().y()))
content_bitmap().set_pixel(x, y, Color::Transparent);
}
}
}

View file

@ -98,7 +98,7 @@ void LayerListWidget::get_gadget_rects(Gadget const& gadget, bool is_masked, Gfx
inner_thumbnail_rect.center_within(outer_thumbnail_rect);
if (is_masked) {
outer_mask_thumbnail_rect = { outer_thumbnail_rect.top_right().x() + 5, outer_thumbnail_rect.y(), outer_thumbnail_rect.width(), outer_thumbnail_rect.height() };
outer_mask_thumbnail_rect = { outer_thumbnail_rect.right() + 4, outer_thumbnail_rect.y(), outer_thumbnail_rect.width(), outer_thumbnail_rect.height() };
inner_mask_thumbnail_rect = { 0, 0, thumbnail_size.width(), thumbnail_size.height() };
inner_mask_thumbnail_rect.center_within(outer_mask_thumbnail_rect);
} else {
@ -106,7 +106,7 @@ void LayerListWidget::get_gadget_rects(Gadget const& gadget, bool is_masked, Gfx
inner_mask_thumbnail_rect = inner_thumbnail_rect;
}
text_rect = { outer_mask_thumbnail_rect.right() + 10, outer_rect.y(), outer_rect.width(), outer_rect.height() };
text_rect = { outer_mask_thumbnail_rect.right() + 9, outer_rect.y(), outer_rect.width(), outer_rect.height() };
text_rect.intersect(outer_rect);
}
@ -279,7 +279,7 @@ void LayerListWidget::mousemove_event(GUI::MouseEvent& event)
VERIFY(gadget.is_moving);
gadget.movement_delta.set_y(delta.y());
auto inner_rect_max_height = widget_inner_rect().height() - 2 + vertical_scrollbar().max();
auto inner_rect_max_height = widget_inner_rect().height() - 1 + vertical_scrollbar().max();
if (delta.y() < 0 && gadget.rect.y() < -delta.y())
gadget.movement_delta.set_y(-gadget.rect.y());
@ -346,12 +346,12 @@ void LayerListWidget::automatic_scrolling_timer_did_fire()
vertical_scrollbar().increase_slider_by(m_automatic_scroll_delta.y());
gadget.movement_delta.set_y(gadget.movement_delta.y() + m_automatic_scroll_delta.y());
auto inner_rect_max_height = widget_inner_rect().height() - 2 + vertical_scrollbar().max();
auto inner_rect_max_height = widget_inner_rect().height() - 1 + vertical_scrollbar().max();
auto gadget_absolute_position = gadget.rect.y() + gadget.movement_delta.y();
if (gadget_absolute_position < 0)
gadget.movement_delta.set_y(-gadget.rect.y());
else if (gadget_absolute_position + gadget.rect.height() >= inner_rect_max_height)
else if (gadget_absolute_position + gadget.rect.height() >= inner_rect_max_height - 1)
gadget.movement_delta.set_y(inner_rect_max_height - gadget.rect.bottom());
else
relayout_gadgets();

View file

@ -61,10 +61,9 @@ public:
template<typename Func>
void for_each_pixel(Func func) const
{
for (int x = m_bounding_rect.left(); x <= m_bounding_rect.right(); x++) {
for (int y = m_bounding_rect.top(); y <= m_bounding_rect.bottom(); y++) {
for (int x = m_bounding_rect.left(); x < m_bounding_rect.right(); x++) {
for (int y = m_bounding_rect.top(); y < m_bounding_rect.bottom(); y++)
func(x, y);
}
}
}

View file

@ -115,7 +115,7 @@ PaletteWidget::PaletteWidget()
m_primary_color_widget->set_fill_with_background_color(true);
m_color_container = add<GUI::Widget>();
m_color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 33);
m_color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 1, 2, 500, 33);
m_color_container->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 1);
auto& top_color_container = m_color_container->add<GUI::Widget>();

View file

@ -78,19 +78,19 @@ void MoveTool::on_mousemove(Layer* layer, MouseEvent& event)
switch (m_resize_anchor_location.value()) {
case ResizeAnchorLocation::TopLeft:
scaling_origin = rect_being_moved.top_left();
opposite_corner = rect_being_moved.bottom_right().translated(1, 1);
opposite_corner = rect_being_moved.bottom_right();
break;
case ResizeAnchorLocation::BottomRight:
scaling_origin = rect_being_moved.bottom_right().translated(1, 1);
scaling_origin = rect_being_moved.bottom_right();
opposite_corner = rect_being_moved.top_left();
break;
case ResizeAnchorLocation::BottomLeft:
scaling_origin = rect_being_moved.bottom_left().translated(0, 1);
opposite_corner = rect_being_moved.top_right().translated(1, 0);
scaling_origin = rect_being_moved.bottom_left();
opposite_corner = rect_being_moved.top_right();
break;
case ResizeAnchorLocation::TopRight:
scaling_origin = rect_being_moved.top_right().translated(1, 0);
opposite_corner = rect_being_moved.bottom_left().translated(0, 1);
scaling_origin = rect_being_moved.top_right();
opposite_corner = rect_being_moved.bottom_left();
break;
}
scaling_origin.translate_by(delta);
@ -233,9 +233,9 @@ Array<Gfx::IntRect, 4> MoveTool::resize_anchor_rects(Gfx::IntRect layer_rect_in_
{
return Array {
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_left(), resize_anchor_size),
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_right().translated(1, 0), resize_anchor_size),
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_left().translated(0, 1), resize_anchor_size),
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_right().translated(1), resize_anchor_size)
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.top_right(), resize_anchor_size),
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_left(), resize_anchor_size),
resize_anchor_rect_from_position(layer_rect_in_frame_coordinates.bottom_right(), resize_anchor_size)
};
}
@ -265,11 +265,11 @@ Optional<ResizeAnchorLocation const> MoveTool::resize_anchor_location_from_curso
if (cursor_within_resize_anchor_rect(layer_rect.top_left()))
return ResizeAnchorLocation::TopLeft;
if (cursor_within_resize_anchor_rect(layer_rect.top_right().translated(1, 0)))
if (cursor_within_resize_anchor_rect(layer_rect.top_right()))
return ResizeAnchorLocation::TopRight;
if (cursor_within_resize_anchor_rect(layer_rect.bottom_left().translated(0, 1)))
if (cursor_within_resize_anchor_rect(layer_rect.bottom_left()))
return ResizeAnchorLocation::BottomLeft;
if (cursor_within_resize_anchor_rect(layer_rect.bottom_right().translated(1)))
if (cursor_within_resize_anchor_rect(layer_rect.bottom_right()))
return ResizeAnchorLocation::BottomRight;
return {};
}