summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/Tools/PickerTool.cpp
blob: 955beb545b98f43d6f70fa401ddb9bf0ff93d574 (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) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "PickerTool.h"
#include "../ImageEditor.h"
#include "../Layer.h"
#include <LibGUI/BoxLayout.h>
#include <LibGUI/CheckBox.h>

namespace PixelPaint {

PickerTool::PickerTool()
{
}

PickerTool::~PickerTool()
{
}

void PickerTool::on_mousedown(Layer* layer, MouseEvent& event)
{
    auto& position = event.layer_event().position();

    Color color;
    if (m_sample_all_layers) {
        color = m_editor->image().color_at(position);
    } else {
        if (!layer || !layer->rect().contains(position))
            return;
        color = layer->bitmap().get_pixel(position);
    }

    // We picked a transparent pixel, do nothing.
    if (!color.alpha())
        return;

    if (event.layer_event().button() == GUI::MouseButton::Left)
        m_editor->set_primary_color(color);
    else if (event.layer_event().button() == GUI::MouseButton::Right)
        m_editor->set_secondary_color(color);
}

GUI::Widget* PickerTool::get_properties_widget()
{
    if (!m_properties_widget) {
        m_properties_widget = GUI::Widget::construct();
        m_properties_widget->set_layout<GUI::VerticalBoxLayout>();

        auto& sample_checkbox = m_properties_widget->add<GUI::CheckBox>("Sample all layers");
        sample_checkbox.set_checked(m_sample_all_layers);
        sample_checkbox.on_checked = [&](bool value) {
            m_sample_all_layers = value;
        };
    }

    return m_properties_widget.ptr();
}

}