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