]> git.localhorst.tv Git - blank.git/blob - src/io/event.cpp
store players in world save
[blank.git] / src / io / event.cpp
1 #include "event.hpp"
2
3 #include <cctype>
4 #include <ostream>
5
6 using std::ostream;
7
8
9 namespace blank {
10
11 ostream &operator <<(ostream &out, const SDL_Event &evt) {
12         switch (evt.type) {
13 #if SDL_VERSION_ATLEAST(2, 0, 4)
14                 case SDL_AUDIODEVICEADDED:
15                         out << "audio device added: " << evt.adevice;
16                         break;
17                 case SDL_AUDIODEVICEREMOVED:
18                         out << "audio device removed: " << evt.adevice;
19                         break;
20 #endif
21                 case SDL_CONTROLLERAXISMOTION:
22                         out << "controller axis motion: " << evt.caxis;
23                         break;
24                 case SDL_CONTROLLERBUTTONDOWN:
25                         out << "controller button down: " << evt.cbutton;
26                         break;
27                 case SDL_CONTROLLERBUTTONUP:
28                         out << "controller button up: " << evt.cbutton;
29                         break;
30                 case SDL_CONTROLLERDEVICEADDED:
31                         out << "controller device added: " << evt.cdevice;
32                         break;
33                 case SDL_CONTROLLERDEVICEREMOVED:
34                         out << "controller device removed: " << evt.cdevice;
35                         break;
36                 case SDL_CONTROLLERDEVICEREMAPPED:
37                         out << "controller device remapped: " << evt.cdevice;
38                         break;
39                 case SDL_DOLLARGESTURE:
40                         out << "dollar gesture: " << evt.dgesture;
41                         break;
42                 case SDL_DOLLARRECORD:
43                         out << "dollar record: " << evt.dgesture;
44                         break;
45                 case SDL_DROPFILE:
46                         out << "drop file: " << evt.drop;
47                         break;
48                 case SDL_FINGERMOTION:
49                         out << "finger motion: " << evt.tfinger;
50                         break;
51                 case SDL_FINGERDOWN:
52                         out << "finger down: " << evt.tfinger;
53                         break;
54                 case SDL_FINGERUP:
55                         out << "finger up: " << evt.tfinger;
56                         break;
57                 case SDL_KEYDOWN:
58                         out << "key down: " << evt.key;
59                         break;
60                 case SDL_KEYUP:
61                         out << "key up: " << evt.key;
62                         break;
63                 case SDL_JOYAXISMOTION:
64                         out << "joystick axis motion: " << evt.jaxis;
65                         break;
66                 case SDL_JOYBALLMOTION:
67                         out << "joystick ball motion: " << evt.jball;
68                         break;
69                 case SDL_JOYHATMOTION:
70                         out << "joystick hat motion: " << evt.jhat;
71                         break;
72                 case SDL_JOYBUTTONDOWN:
73                         out << "joystick button down: " << evt.jbutton;
74                         break;
75                 case SDL_JOYBUTTONUP:
76                         out << "joystick button up: " << evt.jbutton;
77                         break;
78                 case SDL_JOYDEVICEADDED:
79                         out << "joystick device added: " << evt.jdevice;
80                         break;
81                 case SDL_JOYDEVICEREMOVED:
82                         out << "joystick device removed: " << evt.jdevice;
83                         break;
84                 case SDL_MOUSEMOTION:
85                         out << "mouse motion: " << evt.motion;
86                         break;
87                 case SDL_MOUSEBUTTONDOWN:
88                         out << "mouse button down: " << evt.button;
89                         break;
90                 case SDL_MOUSEBUTTONUP:
91                         out << "mouse button up: " << evt.button;
92                         break;
93                 case SDL_MOUSEWHEEL:
94                         out << "mouse wheel: " << evt.wheel;
95                         break;
96                 case SDL_MULTIGESTURE:
97                         out << "multi gesture: " << evt.mgesture;
98                         break;
99                 case SDL_QUIT:
100                         out << "quit: " << evt.quit;
101                         break;
102                 case SDL_SYSWMEVENT:
103                         out << "sys wm: " << evt.syswm;
104                         break;
105                 case SDL_TEXTEDITING:
106                         out << "text editing: " << evt.edit;
107                         break;
108                 case SDL_TEXTINPUT:
109                         out << "text input: " << evt.text;
110                         break;
111                 case SDL_USEREVENT:
112                         out << "user: " << evt.user;
113                         break;
114                 case SDL_WINDOWEVENT:
115                         out << "window: " << evt.window;
116                         break;
117                 default:
118                         out << "unknown";
119                         break;
120         }
121         return out;
122 }
123
124 ostream &operator <<(ostream &out, const SDL_WindowEvent &evt) {
125         switch (evt.event) {
126                 case SDL_WINDOWEVENT_SHOWN:
127                         out << "shown, window ID: " << evt.windowID;
128                         break;
129                 case SDL_WINDOWEVENT_HIDDEN:
130                         out << "hidden, window ID: " << evt.windowID;
131                         break;
132                 case SDL_WINDOWEVENT_EXPOSED:
133                         out << "exposed, window ID: " << evt.windowID;
134                         break;
135                 case SDL_WINDOWEVENT_MOVED:
136                         out << "moved, window ID: " << evt.windowID
137                                 << ", position: " << evt.data1 << '+' << evt.data2;
138                         break;
139                 case SDL_WINDOWEVENT_RESIZED:
140                         out << "resized, window ID: " << evt.windowID
141                                 << ", size: " << evt.data1 << 'x' << evt.data2;
142                         break;
143                 case SDL_WINDOWEVENT_SIZE_CHANGED:
144                         out << "size changed, window ID: " << evt.windowID
145                                 << ", size: " << evt.data1 << 'x' << evt.data2;
146                         break;
147                 case SDL_WINDOWEVENT_MINIMIZED:
148                         out << "minimized, window ID: " << evt.windowID;
149                         break;
150                 case SDL_WINDOWEVENT_MAXIMIZED:
151                         out << "maximized, window ID: " << evt.windowID;
152                         break;
153                 case SDL_WINDOWEVENT_RESTORED:
154                         out << "restored, window ID: " << evt.windowID;
155                         break;
156                 case SDL_WINDOWEVENT_ENTER:
157                         out << "mouse entered, window ID: " << evt.windowID;
158                         break;
159                 case SDL_WINDOWEVENT_LEAVE:
160                         out << "mouse left, window ID: " << evt.windowID;
161                         break;
162                 case SDL_WINDOWEVENT_FOCUS_GAINED:
163                         out << "focus gained, window ID: " << evt.windowID;
164                         break;
165                 case SDL_WINDOWEVENT_FOCUS_LOST:
166                         out << "focus lost, window ID: " << evt.windowID;
167                         break;
168                 case SDL_WINDOWEVENT_CLOSE:
169                         out << "closed, window ID: " << evt.windowID;
170                         break;
171                 default:
172                         out << "unknown";
173                         break;
174         }
175         return out;
176 }
177
178 ostream &operator <<(ostream &out, const SDL_KeyboardEvent &evt) {
179         out << "window ID: " << evt.windowID
180                 << ", state: " << (evt.state == SDL_PRESSED ? "pressed" : "released")
181                 << ", repeat: " << (evt.repeat ? "yes" : "no")
182                 << ", keysym: " << evt.keysym;
183         return out;
184 }
185
186 ostream &operator <<(ostream &out, const SDL_Keysym &keysym) {
187         out << "scancode: " << int(keysym.scancode)
188                 << ", sym: " << int(keysym.sym);
189         if (std::isprint(keysym.sym)) {
190                 out << " (\"" << char(keysym.sym) << "\")";
191         }
192         if (keysym.mod) {
193                 out << "mod:";
194                 if (keysym.mod & KMOD_LSHIFT) {
195                         out << " LSHIFT";
196                 }
197                 if (keysym.mod & KMOD_RSHIFT) {
198                         out << " RSHIFT";
199                 }
200                 if (keysym.mod & KMOD_LCTRL) {
201                         out << " LCTRL";
202                 }
203                 if (keysym.mod & KMOD_RCTRL) {
204                         out << " RCTRL";
205                 }
206                 if (keysym.mod & KMOD_LALT) {
207                         out << " LALT";
208                 }
209                 if (keysym.mod & KMOD_RALT) {
210                         out << " RALT";
211                 }
212                 if (keysym.mod & KMOD_LGUI) {
213                         out << " LSUPER";
214                 }
215                 if (keysym.mod & KMOD_RGUI) {
216                         out << " RSUPER";
217                 }
218                 if (keysym.mod & KMOD_NUM) {
219                         out << " NUM";
220                 }
221                 if (keysym.mod & KMOD_CAPS) {
222                         out << " CAPS";
223                 }
224                 if (keysym.mod & KMOD_MODE) {
225                         out << " ALTGR";
226                 }
227         }
228         return out;
229 }
230
231 ostream &operator <<(ostream &out, const SDL_TextEditingEvent &evt) {
232         out << "window ID: " << evt.windowID
233                 << ", text: \"" << evt.text
234                 << "\", start: " << evt.start
235                 << ", length: " << evt.length;
236         return out;
237 }
238
239 ostream &operator <<(ostream &out, const SDL_TextInputEvent &evt) {
240         out << "window ID: " << evt.windowID
241                 << ", text: \"" << evt.text << '"';
242         return out;
243 }
244
245 ostream &operator <<(ostream &out, const SDL_MouseMotionEvent &evt) {
246         out << "window ID: " << evt.windowID
247                 << ", mouse ID: " << evt.which
248                 << ", position: " << evt.x << '+' << evt.y
249                 << ", delta: " << evt.xrel << '+' << evt.yrel;
250         if (evt.state) {
251                 out << "buttons:";
252                 if (evt.state & SDL_BUTTON_LMASK) {
253                         out << " left";
254                 }
255                 if (evt.state & SDL_BUTTON_MMASK) {
256                         out << " middle";
257                 }
258                 if (evt.state & SDL_BUTTON_RMASK) {
259                         out << " right";
260                 }
261                 if (evt.state & SDL_BUTTON_X1MASK) {
262                         out << " X1";
263                 }
264                 if (evt.state & SDL_BUTTON_X2MASK) {
265                         out << " X2";
266                 }
267         }
268         return out;
269 }
270
271 ostream &operator <<(ostream &out, const SDL_MouseButtonEvent &evt) {
272         out << "window ID: " << evt.windowID
273                 << ", mouse ID: " << evt.which
274                 << ", button: ";
275         switch (evt.button) {
276                 case SDL_BUTTON_LEFT:
277                         out << "left";
278                         break;
279                 case SDL_BUTTON_MIDDLE:
280                         out << "middle";
281                         break;
282                 case SDL_BUTTON_RIGHT:
283                         out << "right";
284                         break;
285                 case SDL_BUTTON_X1:
286                         out << "X1";
287                         break;
288                 case SDL_BUTTON_X2:
289                         out << "X2";
290                         break;
291                 default:
292                         out << int(evt.button);
293                         break;
294         }
295         out << ", state: " << (evt.state == SDL_PRESSED ? "pressed" : "released")
296                 << ", clicks: " << int(evt.clicks)
297                 << ", position: " << evt.x << '+' << evt.y;
298         return out;
299 }
300
301 ostream &operator <<(ostream &out, const SDL_MouseWheelEvent &evt) {
302         out << "window ID: " << evt.windowID
303                 << ", mouse ID: " << evt.which
304                 << ", delta: " << evt.x << '+' << evt.y
305 #if SDL_VERSION_ATLEAST(2, 0, 4)
306                 << ", direction: " << (evt.direction == SDL_MOUSEWHEEL_NORMAL ? "normal" : "flipped")
307 #endif
308                 ;
309         return out;
310 }
311
312 ostream &operator <<(ostream &out, const SDL_JoyAxisEvent &evt) {
313         out << "joystick ID: " << evt.which
314                 << ", axis ID: " << int(evt.axis)
315                 << ", value: " << (float(evt.value) / 32768.0f);
316         return out;
317 }
318
319 ostream &operator <<(ostream &out, const SDL_JoyBallEvent &evt) {
320         out << "joystick ID: " << evt.which
321                 << ", ball ID: " << int(evt.ball)
322                 << ", delta: " << evt.xrel << '+' << evt.yrel;
323         return out;
324 }
325
326 ostream &operator <<(ostream &out, const SDL_JoyHatEvent &evt) {
327         out << "joystick ID: " << evt.which
328                 << ", hat ID: " << int(evt.hat)
329                 << ", value: ";
330         switch (evt.value) {
331                 case SDL_HAT_LEFTUP:
332                         out << "left up";
333                         break;
334                 case SDL_HAT_UP:
335                         out << "up";
336                         break;
337                 case SDL_HAT_RIGHTUP:
338                         out << "right up";
339                         break;
340                 case SDL_HAT_LEFT:
341                         out << "left";
342                         break;
343                 case SDL_HAT_CENTERED:
344                         out << "center";
345                         break;
346                 case SDL_HAT_RIGHT:
347                         out << "right";
348                         break;
349                 case SDL_HAT_LEFTDOWN:
350                         out << "left down";
351                         break;
352                 case SDL_HAT_DOWN:
353                         out << "down";
354                         break;
355                 case SDL_HAT_RIGHTDOWN:
356                         out << "right down";
357                         break;
358                 default:
359                         out << "unknown";
360                         break;
361         }
362         return out;
363 }
364
365 ostream &operator <<(ostream &out, const SDL_JoyButtonEvent &evt) {
366         out << "joystick ID: " << evt.which
367                 << ", button ID: " << int(evt.button)
368                 << ", state: " << (evt.state == SDL_PRESSED ? "pressed" : "released");
369         return out;
370 }
371
372 ostream &operator <<(ostream &out, const SDL_JoyDeviceEvent &evt) {
373         out << "joystick ID: \"" << evt.which;
374         return out;
375 }
376
377 ostream &operator <<(ostream &out, const SDL_ControllerAxisEvent &evt) {
378         out << "controller ID: " << evt.which
379                 << ", axis ID: " << int(evt.axis)
380                 << ", value: " << (float(evt.value) / 32768.0f);
381         return out;
382 }
383
384 ostream &operator <<(ostream &out, const SDL_ControllerButtonEvent &evt) {
385         out << "controller ID: " << evt.which
386                 << ", button ID: " << int(evt.button)
387                 << ", state: " << (evt.state == SDL_PRESSED ? "pressed" : "released");
388         return out;
389 }
390
391 ostream &operator <<(ostream &out, const SDL_ControllerDeviceEvent &evt) {
392         out << "controller ID: " << evt.which;
393         return out;
394 }
395
396 #if SDL_VERSION_ATLEAST(2, 0, 4)
397 ostream &operator <<(ostream &out, const SDL_AudioDeviceEvent &evt) {
398         out << "device ID: " << evt.which
399                 << ", capture: " << (evt.iscapture ? "yes" : "no");
400         return out;
401 }
402 #endif
403
404 ostream &operator <<(ostream &out, const SDL_QuitEvent &evt) {
405         out << "quit";
406         return out;
407 }
408
409 ostream &operator <<(ostream &out, const SDL_UserEvent &evt) {
410         out << "window ID: " << evt.windowID
411                 << ", code: " << evt.code
412                 << ", data 1: " << evt.data1
413                 << ", data 2: " << evt.data2;
414         return out;
415 }
416
417 ostream &operator <<(ostream &out, const SDL_SysWMEvent &evt) {
418         if (evt.msg) {
419                 out << "with message";
420         } else {
421                 out << "without message";
422         }
423         return out;
424 }
425
426 ostream &operator <<(ostream &out, const SDL_TouchFingerEvent &evt) {
427         out << "device ID: " << evt.touchId
428                 << ", finger ID: " << evt.fingerId
429                 << ", position: " << evt.x << ' ' << evt.y
430                 << ", delta: " << evt.dx << ' ' << evt.dy
431                 << ", pressure: " << evt.pressure;
432         return out;
433 }
434
435 ostream &operator <<(ostream &out, const SDL_MultiGestureEvent &evt) {
436         out << "device ID: " << evt.touchId
437                 << ", theta: " << evt.dTheta
438                 << ", distance: " << evt.dDist
439                 << ", position: " << evt.x << ' ' << evt.y
440                 << ", fingers: " << evt.numFingers;
441         return out;
442 }
443
444 ostream &operator <<(ostream &out, const SDL_DollarGestureEvent &evt) {
445         out << "device ID: " << evt.touchId
446                 << ", gesture ID: " << evt.gestureId
447                 << ", fingers: " << evt.numFingers
448                 << ", error: " << evt.error
449                 << ", position: " << evt.x << ' ' << evt.y;
450         return out;
451 }
452
453 ostream &operator <<(ostream &out, const SDL_DropEvent &evt) {
454         out << "file: " << evt.file;
455         return out;
456 }
457
458 }