RectangleTool.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "RectangleTool.h"
  8. #include "../ImageEditor.h"
  9. #include "../Layer.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/RadioButton.h>
  16. #include <LibGUI/TextBox.h>
  17. #include <LibGUI/ValueSlider.h>
  18. #include <LibGfx/Rect.h>
  19. namespace PixelPaint {
  20. RectangleTool::RectangleTool()
  21. {
  22. }
  23. RectangleTool::~RectangleTool()
  24. {
  25. }
  26. void RectangleTool::draw_using(GUI::Painter& painter, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position, int thickness)
  27. {
  28. Gfx::IntRect rect;
  29. if (m_draw_mode == DrawMode::FromCenter) {
  30. auto delta = end_position - start_position;
  31. rect = Gfx::IntRect::from_two_points(start_position - delta, end_position);
  32. } else {
  33. rect = Gfx::IntRect::from_two_points(start_position, end_position);
  34. }
  35. switch (m_fill_mode) {
  36. case FillMode::Fill:
  37. painter.fill_rect(rect, m_editor->color_for(m_drawing_button));
  38. break;
  39. case FillMode::Outline:
  40. painter.draw_rect_with_thickness(rect, m_editor->color_for(m_drawing_button), thickness);
  41. break;
  42. case FillMode::Gradient:
  43. painter.fill_rect_with_gradient(rect, m_editor->primary_color(), m_editor->secondary_color());
  44. break;
  45. default:
  46. VERIFY_NOT_REACHED();
  47. }
  48. }
  49. void RectangleTool::on_mousedown(Layer* layer, MouseEvent& event)
  50. {
  51. if (!layer)
  52. return;
  53. auto& layer_event = event.layer_event();
  54. if (layer_event.button() != GUI::MouseButton::Primary && layer_event.button() != GUI::MouseButton::Secondary)
  55. return;
  56. if (m_drawing_button != GUI::MouseButton::None)
  57. return;
  58. m_drawing_button = layer_event.button();
  59. m_rectangle_start_position = layer_event.position();
  60. m_rectangle_end_position = layer_event.position();
  61. m_editor->update();
  62. }
  63. void RectangleTool::on_mouseup(Layer* layer, MouseEvent& event)
  64. {
  65. if (!layer)
  66. return;
  67. if (event.layer_event().button() == m_drawing_button) {
  68. GUI::Painter painter(layer->bitmap());
  69. draw_using(painter, m_rectangle_start_position, m_rectangle_end_position, m_thickness);
  70. m_drawing_button = GUI::MouseButton::None;
  71. layer->did_modify_bitmap();
  72. m_editor->update();
  73. m_editor->did_complete_action();
  74. }
  75. }
  76. void RectangleTool::on_mousemove(Layer* layer, MouseEvent& event)
  77. {
  78. if (!layer)
  79. return;
  80. if (m_drawing_button == GUI::MouseButton::None)
  81. return;
  82. m_draw_mode = event.layer_event().alt() ? DrawMode::FromCenter : DrawMode::FromCorner;
  83. if (event.layer_event().shift())
  84. m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), 1.0);
  85. else if (m_aspect_ratio.has_value())
  86. m_rectangle_end_position = m_rectangle_start_position.end_point_for_aspect_ratio(event.layer_event().position(), m_aspect_ratio.value());
  87. else
  88. m_rectangle_end_position = event.layer_event().position();
  89. m_editor->update();
  90. }
  91. void RectangleTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
  92. {
  93. if (!layer || m_drawing_button == GUI::MouseButton::None)
  94. return;
  95. GUI::Painter painter(*m_editor);
  96. painter.add_clip_rect(event.rect());
  97. auto start_position = editor_stroke_position(m_rectangle_start_position, m_thickness);
  98. auto end_position = editor_stroke_position(m_rectangle_end_position, m_thickness);
  99. draw_using(painter, start_position, end_position, AK::max(m_thickness * m_editor->scale(), 1));
  100. }
  101. void RectangleTool::on_keydown(GUI::KeyEvent& event)
  102. {
  103. Tool::on_keydown(event);
  104. if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
  105. m_drawing_button = GUI::MouseButton::None;
  106. m_editor->update();
  107. event.accept();
  108. }
  109. }
  110. GUI::Widget* RectangleTool::get_properties_widget()
  111. {
  112. if (!m_properties_widget) {
  113. m_properties_widget = GUI::Widget::construct();
  114. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  115. auto& thickness_container = m_properties_widget->add<GUI::Widget>();
  116. thickness_container.set_fixed_height(20);
  117. thickness_container.set_layout<GUI::HorizontalBoxLayout>();
  118. auto& thickness_label = thickness_container.add<GUI::Label>("Thickness:");
  119. thickness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  120. thickness_label.set_fixed_size(80, 20);
  121. auto& thickness_slider = thickness_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
  122. thickness_slider.set_range(1, 10);
  123. thickness_slider.set_value(m_thickness);
  124. thickness_slider.on_change = [&](int value) {
  125. m_thickness = value;
  126. };
  127. set_primary_slider(&thickness_slider);
  128. auto& mode_container = m_properties_widget->add<GUI::Widget>();
  129. mode_container.set_fixed_height(70);
  130. mode_container.set_layout<GUI::HorizontalBoxLayout>();
  131. auto& mode_label = mode_container.add<GUI::Label>("Mode:");
  132. mode_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  133. mode_label.set_fixed_size(80, 20);
  134. auto& mode_radio_container = mode_container.add<GUI::Widget>();
  135. mode_radio_container.set_layout<GUI::VerticalBoxLayout>();
  136. auto& outline_mode_radio = mode_radio_container.add<GUI::RadioButton>("Outline");
  137. auto& fill_mode_radio = mode_radio_container.add<GUI::RadioButton>("Fill");
  138. auto& gradient_mode_radio = mode_radio_container.add<GUI::RadioButton>("Gradient");
  139. outline_mode_radio.on_checked = [&](bool) {
  140. m_fill_mode = FillMode::Outline;
  141. };
  142. fill_mode_radio.on_checked = [&](bool) {
  143. m_fill_mode = FillMode::Fill;
  144. };
  145. gradient_mode_radio.on_checked = [&](bool) {
  146. m_fill_mode = FillMode::Gradient;
  147. };
  148. outline_mode_radio.set_checked(true);
  149. auto& aspect_container = m_properties_widget->add<GUI::Widget>();
  150. aspect_container.set_fixed_height(20);
  151. aspect_container.set_layout<GUI::HorizontalBoxLayout>();
  152. auto& aspect_label = aspect_container.add<GUI::Label>("Aspect Ratio:");
  153. aspect_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  154. aspect_label.set_fixed_size(80, 20);
  155. m_aspect_w_textbox = aspect_container.add<GUI::TextBox>();
  156. m_aspect_w_textbox->set_fixed_height(20);
  157. m_aspect_w_textbox->set_fixed_width(25);
  158. m_aspect_w_textbox->on_change = [&] {
  159. auto x = m_aspect_w_textbox->text().to_int().value_or(0);
  160. auto y = m_aspect_h_textbox->text().to_int().value_or(0);
  161. if (x > 0 && y > 0) {
  162. m_aspect_ratio = (float)x / (float)y;
  163. } else {
  164. m_aspect_ratio = {};
  165. }
  166. };
  167. auto& multiply_label = aspect_container.add<GUI::Label>("x");
  168. multiply_label.set_text_alignment(Gfx::TextAlignment::Center);
  169. multiply_label.set_fixed_size(10, 20);
  170. m_aspect_h_textbox = aspect_container.add<GUI::TextBox>();
  171. m_aspect_h_textbox->set_fixed_height(20);
  172. m_aspect_h_textbox->set_fixed_width(25);
  173. m_aspect_h_textbox->on_change = [&] { m_aspect_w_textbox->on_change(); };
  174. }
  175. return m_properties_widget.ptr();
  176. }
  177. }