PaintBrush: Remove the PaintableWidget

Moved the current colors to ImageEditor for now, although I don't think
that will be their final home.
This commit is contained in:
Andreas Kling 2020-05-13 12:47:47 +02:00
parent de42ddfd93
commit 8882b6bd9a
Notes: sideshowbarker 2024-07-19 06:41:01 +09:00
18 changed files with 92 additions and 203 deletions

View file

@ -27,7 +27,6 @@
#include "BucketTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <AK/Queue.h>
#include <AK/SinglyLinkedList.h>
#include <LibGUI/Painter.h>
@ -86,7 +85,7 @@ void BucketTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEv
GUI::Painter painter(layer.bitmap());
auto target_color = layer.bitmap().get_pixel(event.x(), event.y());
flood_fill(layer.bitmap(), event.position(), target_color, PaintableWidget::the().color_for(event));
flood_fill(layer.bitmap(), event.position(), target_color, m_editor->color_for(event));
m_editor->update();
}

View file

@ -27,7 +27,6 @@
#include "EllipseTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
@ -49,7 +48,7 @@ void EllipseTool::draw_using(GUI::Painter& painter)
auto ellipse_intersecting_rect = Gfx::Rect::from_two_points(m_ellipse_start_position, m_ellipse_end_position);
switch (m_mode) {
case Mode::Outline:
painter.draw_ellipse_intersecting(ellipse_intersecting_rect, PaintableWidget::the().color_for(m_drawing_button), m_thickness);
painter.draw_ellipse_intersecting(ellipse_intersecting_rect, m_editor->color_for(m_drawing_button), m_thickness);
break;
default:
ASSERT_NOT_REACHED();

View file

@ -27,7 +27,6 @@
#include "EraseTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
@ -110,7 +109,7 @@ void EraseTool::on_contextmenu(GUI::ContextMenuEvent& event)
Color EraseTool::get_color() const
{
if (m_use_secondary_color)
return PaintableWidget::the().secondary_color();
return m_editor->secondary_color();
return Color(255, 255, 255, 0);
}

View file

@ -126,4 +126,41 @@ void ImageEditor::layers_did_change()
update();
}
Color ImageEditor::color_for(GUI::MouseButton button) const
{
if (button == GUI::MouseButton::Left)
return m_primary_color;
if (button == GUI::MouseButton::Right)
return m_secondary_color;
ASSERT_NOT_REACHED();
}
Color ImageEditor::color_for(const GUI::MouseEvent& event) const
{
if (event.buttons() & GUI::MouseButton::Left)
return m_primary_color;
if (event.buttons() & GUI::MouseButton::Right)
return m_secondary_color;
ASSERT_NOT_REACHED();
}
void ImageEditor::set_primary_color(Color color)
{
if (m_primary_color == color)
return;
m_primary_color = color;
if (on_primary_color_change)
on_primary_color_change(color);
}
void ImageEditor::set_secondary_color(Color color)
{
if (m_secondary_color == color)
return;
m_secondary_color = color;
if (on_secondary_color_change)
on_secondary_color_change(color);
}
}

View file

@ -51,6 +51,18 @@ public:
void layers_did_change();
Color primary_color() const { return m_primary_color; }
void set_primary_color(Color);
Color secondary_color() const { return m_secondary_color; }
void set_secondary_color(Color);
Color color_for(const GUI::MouseEvent&) const;
Color color_for(GUI::MouseButton) const;
Function<void(Color)> on_primary_color_change;
Function<void(Color)> on_secondary_color_change;
private:
ImageEditor();
@ -63,6 +75,9 @@ private:
RefPtr<Layer> m_active_layer;
Tool* m_active_tool { nullptr };
Color m_primary_color { Color::Black };
Color m_secondary_color { Color::White };
};
}

View file

