]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.cpp
Add a type enumeration for devices
[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 } // namespace Input
75 } // namespace Msp