]> git.tdb.fi Git - libs/gui.git/blob - examples/ev.cpp
Update .gitignore to include build products on Windows
[libs/gui.git] / examples / ev.cpp
1 #include <msp/core/application.h>
2 #include <msp/graphics/simplewindow.h>
3 #include <msp/input/gesturedetector.h>
4 #include <msp/input/keyboard.h>
5 #include <msp/input/mouse.h>
6 #include <msp/input/touchscreen.h>
7 #include <msp/io/print.h>
8 #include <msp/stringcodec/codec.h>
9 #include <msp/stringcodec/utf8.h>
10 #include <msp/time/timedelta.h>
11 #include <msp/time/utils.h>
12
13 using namespace Msp;
14
15 class Ev: public RegisteredApplication<Ev>
16 {
17 private:
18         Graphics::SimpleWindow window;
19         Input::Keyboard keyboard;
20         Input::Mouse mouse;
21         Input::Touchscreen *touch;
22         Input::GestureDetector *gesture;
23
24 public:
25         Ev(int, char **);
26         ~Ev();
27
28 private:
29         virtual void tick();
30
31         void got_focus();
32         void lost_focus();
33         void key_press(unsigned);
34         void key_release(unsigned);
35         void character(StringCodec::unichar);
36         void button_press(unsigned);
37         void button_release(unsigned);
38         void axis_motion(unsigned, float, float);
39         void touch_press(unsigned);
40         void touch_release(unsigned);
41         void touch_motion(unsigned, float, float);
42         void gesture_started(unsigned);
43         void gesture_ended(unsigned);
44         void gesture_axis(unsigned, float, float);
45 };
46
47 Ev::Ev(int, char **):
48         window(200, 200),
49         keyboard(window),
50         mouse(window),
51         touch(0),
52         gesture(0)
53 {
54         window.get_display().signal_got_focus.connect(sigc::mem_fun(this, &Ev::got_focus));
55         window.get_display().signal_lost_focus.connect(sigc::mem_fun(this, &Ev::lost_focus));
56         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Ev::exit), 0));
57         keyboard.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::key_press), false));
58         keyboard.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::key_release), false));
59         keyboard.signal_character.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::character), false));
60         mouse.signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::button_press), false));
61         mouse.signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::button_release), false));
62         mouse.signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::axis_motion), false));
63         if(Input::Touchscreen::is_available())
64         {
65                 touch = new Input::Touchscreen(window);
66                 gesture = new Input::GestureDetector(*touch);
67                 touch->signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::touch_press), false));
68                 touch->signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::touch_release), false));
69                 touch->signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::touch_motion), false));
70                 gesture->signal_button_press.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::gesture_started), false));
71                 gesture->signal_button_release.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::gesture_ended), false));
72                 gesture->signal_axis_motion.connect(sigc::bind_return(sigc::mem_fun(this, &Ev::gesture_axis), false));
73         }
74         window.set_title("Event tester");
75         window.show();
76 }
77
78 Ev::~Ev()
79 {
80         delete gesture;
81         delete touch;
82 }
83
84 void Ev::tick()
85 {
86         window.tick();
87         Time::sleep(Time::msec);
88 }
89
90 void Ev::got_focus()
91 {
92         IO::print("got_focus\n");
93 }
94
95 void Ev::lost_focus()
96 {
97         IO::print("lost_focus\n");
98 }
99
100 void Ev::key_press(unsigned key)
101 {
102         IO::print("key_press: %s\n", keyboard.get_button_name(key));
103 }
104
105 void Ev::key_release(unsigned key)
106 {
107         IO::print("key_release: %s\n", keyboard.get_button_name(key));
108 }
109
110 void Ev::character(StringCodec::unichar ch)
111 {
112         IO::print("character: %s\n", StringCodec::encode<StringCodec::Utf8>(StringCodec::ustring(1, ch)));
113 }
114
115 void Ev::button_press(unsigned btn)
116 {
117         IO::print("button_press: %s\n", mouse.get_button_name(btn));
118 }
119
120 void Ev::button_release(unsigned btn)
121 {
122         IO::print("button_release: %s\n", mouse.get_button_name(btn));
123 }
124
125 void Ev::axis_motion(unsigned axis, float value, float rel)
126 {
127         IO::print("axis_motion: %s %.3f %+.3f\n", mouse.get_axis_name(axis), value, rel);
128 }
129
130 void Ev::touch_press(unsigned btn)
131 {
132         IO::print("touch_press: %s\n", touch->get_button_name(btn));
133 }
134
135 void Ev::touch_release(unsigned btn)
136 {
137         IO::print("touch_release: %s\n", touch->get_button_name(btn));
138 }
139
140 void Ev::touch_motion(unsigned axis, float value, float rel)
141 {
142         IO::print("touch_motion: %s %.3f %+.3f\n", touch->get_axis_name(axis), value, rel);
143 }
144
145 void Ev::gesture_started(unsigned type)
146 {
147         IO::print("gesture_started: %s\n", gesture->get_button_name(type));
148 }
149
150 void Ev::gesture_ended(unsigned type)
151 {
152         IO::print("gesture_ended: %s\n", gesture->get_button_name(type));
153 }
154
155 void Ev::gesture_axis(unsigned axis, float value, float rel)
156 {
157         IO::print("gesture_axis: %s %.3f %.3f\n", gesture->get_axis_name(axis), value, rel);
158 }