mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +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:
parent
a12751695e
commit
56cbe15033
10 changed files with 185 additions and 33 deletions
15
Applications/PaintBrush/BucketTool.cpp
Normal file
15
Applications/PaintBrush/BucketTool.cpp
Normal 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");
|
||||||
|
}
|
14
Applications/PaintBrush/BucketTool.h
Normal file
14
Applications/PaintBrush/BucketTool.h
Normal 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"; }
|
||||||
|
};
|
|
@ -4,6 +4,9 @@ OBJS = \
|
||||||
PaintableWidget.o \
|
PaintableWidget.o \
|
||||||
PaletteWidget.o \
|
PaletteWidget.o \
|
||||||
ToolboxWidget.o \
|
ToolboxWidget.o \
|
||||||
|
Tool.o \
|
||||||
|
PenTool.o \
|
||||||
|
BucketTool.o \
|
||||||
main.o
|
main.o
|
||||||
|
|
||||||
APP = PaintBrush
|
APP = PaintBrush
|
||||||
|
|
|
@ -1,10 +1,20 @@
|
||||||
#include "PaintableWidget.h"
|
#include "PaintableWidget.h"
|
||||||
|
#include "Tool.h"
|
||||||
#include <LibGUI/GPainter.h>
|
#include <LibGUI/GPainter.h>
|
||||||
#include <SharedGraphics/GraphicsBitmap.h>
|
#include <SharedGraphics/GraphicsBitmap.h>
|
||||||
|
|
||||||
|
static PaintableWidget* s_the;
|
||||||
|
|
||||||
|
PaintableWidget& PaintableWidget::the()
|
||||||
|
{
|
||||||
|
return *s_the;
|
||||||
|
}
|
||||||
|
|
||||||
PaintableWidget::PaintableWidget(GWidget* parent)
|
PaintableWidget::PaintableWidget(GWidget* parent)
|
||||||
: GWidget(parent)
|
: GWidget(parent)
|
||||||
{
|
{
|
||||||
|
ASSERT(!s_the);
|
||||||
|
s_the = this;
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
set_background_color(Color::MidGray);
|
set_background_color(Color::MidGray);
|
||||||
m_bitmap = GraphicsBitmap::create(GraphicsBitmap::Format::RGB32, { 600, 400 });
|
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)
|
void PaintableWidget::mousedown_event(GMouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
|
if (m_tool)
|
||||||
return;
|
m_tool->on_mousedown(*this, event);
|
||||||
|
|
||||||
GPainter painter(*m_bitmap);
|
|
||||||
painter.set_pixel(event.position(), color_for(event));
|
|
||||||
update({ event.position(), { 1, 1 } });
|
|
||||||
m_last_drawing_event_position = event.position();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintableWidget::mouseup_event(GMouseEvent& event)
|
void PaintableWidget::mouseup_event(GMouseEvent& event)
|
||||||
{
|
{
|
||||||
if (event.button() == GMouseButton::Left || event.button() == GMouseButton::Right)
|
if (m_tool)
|
||||||
m_last_drawing_event_position = { -1, -1 };
|
m_tool->on_mouseup(*this, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PaintableWidget::mousemove_event(GMouseEvent& event)
|
void PaintableWidget::mousemove_event(GMouseEvent& event)
|
||||||
{
|
{
|
||||||
if (!rect().contains(event.position()))
|
if (m_tool)
|
||||||
return;
|
m_tool->on_mousemove(*this, event);
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,12 @@
|
||||||
|
|
||||||
#include <LibGUI/GWidget.h>
|
#include <LibGUI/GWidget.h>
|
||||||
|
|
||||||
|
class Tool;
|
||||||
|
|
||||||
class PaintableWidget final : public GWidget {
|
class PaintableWidget final : public GWidget {
|
||||||
public:
|
public:
|
||||||
|
static PaintableWidget& the();
|
||||||
|
|
||||||
explicit PaintableWidget(GWidget* parent);
|
explicit PaintableWidget(GWidget* parent);
|
||||||
virtual ~PaintableWidget() override;
|
virtual ~PaintableWidget() override;
|
||||||
|
|
||||||
|
@ -15,17 +19,24 @@ public:
|
||||||
void set_primary_color(Color color) { m_primary_color = color; }
|
void set_primary_color(Color color) { m_primary_color = color; }
|
||||||
void set_secondary_color(Color color) { m_secondary_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:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
virtual void mousedown_event(GMouseEvent&) override;
|
virtual void mousedown_event(GMouseEvent&) override;
|
||||||
virtual void mouseup_event(GMouseEvent&) override;
|
virtual void mouseup_event(GMouseEvent&) override;
|
||||||
virtual void mousemove_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;
|
RetainPtr<GraphicsBitmap> m_bitmap;
|
||||||
|
|
||||||
Color m_primary_color { Color::Black };
|
Color m_primary_color { Color::Black };
|
||||||
Color m_secondary_color { Color::White };
|
Color m_secondary_color { Color::White };
|
||||||
|
|
||||||
|
Tool* m_tool { nullptr };
|
||||||
};
|
};
|
||||||
|
|
48
Applications/PaintBrush/PenTool.cpp
Normal file
48
Applications/PaintBrush/PenTool.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
19
Applications/PaintBrush/PenTool.h
Normal file
19
Applications/PaintBrush/PenTool.h
Normal 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 };
|
||||||
|
};
|
9
Applications/PaintBrush/Tool.cpp
Normal file
9
Applications/PaintBrush/Tool.cpp
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#include "Tool.h"
|
||||||
|
|
||||||
|
Tool::Tool()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Tool::~Tool()
|
||||||
|
{
|
||||||
|
}
|
18
Applications/PaintBrush/Tool.h
Normal file
18
Applications/PaintBrush/Tool.h
Normal 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();
|
||||||
|
};
|
|
@ -1,7 +1,25 @@
|
||||||
#include "ToolboxWidget.h"
|
#include "ToolboxWidget.h"
|
||||||
|
#include "BucketTool.h"
|
||||||
|
#include "PaintableWidget.h"
|
||||||
|
#include "PenTool.h"
|
||||||
#include <LibGUI/GBoxLayout.h>
|
#include <LibGUI/GBoxLayout.h>
|
||||||
#include <LibGUI/GButton.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)
|
ToolboxWidget::ToolboxWidget(GWidget* parent)
|
||||||
: GFrame(parent)
|
: GFrame(parent)
|
||||||
{
|
{
|
||||||
|
@ -18,17 +36,23 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
|
||||||
set_layout(make<GBoxLayout>(Orientation::Vertical));
|
set_layout(make<GBoxLayout>(Orientation::Vertical));
|
||||||
layout()->set_margins({ 4, 4, 4, 4 });
|
layout()->set_margins({ 4, 4, 4, 4 });
|
||||||
|
|
||||||
auto add_tool = [&] (const StringView& name) {
|
auto add_tool = [&](const StringView& name, OwnPtr<Tool>&& tool) {
|
||||||
auto* button = new GButton(name, this);
|
auto* button = new ToolButton(name, this, move(tool));
|
||||||
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
|
||||||
button->set_preferred_size({ 0, 32 });
|
button->set_preferred_size({ 0, 32 });
|
||||||
button->set_checkable(true);
|
button->set_checkable(true);
|
||||||
button->set_exclusive(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("Pen", make<PenTool>());
|
||||||
add_tool("Buck");
|
add_tool("Buck", make<BucketTool>());
|
||||||
add_tool("Pick");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolboxWidget::~ToolboxWidget()
|
ToolboxWidget::~ToolboxWidget()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue