Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
4 * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
5 * Copyright (c) 2022, the SerenityOS developers.
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10#include "LineTool.h"
11#include "../ImageEditor.h"
12#include "../Layer.h"
13#include <AK/Math.h>
14#include <LibGUI/Action.h>
15#include <LibGUI/BoxLayout.h>
16#include <LibGUI/CheckBox.h>
17#include <LibGUI/Label.h>
18#include <LibGUI/Menu.h>
19#include <LibGUI/Painter.h>
20#include <LibGUI/ValueSlider.h>
21#include <LibGfx/AntiAliasingPainter.h>
22
23namespace PixelPaint {
24
25void LineTool::on_mousedown(Layer* layer, MouseEvent& event)
26{
27 if (!layer)
28 return;
29
30 auto& layer_event = event.layer_event();
31 if (layer_event.button() != GUI::MouseButton::Primary && layer_event.button() != GUI::MouseButton::Secondary)
32 return;
33
34 if (m_drawing_button != GUI::MouseButton::None)
35 return;
36
37 m_drawing_button = layer_event.button();
38
39 m_drag_start_position = layer_event.position();
40 m_line_start_position = layer_event.position();
41 m_line_end_position = layer_event.position();
42
43 m_editor->update();
44}
45
46void LineTool::draw_using(GUI::Painter& painter, Gfx::IntPoint start_position, Gfx::IntPoint end_position, Color color, int thickness)
47{
48 if (m_antialias_enabled) {
49 Gfx::AntiAliasingPainter aa_painter { painter };
50 auto as_float_point = [](auto const& point) {
51 return Gfx::FloatPoint { point.x(), point.y() };
52 };
53 aa_painter.draw_line(as_float_point(start_position), as_float_point(end_position), color, thickness);
54 } else {
55 painter.draw_line(start_position, end_position, color, thickness);
56 }
57}
58
59void LineTool::on_mouseup(Layer* layer, MouseEvent& event)
60{
61 if (!layer)
62 return;
63
64 auto& layer_event = event.layer_event();
65 if (layer_event.button() == m_drawing_button) {
66 GUI::Painter painter(layer->get_scratch_edited_bitmap());
67 draw_using(painter, m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
68 m_drawing_button = GUI::MouseButton::None;
69 auto modified_rect = Gfx::IntRect::from_two_points(m_line_start_position, m_line_end_position).inflated(m_thickness * 2, m_thickness * 2);
70 layer->did_modify_bitmap(modified_rect);
71 m_editor->update();
72 m_editor->did_complete_action(tool_name());
73 }
74}
75
76void LineTool::on_mousemove(Layer* layer, MouseEvent& event)
77{
78 if (!layer)
79 return;
80
81 auto& layer_event = event.layer_event();
82 if (m_drawing_button == GUI::MouseButton::None)
83 return;
84
85 if (layer_event.shift())
86 m_line_end_position = constrain_line_angle(m_drag_start_position, layer_event.position());
87 else
88 m_line_end_position = layer_event.position();
89
90 if (layer_event.alt()) {
91 m_line_start_position = m_drag_start_position + (m_drag_start_position - m_line_end_position);
92 } else {
93 m_line_start_position = m_drag_start_position;
94 }
95
96 m_editor->update();
97}
98
99void LineTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
100{
101 if (!layer || m_drawing_button == GUI::MouseButton::None)
102 return;
103
104 GUI::Painter painter(*m_editor);
105 painter.add_clip_rect(event.rect());
106 painter.translate(editor_layer_location(*layer));
107 auto preview_start = editor_stroke_position(m_line_start_position, m_thickness);
108 auto preview_end = editor_stroke_position(m_line_end_position, m_thickness);
109 draw_using(painter, preview_start, preview_end, m_editor->color_for(m_drawing_button), AK::max(m_thickness * m_editor->scale(), 1));
110}
111
112bool LineTool::on_keydown(GUI::KeyEvent& event)
113{
114 if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
115 m_drawing_button = GUI::MouseButton::None;
116 m_editor->update();
117 return true;
118 }
119 return Tool::on_keydown(event);
120}
121
122ErrorOr<GUI::Widget*> LineTool::get_properties_widget()
123{
124 if (!m_properties_widget) {
125 auto properties_widget = TRY(GUI::Widget::try_create());
126 (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
127
128 auto thickness_container = TRY(properties_widget->try_add<GUI::Widget>());
129 thickness_container->set_fixed_height(20);
130 (void)TRY(thickness_container->try_set_layout<GUI::HorizontalBoxLayout>());
131
132 auto thickness_label = TRY(thickness_container->try_add<GUI::Label>("Thickness:"));
133 thickness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
134 thickness_label->set_fixed_size(80, 20);
135
136 auto thickness_slider = TRY(thickness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string));
137 thickness_slider->set_range(1, 10);
138 thickness_slider->set_value(m_thickness);
139
140 thickness_slider->on_change = [this](int value) {
141 m_thickness = value;
142 };
143 set_primary_slider(thickness_slider);
144
145 auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
146 mode_container->set_fixed_height(20);
147 (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
148
149 auto mode_label = TRY(mode_container->try_add<GUI::Label>("Mode:"));
150 mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
151 mode_label->set_fixed_size(80, 20);
152
153 auto aa_enable_checkbox = TRY(mode_container->try_add<GUI::CheckBox>(TRY("Anti-alias"_string)));
154 aa_enable_checkbox->on_checked = [this](bool checked) {
155 m_antialias_enabled = checked;
156 };
157 aa_enable_checkbox->set_checked(true);
158 m_properties_widget = properties_widget;
159 }
160
161 return m_properties_widget.ptr();
162}
163
164}