2 #include <msp/strings/format.h>
9 Device::Device(DeviceType t):
16 Device *Device::find_subdevice(const string &n)
18 return (n==name ? this : 0);
21 Device *Device::find_subdevice(DeviceType t, unsigned i)
23 return (t==type && i==0 ? this : 0);
26 bool Device::get_button_state(unsigned btn) const
28 if(btn>=buttons.size())
34 float Device::get_axis_value(unsigned axis) const
42 std::string Device::get_button_name(unsigned btn) const
44 return format("Button %d", btn);
47 std::string Device::get_axis_name(unsigned axis) const
49 return format("Axis %d", axis);
52 void Device::set_button_state(unsigned btn, bool state, bool event)
54 if(btn>=buttons.size())
55 buttons.resize(btn+1, false);
57 if(state!=buttons[btn])
64 signal_button_press.emit(btn);
66 signal_button_release.emit(btn);
71 void Device::set_axis_value(unsigned axis, float value, bool event)
74 axes.resize(axis+1, 0);
78 float old = axes[axis];
82 signal_axis_motion.emit(axis, value, value-old);
87 void operator>>(const LexicalConverter &conv, DeviceType &type)
89 if(conv.get()=="UNSPECIFIED")
91 else if(conv.get()=="KEYBOARD")
93 else if(conv.get()=="MOUSE")
95 else if(conv.get()=="TOUCH_SURFACE")
97 else if(conv.get()=="GAME_CONTROLLER")
98 type = GAME_CONTROLLER;
100 throw lexical_error(format("conversion of '%s' to DeviceType", conv.get()));
103 void operator<<(LexicalConverter &conv, DeviceType type)
107 case UNSPECIFIED: conv.result("UNSPECIFIED"); break;
108 case KEYBOARD: conv.result("KEYBOARD"); break;
109 case MOUSE: conv.result("MOUSE"); break;
110 case TOUCH_SURFACE: conv.result("TOUCH_SURFACE"); break;
111 case GAME_CONTROLLER: conv.result("GAME_CONTROLLER"); break;
112 default: conv.result(format("DeviceType(%#x)", static_cast<int>(type)));