main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Action.h>
  7. #include <LibGUI/Application.h>
  8. #include <LibGUI/Icon.h>
  9. #include <LibGUI/Menu.h>
  10. #include <LibGUI/Menubar.h>
  11. #include <LibGUI/Painter.h>
  12. #include <LibGUI/Widget.h>
  13. #include <LibGUI/Window.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Font.h>
  16. #include <LibGfx/Painter.h>
  17. #include <LibGfx/Palette.h>
  18. #include <LibGfx/Path.h>
  19. #include <LibGfx/SystemTheme.h>
  20. #include <LibGfx/WindowTheme.h>
  21. #include <unistd.h>
  22. const int WIDTH = 300;
  23. const int HEIGHT = 200;
  24. class Canvas final : public GUI::Widget {
  25. C_OBJECT(Canvas)
  26. public:
  27. virtual ~Canvas() override;
  28. private:
  29. Canvas();
  30. RefPtr<Gfx::Bitmap> m_bitmap_1x;
  31. RefPtr<Gfx::Bitmap> m_bitmap_2x;
  32. RefPtr<Gfx::Bitmap> m_bitmap_2x_as_1x;
  33. void draw(Gfx::Painter& painter);
  34. virtual void paint_event(GUI::PaintEvent&) override;
  35. };
  36. Canvas::Canvas()
  37. {
  38. m_bitmap_1x = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { WIDTH, HEIGHT }, 1);
  39. m_bitmap_2x = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { WIDTH, HEIGHT }, 2);
  40. // m_bitmap_1x and m_bitmap_2x have the same logical size, so LibGfx will try to draw them at the same physical size:
  41. // When drawing on a 2x backing store it'd scale m_bitmap_1x up 2x and paint m_bitmap_2x at its physical size.
  42. // When drawing on a 1x backing store it'd draw m_bitmap_1x at its physical size, and it would have to scale down m_bitmap_2x to 0.5x its size.
  43. // But the system can't current scale down, and we want to draw the 2x bitmap at twice the size of the 1x bitmap in this particular application,
  44. // so make a 1x alias of the 2x bitmap to make LibGfx paint it without any scaling at paint time, mapping once pixel to one pixel.
  45. m_bitmap_2x_as_1x = Gfx::Bitmap::try_create_wrapper(Gfx::BitmapFormat::BGRx8888, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0)).release_value_but_fixme_should_propagate_errors();
  46. Gfx::Painter painter_1x(*m_bitmap_1x);
  47. draw(painter_1x);
  48. Gfx::Painter painter_2x(*m_bitmap_2x);
  49. draw(painter_2x);
  50. update();
  51. }
  52. Canvas::~Canvas()
  53. {
  54. }
  55. void Canvas::paint_event(GUI::PaintEvent& event)
  56. {
  57. GUI::Painter painter(*this);
  58. painter.add_clip_rect(event.rect());
  59. painter.fill_rect(event.rect(), Color::Magenta);
  60. painter.blit({ 0, 0 }, *m_bitmap_1x, m_bitmap_1x->rect());
  61. painter.blit({ 0, HEIGHT }, *m_bitmap_2x_as_1x, m_bitmap_2x_as_1x->rect());
  62. }
  63. void Canvas::draw(Gfx::Painter& painter)
  64. {
  65. auto active_window_icon = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/window.png").release_value_but_fixme_should_propagate_errors();
  66. Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends 🐞", *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 }, 0, false);
  67. painter.draw_rect({ 20, 34, WIDTH - 40, HEIGHT - 45 }, palette().color(Gfx::ColorRole::Selection), true);
  68. painter.draw_rect({ 24, 38, WIDTH - 48, HEIGHT - 53 }, palette().color(Gfx::ColorRole::Selection));
  69. // buggie.png has an alpha channel.
  70. auto buggie = Gfx::Bitmap::try_load_from_file("/res/graphics/buggie.png").release_value_but_fixme_should_propagate_errors();
  71. painter.blit({ 25, 39 }, *buggie, { 2, 30, 62, 20 });
  72. painter.draw_scaled_bitmap({ 88, 39, 62 * 2, 20 * 2 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  73. painter.draw_scaled_bitmap({ 202, 39, 80, 40 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  74. painter.draw_tiled_bitmap({ 25, 60, WIDTH - 50, 40 }, *buggie);
  75. painter.blit({ 25, 101 }, *buggie, { 2, 30, 3 * buggie->width(), 20 });
  76. // grid does not have an alpha channel.
  77. auto grid = Gfx::Bitmap::try_load_from_file("/res/wallpapers/grid.png").release_value_but_fixme_should_propagate_errors();
  78. VERIFY(!grid->has_alpha_channel());
  79. painter.fill_rect({ 25, 122, 62, 20 }, Color::Green);
  80. painter.blit({ 25, 122 }, *grid, { (grid->width() - 62) / 2, (grid->height() - 20) / 2 + 40, 62, 20 }, 0.9);
  81. painter.blit_brightened({ 88, 122 }, *buggie, { 2, 30, 62, 20 });
  82. painter.blit_dimmed({ 140, 122 }, *buggie, { 2, 30, 62, 20 });
  83. painter.blit_disabled({ 192, 122 }, *buggie, { 2, 30, 62, 20 }, palette());
  84. }
  85. int main(int argc, char** argv)
  86. {
  87. auto app = GUI::Application::construct(argc, argv);
  88. if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
  89. perror("pledge");
  90. return 1;
  91. }
  92. if (unveil("/res", "r") < 0) {
  93. perror("unveil");
  94. return 1;
  95. }
  96. if (unveil(nullptr, nullptr) < 0) {
  97. perror("unveil");
  98. return 1;
  99. }
  100. auto window = GUI::Window::construct();
  101. window->set_title("LibGfx Scale Demo");
  102. window->set_resizable(false);
  103. window->resize(WIDTH * 2, HEIGHT * 3);
  104. auto& file_menu = window->add_menu("&File");
  105. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  106. auto app_icon = GUI::Icon::default_icon("app-libgfx-demo");
  107. window->set_icon(app_icon.bitmap_for_size(16));
  108. window->set_main_widget<Canvas>();
  109. window->show();
  110. return app->exec();
  111. }