diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-06-23 10:00:02 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-06-23 10:00:02 +0200 |
commit | 5aefd7f828c4c3485c0d154b91f7d7bc5686a41d (patch) | |
tree | 74c473931e75a0e018f5cc0ccb8717def091e139 /Applications | |
parent | 08c04f0a41135057e1a8bbec33094df0df404c2b (diff) | |
download | serenity-5aefd7f828c4c3485c0d154b91f7d7bc5686a41d.zip |
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. :^)
Diffstat (limited to 'Applications')
-rw-r--r-- | Applications/PaintBrush/PenTool.cpp | 38 | ||||
-rw-r--r-- | Applications/PaintBrush/PenTool.h | 5 | ||||
-rw-r--r-- | Applications/PaintBrush/Tool.h | 1 | ||||
-rw-r--r-- | Applications/PaintBrush/ToolboxWidget.cpp | 5 |
4 files changed, 40 insertions, 9 deletions
diff --git a/Applications/PaintBrush/PenTool.cpp b/Applications/PaintBrush/PenTool.cpp index 2a2edc104a..7a9a493be5 100644 --- a/Applications/PaintBrush/PenTool.cpp +++ b/Applications/PaintBrush/PenTool.cpp @@ -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()); +} diff --git a/Applications/PaintBrush/PenTool.h b/Applications/PaintBrush/PenTool.h index 20dbc4f567..2ef8f7c7aa 100644 --- a/Applications/PaintBrush/PenTool.h +++ b/Applications/PaintBrush/PenTool.h @@ -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 }; }; diff --git a/Applications/PaintBrush/Tool.h b/Applications/PaintBrush/Tool.h index 62fa299ad8..a57a79bc60 100644 --- a/Applications/PaintBrush/Tool.h +++ b/Applications/PaintBrush/Tool.h @@ -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(); } diff --git a/Applications/PaintBrush/ToolboxWidget.cpp b/Applications/PaintBrush/ToolboxWidget.cpp index 7c7d0c764d..fec67ad132 100644 --- a/Applications/PaintBrush/ToolboxWidget.cpp +++ b/Applications/PaintBrush/ToolboxWidget.cpp @@ -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; }; |