summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/Tools/LineTool.cpp
blob: a6cfc85caaebd3d067bc443946334a80ebf577ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
 * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "LineTool.h"
#include "../ImageEditor.h"
#include "../Layer.h"
#include <AK/Math.h>
#include <LibGUI/Action.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ValueSlider.h>
#include <LibGfx/AntiAliasingPainter.h>

namespace PixelPaint {

void LineTool::on_mousedown(Layer* layer, MouseEvent& event)
{
    if (!layer)
        return;

    auto& layer_event = event.layer_event();
    if (layer_event.button() != GUI::MouseButton::Primary && layer_event.button() != GUI::MouseButton::Secondary)
        return;

    if (m_drawing_button != GUI::MouseButton::None)
        return;

    m_drawing_button = layer_event.button();

    m_drag_start_position = layer_event.position();
    m_line_start_position = layer_event.position();
    m_line_end_position = layer_event.position();

    m_editor->update();
}

void LineTool::draw_using(GUI::Painter& painter, Gfx::IntPoint start_position, Gfx::IntPoint end_position, Color color, int thickness)
{
    if (m_antialias_enabled) {
        Gfx::AntiAliasingPainter aa_painter { painter };
        auto as_float_point = [](auto const& point) {
            return Gfx::FloatPoint { point.x(), point.y() };
        };
        aa_painter.draw_line(as_float_point(start_position), as_float_point(end_position), color, thickness);
    } else {
        painter.draw_line(start_position, end_position, color, thickness);
    }
}

void LineTool::on_mouseup(Layer* layer, MouseEvent& event)
{
    if (!layer)
        return;

    auto& layer_event = event.layer_event();
    if (layer_event.button() == m_drawing_button) {
        GUI::Painter painter(layer->get_scratch_edited_bitmap());
        draw_using(painter, m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
        m_drawing_button = GUI::MouseButton::None;
        auto modified_rect = Gfx::IntRect::from_two_points(m_line_start_position, m_line_end_position).inflated(m_thickness * 2, m_thickness * 2);
        layer->did_modify_bitmap(modified_rect);
        m_editor->update();
        m_editor->did_complete_action(tool_name());
    }
}

void LineTool::on_mousemove(Layer* layer, MouseEvent& event)
{
    if (!layer)
        return;

    auto& layer_event = event.layer_event();
    if (m_drawing_button == GUI::MouseButton::None)
        return;

    if (layer_event.shift())
        m_line_end_position = constrain_line_angle(m_drag_start_position, layer_event.position());
    else
        m_line_end_position = layer_event.position();

    if (layer_event.alt()) {
        m_line_start_position = m_drag_start_position + (m_drag_start_position - m_line_end_position);
    } else {
        m_line_start_position = m_drag_start_position;
    }

    m_editor->update();
}

void LineTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
{
    if (!layer || m_drawing_button == GUI::MouseButton::None)
        return;

    GUI::Painter painter(*m_editor);
    painter.add_clip_rect(event.rect());
    painter.translate(editor_layer_location(*layer));
    auto preview_start = editor_stroke_position(m_line_start_position, m_thickness);
    auto preview_end = editor_stroke_position(m_line_end_position, m_thickness);
    draw_using(painter, preview_start, preview_end, m_editor->color_for(m_drawing_button), AK::max(m_thickness * m_editor->scale(), 1));
}

bool LineTool::on_keydown(GUI::KeyEvent& event)
{
    if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
        m_drawing_button = GUI::MouseButton::None;
        m_editor->update();
        return true;
    }
    return Tool::on_keydown(event);
}

ErrorOr<GUI::Widget*> LineTool::get_properties_widget()
{
    if (!m_properties_widget) {
        auto properties_widget = TRY(GUI::Widget::try_create());
        (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());

        auto thickness_container = TRY(properties_widget->try_add<GUI::Widget>());
        thickness_container->set_fixed_height(20);
        (void)TRY(thickness_container->try_set_layout<GUI::HorizontalBoxLayout>());

        auto thickness_label = TRY(thickness_container->try_add<GUI::Label>("Thickness:"));
        thickness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
        thickness_label->set_fixed_size(80, 20);

        auto thickness_slider = TRY(thickness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string));
        thickness_slider->set_range(1, 10);
        thickness_slider->set_value(m_thickness);

        thickness_slider->on_change = [this](int value) {
            m_thickness = value;
        };
        set_primary_slider(thickness_slider);

        auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
        mode_container->set_fixed_height(20);
        (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());

        auto mode_label = TRY(mode_container->try_add<GUI::Label>("Mode:"));
        mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
        mode_label->set_fixed_size(80, 20);

        auto aa_enable_checkbox = TRY(mode_container->try_add<GUI::CheckBox>(TRY("Anti-alias"_string)));
        aa_enable_checkbox->on_checked = [this](bool checked) {
            m_antialias_enabled = checked;
        };
        aa_enable_checkbox->set_checked(true);
        m_properties_widget = properties_widget;
    }

    return m_properties_widget.ptr();
}

}