EllipseTool.cpp 8.3 KB

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