summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Applications/PaintBrush/BucketTool.cpp12
-rw-r--r--Applications/PaintBrush/BucketTool.h2
-rw-r--r--Applications/PaintBrush/Makefile1
-rw-r--r--Applications/PaintBrush/PaintableWidget.cpp20
-rw-r--r--Applications/PaintBrush/PaintableWidget.h5
-rw-r--r--Applications/PaintBrush/PenTool.cpp24
-rw-r--r--Applications/PaintBrush/PenTool.h6
-rw-r--r--Applications/PaintBrush/SprayTool.cpp69
-rw-r--r--Applications/PaintBrush/SprayTool.h22
-rw-r--r--Applications/PaintBrush/Tool.h12
-rw-r--r--Applications/PaintBrush/ToolboxWidget.cpp2
11 files changed, 143 insertions, 32 deletions
diff --git a/Applications/PaintBrush/BucketTool.cpp b/Applications/PaintBrush/BucketTool.cpp
index 70ae32dde1..03cdefad28 100644
--- a/Applications/PaintBrush/BucketTool.cpp
+++ b/Applications/PaintBrush/BucketTool.cpp
@@ -44,15 +44,15 @@ static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Colo
}
}
-void BucketTool::on_mousedown(PaintableWidget& paintable_widget, GMouseEvent& event)
+void BucketTool::on_mousedown(GMouseEvent& event)
{
- if (!paintable_widget.rect().contains(event.position()))
+ if (!m_widget->rect().contains(event.position()))
return;
- GPainter painter(paintable_widget.bitmap());
- auto target_color = paintable_widget.bitmap().get_pixel(event.x(), event.y());
+ GPainter painter(m_widget->bitmap());
+ auto target_color = m_widget->bitmap().get_pixel(event.x(), event.y());
- flood_fill(paintable_widget.bitmap(), event.position(), target_color, paintable_widget.color_for(event));
+ flood_fill(m_widget->bitmap(), event.position(), target_color, m_widget->color_for(event));
- paintable_widget.update();
+ m_widget->update();
}
diff --git a/Applications/PaintBrush/BucketTool.h b/Applications/PaintBrush/BucketTool.h
index 0c72c9e7f1..3e03e3da56 100644
--- a/Applications/PaintBrush/BucketTool.h
+++ b/Applications/PaintBrush/BucketTool.h
@@ -7,7 +7,7 @@ public:
BucketTool();
virtual ~BucketTool() override;
- virtual void on_mousedown(PaintableWidget&, GMouseEvent&) override;
+ virtual void on_mousedown(GMouseEvent&) override;
private:
virtual const char* class_name() const override { return "BucketTool"; }
diff --git a/Applications/PaintBrush/Makefile b/Applications/PaintBrush/Makefile
index b0137f7b9c..0163456c64 100644
--- a/Applications/PaintBrush/Makefile
+++ b/Applications/PaintBrush/Makefile
@@ -8,6 +8,7 @@ OBJS = \
PenTool.o \
BucketTool.o \
ColorDialog.o \
+ SprayTool.o \
main.o
APP = PaintBrush
diff --git a/Applications/PaintBrush/PaintableWidget.cpp b/Applications/PaintBrush/PaintableWidget.cpp
index 02ddc2da01..ce0bf08dbf 100644
--- a/Applications/PaintBrush/PaintableWidget.cpp
+++ b/Applications/PaintBrush/PaintableWidget.cpp
@@ -32,6 +32,20 @@ void PaintableWidget::paint_event(GPaintEvent& event)
painter.blit({ 0, 0 }, *m_bitmap, m_bitmap->rect());
}
+void PaintableWidget::set_tool(Tool* tool)
+{
+ if (m_tool)
+ m_tool->clear();
+ m_tool = tool;
+ if (m_tool)
+ m_tool->setup(*this);
+}
+
+Tool* PaintableWidget::tool()
+{
+ return m_tool;
+}
+
Color PaintableWidget::color_for(const GMouseEvent& event)
{
if (event.buttons() & GMouseButton::Left)
@@ -44,17 +58,17 @@ Color PaintableWidget::color_for(const GMouseEvent& event)
void PaintableWidget::mousedown_event(GMouseEvent& event)
{
if (m_tool)
- m_tool->on_mousedown(*this, event);
+ m_tool->on_mousedown(event);
}
void PaintableWidget::mouseup_event(GMouseEvent& event)
{
if (m_tool)
- m_tool->on_mouseup(*this, event);
+ m_tool->on_mouseup(event);
}
void PaintableWidget::mousemove_event(GMouseEvent& event)
{
if (m_tool)
- m_tool->on_mousemove(*this, event);
+ m_tool->on_mousemove(event);
}
diff --git a/Applications/PaintBrush/PaintableWidget.h b/Applications/PaintBrush/PaintableWidget.h
index 7734b60c24..bd6cd4455b 100644
--- a/Applications/PaintBrush/PaintableWidget.h
+++ b/Applications/PaintBrush/PaintableWidget.h
@@ -1,7 +1,6 @@
#pragma once
#include <LibGUI/GWidget.h>
-
class Tool;
class PaintableWidget final : public GWidget {
@@ -19,8 +18,8 @@ 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; }
+ void set_tool(Tool* tool);
+ Tool* tool();
Color color_for(const GMouseEvent&);
diff --git a/Applications/PaintBrush/PenTool.cpp b/Applications/PaintBrush/PenTool.cpp
index 185ef430b5..2a2edc104a 100644
--- a/Applications/PaintBrush/PenTool.cpp
+++ b/Applications/PaintBrush/PenTool.cpp
@@ -10,37 +10,37 @@ PenTool::~PenTool()
{
}
-void PenTool::on_mousedown(PaintableWidget& paintable_widget, GMouseEvent& event)
+void PenTool::on_mousedown(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 } });
+ GPainter painter(m_widget->bitmap());
+ painter.set_pixel(event.position(), m_widget->color_for(event));
+ m_widget->update({ event.position(), { 1, 1 } });
m_last_drawing_event_position = event.position();
}
-void PenTool::on_mouseup(PaintableWidget&, GMouseEvent& event)
+void PenTool::on_mouseup(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)
+void PenTool::on_mousemove(GMouseEvent& event)
{
- if (!paintable_widget.rect().contains(event.position()))
+ if (!m_widget->rect().contains(event.position()))
return;
if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
- GPainter painter(paintable_widget.bitmap());
+ GPainter painter(m_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();
+ painter.draw_line(m_last_drawing_event_position, event.position(), m_widget->color_for(event));
+ m_widget->update();
} else {
- painter.set_pixel(event.position(), paintable_widget.color_for(event));
- paintable_widget.update({ event.position(), { 1, 1 } });
+ painter.set_pixel(event.position(), m_widget->color_for(event));
+ m_widget->update({ event.position(), { 1, 1 } });
}
m_last_drawing_event_position = event.position();
diff --git a/Applications/PaintBrush/PenTool.h b/Applications/PaintBrush/PenTool.h
index 82c1ff2c07..20dbc4f567 100644
--- a/Applications/PaintBrush/PenTool.h
+++ b/Applications/PaintBrush/PenTool.h
@@ -8,9 +8,9 @@ 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;
+ virtual void on_mousedown(GMouseEvent&) override;
+ virtual void on_mousemove(GMouseEvent&) override;
+ virtual void on_mouseup(GMouseEvent&) override;
private:
virtual const char* class_name() const override { return "PenTool"; }
diff --git a/Applications/PaintBrush/SprayTool.cpp b/Applications/PaintBrush/SprayTool.cpp
new file mode 100644
index 0000000000..bb2c19c147
--- /dev/null
+++ b/Applications/PaintBrush/SprayTool.cpp
@@ -0,0 +1,69 @@
+#include "SprayTool.h"
+#include "PaintableWidget.h"
+#include <AK/Queue.h>
+#include <AK/SinglyLinkedList.h>
+#include <LibGUI/GPainter.h>
+#include <SharedGraphics/GraphicsBitmap.h>
+#include <stdio.h>
+
+SprayTool::SprayTool()
+{
+ m_timer.on_timeout = [=]() {
+ paint_it();
+ };
+ m_timer.set_interval(200);
+}
+
+SprayTool::~SprayTool()
+{
+}
+
+static double nrand()
+{
+ return double(rand()) / double(RAND_MAX);
+}
+
+void SprayTool::paint_it()
+{
+ GPainter painter(m_widget->bitmap());
+ auto& bitmap = m_widget->bitmap();
+ ASSERT(bitmap.format() == GraphicsBitmap::Format::RGB32);
+ m_widget->update();
+ const double radius = 15;
+ for (int i = 0; i < 100 + (nrand() * 800); i++) {
+ const int minX = m_last_pos.x() - radius;
+ const int minY = m_last_pos.y() - radius;
+ const int xpos = minX + (radius * 2 * nrand());
+ const int ypos = minY + (radius * 2 * nrand());
+ if (xpos < 0 || xpos >= bitmap.width())
+ continue;
+ if (ypos < 0 || ypos >= bitmap.height())
+ continue;
+ bitmap.set_pixel<GraphicsBitmap::Format::RGB32>(xpos, ypos, m_color);
+ }
+}
+
+void SprayTool::on_mousedown(GMouseEvent& event)
+{
+ if (!m_widget->rect().contains(event.position()))
+ return;
+
+ m_color = m_widget->color_for(event);
+ m_last_pos = event.position();
+ m_timer.start();
+ paint_it();
+}
+
+void SprayTool::on_mousemove(GMouseEvent& event)
+{
+ m_last_pos = event.position();
+ if (m_timer.is_active()) {
+ paint_it();
+ m_timer.restart(m_timer.interval());
+ }
+}
+
+void SprayTool::on_mouseup(GMouseEvent&)
+{
+ m_timer.stop();
+}
diff --git a/Applications/PaintBrush/SprayTool.h b/Applications/PaintBrush/SprayTool.h
new file mode 100644
index 0000000000..ac2e8322a5
--- /dev/null
+++ b/Applications/PaintBrush/SprayTool.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "Tool.h"
+#include <LibGUI/GPainter.h>
+#include <LibCore/CTimer.h>
+
+class SprayTool final : public Tool {
+public:
+ SprayTool();
+ virtual ~SprayTool() override;
+
+ virtual void on_mousedown(GMouseEvent&) override;
+ virtual void on_mouseup(GMouseEvent&) override;
+ virtual void on_mousemove(GMouseEvent&) override;
+
+private:
+ virtual const char* class_name() const override { return "SprayTool"; }
+ void paint_it();
+ CTimer m_timer;
+ Point m_last_pos;
+ Color m_color;
+};
diff --git a/Applications/PaintBrush/Tool.h b/Applications/PaintBrush/Tool.h
index c564cfb6a2..62fa299ad8 100644
--- a/Applications/PaintBrush/Tool.h
+++ b/Applications/PaintBrush/Tool.h
@@ -1,7 +1,7 @@
#pragma once
+#include "PaintableWidget.h"
class GMouseEvent;
-class PaintableWidget;
class Tool {
public:
@@ -9,10 +9,14 @@ public:
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&) { }
+ virtual void on_mousedown(GMouseEvent&) { }
+ virtual void on_mousemove(GMouseEvent&) { }
+ virtual void on_mouseup(GMouseEvent&) { }
+
+ void clear() { m_widget = nullptr; }
+ void setup(PaintableWidget& widget) { m_widget = widget.make_weak_ptr(); }
protected:
Tool();
+ WeakPtr<PaintableWidget> m_widget;
};
diff --git a/Applications/PaintBrush/ToolboxWidget.cpp b/Applications/PaintBrush/ToolboxWidget.cpp
index 1d13b51e9a..bb21293376 100644
--- a/Applications/PaintBrush/ToolboxWidget.cpp
+++ b/Applications/PaintBrush/ToolboxWidget.cpp
@@ -1,5 +1,6 @@
#include "ToolboxWidget.h"
#include "BucketTool.h"
+#include "SprayTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include <LibGUI/GBoxLayout.h>
@@ -57,6 +58,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
add_tool("Pen", "pen", make<PenTool>());
add_tool("Bucket Fill", "bucket", make<BucketTool>());
+ add_tool("Spray", "", make<SprayTool>());
}
ToolboxWidget::~ToolboxWidget()