PenTool.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "PenTool.h"
  9. #include "../ImageEditor.h"
  10. #include "../Layer.h"
  11. #include <LibGUI/Action.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Label.h>
  14. #include <LibGUI/Menu.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/ValueSlider.h>
  17. namespace PixelPaint {
  18. PenTool::PenTool()
  19. {
  20. set_size(1);
  21. }
  22. void PenTool::draw_point(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint point)
  23. {
  24. GUI::Painter painter(bitmap);
  25. painter.draw_line(point, point, color, size());
  26. }
  27. void PenTool::draw_line(Gfx::Bitmap& bitmap, Gfx::Color color, Gfx::IntPoint start, Gfx::IntPoint end)
  28. {
  29. GUI::Painter painter(bitmap);
  30. painter.draw_line(start, end, color, size());
  31. }
  32. ErrorOr<GUI::Widget*> PenTool::get_properties_widget()
  33. {
  34. if (!m_properties_widget) {
  35. auto properties_widget = TRY(GUI::Widget::try_create());
  36. (void)TRY(properties_widget->try_set_layout<GUI::VerticalBoxLayout>());
  37. auto size_container = TRY(properties_widget->try_add<GUI::Widget>());
  38. size_container->set_fixed_height(20);
  39. (void)TRY(size_container->try_set_layout<GUI::HorizontalBoxLayout>());
  40. auto size_label = TRY(size_container->try_add<GUI::Label>("Thickness:"));
  41. size_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  42. size_label->set_fixed_size(80, 20);
  43. auto size_slider = TRY(size_container->try_add<GUI::ValueSlider>(Orientation::Horizontal, String::from_utf8_short_string("px"sv)));
  44. size_slider->set_range(1, 20);
  45. size_slider->set_value(size());
  46. size_slider->on_change = [this](int value) {
  47. set_size(value);
  48. };
  49. set_primary_slider(size_slider);
  50. m_properties_widget = properties_widget;
  51. }
  52. return m_properties_widget.ptr();
  53. }
  54. }