LineTool.cpp 4.6 KB

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