mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:17:44 +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());
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue