]> git.tdb.fi Git - libs/gui.git/blob - source/input/device.h
Add decorations for things which should be exported from the library
[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 <stdexcept>
7 #include <sigc++/signal.h>
8 #include <msp/graphics/mspgui_api.h>
9 #include <msp/strings/lexicalcast.h>
10
11 namespace Msp {
12 namespace Input {
13
14 class MSPGUI_API device_not_available: public std::runtime_error
15 {
16 public:
17         device_not_available(const std::string &w): std::runtime_error(w) { }
18 };
19
20
21 enum DeviceType
22 {
23         UNSPECIFIED,
24         KEYBOARD,
25         MOUSE,
26         TOUCH_SURFACE,
27         GAME_CONTROLLER
28 };
29
30
31 /**
32 Base class for input devices.  Input devices have two types of controls:
33 buttons and axes.  Buttons are either on or off.  Axes have a floating point
34 value nominally in the range [-1, 1].
35
36 Event handlers return a boolean indicating whether the event is considered
37 processed.  If a handler returns true, no further handlers are invoked.
38 */
39 class MSPGUI_API Device
40 {
41 protected:
42         struct EventAccumulator
43         {
44                 typedef void result_type;
45
46                 template<typename Iter>
47                 result_type operator()(Iter begin, Iter end) const
48                 {
49                         for(Iter i=begin; i!=end; ++i)
50                                 if(*i)
51                                         return;
52                 }
53         };
54
55 public:
56         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_press;
57         sigc::signal<bool, unsigned>::accumulated<EventAccumulator> signal_button_release;
58         sigc::signal<bool, unsigned, float, float>::accumulated<EventAccumulator> signal_axis_motion;
59
60 protected:
61         DeviceType type = UNSPECIFIED;
62         std::string name;
63         std::vector<char> buttons;
64         std::vector<float> axes;
65
66         Device(DeviceType);
67 public:
68         virtual ~Device();
69
70         DeviceType get_type() const { return type; }
71         const std::string &get_name() const { return name; }
72         virtual Device *find_subdevice(DeviceType, unsigned = 0);
73         virtual Device *find_subdevice(const std::string &);
74         bool get_button_state(unsigned) const;
75         float get_axis_value(unsigned) const;
76
77         virtual std::string get_button_name(unsigned) const;
78         virtual std::string get_axis_name(unsigned) const;
79 protected:
80         void set_button_state(unsigned, bool, bool);
81         void set_axis_value(unsigned, float, bool);
82 };
83
84
85 MSPGUI_API void operator>>(const LexicalConverter &, DeviceType &);
86 MSPGUI_API void operator<<(LexicalConverter &, DeviceType);
87
88 } // namespace Input
89 } // namespace Msp
90
91 #endif