Tool.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "Tool.h"
  9. #include "../ImageEditor.h"
  10. #include "../Layer.h"
  11. #include <LibGUI/Action.h>
  12. namespace PixelPaint {
  13. void Tool::setup(ImageEditor& editor)
  14. {
  15. m_editor = editor;
  16. }
  17. void Tool::set_action(GUI::Action* action)
  18. {
  19. m_action = action;
  20. }
  21. bool Tool::on_keydown(GUI::KeyEvent& event)
  22. {
  23. switch (event.key()) {
  24. case KeyCode::Key_LeftBracket:
  25. if (m_primary_slider) {
  26. m_primary_slider->decrease_slider_by(1);
  27. return true;
  28. }
  29. break;
  30. case KeyCode::Key_RightBracket:
  31. if (m_primary_slider) {
  32. m_primary_slider->increase_slider_by(1);
  33. return true;
  34. }
  35. break;
  36. case KeyCode::Key_LeftBrace:
  37. if (m_secondary_slider) {
  38. m_secondary_slider->decrease_slider_by(1);
  39. return true;
  40. }
  41. break;
  42. case KeyCode::Key_RightBrace:
  43. if (m_secondary_slider) {
  44. m_secondary_slider->increase_slider_by(1);
  45. return true;
  46. }
  47. break;
  48. default:
  49. break;
  50. }
  51. return false;
  52. }
  53. Gfx::IntPoint Tool::editor_layer_location(Layer const& layer) const
  54. {
  55. return (Gfx::FloatPoint { layer.location() } * m_editor->scale()).to_rounded<int>();
  56. }
  57. Gfx::IntPoint Tool::editor_stroke_position(Gfx::IntPoint pixel_coords, int stroke_thickness) const
  58. {
  59. auto position = m_editor->content_to_frame_position(pixel_coords);
  60. auto offset = (stroke_thickness % 2 == 0) ? 0 : m_editor->scale() / 2;
  61. position = position.translated(offset, offset);
  62. return position.to_type<int>();
  63. }
  64. Gfx::IntPoint Tool::constrain_line_angle(Gfx::IntPoint start_pos, Gfx::IntPoint end_pos, float angle_increment)
  65. {
  66. float current_angle = AK::atan2<float>(end_pos.y() - start_pos.y(), end_pos.x() - start_pos.x()) + float { M_PI * 2 };
  67. float constrained_angle = ((int)((current_angle + angle_increment / 2) / angle_increment)) * angle_increment;
  68. auto diff = end_pos - start_pos;
  69. float line_length = AK::hypot<float>(diff.x(), diff.y());
  70. return { start_pos.x() + (int)(AK::cos(constrained_angle) * line_length),
  71. start_pos.y() + (int)(AK::sin(constrained_angle) * line_length) };
  72. }
  73. }