@ -27,7 +27,6 @@
#include "LineTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
@ -74,7 +73,7 @@ void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&
{
if (event.button() == m_drawing_button) {
GUI::Painter painter(layer.bitmap());
painter.draw_line(m_line_start_position, m_line_end_position, PaintableWidget::the().color_for(m_drawing_button), m_thickness);
painter.draw_line(m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
m_drawing_button = GUI::MouseButton::None;
m_editor->update();
}

View file

@ -8,7 +8,6 @@ OBJS = \
LayerModel.o \
LineTool.o \
MoveTool.o \
PaintableWidget.o \
PaletteWidget.o \
PenTool.o \
PickerTool.o \

View file

@ -27,7 +27,6 @@
#include "MoveTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>

View file

@ -1,97 +0,0 @@
/*
* Copyright (c) 2018-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 "PaintableWidget.h"
#include "Tool.h"
#include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Palette.h>
static PaintableWidget* s_the;
PaintableWidget& PaintableWidget::the()
{
return *s_the;
}
PaintableWidget::PaintableWidget()
{
ASSERT(!s_the);
s_the = this;
set_fill_with_background_color(true);
auto pal = palette();
pal.set_color(ColorRole::Window, Color::MidGray);
set_palette(pal);
set_background_color(Color::MidGray);
m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGBA32, { 600, 400 });
m_bitmap->fill(Color(255, 255, 255, 0));
}
PaintableWidget::~PaintableWidget()
{
}
Color PaintableWidget::color_for(GUI::MouseButton button) const
{
if (button == GUI::MouseButton::Left)
return m_primary_color;
if (button == GUI::MouseButton::Right)
return m_secondary_color;
ASSERT_NOT_REACHED();
}
Color PaintableWidget::color_for(const GUI::MouseEvent& event) const
{
if (event.buttons() & GUI::MouseButton::Left)
return m_primary_color;
if (event.buttons() & GUI::MouseButton::Right)
return m_secondary_color;
ASSERT_NOT_REACHED();
}
void PaintableWidget::set_primary_color(Color color)
{
if (m_primary_color == color)
return;
m_primary_color = color;
if (on_primary_color_change)
on_primary_color_change(color);
}
void PaintableWidget::set_secondary_color(Color color)
{
if (m_secondary_color == color)
return;
m_secondary_color = color;
if (on_secondary_color_change)
on_secondary_color_change(color);
}
void PaintableWidget::set_bitmap(const Gfx::Bitmap& bitmap)
{
m_bitmap = bitmap;
update();
}

View file

@ -1,62 +0,0 @@
/*
* Copyright (c) 2018-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.
*/
#pragma once
#include <LibGUI/Widget.h>
class PaintableWidget final : public GUI::Widget {
C_OBJECT(PaintableWidget)
public:
static PaintableWidget& the();
virtual ~PaintableWidget() override;
Color primary_color() const { return m_primary_color; }
Color secondary_color() const { return m_secondary_color; }
void set_primary_color(Color);
void set_secondary_color(Color);
Color color_for(const GUI::MouseEvent&) const;
Color color_for(GUI::MouseButton) const;
void set_bitmap(const Gfx::Bitmap&);
Gfx::Bitmap& bitmap() { return *m_bitmap; }
const Gfx::Bitmap& bitmap() const { return *m_bitmap; }
Function<void(Color)> on_primary_color_change;
Function<void(Color)> on_secondary_color_change;
private:
PaintableWidget();
RefPtr<Gfx::Bitmap> m_bitmap;
Color m_primary_color { Color::Black };
Color m_secondary_color { Color::White };
};

View file

@ -25,13 +25,16 @@
*/
#include "PaletteWidget.h"
#include "PaintableWidget.h"
#include "ImageEditor.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/ColorPicker.h>
#include <LibGfx/Palette.h>
namespace PaintBrush {
class ColorWidget : public GUI::Frame {
C_OBJECT(ColorWidget)
C_OBJECT(ColorWidget);
public:
explicit ColorWidget(Color color, PaletteWidget& palette_widget)
: m_palette_widget(palette_widget)
@ -68,8 +71,8 @@ private:
Color m_color;
};
PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget)
: m_paintable_widget(paintable_widget)
PaletteWidget::PaletteWidget(ImageEditor& editor)
: m_editor(editor)
{
set_frame_shape(Gfx::FrameShape::Panel);
set_frame_shadow(Gfx::FrameShadow::Raised);
@ -82,20 +85,20 @@ PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget)
m_secondary_color_widget = add<GUI::Frame>();
m_secondary_color_widget->set_relative_rect({ 2, 2, 60, 31 });
m_secondary_color_widget->set_fill_with_background_color(true);
set_secondary_color(paintable_widget.secondary_color());
set_secondary_color(m_editor.secondary_color());
m_primary_color_widget = add<GUI::Frame>();
Gfx::Rect rect { 0, 0, 38, 15 };
rect.center_within(m_secondary_color_widget->relative_rect());
m_primary_color_widget->set_relative_rect(rect);
m_primary_color_widget->set_fill_with_background_color(true);
set_primary_color(paintable_widget.primary_color());
set_primary_color(m_editor.primary_color());
paintable_widget.on_primary_color_change = [this](Color color) {
m_editor.on_primary_color_change = [this](Color color) {
set_primary_color(color);
};
paintable_widget.on_secondary_color_change = [this](Color color) {
m_editor.on_secondary_color_change = [this](Color color) {
set_secondary_color(color);
};
@ -157,7 +160,7 @@ PaletteWidget::~PaletteWidget()
void PaletteWidget::set_primary_color(Color color)
{
m_paintable_widget.set_primary_color(color);
m_editor.set_primary_color(color);
auto pal = m_primary_color_widget->palette();
pal.set_color(ColorRole::Background, color);
m_primary_color_widget->set_palette(pal);
@ -166,9 +169,11 @@ void PaletteWidget::set_primary_color(Color color)
void PaletteWidget::set_secondary_color(Color color)
{
m_paintable_widget.set_secondary_color(color);
m_editor.set_secondary_color(color);
auto pal = m_secondary_color_widget->palette();
pal.set_color(ColorRole::Background, color);
m_secondary_color_widget->set_palette(pal);
m_secondary_color_widget->update();
}
}

View file

@ -28,19 +28,25 @@
#include <LibGUI/Frame.h>
class PaintableWidget;
namespace PaintBrush {
class ImageEditor;
class PaletteWidget final : public GUI::Frame {
C_OBJECT(PaletteWidget)
C_OBJECT(PaletteWidget);
public:
explicit PaletteWidget(PaintableWidget&);
virtual ~PaletteWidget() override;
void set_primary_color(Color);
void set_secondary_color(Color);
private:
PaintableWidget& m_paintable_widget;
explicit PaletteWidget(ImageEditor&);
ImageEditor& m_editor;
RefPtr<GUI::Frame> m_primary_color_widget;
RefPtr<GUI::Frame> m_secondary_color_widget;
};
}

View file

@ -27,7 +27,6 @@
#include "PenTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
@ -48,7 +47,7 @@ void PenTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent
return;
GUI::Painter painter(layer.bitmap());
painter.draw_line(event.position(), event.position(), PaintableWidget::the().color_for(event), m_thickness);
painter.draw_line(event.position(), event.position(), m_editor->color_for(event), m_thickness);
m_editor->update();
m_last_drawing_event_position = event.position();
}
@ -68,9 +67,9 @@ void PenTool::on_mousemove(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent
GUI::Painter painter(layer.bitmap());
if (m_last_drawing_event_position != Gfx::Point(-1, -1))
painter.draw_line(m_last_drawing_event_position, event.position(), PaintableWidget::the().color_for(event), m_thickness);
painter.draw_line(m_last_drawing_event_position, event.position(), m_editor->color_for(event), m_thickness);
else
painter.draw_line(event.position(), event.position(), PaintableWidget::the().color_for(event), m_thickness);
painter.draw_line(event.position(), event.position(), m_editor->color_for(event), m_thickness);
m_editor->update();
m_last_drawing_event_position = event.position();

View file

@ -25,8 +25,8 @@
*/
#include "PickerTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGfx/Bitmap.h>
namespace PaintBrush {
@ -45,9 +45,9 @@ void PickerTool::on_mousedown(Layer& layer, GUI::MouseEvent& event, GUI::MouseEv
return;
auto color = layer.bitmap().get_pixel(event.position());
if (event.button() == GUI::MouseButton::Left)
PaintableWidget::the().set_primary_color(color);
m_editor->set_primary_color(color);
else if (event.button() == GUI::MouseButton::Right)
PaintableWidget::the().set_secondary_color(color);
m_editor->set_secondary_color(color);
}
}

View file

@ -27,7 +27,6 @@
#include "RectangleTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <LibGUI/Action.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
@ -49,13 +48,13 @@ void RectangleTool::draw_using(GUI::Painter& painter)
auto rect_to_draw = Gfx::Rect::from_two_points(m_rectangle_start_position, m_rectangle_end_position);
switch (m_mode) {
case Mode::Fill:
painter.fill_rect(rect_to_draw, PaintableWidget::the().color_for(m_drawing_button));
painter.fill_rect(rect_to_draw, m_editor->color_for(m_drawing_button));
break;
case Mode::Outline:
painter.draw_rect(rect_to_draw, PaintableWidget::the().color_for(m_drawing_button));
painter.draw_rect(rect_to_draw, m_editor->color_for(m_drawing_button));
break;
case Mode::Gradient:
painter.fill_rect_with_gradient(rect_to_draw, PaintableWidget::the().primary_color(), PaintableWidget::the().secondary_color());
painter.fill_rect_with_gradient(rect_to_draw, m_editor->primary_color(), m_editor->secondary_color());
break;
default:
ASSERT_NOT_REACHED();

View file

@ -27,7 +27,6 @@
#include "SprayTool.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include <AK/Queue.h>
#include <AK/SinglyLinkedList.h>
#include <LibGUI/Action.h>
@ -87,7 +86,7 @@ void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
if (!m_editor->rect().contains(event.position()))
return;
m_color = PaintableWidget::the().color_for(event);
m_color = m_editor->color_for(event);
m_last_pos = event.position();
m_timer->start();
paint_it();

View file

@ -30,7 +30,6 @@
#include "EraseTool.h"
#include "LineTool.h"
#include "MoveTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include "PickerTool.h"
#include "RectangleTool.h"

View file

@ -27,7 +27,6 @@
#include "Image.h"
#include "ImageEditor.h"
#include "Layer.h"
#include "PaintableWidget.h"
#include "PaletteWidget.h"
#include "ToolboxWidget.h"
#include <LibGUI/AboutDialog.h>
@ -80,10 +79,7 @@ int main(int argc, char** argv)
image_editor.set_active_tool(tool);
};
auto& paintable_widget = vertical_container.add<PaintableWidget>();
paintable_widget.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
paintable_widget.set_preferred_size(0, 0);
vertical_container.add<PaletteWidget>(paintable_widget);
vertical_container.add<PaintBrush::PaletteWidget>(image_editor);
auto& right_panel = horizontal_container.add<GUI::Widget>();
right_panel.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
@ -109,7 +105,6 @@ int main(int argc, char** argv)
GUI::MessageBox::show(String::format("Failed to load '%s'", open_path.value().characters()), "Open failed", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
return;
}
paintable_widget.set_bitmap(*bitmap);
}));
app_menu.add_separator();
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {