Tool.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. template<>
  74. void Tool::set_pixel_with_possible_mask<Gfx::StorageFormat::BGRA8888>(int x, int y, Gfx::Color color, Gfx::Bitmap& bitmap)
  75. {
  76. if (!m_editor || !m_editor->active_layer())
  77. return;
  78. switch (m_editor->active_layer()->edit_mode()) {
  79. case Layer::EditMode::Content:
  80. bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(x, y, m_editor->active_layer()->modify_pixel_with_editing_mask(x, y, color, bitmap.get_pixel(x, y)));
  81. break;
  82. case Layer::EditMode::Mask:
  83. bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(x, y, color);
  84. break;
  85. }
  86. }
  87. void Tool::set_pixel_with_possible_mask(int x, int y, Gfx::Color color, Gfx::Bitmap& bitmap)
  88. {
  89. if (!m_editor || !m_editor->active_layer())
  90. return;
  91. switch (m_editor->active_layer()->edit_mode()) {
  92. case Layer::EditMode::Content:
  93. bitmap.set_pixel(x, y, m_editor->active_layer()->modify_pixel_with_editing_mask(x, y, color, bitmap.get_pixel(x, y)));
  94. break;
  95. case Layer::EditMode::Mask:
  96. bitmap.set_pixel(x, y, color);
  97. break;
  98. }
  99. }
  100. }