2019-06-14 18:51:57 +02:00
|
|
|
#include "BucketTool.h"
|
2019-06-14 19:11:22 +02:00
|
|
|
#include "PaintableWidget.h"
|
2019-06-15 10:39:45 +02:00
|
|
|
#include <AK/Queue.h>
|
2019-06-14 19:11:22 +02:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
#include <LibGUI/GPainter.h>
|
2019-07-18 10:15:00 +02:00
|
|
|
#include <LibDraw/GraphicsBitmap.h>
|
2019-06-14 18:51:57 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
BucketTool::BucketTool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BucketTool::~BucketTool()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:11:22 +02:00
|
|
|
static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Color target_color, Color fill_color)
|
2019-06-14 18:51:57 +02:00
|
|
|
{
|
2019-06-25 20:33:44 +02:00
|
|
|
ASSERT(bitmap.bpp() == 32);
|
2019-06-15 11:06:02 +02:00
|
|
|
|
2019-06-16 06:34:29 +02:00
|
|
|
if (target_color == fill_color)
|
|
|
|
return;
|
|
|
|
|
2019-06-15 10:39:45 +02:00
|
|
|
Queue<Point> queue;
|
|
|
|
queue.enqueue(Point(start_position));
|
|
|
|
while (!queue.is_empty()) {
|
|
|
|
auto position = queue.dequeue();
|
2019-06-14 19:11:22 +02:00
|
|
|
|
2019-06-15 11:06:02 +02:00
|
|
|
if (bitmap.get_pixel<GraphicsBitmap::Format::RGB32>(position.x(), position.y()) != target_color)
|
2019-06-14 19:11:22 +02:00
|
|
|
continue;
|
2019-06-15 11:06:02 +02:00
|
|
|
bitmap.set_pixel<GraphicsBitmap::Format::RGB32>(position.x(), position.y(), fill_color);
|
2019-06-14 19:11:22 +02:00
|
|
|
|
2019-06-14 21:46:35 +02:00
|
|
|
if (position.x() != 0)
|
2019-06-15 10:48:20 +02:00
|
|
|
queue.enqueue(position.translated(-1, 0));
|
2019-06-14 21:46:35 +02:00
|
|
|
|
|
|
|
if (position.x() != bitmap.width() - 1)
|
2019-06-15 10:48:20 +02:00
|
|
|
queue.enqueue(position.translated(1, 0));
|
2019-06-14 21:46:35 +02:00
|
|
|
|
|
|
|
if (position.y() != 0)
|
2019-06-15 10:48:20 +02:00
|
|
|
queue.enqueue(position.translated(0, -1));
|
2019-06-14 21:46:35 +02:00
|
|
|
|
|
|
|
if (position.y() != bitmap.height() - 1)
|
2019-06-15 10:48:20 +02:00
|
|
|
queue.enqueue(position.translated(0, 1));
|
2019-06-14 19:11:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 11:33:20 +02:00
|
|
|
void BucketTool::on_mousedown(GMouseEvent& event)
|
2019-06-14 19:11:22 +02:00
|
|
|
{
|
2019-06-16 11:33:20 +02:00
|
|
|
if (!m_widget->rect().contains(event.position()))
|
2019-06-14 19:11:22 +02:00
|
|
|
return;
|
|
|
|
|
2019-06-16 11:33:20 +02:00
|
|
|
GPainter painter(m_widget->bitmap());
|
|
|
|
auto target_color = m_widget->bitmap().get_pixel(event.x(), event.y());
|
2019-06-14 19:11:22 +02:00
|
|
|
|
2019-06-16 11:33:20 +02:00
|
|
|
flood_fill(m_widget->bitmap(), event.position(), target_color, m_widget->color_for(event));
|
2019-06-14 19:11:22 +02:00
|
|
|
|
2019-06-16 11:33:20 +02:00
|
|
|
m_widget->update();
|
2019-06-14 18:51:57 +02:00
|
|
|
}
|