RectangleTool.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2022, MacDue <macdue@dueutil.tech>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "RectangleTool.h"
  10. #include "../ImageEditor.h"
  11. #include "../Layer.h"
  12. #include <LibGUI/Action.h>
  13. #include <LibGUI/BoxLayout.h>
  14. #include <LibGUI/CheckBox.h>
  15. #include <LibGUI/Label.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGUI/RadioButton.h>
  19. #include <LibGUI/TextBox.h>
  20. #include <LibGUI/ValueSlider.h>
  21. #include <LibGfx/AntiAliasingPainter.h>
  22. #include <LibGfx/Rect.h>
  23. namespace PixelPaint {
  24. void RectangleTool::draw_using(GUI::Painter& painter, Gfx::IntPoint start_position, Gfx::IntPoint end_position, int thickness, int corner_radius)
  25. {
  26. Gfx::IntRect rect;
  27. if (m_draw_mode == DrawMode::FromCenter) {
  28. auto delta = end_position - start_position;
  29. rect = Gfx::IntRect::from_two_points(start_position - delta, end_position);
  30. } else {
  31. rect = Gfx::IntRect::from_two_points(start_position, end_position);
  32. }
  33. switch (m_fill_mode) {
  34. case FillMode::Fill:
  35. painter.fill_rect(rect, m_editor->color_for(m_drawing_button));
  36. break;
  37. case FillMode::Outline:
  38. painter.draw_rect_with_thickness(rect, m_editor->color_for(m_drawing_button), thickness);
  39. break;
  40. case FillMode::Gradient:
  41. painter.fill_rect_with_gradient(rect, m_editor->primary_color(), m_editor->secondary_color());
  42. break;
  43. case FillMode::RoundedCorners: {
  44. int min_dimension = corner_radius * 2;
  45. if (rect.width() < min_dimension)
  46. rect.set_width(min_dimension);
  47. if (rect.height() < min_dimension)
  48. rect.set_height(min_dimension);
  49. if (m_antialias_enabled) {
  50. Gfx::AntiAliasingPainter aa_painter { painter };
  51. aa_painter.fill_rect_with_rounded_corners(rect, m_editor->color_for(m_drawing_button), corner_radius);
  52. } else {
  53. painter.fill_rect_with_rounded_corners(rect, m_editor->color_for(m_drawing_button), corner_radius);
  54. }
  55. break;
  56. }
  57. default:
  58. VERIFY_NOT_REACHED();
  59. }
  60. }
  61. void RectangleTool::on_mousedown(Layer* layer, MouseEvent& event)
  62. {
  63. if (!layer)
  64. return;
  65. auto& layer_event = event.layer_event();
  66. if (layer_event.button() != GUI::MouseButton::Primary && layer_event.button() != GUI::MouseButton::Secondary)
  67. return;
  68. if (m_drawing_button != GUI::MouseButton::None)
  69. return;
  70. m_drawing_button = layer_event.button();
  71. m_rectangle_start_position = layer_event.position();
  72. m_rectangle_end_position = layer_event.position();
  73. m_editor->update();
  74. }
  75. void RectangleTool::on_mouseup(Layer* layer, MouseEvent& event)
  76. {
  77. if (!layer)
  78. return;
  79. if (event.layer_event().button() == m_drawing_button) {
  80. GUI::Painter painter(layer->get_scratch_edited_bitmap());
  81. draw_using(painter, m_rectangle_start_position, m_rectangle_end_position, m_thickness, m_corner_radius);
  82. m_drawing_button = GUI::MouseButton::None;
  83. auto modified_rect = Gfx::IntRect::from_two_points(m_rectangle_start_position, m_rectangle_end_position).inflated(m_thickness * 2, m_thickness * 2);
  84. layer->did_modify_bitmap(modified_rect);
  85. m_editor->update();
  86. m_editor->did_complete_action(tool_name());
  87. }
  88. }
  89. void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event)
  90. {
  91. if (!layer)
  92. return;
  93. if (m_drawing_button == GUI::MouseButton::None)
  94. return;
  95. m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner;
  96. if (event.layer_event().shift())
  97. m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0);
  98. else if (m_aspect_ratio.has_value())
  99. m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), m_aspect_ratio.value());
  100. else
  101. m_rectangle_end_position = event.layer_event().position();
  102. m_editor->update();
  103. }
  104. void RectangleTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
  105. {
  106. if (!layer || m_drawing_button == GUI::MouseButton::None)
  107. return;
  108. GUI::Painter painter(*m_editor);
  109. painter.add_clip_rect(event.rect());
  110. painter.translate(editor_layer_location(*layer));
  111. auto start_position = editor_stroke_position(m_rectangle_start_position, m_thickness);
  112. auto end_position = editor_stroke_position(m_rectangle_end_position, m_thickness);
  113. draw_using(painter, start_position, end_position, AK::max(m_thickness * m_editor->scale(), 1), m_corner_radius * m_editor->scale());
  114. }
  115. bool RectangleTool::on_keydown(GUI::KeyEvent& event)
  116. {
  117. if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
  118. m_drawing_button = GUI::MouseButton::None;
  119. m_editor->update();
  120. return true;
  121. }
  122. return Tool::on_keydown(event);
  123. }
  124. ErrorOr<GUI::Widget*> RectangleTool::get_properties_widget()
  125. {
  126. if (!m_properties_widget) {
  127. auto properties_widget = TRY(GUI::Widget::try_create());
  128. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  129. auto thickness_or_radius_container = TRY(properties_widget->try_add<GUI::Widget>());
  130. thickness_or_radius_container->set_fixed_height(20);
  131. (void)TRY(thickness_or_radius_container->try_set_layout<GUI::HorizontalBoxLayout>());
  132. auto thickness_or_radius_label = TRY(thickness_or_radius_container->try_add<GUI::Label>());
  133. thickness_or_radius_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  134. thickness_or_radius_label->set_fixed_size(80, 20);
  135. auto thickness_or_radius_slider = TRY(thickness_or_radius_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, String::from_utf8_short_string("px"sv)));
  136. thickness_or_radius_slider->on_change = [&](int value) {
  137. if (m_fill_mode == FillMode::RoundedCorners) {
  138. m_corner_radius = value;
  139. } else
  140. m_thickness = value;
  141. };
  142. auto update_slider = [this, thickness_or_radius_label, thickness_or_radius_slider] {
  143. auto update_values = [&](auto label, int value, int range_min, int range_max = 10) {
  144. thickness_or_radius_label->set_text(label);
  145. thickness_or_radius_slider->set_range(range_min, range_max);
  146. thickness_or_radius_slider->set_value(value);
  147. };
  148. if (m_fill_mode == FillMode::RoundedCorners)
  149. update_values("Radius:", m_corner_radius, 0, 50);
  150. else
  151. update_values("Thickness:", m_thickness, 1);
  152. };
  153. update_slider();
  154. set_primary_slider(thickness_or_radius_slider);
  155. auto mode_container = TRY(properties_widget->try_add<GUI::Widget>());
  156. mode_container->set_fixed_height(90);
  157. (void)TRY(mode_container->try_set_layout<GUI::HorizontalBoxLayout>());
  158. auto mode_label = TRY(mode_container->try_add<GUI::Label>("Mode:"));
  159. mode_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  160. mode_label->set_fixed_size(30, 20);
  161. auto mode_radio_container = TRY(mode_container->try_add<GUI::Widget>());
  162. (void)TRY(mode_radio_container->try_set_layout<GUI::VerticalBoxLayout>());
  163. auto outline_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>(String::from_utf8_short_string("Outline"sv)));
  164. auto fill_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>(String::from_utf8_short_string("Fill"sv)));
  165. auto gradient_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>(TRY(String::from_utf8("Gradient"sv))));
  166. mode_radio_container->set_fixed_width(70);
  167. auto rounded_corners_mode_radio = TRY(mode_radio_container->try_add<GUI::RadioButton>(String::from_utf8_short_string("Rounded"sv)));
  168. outline_mode_radio->on_checked = [this, update_slider](bool) {
  169. m_fill_mode = FillMode::Outline;
  170. update_slider();
  171. };
  172. fill_mode_radio->on_checked = [this, update_slider](bool) {
  173. m_fill_mode = FillMode::Fill;
  174. update_slider();
  175. };
  176. gradient_mode_radio->on_checked = [this, update_slider](bool) {
  177. m_fill_mode = FillMode::Gradient;
  178. update_slider();
  179. };
  180. rounded_corners_mode_radio->on_checked = [this, update_slider](bool) {
  181. m_fill_mode = FillMode::RoundedCorners;
  182. update_slider();
  183. };
  184. outline_mode_radio->set_checked(true);
  185. auto mode_extras_container = TRY(mode_container->try_add<GUI::Widget>());
  186. (void)TRY(mode_extras_container->try_set_layout<GUI::VerticalBoxLayout>());
  187. auto aa_enable_checkbox = TRY(mode_extras_container->try_add<GUI::CheckBox>(TRY(String::from_utf8("Anti-alias"sv))));
  188. aa_enable_checkbox->on_checked = [this](bool checked) {
  189. m_antialias_enabled = checked;
  190. };
  191. aa_enable_checkbox->set_checked(true);
  192. auto aspect_container = TRY(mode_extras_container->try_add<GUI::Widget>());
  193. (void)TRY(aspect_container->try_set_layout<GUI::VerticalBoxLayout>());
  194. aspect_container->set_fixed_width(75);
  195. auto aspect_label = TRY(aspect_container->try_add<GUI::Label>("Aspect Ratio:"));
  196. aspect_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  197. aspect_label->set_fixed_size(75, 20);
  198. auto aspect_fields_container = TRY(aspect_container->try_add<GUI::Widget>());
  199. aspect_fields_container->set_fixed_width(75);
  200. (void)TRY(aspect_fields_container->try_set_layout<GUI::HorizontalBoxLayout>());
  201. m_aspect_w_textbox = TRY(aspect_fields_container->try_add<GUI::TextBox>());
  202. m_aspect_w_textbox->set_fixed_height(20);
  203. m_aspect_w_textbox->set_fixed_width(25);
  204. m_aspect_w_textbox->on_change = [this] {
  205. auto x = m_aspect_w_textbox->text().to_int().value_or(0);
  206. auto y = m_aspect_h_textbox->text().to_int().value_or(0);
  207. if (x > 0 && y > 0) {
  208. m_aspect_ratio = (float)x / (float)y;
  209. } else {
  210. m_aspect_ratio = {};
  211. }
  212. };
  213. auto multiply_label = TRY(aspect_fields_container->try_add<GUI::Label>("x"));
  214. multiply_label->set_text_alignment(Gfx::TextAlignment::Center);
  215. multiply_label->set_fixed_size(10, 20);
  216. m_aspect_h_textbox = TRY(aspect_fields_container->try_add<GUI::TextBox>());
  217. m_aspect_h_textbox->set_fixed_height(20);
  218. m_aspect_h_textbox->set_fixed_width(25);
  219. m_aspect_h_textbox->on_change = [this] { m_aspect_w_textbox->on_change(); };
  220. m_properties_widget = properties_widget;
  221. }
  222. return m_properties_widget.ptr();
  223. }
  224. }