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 "LineTool.h"
28#include "PaintableWidget.h"
29#include <LibGUI/Action.h>
30#include <LibGUI/Menu.h>
31#include <LibGUI/Painter.h>
32#include <LibM/math.h>
33
34static Gfx::Point constrain_line_angle(const Gfx::Point& start_pos, const Gfx::Point& end_pos, float angle_increment)
35{
36 float current_angle = atan2(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + M_PI * 2.;
37
38 float constrained_angle = ((int)((current_angle + angle_increment / 2.) / angle_increment)) * angle_increment;
39
40 auto diff = end_pos - start_pos;
41 float line_length = sqrt(diff.x() * diff.x() + diff.y() * diff.y());
42
43 return { start_pos.x() + (int)(cos(constrained_angle) * line_length),
44 start_pos.y() + (int)(sin(constrained_angle) * line_length) };
45}
46
47LineTool::LineTool()
48{
49}
50
51LineTool::~LineTool()
52{
53}
54
55void LineTool::on_mousedown(GUI::MouseEvent& event)
56{
57 if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
58 return;
59
60 if (m_drawing_button != GUI::MouseButton::None)
61 return;
62
63 m_drawing_button = event.button();
64 m_line_start_position = event.position();
65 m_line_end_position = event.position();
66 m_widget->update();
67}
68
69void LineTool::on_mouseup(GUI::MouseEvent& event)
70{
71 if (event.button() == m_drawing_button) {
72 GUI::Painter painter(m_widget->bitmap());
73 painter.draw_line(m_line_start_position, m_line_end_position, m_widget->color_for(m_drawing_button), m_thickness);
74 m_drawing_button = GUI::MouseButton::None;
75 m_widget->update();
76 }
77}
78
79void LineTool::on_mousemove(GUI::MouseEvent& event)
80{
81 if (m_drawing_button == GUI::MouseButton::None)
82 return;
83
84 if (!m_widget->rect().contains(event.position()))
85 return;
86
87 if (!m_constrain_angle) {
88 m_line_end_position = event.position();
89 } else {
90 const float ANGLE_STEP = M_PI / 8.0f;
91 m_line_end_position = constrain_line_angle(m_line_start_position, event.position(), ANGLE_STEP);
92 }
93 m_widget->update();
94}
95
96void LineTool::on_second_paint(GUI::PaintEvent& event)
97{
98 if (m_drawing_button == GUI::MouseButton::None)
99 return;
100
101 GUI::Painter painter(*m_widget);
102 painter.add_clip_rect(event.rect());
103 painter.draw_line(m_line_start_position, m_line_end_position, m_widget->color_for(m_drawing_button), m_thickness);
104}
105
106void LineTool::on_keydown(GUI::KeyEvent& event)
107{
108 if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
109 m_drawing_button = GUI::MouseButton::None;
110 m_widget->update();
111 event.accept();
112 }
113
114 if (event.key() == Key_Shift) {
115 m_constrain_angle = true;
116 m_widget->update();
117 event.accept();
118 }
119}
120
121void LineTool::on_keyup(GUI::KeyEvent& event)
122{
123 if (event.key() == Key_Shift) {
124 m_constrain_angle = false;
125 m_widget->update();
126 event.accept();
127 }
128}
129
130void LineTool::on_contextmenu(GUI::ContextMenuEvent& event)
131{
132 if (!m_context_menu) {
133 m_context_menu = GUI::Menu::construct();
134 m_thickness_actions.set_exclusive(true);
135 auto insert_action = [&](int size, bool checked = false) {
136 auto action = GUI::Action::create(String::number(size), [this, size](auto& action) {
137 m_thickness = size;
138 action.set_checked(true);
139 });
140 action->set_checkable(true);
141 action->set_checked(checked);
142 m_thickness_actions.add_action(*action);
143 m_context_menu->add_action(move(action));
144 };
145 insert_action(1, true);
146 insert_action(2);
147 insert_action(3);
148 insert_action(4);
149 }
150 m_context_menu->popup(event.screen_position());
151}