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