1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:07:36 +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

@ -11,13 +11,13 @@
#include <LibGfx/Palette.h>
#include <LibGfx/Path.h>
void AnalogClock::draw_graduations(GUI::Painter& painter, Gfx::IntRect& rect, int x, int y)
void AnalogClock::draw_graduations(GUI::Painter& painter, Gfx::IntRect rect, int x, int y)
{
rect.set_x(x);
rect.set_y(y);
rect.set_location({ x, y });
painter.fill_rect(rect, palette().active_window_border2());
rect.shrink(0, 1, 1, 0);
painter.draw_line(rect.top_left(), rect.top_right(), palette().threed_highlight());
painter.draw_line(rect.bottom_left(), rect.bottom_right(), palette().active_window_border1().darkened(0.7f));
painter.draw_line(rect.bottom_right(), rect.top_right(), palette().active_window_border1().darkened(0.7f));

View file

@ -43,7 +43,7 @@ protected:
void paint_event(GUI::PaintEvent&) override;
void draw_face(GUI::Painter&);
void draw_mirrored_graduations(GUI::Painter&, Gfx::IntRect&, int x, int y, int rect_center_offset);
void draw_graduations(GUI::Painter&, Gfx::IntRect&, int x, int y);
void draw_graduations(GUI::Painter&, Gfx::IntRect, int x, int y);
void draw_hand(GUI::Painter&, double angle, double length, Gfx::Color hand_color);
void draw_seconds_hand(GUI::Painter&, double angle);
void update_title_date();

View file

@ -144,7 +144,7 @@ Tab::Tab(BrowserWindow& window)
i++;
m_go_back_context_menu->add_action(GUI::Action::create(url.to_deprecated_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_back(i); }));
}
m_go_back_context_menu->popup(go_back_button.screen_relative_rect().bottom_left());
m_go_back_context_menu->popup(go_back_button.screen_relative_rect().bottom_left().moved_up(1));
};
auto& go_forward_button = toolbar.add_action(window.go_forward_action());
@ -157,7 +157,7 @@ Tab::Tab(BrowserWindow& window)
i++;
m_go_forward_context_menu->add_action(GUI::Action::create(url.to_deprecated_string(), g_icon_bag.filetype_html, [this, i](auto&) { go_forward(i); }));
}
m_go_forward_context_menu->popup(go_forward_button.screen_relative_rect().bottom_left());
m_go_forward_context_menu->popup(go_forward_button.screen_relative_rect().bottom_left().moved_up(1));
};
auto& go_home_button = toolbar.add_action(window.go_home_action());

View file

@ -167,7 +167,7 @@ private:
auto rect_for_square = [&](Chess::Square const& square) {
return Gfx::IntRect {
frame_inner_rect().left() + square.file * square_size,
frame_inner_rect().bottom() + 1 - (square.rank + 1) * square_size,
frame_inner_rect().bottom() - (square.rank + 1) * square_size,
square_size,
square_size
};

View file

@ -605,7 +605,7 @@ void HexEditor::paint_event(GUI::PaintEvent& event)
height() - height_occupied_by_horizontal_scrollbar() //(total_rows() * line_height()) + 5
};
painter.fill_rect(offset_clip_rect, palette().ruler());
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right(), palette().ruler_border());
painter.draw_line(offset_clip_rect.top_right(), offset_clip_rect.bottom_right().translated(-1), palette().ruler_border());
auto margin_and_hex_width = static_cast<int>(offset_margin_width() + m_bytes_per_row * cell_width() + 3 * m_padding);
painter.draw_line({ margin_and_hex_width, 0 },

View file

@ -117,11 +117,11 @@ void MagnifierWidget::paint_event(GUI::PaintEvent& event)
int end_y = bitmap_rect.bottom();
int end_x = bitmap_rect.right();
for (int current_y = start_y; current_y <= end_y; current_y += m_scale_factor)
painter.draw_line({ start_x, current_y }, { end_x, current_y }, m_grid_color);
for (int current_y = start_y; current_y < end_y; current_y += m_scale_factor)
painter.draw_line({ start_x, current_y }, { end_x - 1, current_y }, m_grid_color);
for (int current_x = start_y; current_x <= end_x; current_x += m_scale_factor)
painter.draw_line({ current_x, start_y }, { current_x, end_y }, m_grid_color);
for (int current_x = start_y; current_x < end_x; current_x += m_scale_factor)
painter.draw_line({ current_x, start_y }, { current_x, end_y - 1 }, m_grid_color);
}
}

