LineTool.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 "LineTool.h"
  10. #include "../ImageEditor.h"
  11. #include "../Layer.h"
  12. #include <AK/Math.h>
  13. #include <LibGUI/Action.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/CheckBox.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/Menu.h>
  18. #include <LibGUI/Painter.h>
  19. #include <LibGUI/ValueSlider.h>
  20. #include <LibGfx/AntiAliasingPainter.h>
  21. namespace PixelPaint {
  22. static Gfx::IntPoint constrain_line_angle(Gfx::IntPoint const& start_pos, Gfx::IntPoint const& end_pos, float angle_increment)
  23. {
  24. float current_angle = AK::atan2<float>(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 };
  25. float constrained_angle = ((int)((current_angle + angle_increment / 2) / angle_increment)) * angle_increment;
  26. auto diff = end_pos - start_pos;
  27. float line_length = AK::hypot<float>(diff.x(), diff.y());
  28. return { start_pos.x() + (int)(AK::cos(constrained_angle) * line_length),
  29. start_pos.y() + (int)(AK::sin(constrained_angle) * line_length) };
  30. }
  31. void LineTool::on_mousedown(Layer* layer, MouseEvent& event)
  32. {
  33. if (!layer)
  34. return;
  35. auto& layer_event = event.layer_event();
  36. if (layer_event.button() != GUI::MouseButton::Primary && layer_event.button() != GUI::MouseButton::Secondary)
  37. return;
  38. if (m_drawing_button != GUI::MouseButton::None)
  39. return;
  40. m_drawing_button = layer_event.button();
  41. m_drag_start_position = layer_event.position();
  42. m_line_start_position = layer_event.position();
  43. m_line_end_position = layer_event.position();
  44. m_editor->update();
  45. }
  46. void LineTool::draw_using(GUI::Painter& painter, Gfx::IntPoint const& start_position, Gfx::IntPoint const& end_position, Color color, int thickness)
  47. {
  48. if (m_antialias_enabled) {
  49. Gfx::AntiAliasingPainter aa_painter { painter };
  50. auto as_float_point = [](auto const& point) {
  51. return Gfx::FloatPoint { point.x(), point.y() };
  52. };
  53. aa_painter.draw_line(as_float_point(start_position), as_float_point(end_position), color, thickness);
  54. } else {
  55. painter.draw_line(start_position, end_position, color, thickness);
  56. }
  57. }
  58. void LineTool::on_mouseup(Layer* layer, MouseEvent& event)
  59. {
  60. if (!layer)
  61. return;
  62. auto& layer_event = event.layer_event();
  63. if (layer_event.button() == m_drawing_button) {
  64. GUI::Painter painter(layer->currently_edited_bitmap());
  65. draw_using(painter, m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
  66. m_drawing_button = GUI::MouseButton::None;
  67. layer->did_modify_bitmap();
  68. m_editor->update();
  69. m_editor->did_complete_action();
  70. }
  71. }
  72. void LineTool::on_mousemove(Layer* layer, MouseEvent& event)
  73. {
  74. if (!layer)
  75. return;
  76. auto& layer_event = event.layer_event();
  77. if (m_drawing_button == GUI::MouseButton::None)
  78. return;
  79. if (layer_event.shift()) {
  80. constexpr auto ANGLE_STEP = M_PI / 8;
  81. m_line_end_position = constrain_line_angle(m_drag_start_position, layer_event.position(), ANGLE_STEP);
  82. } else {
  83. m_line_end_position = layer_event.position();
  84. }
  85. if (layer_event.alt()) {
  86. m_line_start_position = m_drag_start_position + (m_drag_start_position - m_line_end_position);
  87. } else {
  88. m_line_start_position = m_drag_start_position;
  89. }
  90. m_editor->update();
  91. }
  92. void LineTool::on_second_paint(Layer const* layer, GUI::PaintEvent& event)
  93. {
  94. if (!layer || m_drawing_button == GUI::MouseButton::None)
  95. return;
  96. GUI::Painter painter(*m_editor);
  97. painter.add_clip_rect(event.rect());
  98. auto preview_start = editor_stroke_position(m_line_start_position, m_thickness);
  99. auto preview_end = editor_stroke_position(m_line_end_position, m_thickness);
  100. draw_using(painter, preview_start, preview_end, m_editor->color_for(m_drawing_button), AK::max(m_thickness * m_editor->scale(), 1));
  101. }
  102. void LineTool::on_keydown(GUI::KeyEvent& event)
  103. {
  104. Tool::on_keydown(event);
  105. if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
  106. m_drawing_button = GUI::MouseButton::None;
  107. m_editor->update();
  108. event.accept();
  109. }
  110. }
  111. GUI::Widget* LineTool::get_properties_widget()
  112. {
  113. if (!m_properties_widget) {
  114. m_properties_widget = GUI::Widget::construct();
  115. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  116. auto& thickness_container = m_properties_widget->add<GUI::Widget>();
  117. thickness_container.set_fixed_height(20);
  118. thickness_container.set_layout<GUI::HorizontalBoxLayout>();
  119. auto& thickness_label = thickness_container.add<GUI::Label>("Thickness:");
  120. thickness_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  121. thickness_label.set_fixed_size(80, 20);
  122. auto& thickness_slider = thickness_container.add<GUI::ValueSlider>(Orientation::Horizontal, "px");
  123. thickness_slider.set_range(1, 10);
  124. thickness_slider.set_value(m_thickness);
  125. thickness_slider.on_change = [&](int value) {
  126. m_thickness = value;
  127. };
  128. set_primary_slider(&thickness_slider);
  129. auto& mode_container = m_properties_widget->add<GUI::Widget>();
  130. mode_container.set_fixed_height(20);
  131. mode_container.set_layout<GUI::HorizontalBoxLayout>();
  132. auto& mode_label = mode_container.add<GUI::Label>("Mode:");
  133. mode_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  134. mode_label.set_fixed_size(80, 20);
  135. auto& aa_enable_checkbox = mode_container.add<GUI::CheckBox>("Anti-alias");
  136. aa_enable_checkbox.on_checked = [&](bool checked) {
  137. m_antialias_enabled = checked;
  138. };
  139. }
  140. return m_properties_widget.ptr();
  141. }
  142. }