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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
/*
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CloneTool.h"
#include "../ImageEditor.h"
#include "../Layer.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Label.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Painter.h>
#include <LibGUI/ValueSlider.h>
#include <LibGfx/Bitmap.h>
namespace PixelPaint {
void CloneTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color, Gfx::IntPoint point)
{
if (!m_sample_location.has_value())
return;
auto source_point = point - m_cursor_offset.value();
for (int y = -size(); y < size(); y++) {
for (int x = -size(); x < size(); x++) {
auto target_x = point.x() + x;
auto target_y = point.y() + y;
auto distance = point.distance_from({ target_x, target_y });
if (target_x < 0 || target_x >= bitmap.width() || target_y < 0 || target_y >= bitmap.height())
continue;
if (distance >= size())
continue;
auto source_x = source_point.x() + x;
auto source_y = source_point.y() + y;
if (source_x < 0 || source_x >= bitmap.width() || source_y < 0 || source_y >= bitmap.height())
continue;
auto falloff = get_falloff(distance);
auto pixel_color = bitmap.get_pixel(source_x, source_y);
pixel_color.set_alpha(falloff * pixel_color.alpha());
bitmap.set_pixel(target_x, target_y, bitmap.get_pixel(target_x, target_y).blend(pixel_color));
}
}
}
void CloneTool::draw_line(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint start, Gfx::IntPoint end)
{
if (!m_sample_location.has_value())
return;
BrushTool::draw_line(bitmap, color, start, end);
}
Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> CloneTool::cursor()
{
if (m_is_selecting_location)
return Gfx::StandardCursor::Eyedropper;
return Gfx::StandardCursor::Crosshair;
}
void CloneTool::on_mousemove(Layer* layer, MouseEvent& event)
{
auto& image_event = event.image_event();
if (image_event.alt())
return;
if (m_cursor_offset.has_value()) {
auto old_sample_marker_rect = sample_marker_rect();
m_sample_location = image_event.position() - m_cursor_offset.value();
update_sample_marker(old_sample_marker_rect);
}
BrushTool::on_mousemove(layer, event);
}
void CloneTool::on_mousedown(Layer* layer, MouseEvent& event)
{
auto& image_event = event.image_event();
if (image_event.alt()) {
auto old_sample_marker_rect = sample_marker_rect();
m_sample_location = image_event.position();
m_cursor_offset = {};
update_sample_marker(old_sample_marker_rect);
return;
}
if (!m_sample_location.has_value())
return;
if (!m_cursor_offset.has_value())
m_cursor_offset = event.image_event().position() - m_sample_location.value();
BrushTool::on_mousedown(layer, event);
}
void CloneTool::on_second_paint(Layer const*, GUI::PaintEvent& event)
{
if (!m_sample_location.has_value())
return;
GUI::Painter painter(*m_editor);
painter.add_clip_rect(event.rect());
auto rect = sample_marker_rect();
painter.draw_ellipse_intersecting(rect.value(), m_marker_color, 1);
}
bool CloneTool::on_keydown(GUI::KeyEvent& event)
{
if (event.key() == KeyCode::Key_Alt && !m_is_selecting_location) {
m_is_selecting_location = true;
m_editor->update_tool_cursor();
return true;
}
return Tool::on_keydown(event);
}
void CloneTool::on_keyup(GUI::KeyEvent& event)
{
if (m_is_selecting_location && event.key() == KeyCode::Key_Alt) {
m_is_selecting_location = false;
m_editor->update_tool_cursor();
return;
}
}
ErrorOr<GUI::Widget*> CloneTool::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 size_container = TRY(properties_widget->try_add<GUI::Widget>());
size_container->set_fixed_height(20);
(void)TRY(size_container->try_set_layout<GUI::HorizontalBoxLayout>());
auto size_label = TRY(size_container->try_add<GUI::Label>("Size:"_short_string));
size_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
size_label->set_fixed_size(80, 20);
auto size_slider = TRY(size_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "px"_short_string));
size_slider->set_range(1, 100);
size_slider->set_value(size());
size_slider->on_change = [this](int value) {
auto old_sample_marker_rect = sample_marker_rect();
set_size(value);
update_sample_marker(old_sample_marker_rect);
};
set_primary_slider(size_slider);
auto hardness_container = TRY(properties_widget->try_add<GUI::Widget>());
hardness_container->set_fixed_height(20);
(void)TRY(hardness_container->try_set_layout<GUI::HorizontalBoxLayout>());
auto hardness_label = TRY(hardness_container->try_add<GUI::Label>(TRY("Hardness:"_string)));
hardness_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
hardness_label->set_fixed_size(80, 20);
auto hardness_slider = TRY(hardness_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, "%"_short_string));
hardness_slider->set_range(1, 100);
hardness_slider->on_change = [&](int value) {
set_hardness(value);
};
hardness_slider->set_value(100);
set_secondary_slider(hardness_slider);
m_properties_widget = properties_widget;
}
return m_properties_widget.ptr();
}
Optional<Gfx::IntRect> CloneTool::sample_marker_rect()
{
if (!m_sample_location.has_value())
return {};
auto offset = AK::max(2, size());
Gfx::IntRect content_rect = {
m_sample_location.value().x() - offset,
m_sample_location.value().y() - offset,
offset * 2,
offset * 2
};
return m_editor->content_to_frame_rect(content_rect).to_type<int>();
}
void CloneTool::update_sample_marker(Optional<Gfx::IntRect> old_rect)
{
if (old_rect.has_value())
m_editor->update(old_rect.value().inflated(2, 2));
auto current_rect = sample_marker_rect();
if (current_rect.has_value())
m_editor->update(current_rect.value().inflated(2, 2));
}
}
|