GradientTool.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2023, Torsten Engelmann <engelTorsten@gmx.de>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "../ImageEditor.h"
  8. #include "Tool.h"
  9. namespace PixelPaint {
  10. class GradientTool : public Tool {
  11. public:
  12. GradientTool() = default;
  13. virtual ~GradientTool() override = default;
  14. virtual void on_mousedown(Layer*, MouseEvent&) override;
  15. virtual void on_mousemove(Layer*, MouseEvent&) override;
  16. virtual void on_mouseup(Layer*, MouseEvent&) override;
  17. virtual bool on_keydown(GUI::KeyEvent&) override;
  18. virtual void on_keyup(GUI::KeyEvent&) override;
  19. virtual void on_primary_color_change(Color) override;
  20. virtual void on_secondary_color_change(Color) override;
  21. virtual void on_tool_activation() override;
  22. virtual NonnullRefPtr<GUI::Widget> get_properties_widget() override;
  23. virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> cursor() override;
  24. virtual void on_second_paint(Layer const*, GUI::PaintEvent&) override;
  25. protected:
  26. virtual StringView tool_name() const override { return "Gradient Tool"sv; }
  27. private:
  28. enum class GradientMode {
  29. Linear,
  30. Radial,
  31. __Count,
  32. };
  33. enum class IconStyle {
  34. None,
  35. ChangeWidthAndAngle,
  36. RadialWidth,
  37. };
  38. RefPtr<GUI::Widget> m_properties_widget;
  39. Optional<Gfx::IntPoint> m_gradient_start;
  40. Optional<Gfx::IntPoint> m_gradient_center;
  41. Optional<Gfx::IntPoint> m_gradient_end;
  42. Optional<Gfx::IntPoint> m_gradient_transversal_a;
  43. Optional<Gfx::IntPoint> m_gradient_transversal_b;
  44. Gfx::IntPoint m_perpendicular_point;
  45. GradientMode m_mode = GradientMode::Linear;
  46. int m_hardness = 25;
  47. float m_gradient_half_length = 0;
  48. float m_physical_diagonal_layer_length = 0;
  49. bool m_button_pressed = false;
  50. bool m_shift_pressed = false;
  51. bool m_hover_over_drag_handle = false;
  52. bool m_hover_over_start_handle = false;
  53. bool m_hover_over_end_handle = false;
  54. bool m_hover_over_transversal_a_handle = false;
  55. bool m_hover_over_transversal_b_handle = false;
  56. int m_opacity = 100;
  57. bool m_use_secondary_color { false };
  58. Gfx::FloatLine m_gradient_begin_line;
  59. Gfx::FloatLine m_gradient_center_line;
  60. Gfx::FloatLine m_gradient_end_line;
  61. void calculate_gradient_lines();
  62. void calculate_transversal_points(float scale_fraction);
  63. void draw_gradient(GUI::Painter&, bool with_guidelines = false, const Gfx::FloatPoint drawing_offset = { 0.0f, 0.0f }, float scale = 1, Optional<Gfx::IntRect const&> gradient_clip = {});
  64. bool has_gradient_data() { return m_gradient_center.has_value() && m_gradient_end.has_value() && m_gradient_start.has_value(); }
  65. void move_gradient_position(Gfx::IntPoint const movement_delta);
  66. void rasterize_gradient();
  67. void reset();
  68. void rotate_gradient_points(Gfx::IntPoint const delta);
  69. void update_gradient_with_initial_values(Gfx::IntPoint const);
  70. };
  71. }