Skip to content

Commit 662e47e

Browse files
phoffmeisterawesomekling
authored andcommitted
MouseDemo: A more visual approach for MouseEvents :^)
1 parent 21712ed commit 662e47e

File tree

1 file changed

+106
-52
lines changed

1 file changed

+106
-52
lines changed

Demos/Mouse/main.cpp

Lines changed: 106 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,97 +9,150 @@
99
#include <LibGUI/Widget.h>
1010
#include <LibGUI/Window.h>
1111
#include <LibGfx/Bitmap.h>
12+
#include <LibGfx/Path.h>
1213

13-
static unsigned s_mouse_button_state;
14+
#include <math.h>
1415

15-
class MouseButtonIndicator final : public GUI::Frame {
16-
C_OBJECT(MouseButtonIndicator);
16+
class MainFrame final : public GUI::Frame {
17+
C_OBJECT(MainFrame);
1718

1819
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+
}
2027

21-
private:
2228
virtual void paint_event(GUI::PaintEvent& event) override
2329
{
24-
Frame::paint_event(event);
25-
2630
GUI::Painter painter(*this);
2731
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+
}
2871

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);
3875
}
3976

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);
4379

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);
4882

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);
5385

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;
5990

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;
6395

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+
}
66112

67-
public:
68113
void mousedown_event(GUI::MouseEvent& event) override
69114
{
70-
s_mouse_button_state = event.buttons();
115+
m_buttons = event.buttons();
71116
update();
72-
Widget::mousedown_event(event);
73117
}
74118

75119
void mouseup_event(GUI::MouseEvent& event) override
76120
{
77-
s_mouse_button_state = event.buttons();
121+
m_buttons = event.buttons();
78122
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);
80132
}
81133

82134
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+
}
84144
};
85145

86146
int main(int argc, char** argv)
87147
{
88148
GUI::Application app(argc, argv);
89149
auto window = GUI::Window::construct();
90150
window->set_title("Mouse button demo");
151+
window->resize(160, 155);
91152

92-
auto& main_widget = window->set_main_widget<MainWidget>();
153+
auto& main_widget = window->set_main_widget<MainFrame>();
93154
main_widget.set_fill_with_background_color(true);
94155

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-
103156
auto menubar = GUI::MenuBar::construct();
104157
auto& app_menu = menubar->add_menu("Mouse Demo");
105158
app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app.quit(); }));
@@ -110,6 +163,7 @@ int main(int argc, char** argv)
110163
}));
111164

112165
app.set_menubar(move(menubar));
166+
window->set_resizable(false);
113167
window->show();
114168
return app.exec();
115169
}

0 commit comments

Comments
 (0)