mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:27:36 +00:00
PaintBrush: Add an erase tool
This commit is contained in:
parent
e971f5604c
commit
24ada6bd1f
4 changed files with 92 additions and 0 deletions
67
Applications/PaintBrush/EraseTool.cpp
Normal file
67
Applications/PaintBrush/EraseTool.cpp
Normal file
|
@ -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());
|
||||||
|
}
|
||||||
|
|
22
Applications/PaintBrush/EraseTool.h
Normal file
22
Applications/PaintBrush/EraseTool.h
Normal file
|
@ -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 };
|
||||||
|
};
|
|
@ -6,6 +6,7 @@ OBJS = \
|
||||||
ToolboxWidget.o \
|
ToolboxWidget.o \
|
||||||
Tool.o \
|
Tool.o \
|
||||||
PenTool.o \
|
PenTool.o \
|
||||||
|
EraseTool.o \
|
||||||
BucketTool.o \
|
BucketTool.o \
|
||||||
ColorDialog.o \
|
ColorDialog.o \
|
||||||
SprayTool.o \
|
SprayTool.o \
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
#include "PaintableWidget.h"
|
#include "PaintableWidget.h"
|
||||||
#include "PenTool.h"
|
#include "PenTool.h"
|
||||||
#include "PickerTool.h"
|
#include "PickerTool.h"
|
||||||
|
#include "EraseTool.h"
|
||||||
#include <LibGUI/GBoxLayout.h>
|
#include <LibGUI/GBoxLayout.h>
|
||||||
#include <LibGUI/GButton.h>
|
#include <LibGUI/GButton.h>
|
||||||
#include <SharedGraphics/PNGLoader.h>
|
#include <SharedGraphics/PNGLoader.h>
|
||||||
|
@ -66,6 +67,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
|
||||||
add_tool("Bucket Fill", "bucket", make<BucketTool>());
|
add_tool("Bucket Fill", "bucket", make<BucketTool>());
|
||||||
add_tool("Spray", "spray", make<SprayTool>());
|
add_tool("Spray", "spray", make<SprayTool>());
|
||||||
add_tool("Color Picker", "picker", make<PickerTool>());
|
add_tool("Color Picker", "picker", make<PickerTool>());
|
||||||
|
add_tool("Erase", "", make<EraseTool>());
|
||||||
}
|
}
|
||||||
|
|
||||||
ToolboxWidget::~ToolboxWidget()
|
ToolboxWidget::~ToolboxWidget()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue