LineTool.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "LineTool.h"
  27. #include "ImageEditor.h"
  28. #include "Layer.h"
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Menu.h>
  31. #include <LibGUI/Painter.h>
  32. #include <math.h>
  33. namespace PixelPaint {
  34. static Gfx::IntPoint constrain_line_angle(const Gfx::IntPoint& start_pos, const Gfx::IntPoint& end_pos, float angle_increment)
  35. {
  36. float current_angle = atan2f(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 };
  37. float constrained_angle = ((int)((current_angle + angle_increment / 2) / angle_increment)) * angle_increment;
  38. auto diff = end_pos - start_pos;
  39. float line_length = sqrt(diff.x() * diff.x() + diff.y() * diff.y());
  40. return { start_pos.x() + (int)(cosf(constrained_angle) * line_length),
  41. start_pos.y() + (int)(sinf(constrained_angle) * line_length) };
  42. }
  43. LineTool::LineTool()
  44. {
  45. }
  46. LineTool::~LineTool()
  47. {
  48. }
  49. void LineTool::on_mousedown(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent&)
  50. {
  51. if (layer_event.button() != GUI::MouseButton::Left && layer_event.button() != GUI::MouseButton::Right)
  52. return;
  53. if (m_drawing_button != GUI::MouseButton::None)
  54. return;
  55. m_drawing_button = layer_event.button();
  56. m_line_start_position = layer_event.position();
  57. m_line_end_position = layer_event.position();
  58. m_editor->update();
  59. }
  60. void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
  61. {
  62. if (event.button() == m_drawing_button) {
  63. GUI::Painter painter(layer.bitmap());
  64. painter.draw_line(m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
  65. m_drawing_button = GUI::MouseButton::None;
  66. layer.did_modify_bitmap(*m_editor->image());
  67. m_editor->did_complete_action();
  68. }
  69. }
  70. void LineTool::on_mousemove(Layer&, GUI::MouseEvent& layer_event, GUI::MouseEvent&)
  71. {
  72. if (m_drawing_button == GUI::MouseButton::None)
  73. return;
  74. if (!m_constrain_angle) {
  75. m_line_end_position = layer_event.position();
  76. } else {
  77. constexpr auto ANGLE_STEP = M_PI / 8;
  78. m_line_end_position = constrain_line_angle(m_line_start_position, layer_event.position(), ANGLE_STEP);
  79. }
  80. m_editor->update();
  81. }
  82. void LineTool::on_second_paint(const Layer& layer, GUI::PaintEvent& event)
  83. {
  84. if (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 = m_editor->layer_position_to_editor_position(layer, m_line_start_position).to_type<int>();
  89. auto preview_end = m_editor->layer_position_to_editor_position(layer, m_line_end_position).to_type<int>();
  90. painter.draw_line(preview_start, preview_end, m_editor->color_for(m_drawing_button), m_thickness);
  91. }
  92. void LineTool::on_keydown(GUI::KeyEvent& event)
  93. {
  94. if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
  95. m_drawing_button = GUI::MouseButton::None;
  96. m_editor->update();
  97. event.accept();
  98. }
  99. if (event.key() == Key_Shift) {
  100. m_constrain_angle = true;
  101. m_editor->update();
  102. event.accept();
  103. }
  104. }
  105. void LineTool::on_keyup(GUI::KeyEvent& event)
  106. {
  107. if (event.key() == Key_Shift) {
  108. m_constrain_angle = false;
  109. m_editor->update();
  110. event.accept();
  111. }
  112. }
  113. void LineTool::on_tool_button_contextmenu(GUI::ContextMenuEvent& event)
  114. {
  115. if (!m_context_menu) {
  116. m_context_menu = GUI::Menu::construct();
  117. m_thickness_actions.set_exclusive(true);
  118. auto insert_action = [&](int size, bool checked = false) {
  119. auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
  120. m_thickness = size;
  121. });
  122. action->set_checked(checked);
  123. m_thickness_actions.add_action(*action);
  124. m_context_menu->add_action(move(action));
  125. };
  126. insert_action(1, true);
  127. insert_action(2);
  128. insert_action(3);
  129. insert_action(4);
  130. }
  131. m_context_menu->popup(event.screen_position());
  132. }
  133. }