Repository navigation
Expand file tree
/
Copy pathdevice_config.cpp
More file actions
375 lines (336 loc) · 12.6 KB
/
Copy pathdevice_config.cpp
File metadata and controls
375 lines (336 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2010-2015 SuperTuxKart-Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "input/device_config.hpp"
#include "input/gamepad_config.hpp"
#include "input/keyboard_config.hpp"
#include "io/xml_node.hpp"
#include "io/utf_writer.hpp"
#include "utils/log.hpp"
#include <SKeyMap.h>
#include <assert.h>
using namespace irr;
// ------------------------------------------------------------------------
/** A simple factory that creates either a gamepad or a keyboard
* configuration.
* \param type "gamepad" or "keyboard".
* \param config The XML node with additional configuration parameters.
*/
DeviceConfig* DeviceConfig::create(const XMLNode *config)
{
DeviceConfig *device_config = NULL;
if(config->getName()=="keyboard")
{
device_config = new KeyboardConfig();
}
else if(config->getName()=="gamepad")
{
device_config = new GamepadConfig();
}
else
{
Log::error("DeviceConfig", "Incorrect type: '%s'.",
config->getName().c_str());
return NULL;
}
// A default keyboard etc is created without
if(config && !device_config->load(config))
{
delete device_config;
return NULL;
}
return device_config;
} // create
// ------------------------------------------------------------------------
DeviceConfig::DeviceConfig()
{
m_name = "";
m_enabled = true;
m_plugged = 0;
m_config_name= L"";
} // DeviceConfig
// ------------------------------------------------------------------------
/** Get a user-readable string describing the bound action.
*/
irr::core::stringw DeviceConfig::getBindingAsString(const PlayerAction action) const
{
irr::core::stringw return_string = "";
if ((action < PA_COUNT) && (action >= 0))
{
return_string = m_bindings[action].getAsString();
}
return return_string;
} // getBindingAsString
//------------------------------------------------------------------------------
/** Get an internal unique string describing the bound action.
* \param action The action for which to get the string.
*/
irr::core::stringw DeviceConfig::getMappingIdString (const PlayerAction action) const
{
irr::core::stringw return_string = "";
if ((action < PA_COUNT) && (action >= 0))
{
const Input::InputType type = m_bindings[action].getType();
const int id = m_bindings[action].getId();
const Input::AxisDirection dir = m_bindings[action].getDirection();
const Input::AxisRange range = m_bindings[action].getRange();
switch (type)
{
case Input::IT_KEYBOARD:
return_string += "keyb_";
return_string += id;
break;
case Input::IT_STICKMOTION:
return_string += "stkmo_";
return_string += id;
return_string += "$";
return_string += dir;
return_string += "$";
return_string += range;
break;
case Input::IT_STICKBUTTON:
return_string += "stkbt_";
return_string += id;
break;
case Input::IT_MOUSEMOTION:
return_string += "mousemo_";
return_string += id;
return_string += "$";
return_string += dir;
break;
case Input::IT_MOUSEBUTTON:
return_string += "mousebtn_";
return_string += id;
break;
case Input::IT_NONE:
return_string += "none";
break;
default:
assert(false);
return_string += type;
return_string += "_";
return_string += id;
return_string += "$";
return_string += dir;
}
}
return return_string;
} // getMappingIdString
//------------------------------------------------------------------------------
irr::core::stringw DeviceConfig::toString ()
{
irr::core::stringw return_string = "";
for (int n = 0; n < PA_COUNT; n++)
{
return_string += KartActionStrings[n].c_str();
return_string += ": ";
return_string += m_bindings[n].getAsString();
return_string += "\n";
}
return return_string;
} // toString
//------------------------------------------------------------------------------
/** Sets the bindings for an action.
* \param action The action to be bound.
* \param type Input type (stick button, stick motion, ...).
* \param id An id for this binding.
* \param direction In which direction the stick is moved.
* \param
*/
void DeviceConfig::setBinding ( const PlayerAction action,
const Input::InputType type,
const int id,
Input::AxisDirection direction,
Input::AxisRange range)
{
m_bindings[action].set(type, id, direction, range);
} // setBinding
//------------------------------------------------------------------------------
/** Searches for a game actions associated with the given input event.
* \note Don't call this directly unless you are KeyboardDevice or
* GamepadDevice.
* \param[out] action the result, only set if method returned true.
* \return whether finding an action associated to this input was
* successful.
*/
bool DeviceConfig::getGameAction(Input::InputType type,
const int id,
int* value, /* inout */
PlayerAction* action /* out */ )
{
return doGetAction(type, id, value, PA_FIRST_GAME_ACTION,
PA_LAST_GAME_ACTION, action);
} // getGameAction
//------------------------------------------------------------------------------
/** Searches for a game actions associated with the given input event.
* \note Don't call this directly unless you are KeyboardDevice or
* GamepadDevice.
* \param[out] action The result, only set if method returned true.
* \return Whether finding an action associated to this input
* was successful
*/
bool DeviceConfig::getMenuAction(Input::InputType type,
const int id,
int* value,
PlayerAction* action /* out */ )
{
return doGetAction(type, id, value, PA_FIRST_MENU_ACTION,
PA_LAST_MENU_ACTION, action);
} // getMenuAction
//------------------------------------------------------------------------------
/** internal helper method for DeviceConfig::getGameAction and
* DeviceConfig::getMenuAction
*/
bool DeviceConfig::doGetAction(Input::InputType type,
const int id,
int* value, /* inout */
const PlayerAction firstActionToCheck,
const PlayerAction lastActionToCheck,
PlayerAction* action /* out */ )
{
if (!m_enabled) return false;
bool success = false;
int n;
for (n = firstActionToCheck; ((n <= lastActionToCheck) && (!success)); n++)
{
if ((m_bindings[n].getType() == type) && (m_bindings[n].getId() == id))
{
if (type == Input::IT_STICKMOTION)
{
if(m_bindings[n].getRange() == Input::AR_HALF)
{
if ( ((m_bindings[n].getDirection() == Input::AD_POSITIVE)
&& (*value > 0)) ||
((m_bindings[n].getDirection() == Input::AD_NEGATIVE)
&& (*value < 0)) )
{
success = true;
*action = (PlayerAction)n;
}
}
else
{
if ( ((m_bindings[n].getDirection() == Input::AD_POSITIVE)
&& (*value != -Input::MAX_VALUE)) ||
((m_bindings[n].getDirection() == Input::AD_NEGATIVE)
&& (*value != Input::MAX_VALUE)) )
{
success = true;
*action = (PlayerAction)n;
if(m_bindings[n].getDirection() == Input::AD_NEGATIVE)
*value = -*value;
*value = (*value + Input::MAX_VALUE) / 2;
}
}
}
else
{
success = true;
*action = (PlayerAction)n;
}
}
} // end for n
return success;
} // doGetAction
//------------------------------------------------------------------------------
/** Saves the configuration to a file. The calling node must have written
* the beginning of the xml node, so that this function can immediately
* start writing attributes.
* \param stream The stream to save to.
*/
void DeviceConfig::save (std::ofstream& stream)
{
stream << "enabled=\""
<< (m_enabled ? "true\"" : "false\"")
<< " configName=\""
<< StringUtils::xmlEncode(m_config_name)
<< "\">\n ";
for(int n = 0; n < PA_COUNT; n++) // Start at 0?
{
stream << " "
<< "<action "
<< "name=\"" << KartActionStrings[n] << "\" ";
m_bindings[n].save(stream);
stream << "/>\n";
}
} // save
//------------------------------------------------------------------------------
/** Reads a device configuration from input.xml.
* \param config The XML Node of the configuration.
* \return False if an error occurred.
*/
bool DeviceConfig::load(const XMLNode *config)
{
config->get("name", &m_name);
config->get("enabled", &m_enabled);
config->getAndDecode("configName", &m_config_name);
bool error = false;
for(unsigned int i=0; i<config->getNumNodes(); i++)
{
const XMLNode *action = config->getNode(i);
if(action->getName()!="action")
{
Log::warn("DeviceConfig", "Invalid configuration '%s' - ignored.", action->getName().c_str());
continue;
}
std::string name;
action->get("name", &name);
int binding_id = -1;
for (int i = 0; i < PA_COUNT; i++)
{
if (name==KartActionStrings[i])
{
binding_id = i;
break;
}
}
if (binding_id == -1)
{
Log::warn("DeviceConfig",
"DeviceConfig::deserializeAction : action '%s' is unknown.",
name.c_str());
error=true;
continue;
}
if(!m_bindings[binding_id].load(action))
{
Log::error("Device manager",
"Ignoring an ill-formed keyboard action in input config.");
error=true;
}
} // for i in nodes
return !error;
} // load
//-----------------------------------------------------------------------------
bool DeviceConfig::hasBindingFor(const int button_id) const
{
for (int n=0; n<PA_COUNT; n++)
{
if (m_bindings[n].getId() == button_id) return true;
}
return false;
} // hasBindingFor
//-----------------------------------------------------------------------------
bool DeviceConfig::hasBindingFor(const int button_id, PlayerAction from,
PlayerAction to) const
{
for (int n=from; n<=to; n++)
{
if (m_bindings[n].getId() == button_id) return true;
}
return false;
} // hasBindingFor