LineTool.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "PaintableWidget.h"
  30. #include <LibGUI/Action.h>
  31. #include <LibGUI/Menu.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibM/math.h>
  34. namespace PaintBrush {
  35. static Gfx::Point constrain_line_angle(const Gfx::Point& start_pos, const Gfx::Point& end_pos, float angle_increment)
  36. {
  37. float current_angle = atan2(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + M_PI * 2.;
  38. float constrained_angle = ((int)((current_angle + angle_increment / 2.) / angle_increment)) * angle_increment;
  39. auto diff = end_pos - start_pos;
  40. float line_length = sqrt(diff.x() * diff.x() + diff.y() * diff.y());
  41. return { start_pos.x() + (int)(cos(constrained_angle) * line_length),
  42. start_pos.y() + (int)(sin(constrained_angle) * line_length) };
  43. }
  44. LineTool::LineTool()
  45. {
  46. }
  47. LineTool::~LineTool()
  48. {
  49. }
  50. void LineTool::on_mousedown(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
  51. {
  52. if (event.button() != GUI::MouseButton::Left && event.button() != GUI::MouseButton::Right)
  53. return;
  54. if (m_drawing_button != GUI::MouseButton::None)
  55. return;
  56. m_drawing_button = event.button();
  57. m_line_start_position = event.position();
  58. m_line_end_position = event.position();
  59. m_editor->update();
  60. }
  61. void LineTool::on_mouseup(Layer& layer, GUI::MouseEvent& event, GUI::MouseEvent&)
  62. {
  63. if (event.button() == m_drawing_button) {
  64. GUI::Painter painter(layer.bitmap());
  65. painter.draw_line(m_line_start_position, m_line_end_position, PaintableWidget::the().color_for(m_drawing_button), m_thickness);
  66. m_drawing_button = GUI::MouseButton::None;
  67. m_editor->update();
  68. }
  69. }
  70. void LineTool::on_mousemove(Layer&, GUI::MouseEvent& event, GUI::MouseEvent&)
  71. {
  72. if (m_drawing_button == GUI::MouseButton::None)
  73. return;
  74. if (!m_editor->rect().contains(event.position()))
  75. return;
  76. if (!m_constrain_angle) {
  77. m_line_end_position = event.position();
  78. } else {
  79. const float ANGLE_STEP = M_PI / 8.0f;
  80. m_line_end_position = constrain_line_angle(m_line_start_position, event.position(), ANGLE_STEP);
  81. }
  82. m_editor->update();
  83. }
  84. void LineTool::on_second_paint(GUI::PaintEvent& event)
  85. {
  86. if (m_drawing_button == GUI::MouseButton::None)
  87. return;
  88. (void)event;
  89. #if 0
  90. GUI::Painter painter(*m_widget);
  91. painter.add_clip_rect(event.rect());
  92. painter.draw_line(m_line_start_position, m_line_end_position, m_editor->color_for(m_drawing_button), m_thickness);
  93. #endif
  94. }
  95. void LineTool::on_keydown(GUI::KeyEvent& event)
  96. {
  97. if (event.key() == Key_Escape && m_drawing_button != GUI::MouseButton::None) {
  98. m_drawing_button = GUI::MouseButton::None;
  99. m_editor->update();
  100. event.accept();
  101. }
  102. if (event.key() == Key_Shift) {
  103. m_constrain_angle = true;
  104. m_editor->update();
  105. event.accept();
  106. }
  107. }
  108. void LineTool::on_keyup(GUI::KeyEvent& event)
  109. {
  110. if (event.key() == Key_Shift) {
  111. m_constrain_angle = false;
  112. m_editor->update();
  113. event.accept();
  114. }
  115. }
  116. void LineTool::on_contextmenu(GUI::ContextMenuEvent& event)
  117. {
  118. if (!m_context_menu) {
  119. m_context_menu = GUI::Menu::construct();
  120. m_thickness_actions.set_exclusive(true);
  121. auto insert_action = [&](int size, bool checked = false) {
  122. auto action = GUI::Action::create_checkable(String::number(size), [this, size](auto&) {
  123. m_thickness = size;
  124. });
  125. action->set_checked(checked);
  126. m_thickness_actions.add_action(*action);
  127. m_context_menu->add_action(move(action));
  128. };
  129. insert_action(1, true);
  130. insert_action(2);
  131. insert_action(3);
  132. insert_action(4);
  133. }
  134. m_context_menu->popup(event.screen_position());
  135. }
  136. }