main.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Math.h>
  8. #include <LibGUI/Action.h>
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Frame.h>
  12. #include <LibGUI/Icon.h>
  13. #include <LibGUI/Menu.h>
  14. #include <LibGUI/Menubar.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/Widget.h>
  17. #include <LibGUI/Window.h>
  18. #include <LibGUI/WindowServerConnection.h>
  19. #include <LibGfx/Bitmap.h>
  20. #include <LibGfx/Path.h>
  21. #include <unistd.h>
  22. class MainFrame final : public GUI::Frame {
  23. C_OBJECT(MainFrame);
  24. public:
  25. virtual void timer_event(Core::TimerEvent&) override
  26. {
  27. m_show_scroll_wheel = false;
  28. stop_timer();
  29. update();
  30. }
  31. virtual void paint_event(GUI::PaintEvent& event) override
  32. {
  33. GUI::Painter painter(*this);
  34. painter.add_clip_rect(event.rect());
  35. painter.fill_rect(frame_inner_rect(), Color::White);
  36. Gfx::Path path;
  37. // draw mouse outline
  38. path.move_to({ 30, 140 });
  39. path.line_to({ 30, 20 });
  40. path.line_to({ 65, 12 });
  41. path.line_to({ 95, 12 });
  42. path.line_to({ 130, 20 });
  43. path.line_to({ 130, 140 });
  44. path.line_to({ 30, 140 });
  45. // draw button separator
  46. path.move_to({ 30, 65 });
  47. path.line_to({ 130, 65 });
  48. path.move_to({ 65, 65 });
  49. path.line_to({ 65, 13 });
  50. path.move_to({ 95, 65 });
  51. path.line_to({ 95, 13 });
  52. // draw fw and back button outlines
  53. path.move_to({ 30, 43 });
  54. path.line_to({ 25, 43 });
  55. path.line_to({ 25, 60 });
  56. path.line_to({ 30, 60 });
  57. path.move_to({ 30, 70 });
  58. path.line_to({ 25, 70 });
  59. path.line_to({ 25, 87 });
  60. path.line_to({ 30, 87 });
  61. painter.stroke_path(path, Color::Black, 1);
  62. auto primary_secondary_switched = GUI::WindowServerConnection::the().get_buttons_switched();
  63. auto primary_pressed = m_buttons & GUI::MouseButton::Primary;
  64. auto secondary_pressed = m_buttons & GUI::MouseButton::Secondary;
  65. if (primary_secondary_switched ? secondary_pressed : primary_pressed) {
  66. painter.fill_rect({ 31, 21, 34, 44 }, Color::Blue);
  67. painter.draw_triangle({ 30, 21 }, { 65, 21 }, { 65, 12 }, Color::Blue);
  68. }
  69. if (primary_secondary_switched ? primary_pressed : secondary_pressed) {
  70. painter.fill_rect({ 96, 21, 34, 44 }, Color::Blue);
  71. painter.draw_triangle({ 96, 12 }, { 96, 21 }, { 132, 21 }, Color::Blue);
  72. }
  73. if (m_buttons & GUI::MouseButton::Middle)
  74. painter.fill_rect({ 66, 13, 29, 52 }, Color::Blue);
  75. if (m_buttons & GUI::MouseButton::Forward)
  76. painter.fill_rect({ 26, 44, 4, 16 }, Color::Blue);
  77. if (m_buttons & GUI::MouseButton::Backward)
  78. painter.fill_rect({ 26, 71, 4, 16 }, Color::Blue);
  79. if (m_show_scroll_wheel) {
  80. auto radius = 10;
  81. auto off_x = 80;
  82. auto off_y = 38;
  83. Gfx::IntPoint p1;
  84. Gfx::IntPoint p2;
  85. Gfx::IntPoint p3;
  86. Gfx::IntPoint p4;
  87. p1.set_x(radius * AK::cos(AK::Pi<double> * m_wheel_delta_acc / 18.) + off_x);
  88. p1.set_y(radius * AK::sin(AK::Pi<double> * m_wheel_delta_acc / 18.) + off_y);
  89. p2.set_x(radius * AK::cos(AK::Pi<double> * (m_wheel_delta_acc + 18) / 18.) + off_x);
  90. p2.set_y(radius * AK::sin(AK::Pi<double> * (m_wheel_delta_acc + 18) / 18.) + off_y);
  91. p3.set_x(radius * AK::cos(AK::Pi<double> * (m_wheel_delta_acc + 9) / 18.) + off_x);
  92. p3.set_y(radius * AK::sin(AK::Pi<double> * (m_wheel_delta_acc + 9) / 18.) + off_y);
  93. p4.set_x(radius * AK::cos(AK::Pi<double> * (m_wheel_delta_acc + 27) / 18.) + off_x);
  94. p4.set_y(radius * AK::sin(AK::Pi<double> * (m_wheel_delta_acc + 27) / 18.) + off_y);
  95. painter.draw_line(p1, p2, Color::Red, 2);
  96. painter.draw_line(p3, p4, Color::Red, 2);
  97. }
  98. }
  99. void mousedown_event(GUI::MouseEvent& event) override
  100. {
  101. m_buttons = event.buttons();
  102. update();
  103. }
  104. void mouseup_event(GUI::MouseEvent& event) override
  105. {
  106. m_buttons = event.buttons();
  107. update();
  108. }
  109. void mousewheel_event(GUI::MouseEvent& event) override
  110. {
  111. m_wheel_delta_acc = (m_wheel_delta_acc + event.wheel_delta() + 36) % 36;
  112. m_show_scroll_wheel = true;
  113. update();
  114. if (!has_timer())
  115. start_timer(500);
  116. }
  117. private:
  118. unsigned m_buttons;
  119. unsigned m_wheel_delta_acc;
  120. bool m_show_scroll_wheel;
  121. MainFrame()
  122. : m_buttons { 0 }
  123. , m_wheel_delta_acc { 0 }
  124. , m_show_scroll_wheel { false }
  125. {
  126. }
  127. };
  128. int main(int argc, char** argv)
  129. {
  130. auto app = GUI::Application::construct(argc, argv);
  131. auto app_icon = GUI::Icon::default_icon("app-mouse");
  132. if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
  133. perror("pledge");
  134. return 1;
  135. }
  136. if (unveil("/res", "r") < 0) {
  137. perror("unveil");
  138. return 1;
  139. }
  140. if (unveil(nullptr, nullptr) < 0) {
  141. perror("unveil");
  142. return 1;
  143. }
  144. auto window = GUI::Window::construct();
  145. window->set_title("Mouse demo");
  146. window->set_icon(app_icon.bitmap_for_size(16));
  147. window->resize(160, 155);
  148. auto& main_widget = window->set_main_widget<MainFrame>();
  149. main_widget.set_fill_with_background_color(true);
  150. auto& file_menu = window->add_menu("&File");
  151. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  152. auto& help_menu = window->add_menu("&Help");
  153. help_menu.add_action(GUI::CommonActions::make_about_action("Mouse Demo", app_icon, window));
  154. window->set_resizable(false);
  155. window->show();
  156. return app->exec();
  157. }