Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "PenTool.h"
28#include "PaintableWidget.h"
29#include <LibGUI/Action.h>
30#include <LibGUI/Menu.h>
31#include <LibGUI/Painter.h>
32
33PenTool::PenTool()
34{
35}
36
37PenTool::~PenTool()
38{
39}
40
41void PenTool::on_mousedown(GUI::MouseEvent& event)
42{
43 if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
44 return;
45
46 GUI::Painter painter(m_widget->bitmap());
47 painter.draw_line(event.position(), event.position(), m_widget->color_for(event), m_thickness);
48 m_widget->update();
49 m_last_drawing_event_position = event.position();
50}
51
52void PenTool::on_mouseup(GUI::MouseEvent& event)
53{
54 if (event.button() == GUI::MouseButton::Left || event.button() == GUI::MouseButton::Right)
55 m_last_drawing_event_position = { -1, -1 };
56}
57
58void PenTool::on_mousemove(GUI::MouseEvent& event)
59{
60 if (!m_widget->rect().contains(event.position()))
61 return;
62
63 if (event.buttons() & GUI::MouseButton::Left || event.buttons() & GUI::MouseButton::Right) {
64 GUI::Painter painter(m_widget->bitmap());
65
66 if (m_last_drawing_event_position != Gfx::Point(-1, -1))
67 painter.draw_line(m_last_drawing_event_position, event.position(), m_widget->color_for(event), m_thickness);
68 else
69 painter.draw_line(event.position(), event.position(), m_widget->color_for(event), m_thickness);
70 m_widget->update();
71
72 m_last_drawing_event_position = event.position();
73 }
74}
75
76void PenTool::on_contextmenu(GUI::ContextMenuEvent& event)
77{
78 if (!m_context_menu) {
79 m_context_menu = GUI::Menu::construct();
80 m_thickness_actions.set_exclusive(true);
81 auto insert_action = [&](int size, bool checked = false) {
82 auto action = GUI::Action::create(String::number(size), [this, size](auto& action) {
83 m_thickness = size;
84 action.set_checked(true);
85 });
86 action->set_checkable(true);
87 action->set_checked(checked);
88 m_thickness_actions.add_action(*action);
89 m_context_menu->add_action(move(action));
90 };
91 insert_action(1, true);
92 insert_action(2);
93 insert_action(3);
94 insert_action(4);
95 }
96 m_context_menu->popup(event.screen_position());
97}