]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.cpp
Add conversions to/from string for key names and device types
[libs/gui.git] / source / input / device.cpp
1 #include <msp/strings/format.h>
2 #include "device.h"
3
4 namespace Msp {
5 namespace Input {
6
7 Device::Device(DeviceType t):
8         type(t)
9 { }
10
11 Device::~Device()
12 { }
13
14 bool Device::get_button_state(unsigned btn) const
15 {
16         if(btn>=buttons.size())
17                 return false;
18
19         return buttons[btn];
20 }
21
22 float Device::get_axis_value(unsigned axis) const
23 {
24         if(axis>=axes.size())
25                 return 0;
26
27         return axes[axis];
28 }
29
30 std::string Device::get_button_name(unsigned btn) const
31 {
32         return format("Button %d", btn);
33 }
34
35 std::string Device::get_axis_name(unsigned axis) const
36 {
37         return format("Axis %d", axis);
38 }
39
40 void Device::set_button_state(unsigned btn, bool state, bool event)
41 {
42         if(btn>=buttons.size())
43                 buttons.resize(btn+1, false);
44
45         if(state!=buttons[btn])
46         {
47                 buttons[btn] = state;
48
49                 if(event)
50                 {
51                         if(state)
52                                 signal_button_press.emit(btn);
53                         else
54                                 signal_button_release.emit(btn);
55                 }
56         }
57 }
58
59 void Device::set_axis_value(unsigned axis, float value, bool event)
60 {
61         if(axis>=axes.size())
62                 axes.resize(axis+1, 0);
63
64         if(value!=axes[axis])
65         {
66                 float old = axes[axis];
67                 axes[axis] = value;
68
69                 if(event)
70                         signal_axis_motion.emit(axis, value, value-old);
71         }
72 }
73
74
75 void operator>>(const LexicalConverter &conv, DeviceType &type)
76 {
77         if(conv.get()=="UNSPECIFIED")
78                 type = UNSPECIFIED;
79         else if(conv.get()=="KEYBOARD")
80                 type = KEYBOARD;
81         else if(conv.get()=="MOUSE")
82                 type = MOUSE;
83         else if(conv.get()=="TOUCH_SURFACE")
84                 type = TOUCH_SURFACE;
85         else if(conv.get()=="GAME_CONTROLLER")
86                 type = GAME_CONTROLLER;
87         else
88                 throw lexical_error(format("conversion of '%s' to DeviceType", conv.get()));
89 }
90
91 void operator<<(LexicalConverter &conv, DeviceType type)
92 {
93         switch(type)
94         {
95         case UNSPECIFIED: conv.result("UNSPECIFIED"); break;
96         case KEYBOARD: conv.result("KEYBOARD"); break;
97         case MOUSE: conv.result("MOUSE"); break;
98         case TOUCH_SURFACE: conv.result("TOUCH_SURFACE"); break;
99         case GAME_CONTROLLER: conv.result("GAME_CONTROLLER"); break;
100         default: conv.result(format("DeviceType(%#x)", static_cast<int>(type)));
101         }
102 }
103
104 } // namespace Input
105 } // namespace Msp