Debian Codegraph @

Tests/InputTest/main.cpp

raw
1 #include <ClanLib/application.h>
2 #include <ClanLib/core.h>
3 #include <ClanLib/display.h>
4 #include <ClanLib/gl.h>
5
6 /////////////////////////////////////////////////////////////////////////////
7 // Application class
8
9 class Application : public CL_ClanApplication
10 {
11 public:
12 virtual int main(int argc, char** argv);
13
14 private:
15 void input_test();
16
17 void on_keyboard_down(const CL_InputEvent &key);
18 void on_keyboard_up(const CL_InputEvent &key);
19 void on_keyboard_dblclk(const CL_InputEvent &key);
20 void on_mouse_down(const CL_InputEvent &key);
21 void on_mouse_up(const CL_InputEvent &key);
22 void on_mouse_dblclk(const CL_InputEvent &key);
23 void on_mouse_move(const CL_InputEvent &key);
24 void on_joystick_down(const CL_InputEvent &key);
25 void on_joystick_up(const CL_InputEvent &key);
26 void on_joystick_dblclk(const CL_InputEvent &key);
27 void on_joystick_move(const CL_InputEvent &key);
28
29 CL_InputEvent last_key;
30 std::string last_event;
31
32 CL_SlotContainer slots;
33
34 CL_Font *font;
35 } app;
36
37 /////////////////////////////////////////////////////////////////////////////
38 // Application main entry point:
39
40 int Application::main(int argc, char** argv)
41 {
42 CL_ConsoleWindow debug_console("Debug console");
43 debug_console.redirect_stdio();
44
45 try
46 {
47 CL_SetupCore::init();
48 CL_SetupDisplay::init();
49 CL_SetupGL::init();
50
51 CL_DisplayWindow window("ClanLib Input test program", 800, 600, false, true);
52
53 CL_ResourceManager resources("resources.xml", false);
54 font = new CL_Font("font_gray", &resources);
55
56 input_test();
57
58 delete font;
59
60 CL_SetupDisplay::deinit();
61 CL_SetupCore::deinit();
62 }
63 catch (CL_Error error)
64 {
65 std::cout << "Uncaught error: " << error.message.c_str() << std::endl;
66 debug_console.wait_for_key();
67 }
68
69 return 0;
70 }
71
72 void Application::input_test()
73 {
74 slots.connect(CL_Keyboard::sig_key_down(), this, &Application::on_keyboard_down);
75 slots.connect(CL_Keyboard::sig_key_up(), this, &Application::on_keyboard_up);
76 slots.connect(CL_Keyboard::sig_key_dblclk(), this, &Application::on_keyboard_dblclk);
77
78 slots.connect(CL_Mouse::sig_key_down(), this, &Application::on_mouse_down);
79 slots.connect(CL_Mouse::sig_key_up(), this, &Application::on_mouse_up);
80 slots.connect(CL_Mouse::sig_key_dblclk(), this, &Application::on_mouse_dblclk);
81 slots.connect(CL_Mouse::sig_move(), this, &Application::on_mouse_move);
82
83 /* slots.connect(CL_Joystick::sig_key_down(), this, &Application::on_joystick_down);
84 slots.connect(CL_Joystick::sig_key_up(), this, &Application::on_joystick_up);
85 slots.connect(CL_Joystick::sig_key_dblclk(), this, &Application::on_joystick_dblclk);
86 slots.connect(CL_Joystick::sig_move(), this, &Application::on_joystick_move);
87 */
88 while (!CL_Keyboard::get_keycode(CL_KEY_ESCAPE))
89 {
90 CL_Display::clear();
91
92 int count, i;
93
94 count = CL_Mouse::get_device_count();
95 font->draw(20, 0, "Mouse devices: " + CL_String::from_int(count));
96 for(i = 0; i < count; i++)
97 {
98 std::string str = CL_String::format("- get_device(%1): %2",
99 i,
100 (int)&CL_Mouse::get_device(i)
101 );
102 font->draw(20, 10 + i*10, str);
103 }
104
105 int x = CL_Mouse::get_x();
106 int y = CL_Mouse::get_y();
107
108 font->draw(180, 00, "Mouse x: " + CL_String::from_int(x));
109 font->draw(180, 10, "Mouse y: " + CL_String::from_int(y));
110
111 font->draw(300, 00, CL_String::format("Mouse %1 (%2): %3", "left", CL_Mouse::get_key_name(CL_MOUSE_LEFT), CL_String::from_int(CL_Mouse::get_keycode(CL_MOUSE_LEFT))));
112 font->draw(300, 10, CL_String::format("Mouse %1 (%2): %3", "middle", CL_Mouse::get_key_name(CL_MOUSE_MIDDLE), CL_String::from_int(CL_Mouse::get_keycode(CL_MOUSE_MIDDLE))));
113 font->draw(300, 20, CL_String::format("Mouse %1 (%2): %3", "right", CL_Mouse::get_key_name(CL_MOUSE_RIGHT), CL_String::from_int(CL_Mouse::get_keycode(CL_MOUSE_RIGHT))));
114 font->draw(300, 30, CL_String::format("Mouse %1 (%2): %3", "xbutton1", CL_Mouse::get_key_name(CL_MOUSE_XBUTTON1), CL_String::from_int(CL_Mouse::get_keycode(CL_MOUSE_XBUTTON1))));
115 font->draw(300, 40, CL_String::format("Mouse %1 (%2): %3", "xbutton2", CL_Mouse::get_key_name(CL_MOUSE_XBUTTON2), CL_String::from_int(CL_Mouse::get_keycode(CL_MOUSE_XBUTTON2))));
116
117 font->draw(180, 80, "Press 1, 2 or 3 to position mouse");
118 if(CL_Keyboard::get_keycode(CL_KEY_1))
119 CL_Mouse::set_position(0, 0);
120 if(CL_Keyboard::get_keycode(CL_KEY_2))
121 CL_Mouse::set_position(400, 300);
122 if(CL_Keyboard::get_keycode(CL_KEY_3))
123 CL_Mouse::set_position(799, 599);
124
125 count = CL_Keyboard::get_device_count();
126 font->draw(20, 120, "Keyboard devices: " + CL_String::from_int(count));
127 for(i = 0; i < count; i++)
128 {
129 std::string str = CL_String::format("- get_device(%1): %2",
130 i,
131 (int)&CL_Keyboard::get_device(i)
132 );
133 font->draw(20, 130 + i*10, str);
134 }
135
136 font->draw(300, 120, CL_String::format("Keyboard %1 (%2): %3", "a", CL_Keyboard::get_key_name(CL_KEY_A), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_A))));
137 font->draw(300, 130, CL_String::format("Keyboard %1 (%2): %3", "backspace", CL_Keyboard::get_key_name(CL_KEY_BACKSPACE), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_BACKSPACE))));
138 font->draw(300, 140, CL_String::format("Keyboard %1 (%2): %3", "shift", CL_Keyboard::get_key_name(CL_KEY_SHIFT), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_SHIFT))));
139 font->draw(300, 150, CL_String::format("Keyboard %1 (%2): %3", "left shift", CL_Keyboard::get_key_name(CL_KEY_LSHIFT), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_LSHIFT))));
140 font->draw(300, 160, CL_String::format("Keyboard %1 (%2): %3", "right shift", CL_Keyboard::get_key_name(CL_KEY_RSHIFT), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_RSHIFT))));
141 font->draw(300, 170, CL_String::format("Keyboard %1 (%2): %3", "control", CL_Keyboard::get_key_name(CL_KEY_CONTROL), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_CONTROL))));
142 font->draw(300, 180, CL_String::format("Keyboard %1 (%2): %3", "menu", CL_Keyboard::get_key_name(CL_KEY_MENU), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_MENU))));
143 font->draw(300, 190, CL_String::format("Keyboard %1 (%2): %3", "numpad 0", CL_Keyboard::get_key_name(CL_KEY_NUMPAD0), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_NUMPAD0))));
144 font->draw(300, 200, CL_String::format("Keyboard %1 (%2): %3", "f1", CL_Keyboard::get_key_name(CL_KEY_F1), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_F1))));
145
146 font->draw(550, 120, CL_String::format("Keyboard %1 (%2): %3", "up", CL_Keyboard::get_key_name(CL_KEY_UP), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_UP))));
147 font->draw(550, 130, CL_String::format("Keyboard %1 (%2): %3", "left", CL_Keyboard::get_key_name(CL_KEY_LEFT), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_LEFT))));
148 font->draw(550, 140, CL_String::format("Keyboard %1 (%2): %3", "right", CL_Keyboard::get_key_name(CL_KEY_RIGHT), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_RIGHT))));
149 font->draw(550, 150, CL_String::format("Keyboard %1 (%2): %3", "down", CL_Keyboard::get_key_name(CL_KEY_DOWN), CL_String::from_int(CL_Keyboard::get_keycode(CL_KEY_DOWN))));
150
151 count = CL_Joystick::get_device_count();
152 font->draw(20, 240, "Joystick devices: " + CL_String::from_int(count));
153 for(i = 0; i < count; i++)
154 {
155 std::string str = CL_String::format("- get_device(%1): %2",
156 i,
157 (int)&CL_Joystick::get_device(i)
158 );
159 font->draw(20, 250 + i*10, str);
160 }
161
162 if(!last_event.empty())
163 {
164 font->draw(20, 420, last_event);
165 font->draw(20, 430, "str: " + last_key.str);
166 font->draw(20, 440, "id: " + CL_String::from_int(last_key.id));
167 font->draw(60, 440, last_key.device.get_key_name(last_key.id));
168 /* if(last_key.device.get_type() == CL_InputDevice::keyboard)
169 font->draw(60, 440, CL_String(CL_Keyboard::get_key_name(last_key.id)));
170 if(last_key.device.get_type() == CL_InputDevice::mouse)
171 font->draw(60, 440, CL_String(CL_Mouse::get_key_name(last_key.id)));
172 if(last_key.device.get_type() == CL_InputDevice::joystick)
173 font->draw(60, 440, CL_String(CL_Joystick::get_key_name(last_key.id)));
174 */ font->draw(20, 450, "type: " + CL_String::from_int(last_key.type));
175 font->draw(20, 460, "left_alt: " + CL_String::from_int(last_key.left_alt));
176 font->draw(20, 470, "left_ctrl: " + CL_String::from_int(last_key.left_ctrl));
177 font->draw(20, 480, "left_shift: " + CL_String::from_int(last_key.left_shift));
178 font->draw(20, 490, "right_alt: " + CL_String::from_int(last_key.right_alt));
179 font->draw(20, 500, "right_ctrl: " + CL_String::from_int(last_key.right_ctrl));
180 font->draw(20, 510, "right_shift: " + CL_String::from_int(last_key.right_shift));
181 font->draw(20, 520, "mouse_pos.x: " + CL_String::from_int(last_key.mouse_pos.x));
182 font->draw(20, 530, "mouse_pos.y: " + CL_String::from_int(last_key.mouse_pos.y));
183 font->draw(20, 540, "repeat_count: " + CL_String::from_int(last_key.repeat_count));
184 // font->draw(20, 550, "Device: " + last_key.device);
185 }
186
187 CL_Display::flip();
188 CL_System::keep_alive(10);
189 }
190 }
191
192 void Application::on_keyboard_down(const CL_InputEvent &key)
193 {
194 last_key = key;
195 last_event = "sig_keyboard_down";
196 }
197
198 void Application::on_keyboard_up(const CL_InputEvent &key)
199 {
200 last_key = key;
201 last_event = "sig_keyboard_up";
202 }
203
204 void Application::on_keyboard_dblclk(const CL_InputEvent &key)
205 {
206 last_key = key;
207 last_event = "sig_keyboard_dblclk";
208 }
209
210 void Application::on_mouse_down(const CL_InputEvent &key)
211 {
212 last_key = key;
213 last_event = "sig_mouse_down";
214 }
215
216 void Application::on_mouse_up(const CL_InputEvent &key)
217 {
218 last_key = key;
219 last_event = "sig_mouse_up";
220 }
221
222 void Application::on_mouse_dblclk(const CL_InputEvent &key)
223 {
224 last_key = key;
225 last_event = "sig_mouse_dblclk";
226 }
227
228 void Application::on_mouse_move(const CL_InputEvent &key)
229 {
230 last_key = key;
231 last_event = "sig_mouse_move";
232 }
233
234 void Application::on_joystick_down(const CL_InputEvent &key)
235 {
236 last_key = key;
237 last_event = "sig_joystick_down";
238 }
239
240 void Application::on_joystick_up(const CL_InputEvent &key)
241 {
242 last_key = key;
243 last_event = "sig_joystick_up";
244 }
245
246 void Application::on_joystick_dblclk(const CL_InputEvent &key)
247 {
248 last_key = key;
249 last_event = "sig_joystick_dblclk";
250 }
251
252 void Application::on_joystick_move(const CL_InputEvent &key)
253 {
254 last_key = key;
255 last_event = "sig_joystick_move";
256 }