main.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2020, Nico Weber <thakis@chromium.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibCore/System.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/Icon.h>
  11. #include <LibGUI/Menu.h>
  12. #include <LibGUI/Menubar.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGUI/Widget.h>
  15. #include <LibGUI/Window.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/Font/Font.h>
  18. #include <LibGfx/Painter.h>
  19. #include <LibGfx/Palette.h>
  20. #include <LibGfx/Path.h>
  21. #include <LibGfx/SystemTheme.h>
  22. #include <LibGfx/WindowTheme.h>
  23. #include <LibMain/Main.h>
  24. #include <unistd.h>
  25. int const WIDTH = 300;
  26. int const HEIGHT = 200;
  27. class Canvas final : public GUI::Widget {
  28. C_OBJECT(Canvas)
  29. public:
  30. virtual ~Canvas() override = default;
  31. private:
  32. Canvas();
  33. RefPtr<Gfx::Bitmap> m_bitmap_1x;
  34. RefPtr<Gfx::Bitmap> m_bitmap_2x;
  35. RefPtr<Gfx::Bitmap> m_bitmap_2x_as_1x;
  36. void draw(Gfx::Painter& painter);
  37. virtual void paint_event(GUI::PaintEvent&) override;
  38. };
  39. Canvas::Canvas()
  40. {
  41. m_bitmap_1x = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { WIDTH, HEIGHT }, 1).release_value_but_fixme_should_propagate_errors();
  42. m_bitmap_2x = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { WIDTH, HEIGHT }, 2).release_value_but_fixme_should_propagate_errors();
  43. // m_bitmap_1x and m_bitmap_2x have the same logical size, so LibGfx will try to draw them at the same physical size:
  44. // When drawing on a 2x backing store it'd scale m_bitmap_1x up 2x and paint m_bitmap_2x at its physical size.
  45. // 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.
  46. // 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,
  47. // 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.
  48. m_bitmap_2x_as_1x = Gfx::Bitmap::create_wrapper(Gfx::BitmapFormat::BGRA8888, m_bitmap_2x->physical_size(), 1, m_bitmap_2x->pitch(), m_bitmap_2x->scanline(0)).release_value_but_fixme_should_propagate_errors();
  49. Gfx::Painter painter_1x(*m_bitmap_1x);
  50. draw(painter_1x);
  51. Gfx::Painter painter_2x(*m_bitmap_2x);
  52. draw(painter_2x);
  53. update();
  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::load_from_file("/res/icons/16x16/window.png"sv).release_value_but_fixme_should_propagate_errors();
  66. Gfx::WindowTheme::current().paint_normal_frame(painter, Gfx::WindowTheme::WindowState::Active, Gfx::WindowTheme::WindowMode::Other, { 4, 18, WIDTH - 8, HEIGHT - 29 }, "Well hello friends 🐞"sv, *active_window_icon, palette(), { WIDTH - 20, 6, 16, 16 }, 0, false);
  67. painter.fill_rect({ 4, 25, WIDTH - 8, HEIGHT - 30 }, palette().color(Gfx::ColorRole::Background));
  68. painter.draw_rect({ 20, 34, WIDTH - 40, HEIGHT - 45 }, palette().color(Gfx::ColorRole::Selection), true);
  69. painter.draw_rect({ 24, 38, WIDTH - 48, HEIGHT - 53 }, palette().color(Gfx::ColorRole::Selection));
  70. // buggie.png has an alpha channel.
  71. auto buggie = Gfx::Bitmap::load_from_file("/res/graphics/buggie.png"sv).release_value_but_fixme_should_propagate_errors();
  72. painter.blit({ 25, 39 }, *buggie, { 2, 30, 62, 20 });
  73. painter.draw_scaled_bitmap({ 88, 39, 62 * 2, 20 * 2 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  74. painter.draw_scaled_bitmap({ 202, 39, 80, 40 }, *buggie, Gfx::IntRect { 2, 30, 62, 20 });
  75. painter.draw_tiled_bitmap({ 25, 60, WIDTH - 50, 40 }, *buggie);
  76. painter.blit({ 25, 101 }, *buggie, { 2, 30, 3 * buggie->width(), 20 });
  77. // grid does not have an alpha channel.
  78. auto grid = Gfx::Bitmap::load_from_file("/res/wallpapers/grid.png"sv).release_value_but_fixme_should_propagate_errors();
  79. VERIFY(!grid->has_alpha_channel());
  80. painter.fill_rect({ 25, 122, 62, 20 }, Color::Green);
  81. painter.blit({ 25, 122 }, *grid, { (grid->width() - 62) / 2, (grid->height() - 20) / 2 + 40, 62, 20 }, 0.9);
  82. painter.blit_brightened({ 88, 122 }, *buggie, { 2, 30, 62, 20 });
  83. painter.blit_dimmed({ 140, 122 }, *buggie, { 2, 30, 62, 20 });
  84. painter.blit_disabled({ 192, 122 }, *buggie, { 2, 30, 62, 20 }, palette());
  85. }
  86. ErrorOr<int> serenity_main(Main::Arguments arguments)
  87. {
  88. auto app = TRY(GUI::Application::try_create(arguments));
  89. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  90. TRY(Core::System::unveil("/res", "r"));
  91. TRY(Core::System::unveil(nullptr, nullptr));
  92. auto window = TRY(GUI::Window::try_create());
  93. window->set_title("LibGfx Scale Demo");
  94. window->set_resizable(false);
  95. window->resize(WIDTH * 2, HEIGHT * 3);
  96. auto file_menu = TRY(window->try_add_menu("&File"_short_string));
  97. TRY(file_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })));
  98. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-libgfx-demo"sv));
  99. window->set_icon(app_icon.bitmap_for_size(16));
  100. (void)TRY(window->set_main_widget<Canvas>());
  101. window->show();
  102. return app->exec();
  103. }