SprayTool.cpp 4.6 KB

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