summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/Selection.h
blob: 5d7d9669af40a01c9425964d63d443c752848f17 (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
64
65
66
67
68
69
70
71
72
/*
 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibCore/Timer.h>
#include <LibGfx/Rect.h>

#include "Mask.h"

namespace PixelPaint {

class Image;

class SelectionClient {
public:
    virtual void selection_did_change() = 0;

protected:
    virtual ~SelectionClient() = default;
};

// Coordinates are image-relative.
class Selection {
public:
    enum class MergeMode {
        Set,
        Add,
        Subtract,
        Intersect,
        __Count,
    };

    explicit Selection(Image&);

    bool is_empty() const { return m_mask.is_null(); }
    void clear();
    void invert();
    void merge(Mask const&, MergeMode);
    void merge(Gfx::IntRect const& rect, MergeMode mode) { merge(Mask::full(rect), mode); }
    Gfx::IntRect bounding_rect() const { return m_mask.bounding_rect(); }

    [[nodiscard]] bool is_selected(int x, int y) const { return m_mask.get(x, y) > 0; }
    [[nodiscard]] bool is_selected(Gfx::IntPoint const& point) const { return is_selected(point.x(), point.y()); }

    [[nodiscard]] u8 get_selection_alpha(int x, int y) const { return m_mask.get(x, y); }
    [[nodiscard]] u8 get_selection_alpha(Gfx::IntPoint const& point) const { return get_selection_alpha(point.x(), point.y()); }

    Mask const& mask() const { return m_mask; }
    void set_mask(Mask);

    void begin_interactive_selection() { m_in_interactive_selection = true; }
    void end_interactive_selection() { m_in_interactive_selection = false; }

    bool in_interactive_selection() const { return m_in_interactive_selection; }

    void add_client(SelectionClient&);
    void remove_client(SelectionClient&);

private:
    Image& m_image;
    Mask m_mask;

    HashTable<SelectionClient*> m_clients;

    bool m_in_interactive_selection { false };
};

}