]> git.tdb.fi Git - libs/gui.git/blob - source/input/bindings.h
b5e3a3b225f15aa03f9a795d1a46b67c392c0763
[libs/gui.git] / source / input / bindings.h
1 #ifndef MSP_INPUT_BINDINGS_H_
2 #define MSP_INPUT_BINDINGS_H_
3
4 #include <msp/datafile/objectloader.h>
5 #include <msp/strings/lexicalcast.h>
6 #include "control.h"
7 #include "device.h"
8 #include "keys.h"
9
10 namespace Msp {
11 namespace Input {
12
13 class ControlScheme;
14
15 class Bindings
16 {
17 public:
18         class Loader: public DataFile::ObjectLoader<Bindings>
19         {
20         private:
21                 static ActionMap shared_actions;
22
23         public:
24                 Loader(Bindings &);
25
26         private:
27                 void init_actions() override;
28
29                 void binding(const std::string &);
30                 void device_type(DeviceType);
31                 void device_name(const std::string &);
32         };
33
34         struct DeviceRef
35         {
36                 DeviceType type = UNSPECIFIED;
37                 std::string name;
38         };
39
40 private:
41         enum AxisSide
42         {
43                 POSITIVE,
44                 NEGATIVE
45         };
46
47         friend void operator>>(const LexicalConverter &, AxisSide &);
48         friend void operator<<(LexicalConverter &, AxisSide);
49
50 public:
51         struct Binding
52         {
53                 class Loader: public DataFile::ObjectLoader<Binding>
54                 {
55                 private:
56                         static ActionMap shared_actions;
57
58                 public:
59                         Loader(Binding &);
60
61                 private:
62                         void init_actions() override;
63
64                         void axis(unsigned, AxisSide);
65                         void button(unsigned);
66                         void key(Key k) { button(k); }
67                         void mouse_axis(MouseAxis a, AxisSide s) { axis(a, s); }
68                         void mouse_button(MouseButton b) { button(b); }
69                 };
70
71                 std::string control;
72                 unsigned device;
73                 ControlSrcType type;
74                 unsigned index;
75         };
76
77 private:
78         std::vector<DeviceRef> devices;
79         std::vector<Binding> bindings;
80
81 public:
82         const std::vector<DeviceRef> &get_devices() const { return devices; }
83         std::vector<Device *> resolve_devices(Device &) const;
84         bool is_compatible(Device &) const;
85         const std::vector<Binding> &get_bindings() const { return bindings; }
86
87         bool apply_to(ControlScheme &, Device &);
88 };
89
90 } // namespace Input
91 } // namespace Msp
92
93 #endif