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 "SprayTool.h"
28#include "PaintableWidget.h"
29#include <AK/Queue.h>
30#include <AK/SinglyLinkedList.h>
31#include <LibGUI/Painter.h>
32#include <LibGUI/Action.h>
33#include <LibGUI/Menu.h>
34#include <LibGfx/Bitmap.h>
35#include <stdio.h>
36#include <LibM/math.h>
37
38SprayTool::SprayTool()
39{
40 m_timer = Core::Timer::construct();
41 m_timer->on_timeout = [&]() {
42 paint_it();
43 };
44 m_timer->set_interval(200);
45}
46
47SprayTool::~SprayTool()
48{
49}
50
51static double nrand()
52{
53 return double(rand()) / double(RAND_MAX);
54}
55
56void SprayTool::paint_it()
57{
58 GUI::Painter painter(m_widget->bitmap());
59 auto& bitmap = m_widget->bitmap();
60 ASSERT(bitmap.bpp() == 32);
61 m_widget->update();
62 const double minimal_radius = 10;
63 const double base_radius = minimal_radius * m_thickness;
64 for (int i = 0; i < 100 + (nrand() * 800); i++) {
65 double radius = base_radius * nrand();
66 double angle = 2 * M_PI * nrand();
67 const int xpos = m_last_pos.x() + radius * cos(angle);
68 const int ypos = m_last_pos.y() - radius * sin(angle);
69 if (xpos < 0 || xpos >= bitmap.width())
70 continue;
71 if (ypos < 0 || ypos >= bitmap.height())
72 continue;
73 bitmap.set_pixel<Gfx::BitmapFormat::RGB32>(xpos, ypos, m_color);
74 }
75}
76
77void SprayTool::on_mousedown(GUI::MouseEvent& event)
78{
79 if (!m_widget->rect().contains(event.position()))
80 return;
81
82 m_color = m_widget->color_for(event);
83 m_last_pos = event.position();
84 m_timer->start();
85 paint_it();
86}
87
88void SprayTool::on_mousemove(GUI::MouseEvent& event)
89{
90 m_last_pos = event.position();
91 if (m_timer->is_active()) {
92 paint_it();
93 m_timer->restart(m_timer->interval());
94 }
95}
96
97void SprayTool::on_mouseup(GUI::MouseEvent&)
98{
99 m_timer->stop();
100}
101
102void SprayTool::on_contextmenu(GUI::ContextMenuEvent& event)
103{
104 if (!m_context_menu) {
105 m_context_menu = GUI::Menu::construct();
106 m_thickness_actions.set_exclusive(true);
107 auto insert_action = [&](int size, bool checked = false) {
108 auto action = GUI::Action::create(String::number(size), [this, size](auto& action) {
109 m_thickness = size;
110 action.set_checked(true);
111 });
112 action->set_checkable(true);
113 action->set_checked(checked);
114 m_thickness_actions.add_action(*action);
115 m_context_menu->add_action(move(action));
116 };
117 insert_action(1, true);
118 insert_action(2);
119 insert_action(3);
120 insert_action(4);
121 }
122 m_context_menu->popup(event.screen_position());
123}
124