History

exposed input key bindings to js to enable key remapping

Sam Gondelman committed 11 years ago

Commit 984d4491 parent a12fd5c8 files · +289 −16Browse files

Showing the changes to interface/src/ui/UserInputMapper.cpp (1 of 8 files). Show all

Unified · Split

interface/src/ui/UserInputMapper.cpp+95−1
@@ -13,7 +13,15 @@ · 12 unmodified lines
13 13
14 14
15// UserInputMapper Class15// UserInputMapper Class
16
17// Default contruct allocate the poutput size with the current hardcoded action channels
18UserInputMapper::UserInputMapper() {
19 assignDefaulActionScales();
20 createActionNames();
21}
22
16bool UserInputMapper::registerDevice(uint16 deviceID, const DeviceProxy::Pointer& proxy){23bool UserInputMapper::registerDevice(uint16 deviceID, const DeviceProxy::Pointer& proxy){
24 proxy->_name += " (" + QString::number(deviceID) + ")";
17 _registeredDevices[deviceID] = proxy;25 _registeredDevices[deviceID] = proxy;
18 return true;26 return true;
19}27}
@@ -27,6 +35,12 @@ UserInputMapper::DeviceProxy::Pointer UserInputMapper::getDeviceProxy(const Inpu · 7 unmodified lines
27 }35 }
28}36}
29 37
38void UserInputMapper::resetAllDeviceBindings() {
39 for (auto device : _registeredDevices) {
40 device.second->resetDeviceBindings();
41 }
42}
43
30bool UserInputMapper::addInputChannel(Action action, const Input& input, float scale) {44bool UserInputMapper::addInputChannel(Action action, const Input& input, float scale) {
31 return addInputChannel(action, input, Input(), scale);45 return addInputChannel(action, input, Input(), scale);
32}46}
@@ -37,7 +51,7 @@ bool UserInputMapper::addInputChannel(Action action, const Input& input, const I · 4 unmodified lines
37 qDebug() << "UserInputMapper::addInputChannel: The input comes from a device #" << input.getDevice() << "is unknown. no inputChannel mapped.";51 qDebug() << "UserInputMapper::addInputChannel: The input comes from a device #" << input.getDevice() << "is unknown. no inputChannel mapped.";
38 return false;52 return false;
39 }53 }
40 54
41 auto inputChannel = InputChannel(input, modifier, action, scale);55 auto inputChannel = InputChannel(input, modifier, action, scale);
42 56
43 // Insert or replace the input to modifiers57 // Insert or replace the input to modifiers
@@ -61,6 +75,37 @@ int UserInputMapper::addInputChannels(const InputChannels& channels) { · 17 unmodified lines
61 return nbAdded;75 return nbAdded;
62}76}
63 77
78bool UserInputMapper::removeInputChannel(InputChannel inputChannel) {
79 // Remove from Input to Modifiers map
80 if (inputChannel.hasModifier()) {
81 _inputToModifiersMap.erase(inputChannel._input.getID());
82 }
83
84 // Remove from Action to Inputs map
85 std::pair<ActionToInputsMap::iterator, ActionToInputsMap::iterator> ret;
86 ret = _actionToInputsMap.equal_range(inputChannel._action);
87 for (ActionToInputsMap::iterator it=ret.first; it!=ret.second; ++it) {
88 if (it->second == inputChannel) {
89 _actionToInputsMap.erase(it);
90 return true;
91 }
92 }
93
94 return false;
95}
96
97void UserInputMapper::removeAllInputChannels() {
98 _inputToModifiersMap.clear();
99 _actionToInputsMap.clear();
100}
101
102void UserInputMapper::removeAllInputChannelsForDevice(uint16 device) {
103 QVector<InputChannel> channels = getAllInputsForDevice(device);
104 for (auto& channel : channels) {
105 removeInputChannel(channel);
106 }
107}
108
64int UserInputMapper::getInputChannels(InputChannels& channels) const {109int UserInputMapper::getInputChannels(InputChannels& channels) const {
65 for (auto& channel : _actionToInputsMap) {110 for (auto& channel : _actionToInputsMap) {
66 channels.push_back(channel.second);111 channels.push_back(channel.second);
@@ -69,6 +114,20 @@ int UserInputMapper::getInputChannels(InputChannels& channels) const { · 2 unmodified lines
69 return _actionToInputsMap.size();114 return _actionToInputsMap.size();
70}115}
71 116
117QVector<UserInputMapper::InputChannel> UserInputMapper::getAllInputsForDevice(uint16 device) {
118 InputChannels allChannels;
119 getInputChannels(allChannels);
120
121 QVector<InputChannel> channels;
122 for (InputChannel inputChannel : allChannels) {
123 if (inputChannel._input._device == device) {
124 channels.push_back(inputChannel);
125 }
126 }
127
128 return channels;
129}
130
72void UserInputMapper::update(float deltaTime) {131void UserInputMapper::update(float deltaTime) {
73 132
74 // Reset the axis state for next loop133 // Reset the axis state for next loop
@@ -130,6 +189,24 @@ void UserInputMapper::update(float deltaTime) { · 55 unmodified lines
130 }189 }
131}190}
132 191
192QVector<UserInputMapper::Action> UserInputMapper::getAllActions() {
193 QVector<Action> actions;
194 for (auto i = 0; i < NUM_ACTIONS; i++) {
195 actions.append(Action(i));
196 }
197 return actions;
198}
199
200QVector<UserInputMapper::InputChannel> UserInputMapper::getInputChannelsForAction(UserInputMapper::Action action) {
201 QVector<InputChannel> inputChannels;
202 std::pair <ActionToInputsMap::iterator, ActionToInputsMap::iterator> ret;
203 ret = _actionToInputsMap.equal_range(action);
204 for (ActionToInputsMap::iterator it=ret.first; it!=ret.second; ++it) {
205 inputChannels.append(it->second);
206 }
207 return inputChannels;
208}
209
133void UserInputMapper::assignDefaulActionScales() {210void UserInputMapper::assignDefaulActionScales() {
134 _actionScales[LONGITUDINAL_BACKWARD] = 1.0f; // 1m per unit211 _actionScales[LONGITUDINAL_BACKWARD] = 1.0f; // 1m per unit
135 _actionScales[LONGITUDINAL_FORWARD] = 1.0f; // 1m per unit212 _actionScales[LONGITUDINAL_FORWARD] = 1.0f; // 1m per unit
@@ -144,3 +221,20 @@ void UserInputMapper::assignDefaulActionScales() { · 8 unmodified lines
144 _actionScales[BOOM_IN] = 1.0f; // 1m per unit221 _actionScales[BOOM_IN] = 1.0f; // 1m per unit
145 _actionScales[BOOM_OUT] = 1.0f; // 1m per unit222 _actionScales[BOOM_OUT] = 1.0f; // 1m per unit
146}223}
224
225// This is only necessary as long as the actions are hardcoded
226// Eventually you can just add the string when you add the action
227void UserInputMapper::createActionNames() {
228 _actionNames[LONGITUDINAL_BACKWARD] = "LONGITUDINAL_BACKWARD";
229 _actionNames[LONGITUDINAL_FORWARD] = "LONGITUDINAL_FORWARD";
230 _actionNames[LATERAL_LEFT] = "LATERAL_LEFT";
231 _actionNames[LATERAL_RIGHT] = "LATERAL_RIGHT";
232 _actionNames[VERTICAL_DOWN] = "VERTICAL_DOWN";
233 _actionNames[VERTICAL_UP] = "VERTICAL_UP";
234 _actionNames[YAW_LEFT] = "YAW_LEFT";
235 _actionNames[YAW_RIGHT] = "YAW_RIGHT";
236 _actionNames[PITCH_DOWN] = "PITCH_DOWN";
237 _actionNames[PITCH_UP] = "PITCH_UP";
238 _actionNames[BOOM_IN] = "BOOM_IN";
239 _actionNames[BOOM_OUT] = "BOOM_OUT";
240}