SprayTool.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "SprayTool.h"
  27. #include "ImageEditor.h"
  28. #include "Layer.h"
  29. #include <AK/Queue.h>
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/Menu.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/Slider.h>
  36. #include <LibGfx/Bitmap.h>
  37. #include <math.h>
  38. #include <stdio.h>
  39. namespace PixelPaint {
  40. SprayTool::SprayTool()
  41. {
  42. m_timer = Core::Timer::construct();
  43. m_timer->on_timeout = [&]() {
  44. paint_it();
  45. };
  46. m_timer->set_interval(200);
  47. }
  48. SprayTool::~SprayTool()
  49. {
  50. }
  51. static double nrand()
  52. {
  53. return double(rand()) / double(RAND_MAX);
  54. }
  55. void SprayTool::paint_it()
  56. {
  57. auto* layer = m_editor->active_layer();
  58. if (!layer)
  59. return;
  60. auto& bitmap = layer->bitmap();
  61. GUI::Painter painter(bitmap);
  62. VERIFY(bitmap.bpp() == 32);
  63. m_editor->update();
  64. const double minimal_radius = 2;
  65. const double base_radius = minimal_radius * m_thickness;
  66. for (int i = 0; i < M_PI * base_radius * base_radius * (m_density / 100.0); i++) {
  67. double radius = base_radius * nrand();
  68. double angle = 2 * M_PI * nrand();
  69. const int xpos = m_last_pos.x() + radius * cos(angle);
  70. const int ypos = m_last_pos.y() - radius * sin(angle);
  71. if (xpos < 0 || xpos >= bitmap.width())
  72. continue;
  73. if (ypos < 0 || ypos >= bitmap.height())
  74. continue;
  75. bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(xpos, ypos, m_color);
  76. }
  77. layer->did_modify_bitmap(*m_editor->image());
  78. }
  79. void SprayTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
  80. {
  81. m_color = m_editor->color_for(event);
  82. m_last_pos = event.position();
  83. m_timer->start();
  84. paint_it();
  85. }
  86. void SprayTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
  87. {
  88. m_last_pos = event.position();
  89. if (m_timer->is_active()) {
  90. paint_it();
  91. m_timer->restart(m_timer->interval());
  92. }
  93. }
  94. void SprayTool::on_mouseup(Layer&, GUI::MouseEvent&, GUI::MouseEvent&)
  95. {
  96. if (m_timer->is_active()) {
  97. m_timer->stop();
  98. m_editor->did_complete_action();
  99. }
  100. }
  101. void SprayTool::on_tool_button_contextmenu(GUI::ContextMenuEvent& event)
  102. {
  103. if (!m_context_menu) {
  104. m_context_menu = GUI::Menu::construct();
  105. m_thickness_actions.set_exclusive(true);
  106. auto insert_action = [&](int size, bool checked = false) {
  107. auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
  108. m_thickness = size;
  109. });
  110. action->set_checked(checked);
  111. m_thickness_actions.add_action(*action);
  112. m_context_menu->add_action(move(action));
  113. };
  114. insert_action(1, true);
  115. insert_action(2);
  116. insert_action(3);
  117. insert_action(4);
  118. }
  119. m_context_menu->popup(event.screen_position());
  120. }
  121. GUI::Widget* SprayTool::get_properties_widget()
  122. {
  123. if (!m_properties_widget) {
  124. m_properties_widget = GUI::Widget::construct();
  125. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  126. auto& thickness_container = m_properties_widget->add<GUI::Widget>();
  127. thickness_container.set_fixed_height(20);
  128. thickness_container.set_layout<GUI::HorizontalBoxLayout>();
  129. auto& thickness_label = thickness_container.add<GUI::Label>("Thickness:");
  130. thickness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  131. thickness_label.set_fixed_size(80, 20);
  132. auto& thickness_slider = thickness_container.add<GUI::HorizontalSlider>();
  133. thickness_slider.set_fixed_height(20);
  134. thickness_slider.set_range(1, 20);
  135. thickness_slider.set_value(m_thickness);
  136. thickness_slider.on_change = [this](int value) {
  137. m_thickness = value;
  138. };
  139. auto& density_container = m_properties_widget->add<GUI::Widget>();
  140. density_container.set_fixed_height(20);
  141. density_container.set_layout<GUI::HorizontalBoxLayout>();
  142. auto& density_label = density_container.add<GUI::Label>("Density:");
  143. density_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  144. density_label.set_fixed_size(80, 20);
  145. auto& density_slider = density_container.add<GUI::HorizontalSlider>();
  146. density_slider.set_fixed_height(30);
  147. density_slider.set_range(1, 100);
  148. density_slider.set_value(m_density);
  149. density_slider.on_change = [this](int value) {
  150. m_density = value;
  151. };
  152. }
  153. return m_properties_widget.ptr();
  154. }
  155. }