View file

@ -46,7 +46,7 @@ void DoubleClickArrowWidget::paint_event(GUI::PaintEvent& event)
});
auto text_rect = rect();
text_rect.set_y(bottom_arrow_rect.bottom());
text_rect.set_y(bottom_arrow_rect.bottom() - 1);
text_rect.set_height(font().pixel_size_rounded_up());
}

View file

@ -93,6 +93,7 @@ void RollWidget::paint_event(GUI::PaintEvent& event)
else
background_painter.fill_rect(rect, Color::White);
rect.shrink(0, 1, 1, 0);
background_painter.draw_line(rect.top_right(), rect.bottom_right(), Color::Black);
background_painter.draw_line(rect.bottom_left(), rect.bottom_right(), Color::Black);
}

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 {};
}

View file

@ -17,7 +17,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app = TRY(GUI::Application::create(arguments));
auto window = TRY(RunWindow::try_create());
window->move_to(16, GUI::Desktop::the().rect().bottom() - GUI::Desktop::the().taskbar_height() - 16 - window->height());
constexpr int margin = 16;
window->move_to(margin, GUI::Desktop::the().rect().bottom() - 1 - GUI::Desktop::the().taskbar_height() - margin - window->height());
window->show();
return app->exec();

View file

@ -59,7 +59,7 @@ void GraphWidget::paint_event(GUI::PaintEvent& event)
auto const& background_color = line_color.with_alpha(0x7f);
m_calculated_points.clear_with_capacity();
for (size_t i = 0; i < m_values.size(); i++) {
int x = inner_rect.right() - (i * 2) + 1;
int x = inner_rect.right() - i * 2;
if (x < 0)
break;
auto const& current_values = m_values.at(m_values.size() - i - 1);
@ -74,7 +74,7 @@ void GraphWidget::paint_event(GUI::PaintEvent& event)
value += current_values[l];
}
float scaled_value = value * scale;
Gfx::IntPoint current_point { x, inner_rect.bottom() - (int)scaled_value };
Gfx::IntPoint current_point { x, inner_rect.bottom() - 1 - (int)scaled_value };
m_calculated_points.append(current_point);
}
VERIFY(m_calculated_points.size() <= m_values.size());
@ -91,14 +91,14 @@ void GraphWidget::paint_event(GUI::PaintEvent& event)
if (points_in_path > 1) {
VERIFY(current_point);
VERIFY(first_point);
path.line_to({ current_point->x() - 1, inner_rect.bottom() + 1 });
path.line_to({ first_point->x() + 1, inner_rect.bottom() + 1 });
path.line_to({ current_point->x() - 1, inner_rect.bottom() });
path.line_to({ first_point->x() + 1, inner_rect.bottom() });
path.close();
painter.fill_path(path, background_color, Gfx::Painter::WindingRule::EvenOdd);
} else if (points_in_path == 1 && current_point) {
// Can't fill any area, we only have one data point.
// Just draw a vertical line as a "fill"...
painter.draw_line(*current_point, { current_point->x(), inner_rect.bottom() }, background_color);
painter.draw_line(*current_point, { current_point->x(), inner_rect.bottom() - 1 }, background_color);
}
path = {};
points_in_path = 0;

View file

@ -42,7 +42,7 @@ public:
else
VERIFY_NOT_REACHED();
painter.draw_line({ x, rect.top() }, { x, rect.bottom() }, color);
painter.draw_line({ x, rect.top() }, { x, rect.bottom() - 1 }, color);
}
painter.draw_rect(rect, Color::Black);