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

PaintBrush: Factor out the pen tool into an actual PenTool class.

Also add a Tool base class, and an empty BucketTool subclass which is the
next thing to implement.
This commit is contained in:
Andreas Kling 2019-06-14 18:51:57 +02:00
parent a12751695e
commit 56cbe15033
10 changed files with 185 additions and 33 deletions

View file

@ -0,0 +1,15 @@
#include "BucketTool.h"
#include <stdio.h>
BucketTool::BucketTool()
{
}
BucketTool::~BucketTool()
{
}
void BucketTool::on_mousedown(PaintableWidget&, GMouseEvent&)
{
dbgprintf("FIXME: Implement BucketTool::on_mousedown\n");
}

View file

@ -0,0 +1,14 @@
#pragma once
#include "Tool.h"
class BucketTool final : public Tool {
public:
BucketTool();
virtual ~BucketTool() override;
virtual void on_mousedown(PaintableWidget&, GMouseEvent&) override;
private:
virtual const char* class_name() const override { return "BucketTool"; }
};

View file

@ -4,6 +4,9 @@ OBJS = \
PaintableWidget.o \
PaletteWidget.o \
ToolboxWidget.o \
Tool.o \
PenTool.o \
BucketTool.o \
main.o
APP = PaintBrush

View file

@ -1,10 +1,20 @@
#include "PaintableWidget.h"
#include "Tool.h"
#include <LibGUI/GPainter.h>
#include <SharedGraphics/GraphicsBitmap.h>
static PaintableWidget* s_the;
PaintableWidget& PaintableWidget::the()
{
return *s_the;
}
PaintableWidget::PaintableWidget(GWidget* parent)
: GWidget(parent)
{
ASSERT(!s_the);
s_the = this;
set_fill_with_background_color(true);
set_background_color(Color::MidGray);
m_bitmap = GraphicsBitmap::create(GraphicsBitmap::Format::RGB32, { 600, 400 });
@ -33,37 +43,18 @@ Color PaintableWidget::color_for(const GMouseEvent& event)
void PaintableWidget::mousedown_event(GMouseEvent& event)
{
if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
return;
GPainter painter(*m_bitmap);
painter.set_pixel(event.position(), color_for(event));
update({ event.position(), { 1, 1 } });
m_last_drawing_event_position = event.position();
if (m_tool)
m_tool->on_mousedown(*this, event);
}
void PaintableWidget::mouseup_event(GMouseEvent& event)
{
if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right)
m_last_drawing_event_position = { -1, -1 };
if (m_tool)
m_tool->on_mouseup(*this, event);
}
void PaintableWidget::mousemove_event(GMouseEvent& event)
{
if (!rect().contains(event.position()))
return;
if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
GPainter painter(*m_bitmap);
if (m_last_drawing_event_position != Point(-1, -1)) {
painter.draw_line(m_last_drawing_event_position, event.position(), color_for(event));
update();
} else {
painter.set_pixel(event.position(), color_for(event));
update({ event.position(), { 1, 1 } });
}
m_last_drawing_event_position = event.position();
}
if (m_tool)
m_tool->on_mousemove(*this, event);
}

View file

@ -2,8 +2,12 @@
#include <LibGUI/GWidget.h>
class Tool;
class PaintableWidget final : public GWidget {
public:
static PaintableWidget& the();
explicit PaintableWidget(GWidget* parent);
virtual ~PaintableWidget() override;
@ -15,17 +19,24 @@ public:
void set_primary_color(Color color) { m_primary_color = color; }
void set_secondary_color(Color color) { m_secondary_color = color; }
void set_tool(Tool* tool) { m_tool = tool; }
Tool* tool() { return m_tool; }
Color color_for(const GMouseEvent&);
GraphicsBitmap& bitmap() { return *m_bitmap; }
const GraphicsBitmap& bitmap() const { return *m_bitmap; }
private:
virtual void paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;
virtual void mouseup_event(GMouseEvent&) override;
virtual void mousemove_event(GMouseEvent&) override;
Color color_for(const GMouseEvent&);
Point m_last_drawing_event_position { -1, -1 };
RetainPtr<GraphicsBitmap> m_bitmap;
Color m_primary_color { Color::Black };
Color m_secondary_color { Color::White };
Tool* m_tool { nullptr };
};

