9
9
#include < LibGUI/Widget.h>
10
10
#include < LibGUI/Window.h>
11
11
#include < LibGfx/Bitmap.h>
12
+ #include < LibGfx/Path.h>
12
13
13
- static unsigned s_mouse_button_state;
14
+ # include < math.h >
14
15
15
- class MouseButtonIndicator final : public GUI::Frame {
16
- C_OBJECT (MouseButtonIndicator );
16
+ class MainFrame final : public GUI::Frame {
17
+ C_OBJECT (MainFrame );
17
18
18
19
public:
19
- virtual ~MouseButtonIndicator () {}
20
+ virtual void timer_event (Core::TimerEvent&) override
21
+ {
22
+
23
+ m_show_scroll_wheel = false ;
24
+ stop_timer ();
25
+ update ();
26
+ }
20
27
21
- private:
22
28
virtual void paint_event (GUI::PaintEvent& event) override
23
29
{
24
- Frame::paint_event (event);
25
-
26
30
GUI::Painter painter (*this );
27
31
painter.add_clip_rect (event.rect ());
32
+ painter.fill_rect (frame_inner_rect (), Color::White);
33
+
34
+ Gfx::Path path;
35
+ // draw mouse outline
36
+ path.move_to ({ 30 , 140 });
37
+ path.line_to ({ 30 , 20 });
38
+ path.line_to ({ 65 , 12 });
39
+ path.line_to ({ 95 , 12 });
40
+ path.line_to ({ 130 , 20 });
41
+ path.line_to ({ 130 , 140 });
42
+ path.line_to ({ 30 , 140 });
43
+
44
+ // draw button separator
45
+ path.move_to ({ 30 , 65 });
46
+ path.line_to ({ 130 , 65 });
47
+
48
+ path.move_to ({ 65 , 65 });
49
+ path.line_to ({ 65 , 13 });
50
+
51
+ path.move_to ({ 95 , 65 });
52
+ path.line_to ({ 95 , 13 });
53
+
54
+ // draw fw and back button outlines
55
+ path.move_to ({ 30 , 43 });
56
+ path.line_to ({ 25 , 43 });
57
+ path.line_to ({ 25 , 60 });
58
+ path.line_to ({ 30 , 60 });
59
+
60
+ path.move_to ({ 30 , 70 });
61
+ path.line_to ({ 25 , 70 });
62
+ path.line_to ({ 25 , 87 });
63
+ path.line_to ({ 30 , 87 });
64
+
65
+ painter.stroke_path (path, Color::Black, 1 );
66
+
67
+ if (m_buttons & GUI::MouseButton::Left) {
68
+ painter.fill_rect ({ 31 , 21 , 34 , 44 }, Color::Blue);
69
+ painter.draw_triangle ({ 30 , 21 }, { 65 , 21 }, { 65 , 12 }, Color::Blue);
70
+ }
28
71
29
- Color background_color;
30
- Color foreground_color;
31
-
32
- if (s_mouse_button_state & m_button) {
33
- background_color = Color::Black;
34
- foreground_color = Color::White;
35
- } else {
36
- background_color = Color::White;
37
- foreground_color = Color::Black;
72
+ if (m_buttons & GUI::MouseButton::Right) {
73
+ painter.fill_rect ({ 96 , 21 , 34 , 44 }, Color::Blue);
74
+ painter.draw_triangle ({ 96 , 12 }, { 96 , 21 }, { 132 , 21 }, Color::Blue);
38
75
}
39
76
40
- painter.fill_rect (frame_inner_rect (), background_color);
41
- painter.draw_text (frame_inner_rect (), m_name, Gfx::TextAlignment::Center, foreground_color);
42
- }
77
+ if (m_buttons & GUI::MouseButton::Middle)
78
+ painter.fill_rect ({ 66 , 13 , 29 , 52 }, Color::Blue);
43
79
44
- void mousedown_event (GUI::MouseEvent& event) override
45
- {
46
- event.ignore ();
47
- }
80
+ if (m_buttons & GUI::MouseButton::Forward)
81
+ painter.fill_rect ({ 26 , 44 , 4 , 16 }, Color::Blue);
48
82
49
- void mouseup_event (GUI::MouseEvent& event) override
50
- {
51
- event.ignore ();
52
- }
83
+ if (m_buttons & GUI::MouseButton::Back)
84
+ painter.fill_rect ({ 26 , 71 , 4 , 16 }, Color::Blue);
53
85
54
- MouseButtonIndicator (const String& name, GUI::MouseButton button)
55
- : m_name(name)
56
- , m_button(button)
57
- {
58
- }
86
+ if (m_show_scroll_wheel) {
87
+ auto radius = 10 ;
88
+ auto off_x = 80 ;
89
+ auto off_y = 38 ;
59
90
60
- String m_name;
61
- GUI::MouseButton m_button;
62
- };
91
+ Gfx::Point p1;
92
+ Gfx::Point p2;
93
+ Gfx::Point p3;
94
+ Gfx::Point p4;
63
95
64
- class MainWidget final : public GUI::Widget {
65
- C_OBJECT (MainWidget);
96
+ p1.set_x (radius * cos (M_PI * m_wheel_delta_acc / 18 ) + off_x);
97
+ p1.set_y (radius * sin (M_PI * m_wheel_delta_acc / 18 ) + off_y);
98
+
99
+ p2.set_x (radius * cos (M_PI * (m_wheel_delta_acc + 18 ) / 18 ) + off_x);
100
+ p2.set_y (radius * sin (M_PI * (m_wheel_delta_acc + 18 ) / 18 ) + off_y);
101
+
102
+ p3.set_x (radius * cos (M_PI * (m_wheel_delta_acc + 9 ) / 18 ) + off_x);
103
+ p3.set_y (radius * sin (M_PI * (m_wheel_delta_acc + 9 ) / 18 ) + off_y);
104
+
105
+ p4.set_x (radius * cos (M_PI * (m_wheel_delta_acc + 27 ) / 18 ) + off_x);
106
+ p4.set_y (radius * sin (M_PI * (m_wheel_delta_acc + 27 ) / 18 ) + off_y);
107
+
108
+ painter.draw_line (p1, p2, Color::Red, 2 );
109
+ painter.draw_line (p3, p4, Color::Red, 2 );
110
+ }
111
+ }
66
112
67
- public:
68
113
void mousedown_event (GUI::MouseEvent& event) override
69
114
{
70
- s_mouse_button_state = event.buttons ();
115
+ m_buttons = event.buttons ();
71
116
update ();
72
- Widget::mousedown_event (event);
73
117
}
74
118
75
119
void mouseup_event (GUI::MouseEvent& event) override
76
120
{
77
- s_mouse_button_state = event.buttons ();
121
+ m_buttons = event.buttons ();
78
122
update ();
79
- Widget::mouseup_event (event);
123
+ }
124
+
125
+ void mousewheel_event (GUI::MouseEvent& event) override
126
+ {
127
+ m_wheel_delta_acc = (m_wheel_delta_acc + event.wheel_delta () + 36 ) % 36 ;
128
+ m_show_scroll_wheel = true ;
129
+ update ();
130
+ if (!has_timer ())
131
+ start_timer (500 );
80
132
}
81
133
82
134
private:
83
- MainWidget () {}
135
+ unsigned m_buttons;
136
+ unsigned m_wheel_delta_acc;
137
+ bool m_show_scroll_wheel;
138
+ MainFrame ()
139
+ : m_buttons { 0 }
140
+ , m_wheel_delta_acc { 0 }
141
+ , m_show_scroll_wheel { false }
142
+ {
143
+ }
84
144
};
85
145
86
146
int main (int argc, char ** argv)
87
147
{
88
148
GUI::Application app (argc, argv);
89
149
auto window = GUI::Window::construct ();
90
150
window->set_title (" Mouse button demo" );
151
+ window->resize (160 , 155 );
91
152
92
- auto & main_widget = window->set_main_widget <MainWidget >();
153
+ auto & main_widget = window->set_main_widget <MainFrame >();
93
154
main_widget.set_fill_with_background_color (true );
94
155
95
- main_widget.set_layout <GUI::VerticalBoxLayout>();
96
-
97
- main_widget.add <MouseButtonIndicator>(" Left" , GUI::MouseButton::Left);
98
- main_widget.add <MouseButtonIndicator>(" Middle" , GUI::MouseButton::Middle);
99
- main_widget.add <MouseButtonIndicator>(" Right" , GUI::MouseButton::Right);
100
- main_widget.add <MouseButtonIndicator>(" Back" , GUI::MouseButton::Back);
101
- main_widget.add <MouseButtonIndicator>(" Forward" , GUI::MouseButton::Forward);
102
-
103
156
auto menubar = GUI::MenuBar::construct ();
104
157
auto & app_menu = menubar->add_menu (" Mouse Demo" );
105
158
app_menu.add_action (GUI::CommonActions::make_quit_action ([&](auto &) { app.quit (); }));
@@ -110,6 +163,7 @@ int main(int argc, char** argv)
110
163
}));
111
164
112
165
app.set_menubar (move (menubar));
166
+ window->set_resizable (false );
113
167
window->show ();
114
168
return app.exec ();
115
169
}
0 commit comments