2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-02-10 16:28:48 -03:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-06-16 06:33:20 -03:00
|
|
|
#include "SprayTool.h"
|
2021-09-17 04:54:55 -03:00
|
|
|
#include "../ImageEditor.h"
|
|
|
|
|
#include "../Layer.h"
|
2021-07-17 13:29:28 -03:00
|
|
|
#include <AK/Math.h>
|
2019-06-16 06:33:20 -03:00
|
|
|
#include <AK/Queue.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Action.h>
|
2020-10-15 14:57:07 -03:00
|
|
|
#include <LibGUI/BoxLayout.h>
|
|
|
|
|
#include <LibGUI/Label.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2020-05-12 18:44:46 -03:00
|
|
|
#include <LibGUI/Painter.h>
|
2021-08-05 14:36:38 -03:00
|
|
|
#include <LibGUI/ValueSlider.h>
|
2020-02-06 08:07:05 -03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2020-05-12 18:44:46 -03:00
|
|
|
|
2020-05-20 15:35:35 -03:00
|
|
|
namespace PixelPaint {
|
2019-06-16 06:33:20 -03:00
|
|
|
|
|
|
|
|
SprayTool::SprayTool()
|
|
|
|
|
{
|
2020-02-02 08:34:39 -03:00
|
|
|
m_timer = Core::Timer::construct();
|
2019-09-20 10:19:46 -03:00
|
|
|
m_timer->on_timeout = [&]() {
|
2019-06-16 06:33:20 -03:00
|
|
|
paint_it();
|
|
|
|
|
};
|
2019-09-20 10:19:46 -03:00
|
|
|
m_timer->set_interval(200);
|
2019-06-16 06:33:20 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double nrand()
|
|
|
|
|
{
|
|
|
|
|
return double(rand()) / double(RAND_MAX);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SprayTool::paint_it()
|
|
|
|
|
{
|
2020-05-12 18:44:46 -03:00
|
|
|
auto* layer = m_editor->active_layer();
|
|
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-03-07 18:37:26 -03:00
|
|
|
auto& bitmap = layer->currently_edited_bitmap();
|
2020-05-12 18:44:46 -03:00
|
|
|
GUI::Painter painter(bitmap);
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(bitmap.bpp() == 32);
|
2022-04-01 14:58:27 -03:00
|
|
|
double const minimal_radius = 2;
|
|
|
|
|
double const base_radius = minimal_radius * m_thickness;
|
2021-04-15 04:36:14 -03:00
|
|
|
for (int i = 0; i < M_PI * base_radius * base_radius * (m_density / 100.0); i++) {
|
2019-06-17 10:45:46 -03:00
|
|
|
double radius = base_radius * nrand();
|
|
|
|
|
double angle = 2 * M_PI * nrand();
|
2022-04-01 14:58:27 -03:00
|
|
|
int const xpos = m_last_pos.x() + radius * AK::cos(angle);
|
|
|
|
|
int const ypos = m_last_pos.y() - radius * AK::sin(angle);
|
2019-06-16 06:33:20 -03:00
|
|
|
if (xpos < 0 || xpos >= bitmap.width())
|
|
|
|
|
continue;
|
|
|
|
|
if (ypos < 0 || ypos >= bitmap.height())
|
|
|
|
|
continue;
|
2021-03-16 08:00:43 -03:00
|
|
|
bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(xpos, ypos, m_color);
|
2019-06-16 06:33:20 -03:00
|
|
|
}
|
2020-05-25 17:49:50 -03:00
|
|
|
|
2021-07-07 06:58:32 -03:00
|
|
|
layer->did_modify_bitmap(Gfx::IntRect::centered_on(m_last_pos, Gfx::IntSize(base_radius * 2, base_radius * 2)));
|
2019-06-16 06:33:20 -03:00
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void SprayTool::on_mousedown(Layer* layer, MouseEvent& event)
|
2019-06-16 06:33:20 -03:00
|
|
|
{
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
auto& layer_event = event.layer_event();
|
|
|
|
|
m_color = m_editor->color_for(layer_event);
|
|
|
|
|
m_last_pos = layer_event.position();
|
2019-09-20 10:19:46 -03:00
|
|
|
m_timer->start();
|
2019-06-16 06:33:20 -03:00
|
|
|
paint_it();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void SprayTool::on_mousemove(Layer* layer, MouseEvent& event)
|
2019-06-16 06:33:20 -03:00
|
|
|
{
|
2021-08-25 05:07:24 -03:00
|
|
|
if (!layer)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-08-25 05:06:00 -03:00
|
|
|
m_last_pos = event.layer_event().position();
|
2019-09-20 10:19:46 -03:00
|
|
|
if (m_timer->is_active()) {
|
2019-06-16 06:33:20 -03:00
|
|
|
paint_it();
|
2019-09-20 10:19:46 -03:00
|
|
|
m_timer->restart(m_timer->interval());
|
2019-06-16 06:33:20 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 05:07:24 -03:00
|
|
|
void SprayTool::on_mouseup(Layer*, MouseEvent&)
|
2019-06-16 06:33:20 -03:00
|
|
|
{
|
2020-10-17 13:47:34 -03:00
|
|
|
if (m_timer->is_active()) {
|
|
|
|
|
m_timer->stop();
|
2022-08-21 15:33:03 -03:00
|
|
|
m_editor->did_complete_action(tool_name());
|
2020-10-17 13:47:34 -03:00
|
|
|
}
|
2019-06-16 06:33:20 -03:00
|
|
|
}
|
2019-06-28 06:20:22 -03:00
|
|
|
|
2020-10-15 14:57:07 -03:00
|
|
|
GUI::Widget* SprayTool::get_properties_widget()
|
|
|
|
|
{
|
|
|
|
|
if (!m_properties_widget) {
|
|
|
|
|
m_properties_widget = GUI::Widget::construct();
|
|
|
|
|
m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
|
|
|
|
|
|
2021-08-03 07:46:10 -03:00
|
|
|
auto& size_container = m_properties_widget->add<GUI::Widget>();
|
|
|
|
|
size_container.set_fixed_height(20);
|
|
|
|
|
size_container.set_layout<GUI::HorizontalBoxLayout>();
|
|
|
|
|
|
|
|
|
|
auto& size_label = size_container.add<GUI::Label>("Size:");
|
|
|
|
|
size_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
|
|
|
|
size_label.set_fixed_size(80, 20);
|
|
|
|
|
|
2021-08-05 14:36:38 -03:00
|
|
|
auto& size_slider = size_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
|
2021-08-03 07:46:10 -03:00
|
|
|
size_slider.set_range(1, 20);
|
|
|
|
|
size_slider.set_value(m_thickness);
|
|
|
|
|
|
|
|
|
|
size_slider.on_change = [&](int value) {
|
2020-10-15 14:57:07 -03:00
|
|
|
m_thickness = value;
|
|
|
|
|
};
|
2021-09-03 02:03:15 -03:00
|
|
|
set_primary_slider(&size_slider);
|
2020-10-15 14:57:07 -03:00
|
|
|
|
|
|
|
|
auto& density_container = m_properties_widget->add<GUI::Widget>();
|
2020-12-29 21:23:32 -03:00
|
|
|
density_container.set_fixed_height(20);
|
2020-10-15 14:57:07 -03:00
|
|
|
density_container.set_layout<GUI::HorizontalBoxLayout>();
|
|
|
|
|
|
|
|
|
|
auto& density_label = density_container.add<GUI::Label>("Density:");
|
|
|
|
|
density_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
2020-12-29 21:23:32 -03:00
|
|
|
density_label.set_fixed_size(80, 20);
|
2020-10-15 14:57:07 -03:00
|
|
|
|
2021-08-05 14:36:38 -03:00
|
|
|
auto& density_slider = density_container.add<GUI::ValueSlider>(Orientation::Horizontal, "%");
|
2020-10-15 14:57:07 -03:00
|
|
|
density_slider.set_range(1, 100);
|
|
|
|
|
density_slider.set_value(m_density);
|
2021-08-03 07:46:10 -03:00
|
|
|
|
|
|
|
|
density_slider.on_change = [&](int value) {
|
2020-10-15 14:57:07 -03:00
|
|
|
m_density = value;
|
|
|
|
|
};
|
2021-09-03 02:03:15 -03:00
|
|
|
set_secondary_slider(&density_slider);
|
2020-10-15 14:57:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return m_properties_widget.ptr();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-12 18:44:46 -03:00
|
|
|
}
|