]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.h
Allow event handling to be interrupted
[libs/gui.git] / source / input / device.h
1 #ifndef MSP_INPUT_INPUTDEVICE_H_
2 #define MSP_INPUT_INPUTDEVICE_H_
3
4 #include <string>
5 #include <vector>
6 #include <sigc++/signal.h>
7
8 namespace Msp {
9 namespace Input {
10
11 /**
12 Base class for input devices.  Input devices have two types of controls:
13 buttons and axes.  Buttons are either on or off.  Axes have a floating point
14 value in the range [-1, 1].
15
16 Event handlers return a boolean indicating whether the event is considered
17 processed.  If a handler returns true, no further handlers are invoked.
18 */
19 class Device
20 {
21 protected:
22         struct EventAccumulator
23         {
24                 typedef void result_type;
25
26                 template<typename Iter>
27                 result_type operator()(Iter begin, Iter end) const
28                 {
29                         for(Iter i=begin; i!=end; ++i)
30                                 if(*i)
31                                         return;
32                 }
33         };
34
35 public:
36         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_press;
37         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_release;
38         sigc::signal<bool, unsigned, float, float>::accumulated<EventAccumulator> signal_axis_motion;
39
40 protected:
41         std::string name;
42         std::vector<char> buttons;
43         std::vector<float> axes;
44
45         Device() { }
46 public:
47         virtual ~Device() { }
48         const std::string &get_name() const { return name; }
49         bool get_button_state(unsigned) const;
50         float get_axis_value(unsigned) const;
51
52         virtual std::string get_button_name(unsigned) const;
53         virtual std::string get_axis_name(unsigned) const;
54 protected:
55         void set_button_state(unsigned, bool, bool);
56         void set_axis_value(unsigned, float, bool);
57 };
58
59 } // namespace Input
60 } // namespace Msp
61
62 #endif