main.cpp 6.6 KB

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