diff options
author | Andreas Kling <kling@serenityos.org> | 2020-05-20 20:35:35 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-20 20:35:35 +0200 |
commit | 58fa9c6e8908c7a1416b9c340225e7b73f9c5e2a (patch) | |
tree | 2dd80b00b17569966e84543efca34122cf49f189 /Applications/PixelPaint/Image.cpp | |
parent | ff4eaa12b4e58a5e3ab0269a6749deea731f2a77 (diff) | |
download | serenity-58fa9c6e8908c7a1416b9c340225e7b73f9c5e2a.zip |
PixelPaint: Rename from PaintBrush :^)
Diffstat (limited to 'Applications/PixelPaint/Image.cpp')
-rw-r--r-- | Applications/PixelPaint/Image.cpp | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/Applications/PixelPaint/Image.cpp b/Applications/PixelPaint/Image.cpp new file mode 100644 index 0000000000..ca20eb146a --- /dev/null +++ b/Applications/PixelPaint/Image.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "Image.h" +#include "Layer.h" +#include "LayerModel.h" +#include <LibGUI/Painter.h> + +//#define PAINT_DEBUG + +namespace PixelPaint { + +RefPtr<Image> Image::create_with_size(const Gfx::Size& size) +{ + if (size.is_empty()) + return nullptr; + + if (size.width() > 16384 || size.height() > 16384) + return nullptr; + + return adopt(*new Image(size)); +} + +Image::Image(const Gfx::Size& size) + : m_size(size) +{ +} + +void Image::paint_into(GUI::Painter& painter, const Gfx::Rect& dest_rect, const Gfx::Rect& src_rect) +{ + (void) src_rect; + Gfx::PainterStateSaver saver(painter); + painter.add_clip_rect(dest_rect); + for (auto& layer : m_layers) { + auto target = dest_rect.translated(layer.location()); + target.set_size(layer.size()); +#ifdef IMAGE_DEBUG + dbg() << "Composite layer " << layer.name() << " target: " << target << ", layer.rect: " << layer.rect(); +#endif + painter.draw_scaled_bitmap(target, layer.bitmap(), layer.rect()); + } +} + +void Image::add_layer(NonnullRefPtr<Layer> layer) +{ + for (auto& existing_layer : m_layers) { + ASSERT(&existing_layer != layer.ptr()); + } + m_layers.append(move(layer)); +} + +GUI::Model& Image::layer_model() +{ + if (!m_layer_model) + m_layer_model = LayerModel::create(*this); + return *m_layer_model; +} + +size_t Image::index_of(const Layer& layer) const +{ + for (size_t i = 0; i < m_layers.size(); ++i) { + if (&m_layers.at(i) == &layer) + return i; + } + ASSERT_NOT_REACHED(); +} + +void Image::move_layer_to_back(Layer& layer) +{ + NonnullRefPtr<Layer> protector(layer); + auto index = index_of(layer); + m_layers.remove(index); + m_layers.prepend(layer); +} + +void Image::move_layer_to_front(Layer& layer) +{ + NonnullRefPtr<Layer> protector(layer); + auto index = index_of(layer); + m_layers.remove(index); + m_layers.append(layer); +} + +void Image::move_layer_down(Layer& layer) +{ + NonnullRefPtr<Layer> protector(layer); + auto index = index_of(layer); + if (!index) + return; + m_layers.remove(index); + m_layers.insert(index - 1, layer); +} + +void Image::move_layer_up(Layer& layer) +{ + NonnullRefPtr<Layer> protector(layer); + auto index = index_of(layer); + if (index == m_layers.size() - 1) + return; + m_layers.remove(index); + m_layers.insert(index + 1, layer); +} + +void Image::remove_layer(Layer& layer) +{ + NonnullRefPtr<Layer> protector(layer); + auto index = index_of(layer); + m_layers.remove(index); +} + +} |