mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
PaintBrush: Implement a naive but working bucket fill tool.
I've used a SinglyLinkedList<Point> for the flood fill queue, since Vector was death slow. This could definitely be made faster with a better algorithm and/or data structure. :^)
This commit is contained in:
parent
1ec5172ce1
commit
e9c021de92
1 changed files with 34 additions and 2 deletions
|
@ -1,4 +1,8 @@
|
||||||
#include "BucketTool.h"
|
#include "BucketTool.h"
|
||||||
|
#include "PaintableWidget.h"
|
||||||
|
#include <AK/SinglyLinkedList.h>
|
||||||
|
#include <LibGUI/GPainter.h>
|
||||||
|
#include <SharedGraphics/GraphicsBitmap.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
BucketTool::BucketTool()
|
BucketTool::BucketTool()
|
||||||
|
@ -9,7 +13,35 @@ BucketTool::~BucketTool()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void BucketTool::on_mousedown(PaintableWidget&, GMouseEvent&)
|
static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Color target_color, Color fill_color)
|
||||||
{
|
{
|
||||||
dbgprintf("FIXME: Implement BucketTool::on_mousedown\n");
|
SinglyLinkedList<Point> queue;
|
||||||
|
|
||||||
|
queue.append(Point(start_position));
|
||||||
|
while (!queue.is_empty()) {
|
||||||
|
auto position = queue.take_first();
|
||||||
|
if (!bitmap.rect().contains(position))
|
||||||
|
continue;
|
||||||
|
if (bitmap.get_pixel(position) != target_color)
|
||||||
|
continue;
|
||||||
|
bitmap.set_pixel(position, fill_color);
|
||||||
|
|
||||||
|
queue.append(position.translated(0, -1));
|
||||||
|
queue.append(position.translated(0, 1));
|
||||||
|
queue.append(position.translated(1, 0));
|
||||||
|
queue.append(position.translated(-1, 0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BucketTool::on_mousedown(PaintableWidget& paintable_widget, GMouseEvent& event)
|
||||||
|
{
|
||||||
|
if (!paintable_widget.rect().contains(event.position()))
|
||||||
|
return;
|
||||||
|
|
||||||
|
GPainter painter(paintable_widget.bitmap());
|
||||||
|
auto target_color = paintable_widget.bitmap().get_pixel(event.x(), event.y());
|
||||||
|
|
||||||
|
flood_fill(paintable_widget.bitmap(), event.position(), target_color, paintable_widget.color_for(event));
|
||||||
|
|
||||||
|
paintable_widget.update();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue