2 #include <msp/strings/format.h>
4 #include "controlscheme.h"
11 vector<Device *> Bindings::resolve_devices(Device &dev) const
13 vector<Device *> resolved_devs;
14 resolved_devs.reserve(devices.size());
15 for(const DeviceRef &d: devices)
17 if(d.type!=UNSPECIFIED)
18 resolved_devs.push_back(dev.find_subdevice(d.type));
19 else if(!d.name.empty())
20 resolved_devs.push_back(dev.find_subdevice(d.name));
22 resolved_devs.push_back(0);
27 bool Bindings::is_compatible(Device &dev) const
29 vector<Device *> resolved_devs = resolve_devices(dev);
30 return all_of(resolved_devs.begin(), resolved_devs.end(), [](Device *d) -> bool { return d; });
33 bool Bindings::apply_to(ControlScheme &control_scheme, Device &dev)
35 vector<Device *> resolved_devs = resolve_devices(dev);
37 for(const Binding &b: bindings)
39 Control *ctrl = control_scheme.find(b.control);
40 Device *bdev = (b.device<resolved_devs.size() ? resolved_devs[b.device] : 0);
43 ctrl->set_source(*bdev, b.type, b.index);
52 DataFile::Loader::ActionMap Bindings::Loader::shared_actions;
54 Bindings::Loader::Loader(Bindings &b):
55 ObjectLoader<Bindings>(b)
57 set_actions(shared_actions);
60 void Bindings::Loader::init_actions()
62 add("binding", &Loader::binding);
63 add("device", &Loader::device_type);
64 add("device", &Loader::device_name);
67 void Bindings::Loader::binding(const string &c)
72 obj.bindings.push_back(bind);
75 void Bindings::Loader::device_type(DeviceType t)
79 obj.devices.push_back(dev);
82 void Bindings::Loader::device_name(const string &n)
86 obj.devices.push_back(dev);
90 DataFile::Loader::ActionMap Bindings::Binding::Loader::shared_actions;
92 Bindings::Binding::Loader::Loader(Binding &b):
93 ObjectLoader<Binding>(b)
95 set_actions(shared_actions);
98 void Bindings::Binding::Loader::init_actions()
100 add("axis", &Loader::axis);
101 add("button", &Loader::button);
102 add("device", &Binding::device);
103 add("key", &Loader::key);
104 add("mouse_axis", &Loader::mouse_axis);
105 add("mouse_button", &Loader::mouse_button);
108 void Bindings::Binding::Loader::axis(unsigned a, AxisSide s)
110 obj.type = (s==NEGATIVE ? AXIS_NEG : AXIS_POS);
114 void Bindings::Binding::Loader::button(unsigned b)
121 void operator>>(const LexicalConverter &conv, Bindings::AxisSide &side)
123 if(conv.get()=="POSITIVE")
124 side = Bindings::POSITIVE;
125 else if(conv.get()=="NEGATIVE")
126 side = Bindings::NEGATIVE;
128 throw lexical_error(format("conversion of '%s' to AxisSide", conv.get()));
131 void operator<<(LexicalConverter &conv, Bindings::AxisSide side)
135 case Bindings::POSITIVE: conv.result("POSITIVE"); break;
136 case Bindings::NEGATIVE: conv.result("NEGATIVE"); break;
137 default: conv.result(format("AxisSide(%#x)", static_cast<int>(side)));