summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Burchell <robin+git@viroteck.net>2019-06-28 11:11:06 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-06-28 15:27:23 +0200
commit24ada6bd1f6f99ddc603bb48c60697efc7349d29 (patch)
treecfd5a16d851265297b56d74c3298b69caccae9cf
parente971f5604c0d94919bd85194b16245b2c3269533 (diff)
downloadserenity-24ada6bd1f6f99ddc603bb48c60697efc7349d29.zip
PaintBrush: Add an erase tool
-rw-r--r--Applications/PaintBrush/EraseTool.cpp67
-rw-r--r--Applications/PaintBrush/EraseTool.h22
-rw-r--r--Applications/PaintBrush/Makefile1
-rw-r--r--Applications/PaintBrush/ToolboxWidget.cpp2
4 files changed, 92 insertions, 0 deletions
diff --git a/Applications/PaintBrush/EraseTool.cpp b/Applications/PaintBrush/EraseTool.cpp
new file mode 100644
index 0000000000..e1244320c4
--- /dev/null
+++ b/Applications/PaintBrush/EraseTool.cpp
@@ -0,0 +1,67 @@
+#include "EraseTool.h"
+#include "PaintableWidget.h"
+#include <LibGUI/GAction.h>
+#include <LibGUI/GMenu.h>
+#include <LibGUI/GPainter.h>
+
+EraseTool::EraseTool()
+{
+}
+
+EraseTool::~EraseTool()
+{
+}
+
+Rect EraseTool::build_rect(const Point& pos, const Rect& widget_rect)
+{
+ const int base_eraser_size = 10;
+ const int eraser_size = (base_eraser_size * m_thickness);
+ const int eraser_radius = eraser_size / 2;
+ const auto ex = pos.x();
+ const auto ey = pos.y();
+ return Rect(ex - eraser_radius, ey - eraser_radius, eraser_size, eraser_size).intersected(widget_rect);
+}
+
+void EraseTool::on_mousedown(GMouseEvent& event)
+{
+ if (event.button() != GMouseButton::Left && event.button() != GMouseButton::Right)
+ return;
+ Rect r = build_rect(event.position(), m_widget->bitmap().rect());
+ GPainter painter(m_widget->bitmap());
+ painter.fill_rect(r, Color(Color::White));
+ m_widget->update();
+}
+
+void EraseTool::on_mousemove(GMouseEvent& event)
+{
+ if (!m_widget->rect().contains(event.position()))
+ return;
+
+ if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
+ Rect r = build_rect(event.position(), m_widget->bitmap().rect());
+ GPainter painter(m_widget->bitmap());
+ painter.fill_rect(r, Color(Color::White));
+ m_widget->update();
+ }
+}
+
+void EraseTool::on_contextmenu(GContextMenuEvent& event)
+{
+ if (!m_context_menu) {
+ m_context_menu = make<GMenu>("EraseTool Context Menu");
+ m_context_menu->add_action(GAction::create("1", [this](auto&) {
+ m_thickness = 1;
+ }));
+ m_context_menu->add_action(GAction::create("2", [this](auto&) {
+ m_thickness = 2;
+ }));
+ m_context_menu->add_action(GAction::create("3", [this](auto&) {
+ m_thickness = 3;
+ }));
+ m_context_menu->add_action(GAction::create("4", [this](auto&) {
+ m_thickness = 4;
+ }));
+ }
+ m_context_menu->popup(event.screen_position());
+}
+
diff --git a/Applications/PaintBrush/EraseTool.h b/Applications/PaintBrush/EraseTool.h
new file mode 100644
index 0000000000..3616edf324
--- /dev/null
+++ b/Applications/PaintBrush/EraseTool.h
@@ -0,0 +1,22 @@
+#pragma once
+
+#include "Tool.h"
+#include <SharedGraphics/Point.h>
+
+class GMenu;
+
+class EraseTool final : public Tool {
+public:
+ EraseTool();
+ virtual ~EraseTool() override;
+
+ virtual void on_mousedown(GMouseEvent&) override;
+ virtual void on_mousemove(GMouseEvent&) override;
+ virtual void on_contextmenu(GContextMenuEvent&) override;
+
+private:
+ virtual const char* class_name() const override { return "EraseTool"; }
+ Rect build_rect(const Point& pos, const Rect& widget_rect);
+ OwnPtr<GMenu> m_context_menu;
+ int m_thickness { 1 };
+};
diff --git a/Applications/PaintBrush/Makefile b/Applications/PaintBrush/Makefile
index c9ec8b39ca..2990169a00 100644
--- a/Applications/PaintBrush/Makefile
+++ b/Applications/PaintBrush/Makefile
@@ -6,6 +6,7 @@ OBJS = \
ToolboxWidget.o \
Tool.o \
PenTool.o \
+ EraseTool.o \
BucketTool.o \
ColorDialog.o \
SprayTool.o \
diff --git a/Applications/PaintBrush/ToolboxWidget.cpp b/Applications/PaintBrush/ToolboxWidget.cpp
index fec67ad132..826b33bd56 100644
--- a/Applications/PaintBrush/ToolboxWidget.cpp
+++ b/Applications/PaintBrush/ToolboxWidget.cpp
@@ -4,6 +4,7 @@
#include "PaintableWidget.h"
#include "PenTool.h"
#include "PickerTool.h"
+#include "EraseTool.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <SharedGraphics/PNGLoader.h>
@@ -66,6 +67,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
add_tool("Bucket Fill", "bucket", make<BucketTool>());
add_tool("Spray", "spray", make<SprayTool>());
add_tool("Color Picker", "picker", make<PickerTool>());
+ add_tool("Erase", "", make<EraseTool>());
}
ToolboxWidget::~ToolboxWidget()