mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 06:47:35 +00:00
PaintBrush: Implement a thickness setting for the pen tool.
Painter gains the ability to draw lines with arbitrary thickness. It's basically implemented by drawing filled rects for thickness>1. In PaintBrush, Tool classes can now override on_contextmenu() to provide a context menu for the toolbox button. :^)
This commit is contained in:
parent
08c04f0a41
commit
5aefd7f828
6 changed files with 57 additions and 21 deletions
|
@ -1,5 +1,7 @@
|
|||
#include "PenTool.h"
|
||||
#include "PaintableWidget.h"
|
||||
#include <LibGUI/GAction.h>
|
||||
#include <LibGUI/GMenu.h>
|
||||
#include <LibGUI/GPainter.h>
|
||||
|
||||
PenTool::PenTool()
|
||||
|
@ -16,8 +18,8 @@ void PenTool::on_mousedown(GMouseEvent& event)
|
|||
return;
|
||||
|
||||
GPainter painter(m_widget->bitmap());
|
||||
painter.set_pixel(event.position(), m_widget->color_for(event));
|
||||
m_widget->update({ event.position(), { 1, 1 } });
|
||||
painter.draw_line(event.position(), event.position(), m_widget->color_for(event), m_thickness);
|
||||
m_widget->update();
|
||||
m_last_drawing_event_position = event.position();
|
||||
}
|
||||
|
||||
|
@ -35,14 +37,32 @@ void PenTool::on_mousemove(GMouseEvent& event)
|
|||
if (event.buttons() & GMouseButton::Left || event.buttons() & GMouseButton::Right) {
|
||||
GPainter painter(m_widget->bitmap());
|
||||
|
||||
if (m_last_drawing_event_position != Point(-1, -1)) {
|
||||
painter.draw_line(m_last_drawing_event_position, event.position(), m_widget->color_for(event));
|
||||
m_widget->update();
|
||||
} else {
|
||||
painter.set_pixel(event.position(), m_widget->color_for(event));
|
||||
m_widget->update({ event.position(), { 1, 1 } });
|
||||
}
|
||||
if (m_last_drawing_event_position != Point(-1, -1))
|
||||
painter.draw_line(m_last_drawing_event_position, event.position(), m_widget->color_for(event), m_thickness);
|
||||
else
|
||||
painter.draw_line(event.position(), event.position(), m_widget->color_for(event), m_thickness);
|
||||
m_widget->update();
|
||||
|
||||
m_last_drawing_event_position = event.position();
|
||||
}
|
||||
}
|
||||
|
||||
void PenTool::on_contextmenu(GContextMenuEvent& event)
|
||||
{
|
||||
if (!m_context_menu) {
|
||||
m_context_menu = make<GMenu>("PenTool 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());
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
#include "Tool.h"
|
||||
#include <SharedGraphics/Point.h>
|
||||
|
||||
class GMenu;
|
||||
|
||||
class PenTool final : public Tool {
|
||||
public:
|
||||
PenTool();
|
||||
|
@ -11,9 +13,12 @@ public:
|
|||
virtual void on_mousedown(GMouseEvent&) override;
|
||||
virtual void on_mousemove(GMouseEvent&) override;
|
||||
virtual void on_mouseup(GMouseEvent&) override;
|
||||
virtual void on_contextmenu(GContextMenuEvent&) override;
|
||||
|
||||
private:
|
||||
virtual const char* class_name() const override { return "PenTool"; }
|
||||
|
||||
Point m_last_drawing_event_position { -1, -1 };
|
||||
OwnPtr<GMenu> m_context_menu;
|
||||
int m_thickness { 1 };
|
||||
};
|
||||
|
|
|
@ -12,6 +12,7 @@ public:
|
|||
virtual void on_mousedown(GMouseEvent&) { }
|
||||
virtual void on_mousemove(GMouseEvent&) { }
|
||||
virtual void on_mouseup(GMouseEvent&) { }
|
||||
virtual void on_contextmenu(GContextMenuEvent&) { }
|
||||
|
||||
void clear() { m_widget = nullptr; }
|
||||
void setup(PaintableWidget& widget) { m_widget = widget.make_weak_ptr(); }
|
||||
|
|
|
@ -20,6 +20,11 @@ public:
|
|||
const Tool& tool() const { return *m_tool; }
|
||||
Tool& tool() { return *m_tool; }
|
||||
|
||||
virtual void context_menu_event(GContextMenuEvent& event) override
|
||||
{
|
||||
m_tool->on_contextmenu(event);
|
||||
}
|
||||
|
||||
private:
|
||||
OwnPtr<Tool> m_tool;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue