summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/Tools/GradientTool.h
blob: 0ced524a2a5f00ec86dce16fe50173192d05d018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright (c) 2023, Torsten Engelmann <engelTorsten@gmx.de>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "../ImageEditor.h"
#include "Tool.h"

namespace PixelPaint {

class GradientTool : public Tool {
public:
    GradientTool() = default;
    virtual ~GradientTool() override = default;
    virtual void on_mousedown(Layer*, MouseEvent&) override;
    virtual void on_mousemove(Layer*, MouseEvent&) override;
    virtual void on_mouseup(Layer*, MouseEvent&) override;
    virtual bool on_keydown(GUI::KeyEvent&) override;
    virtual void on_keyup(GUI::KeyEvent&) override;
    virtual void on_primary_color_change(Color) override;
    virtual void on_secondary_color_change(Color) override;
    virtual void on_tool_activation() override;
    virtual ErrorOr<GUI::Widget*> get_properties_widget() override;

    virtual Variant<Gfx::StandardCursor, NonnullRefPtr<Gfx::Bitmap const>> cursor() override;
    virtual void on_second_paint(Layer const*, GUI::PaintEvent&) override;

protected:
    virtual StringView tool_name() const override { return "Gradient Tool"sv; }

private:
    RefPtr<GUI::Widget> m_properties_widget;
    Optional<Gfx::IntPoint> m_gradient_start;
    Optional<Gfx::IntPoint> m_gradient_center;
    Optional<Gfx::IntPoint> m_gradient_end;
    Gfx::IntPoint m_perpendicular_point;

    float m_gradient_half_length = 0;
    float m_physical_diagonal_layer_length = 0;
    bool m_button_pressed = false;
    bool m_shift_pressed = false;
    bool m_hover_over_drag_handle = false;
    bool m_hover_over_start_handle = false;
    bool m_hover_over_end_handle = false;
    int m_opacity = 100;
    bool m_use_secondary_color { false };
    Gfx::FloatLine m_gradient_begin_line;
    Gfx::FloatLine m_gradient_center_line;
    Gfx::FloatLine m_gradient_end_line;

    void reset();
    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 = {});
    void rasterize_gradient();
    void calculate_gradient_lines();
    void update_gradient_end_and_derive_start(Gfx::IntPoint const);
    void translate_gradient_start_end(Gfx::IntPoint const delta, bool update_start_counterwise = true);
    bool has_gradient_start_end() { return m_gradient_center.has_value() && m_gradient_end.has_value() && m_gradient_start.has_value(); }
};

}