PickerTool.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "PickerTool.h"
  8. #include "../ImageEditor.h"
  9. #include "../Layer.h"
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/CheckBox.h>
  12. namespace PixelPaint {
  13. PickerTool::PickerTool()
  14. {
  15. }
  16. PickerTool::~PickerTool()
  17. {
  18. }
  19. void PickerTool::on_mousedown(Layer* layer, MouseEvent& event)
  20. {
  21. auto& position = event.layer_event().position();
  22. Color color;
  23. if (m_sample_all_layers) {
  24. color = m_editor->image().color_at(position);
  25. } else {
  26. if (!layer || !layer->rect().contains(position))
  27. return;
  28. color = layer->bitmap().get_pixel(position);
  29. }
  30. // We picked a transparent pixel, do nothing.
  31. if (!color.alpha())
  32. return;
  33. if (event.layer_event().button() == GUI::MouseButton::Primary)
  34. m_editor->set_primary_color(color);
  35. else if (event.layer_event().button() == GUI::MouseButton::Secondary)
  36. m_editor->set_secondary_color(color);
  37. }
  38. GUI::Widget* PickerTool::get_properties_widget()
  39. {
  40. if (!m_properties_widget) {
  41. m_properties_widget = GUI::Widget::construct();
  42. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  43. auto& sample_checkbox = m_properties_widget->add<GUI::CheckBox>("Sample all layers");
  44. sample_checkbox.set_checked(m_sample_all_layers);
  45. sample_checkbox.on_checked = [&](bool value) {
  46. m_sample_all_layers = value;
  47. };
  48. }
  49. return m_properties_widget.ptr();
  50. }
  51. }