]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.cpp
1fdb170db8d70c95534680844e9199f1474e0550
[libs/gui.git] / source / input / device.cpp
1 #include <msp/strings/formatter.h>
2 #include "device.h"
3
4 namespace Msp {
5 namespace Input {
6
7 bool Device::get_button_state(unsigned btn) const
8 {
9         if(btn>buttons.size())
10                 return false;
11
12         return buttons[btn];
13 }
14
15 float Device::get_axis_value(unsigned axis) const
16 {
17         if(axis>axes.size())
18                 return 0;
19
20         return axes[axis];
21 }
22
23 std::string Device::get_button_name(unsigned btn) const
24 {
25         return format("Button %d", btn);
26 }
27
28 std::string Device::get_axis_name(unsigned axis) const
29 {
30         return format("Axis %d", axis);
31 }
32
33 void Device::set_button_state(unsigned btn, bool state, bool event)
34 {
35         if(btn>=buttons.size())
36                 buttons.resize(btn+1, false);
37
38         if(state!=buttons[btn])
39         {
40                 buttons[btn] = state;
41
42                 if(event)
43                 {
44                         if(state)
45                                 signal_button_press.emit(btn);
46                         else
47                                 signal_button_release.emit(btn);
48                 }
49         }
50 }
51
52 void Device::set_axis_value(unsigned axis, float value, bool event)
53 {
54         if(axis>=axes.size())
55                 axes.resize(axis+1, 0);
56
57         if(value!=axes[axis])
58         {
59                 float old = axes[axis];
60                 axes[axis] = value;
61
62                 if(event)
63                         signal_axis_motion.emit(axis, value, value-old);
64         }
65 }
66
67 } // namespace Input
68 } // namespace Msp