View file

@ -0,0 +1,48 @@
#include "PenTool.h"
#include "PaintableWidget.h"
#include <LibGUI/GPainter.h>
PenTool::PenTool()
{
}
PenTool::~PenTool()
{
}
void PenTool::on_mousedown(PaintableWidget& paintable_widget, GMouseEvent& event)
{
if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
return;
GPainter painter(paintable_widget.bitmap());
painter.set_pixel(event.position(), paintable_widget.color_for(event));
paintable_widget.update({ event.position(), { 1, 1 } });
m_last_drawing_event_position = event.position();
}
void PenTool::on_mouseup(PaintableWidget&, GMouseEvent& event)
{
if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right)
m_last_drawing_event_position = { -1, -1 };
}
void PenTool::on_mousemove(PaintableWidget& paintable_widget, GMouseEvent& event)
{
if (!paintable_widget.rect().contains(event.position()))
return;
if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
GPainter painter(paintable_widget.bitmap());
if (m_last_drawing_event_position != Point(-1, -1)) {
painter.draw_line(m_last_drawing_event_position, event.position(), paintable_widget.color_for(event));
paintable_widget.update();
} else {
painter.set_pixel(event.position(), paintable_widget.color_for(event));
paintable_widget.update({ event.position(), { 1, 1 } });
}
m_last_drawing_event_position = event.position();
}
}

View file

@ -0,0 +1,19 @@
#pragma once
#include "Tool.h"
#include <SharedGraphics/Point.h>
class PenTool final : public Tool {
public:
PenTool();
virtual ~PenTool() override;
virtual void on_mousedown(PaintableWidget&, GMouseEvent&) override;
virtual void on_mousemove(PaintableWidget&, GMouseEvent&) override;
virtual void on_mouseup(PaintableWidget&, GMouseEvent&) override;
private:
virtual const char* class_name() const override { return "PenTool"; }
Point m_last_drawing_event_position { -1, -1 };
};

View file

@ -0,0 +1,9 @@
#include "Tool.h"
Tool::Tool()
{
}
Tool::~Tool()
{
}

View file

@ -0,0 +1,18 @@
#pragma once
class GMouseEvent;
class PaintableWidget;
class Tool {
public:
virtual ~Tool();
virtual const char* class_name() const = 0;
virtual void on_mousedown(PaintableWidget&, GMouseEvent&) { }
virtual void on_mousemove(PaintableWidget&, GMouseEvent&) { }
virtual void on_mouseup(PaintableWidget&, GMouseEvent&) { }
protected:
Tool();
};

View file

@ -1,7 +1,25 @@
#include "ToolboxWidget.h"
#include "BucketTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
class ToolButton final : public GButton {
public:
ToolButton(const String& name, GWidget* parent, OwnPtr<Tool>&& tool)
: GButton(name, parent)
, m_tool(move(tool))
{
}
const Tool& tool() const { return *m_tool; }
Tool& tool() { return *m_tool; }
private:
OwnPtr<Tool> m_tool;
};
ToolboxWidget::ToolboxWidget(GWidget* parent)
: GFrame(parent)
{
@ -18,17 +36,23 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
set_layout(make<GBoxLayout>(Orientation::Vertical));
layout()->set_margins({ 4, 4, 4, 4 });
auto add_tool = [&] (const StringView& name) {
auto* button = new GButton(name, this);
auto add_tool = [&](const StringView& name, OwnPtr<Tool>&& tool) {
auto* button = new ToolButton(name, this, move(tool));
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
button->set_preferred_size({ 0, 32 });
button->set_checkable(true);
button->set_exclusive(true);
button->on_checked = [button](auto checked) {
if (checked)
PaintableWidget::the().set_tool(&button->tool());
else
PaintableWidget::the().set_tool(nullptr);
};
};
add_tool("Pen");
add_tool("Buck");
add_tool("Pick");
add_tool("Pen", make<PenTool>());
add_tool("Buck", make<BucketTool>());
}
ToolboxWidget::~ToolboxWidget()