소스 검색

Add a simple spray fill tool

Could do with some more tweaking no doubt, and it'd be nice to have a
circular spray, but this is better than nothing.
Robin Burchell 6 년 전
부모
커밋
502c54e39a

+ 6 - 6
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();
 }

+ 1 - 1
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"; }

+ 1 - 0
Applications/PaintBrush/Makefile

@@ -8,6 +8,7 @@ OBJS = \
     PenTool.o \
     BucketTool.o \
     ColorDialog.o \
+    SprayTool.o \
     main.o
 
 APP = PaintBrush

+ 17 - 3
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);
 }

+ 2 - 3
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&);
 

+ 12 - 12
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();

+ 3 - 3
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"; }

+ 69 - 0
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();
+}

+ 22 - 0
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;
+};

+ 8 - 4
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;
 };

+ 2 - 0
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()