25
25
* \file controller_layout.cpp
26
* \brief Implementation of the bear::engine::controller_layout class.
26
* \brief Implementation of the ptb::controller_layout class.
27
27
* \author Julien Jorge
29
29
#include "ptb/controller_layout.hpp"
32
#include <claw/configuration_file.hpp>
31
33
#include <claw/logger.hpp>
33
35
/*----------------------------------------------------------------------------*/
37
ptb::controller_layout::s_action_keyboard_section = "Action/Keyboard";
39
ptb::controller_layout::s_action_joystick_section = "Action/Joystick";
41
ptb::controller_layout::s_action_mouse_section = "Action/Mouse";
43
ptb::controller_layout::s_command_keyboard_section = "GUI/Keyboard";
45
ptb::controller_layout::s_command_joystick_section = "GUI/Joystick";
47
ptb::controller_layout::s_command_mouse_section = "GUI/Mouse";
49
/*----------------------------------------------------------------------------*/
51
* \brief Get the controller button associated with an action.
52
* \param a The action to which the button is associated.
54
bear::input::controller_button
55
ptb::controller_layout::get_from_action( player_action::value_type a ) const
57
return m_action.find(a);
58
} // controller_layout::get_from_action()
60
/*----------------------------------------------------------------------------*/
62
* \brief Get the controller button associated with a command.
63
* \param c The command to which the button is associated.
65
bear::input::controller_button
66
ptb::controller_layout::get_from_command( gui_command::value_type c ) const
68
return m_command.find(c);
69
} // controller_layout::get_from_command()
71
/*----------------------------------------------------------------------------*/
73
* \brief Set the button associated with an action.
74
* \param key The button.
75
* \param a The action.
77
void ptb::controller_layout::set_action
78
( const bear::input::controller_button& key, player_action::value_type a )
81
} // controller_layout::set_action()
83
/*----------------------------------------------------------------------------*/
85
* \brief Set the button associated with a gui command.
86
* \param key The button.
87
* \param c The command.
89
void ptb::controller_layout::set_command
90
( const bear::input::controller_button& key, gui_command::value_type c )
92
m_command.set(key, c);
93
} // controller_layout::set_command()
95
/*----------------------------------------------------------------------------*/
35
97
* \brief Set the action associated to a keyboard key.
36
98
* \param key The code of the key.
37
99
* \param a The action done with this key.
39
void ptb::controller_layout::set_key
101
void ptb::controller_layout::set_action_key
40
102
( bear::input::key_code key, player_action::value_type a )
45
} // controller_layout::set_key()
104
m_command.remove_key(key);
105
m_action.set_key(key, a);
106
} // controller_layout::set_action_key()
47
108
/*----------------------------------------------------------------------------*/
50
111
* \param key The code of the key.
52
113
ptb::player_action::value_type
53
ptb::controller_layout::operator()( bear::input::key_code key ) const
55
keyboard_map::const_iterator it;
57
it = m_keyboard.find(key);
59
if ( it != m_keyboard.end() )
62
return player_action::action_null;
63
} // controller_layout::operator()( key_code ) [const]
114
ptb::controller_layout::get_action_from_key( bear::input::key_code key ) const
116
return m_action.find_key_value(key, player_action::action_null);
117
} // controller_layout::get_action_from_key()
119
/*----------------------------------------------------------------------------*/
121
* \brief Get the keyboard key associated with an action.
122
* \return bear::input::keyboard::kc_not_a_key if no key is associated with the
125
bear::input::key_code
126
ptb::controller_layout::get_key_from_action( player_action::value_type a ) const
128
return m_action.find_key(a);
129
} // controller_layout::get_key_from_action()
131
/*----------------------------------------------------------------------------*/
133
* \brief Set the action associated to a button of a joystick.
134
* \param joy The joystick button.
135
* \param a The action done with this key.
137
void ptb::controller_layout::set_action_joystick
138
( const bear::input::joystick_button& joy, player_action::value_type a )
140
set_action_joystick(joy.joystick_index, joy.button, a);
141
} // controller_layout::set_action_joystick()
65
143
/*----------------------------------------------------------------------------*/
85
162
* \param key The code of the key.
87
164
ptb::player_action::value_type
88
ptb::controller_layout::operator()
165
ptb::controller_layout::get_action_from_joystick
89
166
( unsigned int joy, bear::input::joystick::joy_code key ) const
91
joystick_map::const_iterator it;
93
it = m_joystick.find( bear::input::joystick_button(joy, key) );
95
if ( it != m_joystick.end() )
98
return player_action::action_null;
99
} // controller_layout::operator()( unsigned int, joy_code ) [const]
168
return m_action.find_joystick_value(joy, key, player_action::action_null);
169
} // controller_layout::get_action_from_joystick()
171
/*----------------------------------------------------------------------------*/
173
* \brief Get the joystick key and index associated with an action.
174
* \return result.button == bear::input::joystick::jc_invalid if no key is
175
* associated with the action.
177
bear::input::joystick_button
178
ptb::controller_layout::get_joystick_from_action
179
( ptb::player_action::value_type a ) const
181
return m_action.find_joystick(a);
182
} // controller_layout::get_joystick_from_action()
101
184
/*----------------------------------------------------------------------------*/
118
200
* \param button The code of the button.
120
202
ptb::player_action::value_type
121
ptb::controller_layout::operator()
203
ptb::controller_layout::get_action_from_mouse
122
204
( bear::input::mouse::mouse_code button ) const
124
mouse_map::const_iterator it;
126
it = m_mouse.find(button);
128
if ( it != m_mouse.end() )
131
return player_action::action_null;
132
} // controller_layout::operator()( mouse_code ) [const]
134
/*----------------------------------------------------------------------------*/
136
* \brief Get the keyboard key associated with an action.
137
* \return bear::input::keyboard::kc_not_a_key if no key is associated with the
140
bear::input::key_code
141
ptb::controller_layout::find_key( ptb::player_action::value_type a ) const
143
bear::input::key_code result = bear::input::keyboard::kc_not_a_key;
145
keyboard_map::const_iterator it;
147
for ( it=m_keyboard.begin();
148
(it!=m_keyboard.end())
149
&& (result == bear::input::keyboard::kc_not_a_key);
151
if ( it->second == a )
155
} // controller_layout::find_key()
157
/*----------------------------------------------------------------------------*/
159
* \brief Get the joystick key and index associated with an action.
160
* \return result.button == bear::input::joystick::jc_invalid if no key is
161
* associated with the action.
163
bear::input::joystick_button
164
ptb::controller_layout::find_joystick( ptb::player_action::value_type a ) const
166
bear::input::joystick_button result( 0, bear::input::joystick::jc_invalid );
168
joystick_map::const_iterator it;
170
for ( it=m_joystick.begin();
171
(it!=m_joystick.end())
172
&& (result.button == bear::input::joystick::jc_invalid);
174
if ( it->second == a )
178
} // controller_layout::find_joystick()
206
return m_action.find_mouse_value(button, player_action::action_null);
207
} // controller_layout::get_action_from_mouse()
180
209
/*----------------------------------------------------------------------------*/
184
213
* with the action.
186
215
bear::input::mouse::mouse_code
187
ptb::controller_layout::find_mouse( ptb::player_action::value_type a ) const
189
bear::input::mouse::mouse_code result( bear::input::mouse::mc_invalid );
191
mouse_map::const_iterator it;
193
for ( it=m_mouse.begin();
194
(it!=m_mouse.end()) && (result == bear::input::mouse::mc_invalid);
196
if ( it->second == a )
200
} // controller_layout::find_mouse()
216
ptb::controller_layout::get_mouse_from_action
217
( player_action::value_type a ) const
219
return m_action.find_mouse(a);
220
} // controller_layout::get_mouse_from_action()
222
/*----------------------------------------------------------------------------*/
224
* \brief Set the command associated to a keyboard key.
225
* \param key The code of the key.
226
* \param a The command done with this key.
228
void ptb::controller_layout::set_command_key
229
( bear::input::key_code key, gui_command::value_type a )
231
m_action.remove_key(key);
232
m_command.set_key(key, a);
233
} // controller_layout::set_command_key()
235
/*----------------------------------------------------------------------------*/
237
* \brief Get the command associated to a keyboard key.
238
* \param key The code of the key.
240
ptb::gui_command::value_type
241
ptb::controller_layout::get_command_from_key( bear::input::key_code key ) const
243
return m_command.find_key_value(key, gui_command::null_command);
244
} // controller_layout::get_command_from_key()
246
/*----------------------------------------------------------------------------*/
248
* \brief Get the keyboard key associated with an command.
249
* \return bear::input::keyboard::kc_not_a_key if no key is associated with the
252
bear::input::key_code
253
ptb::controller_layout::get_key_from_command( gui_command::value_type a ) const
255
return m_command.find_key(a);
256
} // controller_layout::get_key_from_command()
258
/*----------------------------------------------------------------------------*/
260
* \brief Set the command associated to a button of a joystick.
261
* \param joy The joystick button.
262
* \param a The command done with this key.
264
void ptb::controller_layout::set_command_joystick
265
( const bear::input::joystick_button& joy, gui_command::value_type a )
267
set_command_joystick(joy.joystick_index, joy.button, a);
268
} // controller_layout::set_command_joystick()
270
/*----------------------------------------------------------------------------*/
272
* \brief Set the command associated to a key of a joystick.
273
* \param joy The index of the joystick.
274
* \param key The code of the key.
275
* \param a The command done with this key.
277
void ptb::controller_layout::set_command_joystick
278
( unsigned int joy, bear::input::joystick::joy_code key,
279
gui_command::value_type a )
281
m_action.remove_joy(joy, key);
282
m_command.set_joystick(joy, key, a);
283
} // controller_layout::set_command_joystick()
285
/*----------------------------------------------------------------------------*/
287
* \brief Get the command associated to a key of a joystick.
288
* \param joy The index of the joystick.
289
* \param key The code of the key.
291
ptb::gui_command::value_type
292
ptb::controller_layout::get_command_from_joystick
293
( unsigned int joy, bear::input::joystick::joy_code key ) const
295
return m_command.find_joystick_value(joy, key, gui_command::null_command);
296
} // controller_layout::get_command_from_joystick()
298
/*----------------------------------------------------------------------------*/
300
* \brief Get the joystick key and index associated with an command.
301
* \return result.button == bear::input::joystick::jc_invalid if no key is
302
* associated with the command.
304
bear::input::joystick_button
305
ptb::controller_layout::get_joystick_from_command
306
( ptb::gui_command::value_type a ) const
308
return m_command.find_joystick(a);
309
} // controller_layout::get_joystick_from_command()
311
/*----------------------------------------------------------------------------*/
313
* \brief Set the command associated to a mouse button.
314
* \param button The code of the button.
315
* \param key The code of the key.
317
void ptb::controller_layout::set_command_mouse
318
( bear::input::mouse::mouse_code button, gui_command::value_type a )
320
m_action.remove_mouse(button);
321
m_command.set_mouse(button, a);
322
} // controller_layout::set_command_mouse()
324
/*----------------------------------------------------------------------------*/
326
* \brief Get the command associated to a mouse button.
327
* \param button The code of the button.
329
ptb::gui_command::value_type
330
ptb::controller_layout::get_command_from_mouse
331
( bear::input::mouse::mouse_code button ) const
333
return m_command.find_mouse_value(button, gui_command::null_command);
334
} // controller_layout::get_command_from_mouse()
336
/*----------------------------------------------------------------------------*/
338
* \brief Get the mouse key and index associated with an command.
339
* \return result == bear::input::mouse::mc_invalid if no button is associated
342
bear::input::mouse::mouse_code
343
ptb::controller_layout::get_mouse_from_command
344
( gui_command::value_type a ) const
346
return m_command.find_mouse(a);
347
} // controller_layout::get_mouse_from_command()
202
349
/*----------------------------------------------------------------------------*/
204
351
* \brief Convert the escaped action sequence of a string.
205
* \param str (in/out) The string to convert.
352
* \param str The string to convert.
353
* \param result The resulting string.
207
355
void ptb::controller_layout::escape_action_sequence
208
( const std::string& str, std::vector<std::string>& result ) const
356
( const std::string& str, std::string& result ) const
210
358
unsigned int ref = 0;
211
359
unsigned int prev = 0;
244
392
void ptb::controller_layout::load( std::istream& f )
248
bear::input::key_code key_code;
249
bear::input::joystick::joy_code joy_code;
250
bear::input::mouse::mouse_code mouse_code;
259
for (unsigned int i=0; i!=n; ++i)
260
if ( f >> key_code >> action )
261
m_keyboard[key_code] = (ptb::player_action::value_type)action;
265
for (unsigned int i=0; i!=n; ++i)
266
if ( f >> joy >> joy_code >> action )
268
if ( joy >= bear::input::joystick::number_of_joysticks() )
269
claw::logger << claw::log_warning << "Invalid joystick index: "
272
m_joystick[ bear::input::joystick_button(joy, joy_code) ] =
273
(ptb::player_action::value_type)action;
278
for (unsigned int i=0; i!=n; ++i)
279
if ( f >> mouse_code >> action )
280
m_mouse[mouse_code] = (ptb::player_action::value_type)action;
394
claw::configuration_file config(f);
395
claw::configuration_file::const_section_iterator it;
400
for ( it=config.section_begin(s_action_keyboard_section);
401
it!=config.section_end(s_action_keyboard_section); ++it )
403
( bear::input::keyboard::get_key_named
404
(config(s_action_keyboard_section, *it)),
405
player_action::from_string(*it) );
407
for ( it=config.section_begin(s_action_joystick_section);
408
it!=config.section_end(s_action_joystick_section); ++it )
410
( bear::input::joystick_button::get_button_named
411
(config(s_action_joystick_section, *it)),
412
player_action::from_string(*it) );
414
for ( it=config.section_begin(s_action_mouse_section);
415
it!=config.section_end(s_action_mouse_section); ++it )
417
( bear::input::mouse::get_button_named
418
(config(s_action_mouse_section, *it)),
419
player_action::from_string(*it) );
421
for ( it=config.section_begin(s_command_keyboard_section);
422
it!=config.section_end(s_command_keyboard_section); ++it )
424
( bear::input::keyboard::get_key_named
425
(config(s_command_keyboard_section, *it)),
426
gui_command::from_string(*it) );
428
for ( it=config.section_begin(s_command_joystick_section);
429
it!=config.section_end(s_command_joystick_section); ++it )
431
( bear::input::joystick_button::get_button_named
432
(config(s_command_joystick_section, *it)),
433
gui_command::from_string(*it) );
435
for ( it=config.section_begin(s_command_mouse_section);
436
it!=config.section_end(s_command_mouse_section); ++it )
438
( bear::input::mouse::get_button_named
439
(config(s_command_mouse_section, *it)),
440
gui_command::from_string(*it) );
281
441
} // controller_layout::load()
283
443
/*----------------------------------------------------------------------------*/
288
448
void ptb::controller_layout::save( std::ostream& f ) const
290
keyboard_map::const_iterator it_k;
291
joystick_map::const_iterator it_j;
292
mouse_map::const_iterator it_m;
294
f << m_keyboard.size() << std::endl;
296
for (it_k=m_keyboard.begin(); it_k!=m_keyboard.end(); ++it_k)
297
f << it_k->first << " " << it_k->second << std::endl;
299
f << m_joystick.size() << std::endl;
301
for (it_j=m_joystick.begin(); it_j!=m_joystick.end(); ++it_j)
302
f << it_j->first.joystick_index << " " << it_j->first.button << " "
303
<< it_j->second << std::endl;
305
for (it_m=m_mouse.begin(); it_m!=m_mouse.end(); ++it_m)
306
f << it_m->first << " " << it_m->second << std::endl;
307
452
} // controller_layout::save()
309
454
/*----------------------------------------------------------------------------*/
456
* \brief Remove a button from the controller.
457
* \param b The button to remove.
459
void ptb::controller_layout::remove( const bear::input::controller_button& b )
463
} // controller_layout::remove()
465
/*----------------------------------------------------------------------------*/
311
467
* \brief Remove an action from the controller.
312
468
* \param key The keyboard key of the action to remove.
315
ptb::controller_layout::remove_key( bear::input::key_code key )
470
void ptb::controller_layout::remove_key( bear::input::key_code key )
317
m_keyboard.erase(key);
472
m_action.remove_key(key);
473
m_command.remove_key(key);
318
474
} // controller_layout::remove_key()
320
476
/*----------------------------------------------------------------------------*/
390
547
* \brief Append to a string the string corresponding to the action of a given
392
* \param str (in/out) The string table to which we append the action.
549
* \param str (in/out) The string to which we append the action.
393
550
* \param action The action from which we want the string.
394
551
* \param player The player doing the action.
395
552
* \return The position of the next character to read.
397
554
bool ptb::controller_layout::append_action_string
398
( std::vector<std::string>& str, unsigned int action ) const
555
( std::string& str, unsigned int action ) const
400
557
bool result = true;
402
bear::input::key_code key = find_key( action );
403
bear::input::joystick_button joy = find_joystick( action );
404
bear::input::mouse::mouse_code mouse = find_mouse( action );
559
bear::input::key_code key = get_key_from_action( action );
560
bear::input::joystick_button joy = get_joystick_from_action( action );
561
bear::input::mouse::mouse_code mouse = get_mouse_from_action( action );
406
563
if ( key != bear::input::keyboard::kc_not_a_key )
407
str.push_back( bear::input::keyboard::get_name_of(key) );
564
str += bear::input::keyboard::get_name_of(key);
408
565
else if ( joy.button != bear::input::joystick::jc_invalid )
410
567
std::ostringstream oss;
411
568
oss << joy.joystick_index << ' ';
413
str.push_back( "joystick " );
414
str.push_back( oss.str() );
415
str.push_back( bear::input::joystick::get_name_of(joy.button) );
570
str += gettext("joystick ") + oss.str()
571
+ gettext(bear::input::joystick::get_name_of(joy.button).c_str());
417
573
else if ( mouse != bear::input::mouse::mc_invalid )
418
str.push_back( bear::input::mouse::get_name_of(mouse) );
574
str += bear::input::mouse::get_name_of(mouse);
425
581
/*----------------------------------------------------------------------------*/
427
* \brief Remove an action from all controllers.
428
* \param a The action to remove.
430
void ptb::controller_layout::remove_action( player_action::value_type a )
432
bear::input::key_code key = find_key( a );
434
while ( key != bear::input::keyboard::kc_not_a_key )
440
bear::input::joystick_button joy = find_joystick( a );
442
while ( joy.button != bear::input::joystick::jc_invalid )
444
remove_joy(joy.joystick_index, joy.button);
445
joy = find_joystick( a );
448
bear::input::mouse::mouse_code mouse = find_mouse( a );
450
while ( mouse != bear::input::mouse::mc_invalid )
455
} // controller_layout::remove_action()
583
* \brief Save the actions in a stream.
584
* \param f The stream in which we write.
586
void ptb::controller_layout::save_actions( std::ostream& f ) const
588
claw::configuration_file config;
590
player_action::value_type a;
592
for (a = player_action::min_value; a!=player_action::max_value; ++a)
594
bear::input::key_code key = get_key_from_action( a );
596
if ( key != bear::input::keyboard::kc_not_a_key )
598
( s_action_keyboard_section, player_action::to_string(a),
599
bear::input::keyboard::get_name_of(key) );
602
for (a = player_action::min_value; a!=player_action::max_value; ++a)
604
bear::input::joystick_button joy = get_joystick_from_action( a );
606
if ( joy.button != bear::input::joystick::jc_invalid )
608
( s_action_joystick_section, player_action::to_string(a),
609
bear::input::joystick_button::get_name_of(joy) );
612
for (a = player_action::min_value; a!=player_action::max_value; ++a)
614
bear::input::mouse::mouse_code mouse = get_mouse_from_action( a );
616
if ( mouse != bear::input::mouse::mc_invalid )
618
( s_action_mouse_section, player_action::to_string(a),
619
bear::input::mouse::get_name_of(mouse) );
623
} // controller_layout::save_actions()
625
/*----------------------------------------------------------------------------*/
627
* \brief Save the commands in a stream.
628
* \param f The stream in which we write.
630
void ptb::controller_layout::save_commands( std::ostream& f ) const
632
claw::configuration_file config;
634
gui_command::value_type a;
636
for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
638
bear::input::key_code key = get_key_from_command( a );
640
if ( key != bear::input::keyboard::kc_not_a_key )
642
( s_command_keyboard_section, gui_command::to_string(a),
643
bear::input::keyboard::get_name_of(key) );
646
for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
648
bear::input::joystick_button joy = get_joystick_from_command( a );
650
if ( joy.button != bear::input::joystick::jc_invalid )
652
( s_command_joystick_section, gui_command::to_string(a),
653
bear::input::joystick_button::get_name_of(joy) );
656
for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
658
bear::input::mouse::mouse_code mouse = get_mouse_from_command( a );
660
if ( mouse != bear::input::mouse::mc_invalid )
662
( s_command_mouse_section, gui_command::to_string(a),
663
bear::input::mouse::get_name_of(mouse) );
667
} // controller_layout::save_commands()