main.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/Application.h>
  27. #include <LibGUI/Painter.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGUI/Window.h>
  30. #include <LibGfx/Bitmap.h>
  31. #include <LibGfx/Font.h>
  32. #include <LibGfx/Painter.h>
  33. #include <LibGfx/Path.h>
  34. const int WIDTH = 780;
  35. const int HEIGHT = 600;
  36. class Canvas final : public GUI::Widget {
  37. C_OBJECT(Canvas)
  38. public:
  39. virtual ~Canvas() override;
  40. private:
  41. Canvas();
  42. RefPtr<Gfx::Bitmap> m_bitmap;
  43. void draw();
  44. virtual void paint_event(GUI::PaintEvent&) override;
  45. };
  46. Canvas::Canvas()
  47. {
  48. m_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { WIDTH, HEIGHT });
  49. draw();
  50. }
  51. Canvas::~Canvas()
  52. {
  53. }
  54. void Canvas::paint_event(GUI::PaintEvent& event)
  55. {
  56. GUI::Painter painter(*this);
  57. painter.draw_scaled_bitmap(event.rect(), *m_bitmap, m_bitmap->rect());
  58. }
  59. void Canvas::draw()
  60. {
  61. GUI::Painter painter(*m_bitmap);
  62. painter.fill_rect({ 20, 20, 100, 100 }, Color::Magenta);
  63. painter.draw_rect({ 20, 140, 100, 100 }, Color::Yellow);
  64. painter.fill_rect_with_gradient(Gfx::Orientation::Horizontal, { 140, 20, 100, 100 }, Color::Yellow, Color::DarkGreen);
  65. painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, { 140, 140, 100, 100 }, Color::Red, Color::Blue);
  66. painter.fill_rect_with_dither_pattern({ 260, 20, 100, 100 }, Color::MidGray, Color::Black);
  67. painter.fill_rect_with_checkerboard({ 260, 140, 100, 100 }, { 10, 10 }, Color::LightGray, Color::White);
  68. painter.draw_line({ 430, 35 }, { 465, 70 }, Color::Green);
  69. painter.draw_line({ 465, 70 }, { 430, 105 }, Color::Green);
  70. painter.draw_line({ 430, 105 }, { 395, 70 }, Color::Green);
  71. painter.draw_line({ 395, 70 }, { 430, 35 }, Color::Green);
  72. painter.draw_rect({ 395, 35, 70, 70 }, Color::Blue);
  73. painter.draw_ellipse_intersecting({ 395, 35, 70, 70 }, Color::Red);
  74. painter.draw_rect({ 380, 20, 100, 100 }, Color::Yellow);
  75. painter.fill_rect({ 380, 140, 100, 100 }, Color::Blue);
  76. painter.draw_triangle({ 430, 140 }, { 380, 140 }, { 380, 240 }, Color::Green);
  77. painter.draw_triangle({ 430, 240 }, { 480, 140 }, { 480, 240 }, Color::Red);
  78. painter.draw_rect({ 380, 140, 100, 100 }, Color::Yellow);
  79. painter.draw_line({ 500, 20 }, { 750, 20 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
  80. painter.draw_line({ 500, 30 }, { 750, 30 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
  81. painter.draw_line({ 500, 45 }, { 750, 45 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
  82. painter.draw_line({ 500, 60 }, { 750, 60 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
  83. painter.draw_line({ 500, 70 }, { 750, 70 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
  84. painter.draw_line({ 500, 85 }, { 750, 85 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
  85. painter.draw_line({ 500, 100 }, { 750, 100 }, Color::Green, 1, Gfx::Painter::LineStyle::Dashed);
  86. painter.draw_line({ 500, 110 }, { 750, 110 }, Color::Red, 5, Gfx::Painter::LineStyle::Dashed);
  87. painter.draw_line({ 500, 125 }, { 750, 125 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dashed);
  88. painter.draw_line({ 500, 140 }, { 500, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
  89. painter.draw_line({ 510, 140 }, { 510, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
  90. painter.draw_line({ 525, 140 }, { 525, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
  91. painter.draw_line({ 540, 140 }, { 540, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Dotted);
  92. painter.draw_line({ 550, 140 }, { 550, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Dotted);
  93. painter.draw_line({ 565, 140 }, { 565, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dotted);
  94. painter.draw_line({ 580, 140 }, { 580, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Dashed);
  95. painter.draw_line({ 590, 140 }, { 590, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Dashed);
  96. painter.draw_line({ 605, 140 }, { 605, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Dashed);
  97. painter.draw_line({ 640, 190 }, { 740, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
  98. painter.draw_line({ 640, 140 }, { 740, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
  99. painter.draw_line({ 690, 140 }, { 740, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
  100. painter.draw_line({ 740, 190 }, { 640, 240 }, Color::Green, 1, Gfx::Painter::LineStyle::Solid);
  101. painter.draw_line({ 740, 140 }, { 640, 240 }, Color::Red, 5, Gfx::Painter::LineStyle::Solid);
  102. painter.draw_line({ 690, 140 }, { 640, 240 }, Color::Blue, 10, Gfx::Painter::LineStyle::Solid);
  103. auto bg = Gfx::Bitmap::load_from_file("/res/html/misc/90s-bg.png");
  104. painter.draw_tiled_bitmap({ 20, 260, 480, 320 }, *bg);
  105. painter.draw_line({ 40, 480 }, { 20, 260 }, Color::Red);
  106. painter.draw_line({ 40, 480 }, { 120, 300 }, Color::Red);
  107. painter.draw_quadratic_bezier_curve({ 40, 480 }, { 20, 260 }, { 120, 300 }, Color::Blue);
  108. painter.draw_line({ 240, 280 }, { 80, 420 }, Color::Red, 3);
  109. painter.draw_line({ 240, 280 }, { 260, 360 }, Color::Red, 3);
  110. painter.draw_quadratic_bezier_curve({ 240, 280 }, { 80, 420 }, { 260, 360 }, Color::Blue, 3);
  111. auto path = Gfx::Path();
  112. path.move_to({ 60, 500 });
  113. path.line_to({ 90, 540 });
  114. path.quadratic_bezier_curve_to({ 320, 500 }, { 220, 400 });
  115. path.line_to({ 300, 440 });
  116. path.line_to({ 90, 460 });
  117. path.quadratic_bezier_curve_to({ 260, 500 }, { 200, 540 });
  118. path.close();
  119. painter.fill_path(path, Color::Yellow, Gfx::Painter::WindingRule::EvenOdd);
  120. auto buggie = Gfx::Bitmap::load_from_file("/res/icons/buggie.png");
  121. painter.blit({ 280, 280 }, *buggie, buggie->rect(), 0.5);
  122. painter.blit_scaled({ 360, 280, buggie->rect().width() * 2, buggie->rect().height() * 2 }, *buggie, buggie->rect(), 0.5, 0.5);
  123. painter.draw_rect({ 20, 260, 480, 320 }, Color::DarkGray);
  124. painter.draw_rect({ 520, 260, 240, 80 }, Color::DarkGray);
  125. painter.draw_text({ 520, 260, 240, 80 }, "CenterLeft", Gfx::TextAlignment::CenterLeft, Color::White);
  126. painter.draw_text({ 520, 260, 240, 80 }, "Center", Gfx::TextAlignment::Center, Color::White);
  127. painter.draw_text({ 520, 260, 240, 80 }, "CenterRight", Gfx::TextAlignment::CenterRight, Color::White);
  128. painter.draw_text({ 520, 260, 240, 80 }, "TopLeft", Gfx::TextAlignment::TopLeft, Color::White);
  129. painter.draw_text({ 520, 260, 240, 80 }, "TopRight", Gfx::TextAlignment::TopRight, Color::White);
  130. painter.draw_rect({ 520, 360, 240, 30 }, Color::DarkGray);
  131. painter.draw_text({ 520, 360, 240, 30 }, "Emojis! 🙂😂🐞🦄", Gfx::TextAlignment::Center, Color::White);
  132. painter.draw_rect({ 520, 410, 240, 80 }, Color::DarkGray);
  133. painter.draw_text({ 520, 415, 240, 20 }, "Normal text", Gfx::Font::default_font(), Gfx::TextAlignment::CenterLeft, Color::Red);
  134. painter.draw_text({ 520, 430, 240, 20 }, "Bold text", Gfx::Font::default_bold_font(), Gfx::TextAlignment::CenterLeft, Color::Green);
  135. painter.draw_text({ 520, 450, 240, 20 }, "Normal text (fixed width)", Gfx::Font::default_fixed_width_font(), Gfx::TextAlignment::CenterLeft, Color::Blue);
  136. painter.draw_text({ 520, 465, 240, 20 }, "Bold text (fixed width)", Gfx::Font::default_bold_fixed_width_font(), Gfx::TextAlignment::CenterLeft, Color::Yellow);
  137. auto font = Gfx::Font::load_from_file("/res/fonts/PebbletonBold11.font");
  138. painter.draw_rect({ 520, 510, 240, 30 }, Color::DarkGray);
  139. painter.draw_text({ 520, 510, 240, 30 }, "Hello friends! :^)", *font, Gfx::TextAlignment::Center, Color::White);
  140. painter.fill_rect({ 520, 560, 10, 20 }, Color::White);
  141. painter.fill_rect({ 530, 560, 10, 20 }, Color::WarmGray);
  142. painter.fill_rect({ 540, 560, 10, 20 }, Color::LightGray);
  143. painter.fill_rect({ 550, 560, 10, 20 }, Color::MidGray);
  144. painter.fill_rect({ 560, 560, 10, 20 }, Color::DarkGray);
  145. painter.fill_rect({ 570, 560, 10, 20 }, Color::Black);
  146. painter.fill_rect({ 580, 560, 10, 20 }, Color::Blue);
  147. painter.fill_rect({ 590, 560, 10, 20 }, Color::MidBlue);
  148. painter.fill_rect({ 600, 560, 10, 20 }, Color::DarkBlue);
  149. painter.fill_rect({ 610, 560, 10, 20 }, Color::Cyan);
  150. painter.fill_rect({ 620, 560, 10, 20 }, Color::MidCyan);
  151. painter.fill_rect({ 630, 560, 10, 20 }, Color::DarkCyan);
  152. painter.fill_rect({ 640, 560, 10, 20 }, Color::Green);
  153. painter.fill_rect({ 650, 560, 10, 20 }, Color::MidGreen);
  154. painter.fill_rect({ 660, 560, 10, 20 }, Color::DarkGreen);
  155. painter.fill_rect({ 670, 560, 10, 20 }, Color::Yellow);
  156. painter.fill_rect({ 680, 560, 10, 20 }, Color::Red);
  157. painter.fill_rect({ 690, 560, 10, 20 }, Color::MidRed);
  158. painter.fill_rect({ 700, 560, 10, 20 }, Color::DarkRed);
  159. painter.fill_rect({ 710, 560, 10, 20 }, Color::Magenta);
  160. painter.fill_rect({ 720, 560, 10, 20 }, Color::MidMagenta);
  161. update();
  162. }
  163. int main(int argc, char** argv)
  164. {
  165. auto app = GUI::Application::construct(argc, argv);
  166. auto window = GUI::Window::construct();
  167. window->set_double_buffering_enabled(true);
  168. window->set_title("LibGfx Demo");
  169. window->set_resizable(false);
  170. window->resize(WIDTH, HEIGHT);
  171. window->set_main_widget<Canvas>();
  172. window->show();
  173. return app->exec();
  174. }