1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:07:44 +00:00

PixelPaint: Allow layer to "scale" location when resizing

This commit is contained in:
Andrew Smith 2022-03-18 16:29:29 -05:00 committed by Andreas Kling
parent 02399d4775
commit bccf0a9346
3 changed files with 37 additions and 13 deletions

View file

@ -529,13 +529,24 @@ void Image::crop(Gfx::IntRect const& cropped_rect)
void Image::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode) void Image::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode)
{ {
float scale_x = 1.0f;
float scale_y = 1.0f;
if (size().width() != 0.0f) {
scale_x = new_size.width() / static_cast<float>(size().width());
}
if (size().height() != 0.0f) {
scale_y = new_size.height() / static_cast<float>(size().height());
}
for (auto& layer : m_layers) { for (auto& layer : m_layers) {
layer.resize(new_size, scaling_mode); Gfx::IntPoint new_location(scale_x * layer.location().x(), scale_y * layer.location().y());
layer.resize(new_size, new_location, scaling_mode);
} }
m_size = { new_size.width(), new_size.height() }; m_size = { new_size.width(), new_size.height() };
did_change_rect(); did_change_rect();
} }
Color Image::color_at(Gfx::IntPoint const& point) const Color Image::color_at(Gfx::IntPoint const& point) const

View file

@ -180,31 +180,42 @@ void Layer::crop(Gfx::IntRect const& rect)
did_modify_bitmap(); did_modify_bitmap();
} }
void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode) void Layer::resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode)
{ {
const Gfx::IntRect old_rect(Gfx::IntPoint(0, 0), size()); auto src_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), size());
const Gfx::IntRect new_rect(Gfx::IntPoint(0, 0), new_size); auto dst_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), new_size);
{ {
auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
Gfx::Painter painter(resized); Gfx::Painter painter(dst);
painter.draw_scaled_bitmap(new_rect, *m_content_bitmap, old_rect, 1.0f, scaling_mode); painter.draw_scaled_bitmap(dst_rect, *m_content_bitmap, src_rect, 1.0f, scaling_mode);
m_content_bitmap = move(resized); m_content_bitmap = move(dst);
} }
if (m_mask_bitmap) { if (m_mask_bitmap) {
auto resized = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors(); auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
Gfx::Painter painter(resized); Gfx::Painter painter(dst);
painter.draw_scaled_bitmap(new_rect, *m_mask_bitmap, old_rect, 1.0f, scaling_mode); painter.draw_scaled_bitmap(dst_rect, *m_mask_bitmap, src_rect, 1.0f, scaling_mode);
m_mask_bitmap = move(resized); m_mask_bitmap = move(dst);
} }
set_location(new_location);
did_modify_bitmap(); did_modify_bitmap();
} }
void Layer::resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode)
{
resize(new_rect.size(), new_rect.location(), scaling_mode);
}
void Layer::resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode)
{
resize(new_size, location(), scaling_mode);
}
void Layer::update_cached_bitmap() void Layer::update_cached_bitmap()
{ {
if (!is_masked()) { if (!is_masked()) {

View file

@ -58,6 +58,8 @@ public:
void rotate(Gfx::RotationDirection direction); void rotate(Gfx::RotationDirection direction);
void crop(Gfx::IntRect const& rect); void crop(Gfx::IntRect const& rect);
void resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode); void resize(Gfx::IntSize const& new_size, Gfx::Painter::ScalingMode scaling_mode);
void resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode);
void resize(Gfx::IntSize const& new_size, Gfx::IntPoint const& new_location, Gfx::Painter::ScalingMode scaling_mode);
ErrorOr<void> try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask); ErrorOr<void> try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask);