~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to plee-the-bear/src/ptb/code/controller_layout.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
  Plee the Bear
3
3
 
4
 
  Copyright (C) 2005-2009 Julien Jorge, Sebastien Angibaud
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
5
5
 
6
6
  This program is free software; you can redistribute it and/or modify it
7
7
  under the terms of the GNU General Public License as published by the
23
23
*/
24
24
/**
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
28
28
 */
29
29
#include "ptb/controller_layout.hpp"
30
30
 
 
31
#include <libintl.h>
 
32
#include <claw/configuration_file.hpp>
31
33
#include <claw/logger.hpp>
32
34
 
33
35
/*----------------------------------------------------------------------------*/
 
36
const std::string
 
37
ptb::controller_layout::s_action_keyboard_section = "Action/Keyboard";
 
38
const std::string
 
39
ptb::controller_layout::s_action_joystick_section = "Action/Joystick";
 
40
const std::string
 
41
ptb::controller_layout::s_action_mouse_section = "Action/Mouse";
 
42
const std::string
 
43
ptb::controller_layout::s_command_keyboard_section = "GUI/Keyboard";
 
44
const std::string
 
45
ptb::controller_layout::s_command_joystick_section = "GUI/Joystick";
 
46
const std::string
 
47
ptb::controller_layout::s_command_mouse_section = "GUI/Mouse";
 
48
 
 
49
/*----------------------------------------------------------------------------*/
 
50
/**
 
51
 * \brief Get the controller button associated with an action.
 
52
 * \param a The action to which the button is associated.
 
53
 */
 
54
bear::input::controller_button
 
55
ptb::controller_layout::get_from_action( player_action::value_type a ) const
 
56
{
 
57
  return m_action.find(a);
 
58
} // controller_layout::get_from_action()
 
59
 
 
60
/*----------------------------------------------------------------------------*/
 
61
/**
 
62
 * \brief Get the controller button associated with a command.
 
63
 * \param c The command to which the button is associated.
 
64
 */
 
65
bear::input::controller_button
 
66
ptb::controller_layout::get_from_command( gui_command::value_type c ) const
 
67
{
 
68
  return m_command.find(c);
 
69
} // controller_layout::get_from_command()
 
70
 
 
71
/*----------------------------------------------------------------------------*/
 
72
/**
 
73
 * \brief Set the button associated with an action.
 
74
 * \param key The button.
 
75
 * \param a The action.
 
76
 */
 
77
void ptb::controller_layout::set_action
 
78
( const bear::input::controller_button& key, player_action::value_type a )
 
79
{
 
80
  m_action.set(key, a);
 
81
} // controller_layout::set_action()
 
82
 
 
83
/*----------------------------------------------------------------------------*/
 
84
/**
 
85
 * \brief Set the button associated with a gui command.
 
86
 * \param key The button.
 
87
 * \param c The command.
 
88
 */
 
89
void ptb::controller_layout::set_command
 
90
( const bear::input::controller_button& key, gui_command::value_type c )
 
91
{
 
92
  m_command.set(key, c);
 
93
} // controller_layout::set_command()
 
94
 
 
95
/*----------------------------------------------------------------------------*/
34
96
/**
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.
38
100
 */
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 )
41
103
{
42
 
  remove_key(key);
43
 
  remove_action(a);
44
 
  m_keyboard[key] = 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()
46
107
 
47
108
/*----------------------------------------------------------------------------*/
48
109
/**
50
111
 * \param key The code of the key.
51
112
 */
52
113
ptb::player_action::value_type
53
 
ptb::controller_layout::operator()( bear::input::key_code key ) const
54
 
{
55
 
  keyboard_map::const_iterator it;
56
 
 
57
 
  it = m_keyboard.find(key);
58
 
 
59
 
  if ( it != m_keyboard.end() )
60
 
    return it->second;
61
 
  else
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
 
115
{
 
116
  return m_action.find_key_value(key, player_action::action_null);
 
117
} // controller_layout::get_action_from_key()
 
118
 
 
119
/*----------------------------------------------------------------------------*/
 
120
/**
 
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
 
123
 *         action.
 
124
 */
 
125
bear::input::key_code
 
126
ptb::controller_layout::get_key_from_action( player_action::value_type a ) const
 
127
{
 
128
  return m_action.find_key(a);
 
129
} // controller_layout::get_key_from_action()
 
130
 
 
131
/*----------------------------------------------------------------------------*/
 
132
/**
 
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.
 
136
 */
 
137
void ptb::controller_layout::set_action_joystick
 
138
( const bear::input::joystick_button& joy, player_action::value_type a )
 
139
{
 
140
  set_action_joystick(joy.joystick_index, joy.button, a);
 
141
} // controller_layout::set_action_joystick()
64
142
 
65
143
/*----------------------------------------------------------------------------*/
66
144
/**
69
147
 * \param key The code of the key.
70
148
 * \param a The action done with this key.
71
149
 */
72
 
void ptb::controller_layout::set_joystick
 
150
void ptb::controller_layout::set_action_joystick
73
151
( unsigned int joy, bear::input::joystick::joy_code key,
74
152
  player_action::value_type a )
75
153
{
76
 
  remove_joy(joy, key);
77
 
  remove_action(a);
78
 
  m_joystick[ bear::input::joystick_button(joy, key) ] = a;
79
 
} // controller_layout::set_joystick()
 
154
  m_command.remove_joy(joy, key);
 
155
  m_action.set_joystick(joy, key, a);
 
156
} // controller_layout::set_action_joystick()
80
157
 
81
158
/*----------------------------------------------------------------------------*/
82
159
/**
85
162
 * \param key The code of the key.
86
163
 */
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
90
167
{
91
 
  joystick_map::const_iterator it;
92
 
 
93
 
  it = m_joystick.find( bear::input::joystick_button(joy, key) );
94
 
 
95
 
  if ( it != m_joystick.end() )
96
 
    return it->second;
97
 
  else
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()
 
170
 
 
171
/*----------------------------------------------------------------------------*/
 
172
/**
 
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.
 
176
 */
 
177
bear::input::joystick_button
 
178
ptb::controller_layout::get_joystick_from_action
 
179
( ptb::player_action::value_type a ) const
 
180
{
 
181
  return m_action.find_joystick(a);
 
182
} // controller_layout::get_joystick_from_action()
100
183
 
101
184
/*----------------------------------------------------------------------------*/
102
185
/**
104
187
 * \param button The code of the button.
105
188
 * \param key The code of the key.
106
189
 */
107
 
void ptb::controller_layout::set_mouse
 
190
void ptb::controller_layout::set_action_mouse
108
191
( bear::input::mouse::mouse_code button, player_action::value_type a )
109
192
{
110
 
  remove_mouse(button);
111
 
  remove_action(a);
112
 
  m_mouse[button] = a;
113
 
} // controller_layout::operator()( mouse_code )
 
193
  m_command.remove_mouse(button);
 
194
  m_action.set_mouse(button, a);
 
195
} // controller_layout::set_action_mouse()
114
196
 
115
197
/*----------------------------------------------------------------------------*/
116
198
/**
118
200
 * \param button The code of the button.
119
201
 */
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
123
205
{
124
 
  mouse_map::const_iterator it;
125
 
 
126
 
  it = m_mouse.find(button);
127
 
 
128
 
  if ( it != m_mouse.end() )
129
 
    return it->second;
130
 
  else
131
 
    return player_action::action_null;
132
 
} // controller_layout::operator()( mouse_code ) [const]
133
 
 
134
 
/*----------------------------------------------------------------------------*/
135
 
/**
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
138
 
 *         action.
139
 
 */
140
 
bear::input::key_code
141
 
ptb::controller_layout::find_key( ptb::player_action::value_type a ) const
142
 
{
143
 
  bear::input::key_code result = bear::input::keyboard::kc_not_a_key;
144
 
 
145
 
  keyboard_map::const_iterator it;
146
 
 
147
 
  for ( it=m_keyboard.begin();
148
 
        (it!=m_keyboard.end())
149
 
          && (result == bear::input::keyboard::kc_not_a_key);
150
 
        ++it )
151
 
    if ( it->second == a )
152
 
      result = it->first;
153
 
 
154
 
  return result;
155
 
} // controller_layout::find_key()
156
 
 
157
 
/*----------------------------------------------------------------------------*/
158
 
/**
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.
162
 
 */
163
 
bear::input::joystick_button
164
 
ptb::controller_layout::find_joystick( ptb::player_action::value_type a ) const
165
 
{
166
 
  bear::input::joystick_button result( 0, bear::input::joystick::jc_invalid );
167
 
 
168
 
  joystick_map::const_iterator it;
169
 
 
170
 
  for ( it=m_joystick.begin();
171
 
        (it!=m_joystick.end())
172
 
          && (result.button == bear::input::joystick::jc_invalid);
173
 
        ++it )
174
 
    if ( it->second == a )
175
 
      result = it->first;
176
 
 
177
 
  return result;
178
 
} // controller_layout::find_joystick()
 
206
  return m_action.find_mouse_value(button, player_action::action_null);
 
207
} // controller_layout::get_action_from_mouse()
179
208
 
180
209
/*----------------------------------------------------------------------------*/
181
210
/**
184
213
 *         with the action.
185
214
 */
186
215
bear::input::mouse::mouse_code
187
 
ptb::controller_layout::find_mouse( ptb::player_action::value_type a ) const
188
 
{
189
 
  bear::input::mouse::mouse_code result( bear::input::mouse::mc_invalid );
190
 
 
191
 
  mouse_map::const_iterator it;
192
 
 
193
 
  for ( it=m_mouse.begin();
194
 
        (it!=m_mouse.end()) && (result == bear::input::mouse::mc_invalid);
195
 
        ++it )
196
 
    if ( it->second == a )
197
 
      result = it->first;
198
 
 
199
 
  return result;
200
 
} // controller_layout::find_mouse()
 
216
ptb::controller_layout::get_mouse_from_action
 
217
( player_action::value_type a ) const
 
218
{
 
219
  return m_action.find_mouse(a);
 
220
} // controller_layout::get_mouse_from_action()
 
221
 
 
222
/*----------------------------------------------------------------------------*/
 
223
/**
 
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.
 
227
 */
 
228
void ptb::controller_layout::set_command_key
 
229
( bear::input::key_code key, gui_command::value_type a )
 
230
{
 
231
  m_action.remove_key(key);
 
232
  m_command.set_key(key, a);
 
233
} // controller_layout::set_command_key()
 
234
 
 
235
/*----------------------------------------------------------------------------*/
 
236
/**
 
237
 * \brief Get the command associated to a keyboard key.
 
238
 * \param key The code of the key.
 
239
 */
 
240
ptb::gui_command::value_type
 
241
ptb::controller_layout::get_command_from_key( bear::input::key_code key ) const
 
242
{
 
243
  return m_command.find_key_value(key, gui_command::null_command);
 
244
} // controller_layout::get_command_from_key()
 
245
 
 
246
/*----------------------------------------------------------------------------*/
 
247
/**
 
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
 
250
 *         command.
 
251
 */
 
252
bear::input::key_code
 
253
ptb::controller_layout::get_key_from_command( gui_command::value_type a ) const
 
254
{
 
255
  return m_command.find_key(a);
 
256
} // controller_layout::get_key_from_command()
 
257
 
 
258
/*----------------------------------------------------------------------------*/
 
259
/**
 
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.
 
263
 */
 
264
void ptb::controller_layout::set_command_joystick
 
265
( const bear::input::joystick_button& joy, gui_command::value_type a )
 
266
{
 
267
  set_command_joystick(joy.joystick_index, joy.button, a);
 
268
} // controller_layout::set_command_joystick()
 
269
 
 
270
/*----------------------------------------------------------------------------*/
 
271
/**
 
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.
 
276
 */
 
277
void ptb::controller_layout::set_command_joystick
 
278
( unsigned int joy, bear::input::joystick::joy_code key,
 
279
  gui_command::value_type a )
 
280
{
 
281
  m_action.remove_joy(joy, key);
 
282
  m_command.set_joystick(joy, key, a);
 
283
} // controller_layout::set_command_joystick()
 
284
 
 
285
/*----------------------------------------------------------------------------*/
 
286
/**
 
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.
 
290
 */
 
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
 
294
{
 
295
  return m_command.find_joystick_value(joy, key, gui_command::null_command);
 
296
} // controller_layout::get_command_from_joystick()
 
297
 
 
298
/*----------------------------------------------------------------------------*/
 
299
/**
 
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.
 
303
 */
 
304
bear::input::joystick_button
 
305
ptb::controller_layout::get_joystick_from_command
 
306
( ptb::gui_command::value_type a ) const
 
307
{
 
308
  return m_command.find_joystick(a);
 
309
} // controller_layout::get_joystick_from_command()
 
310
 
 
311
/*----------------------------------------------------------------------------*/
 
312
/**
 
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.
 
316
 */
 
317
void ptb::controller_layout::set_command_mouse
 
318
( bear::input::mouse::mouse_code button, gui_command::value_type a )
 
319
{
 
320
  m_action.remove_mouse(button);
 
321
  m_command.set_mouse(button, a);
 
322
} // controller_layout::set_command_mouse()
 
323
 
 
324
/*----------------------------------------------------------------------------*/
 
325
/**
 
326
 * \brief Get the command associated to a mouse button.
 
327
 * \param button The code of the button.
 
328
 */
 
329
ptb::gui_command::value_type
 
330
ptb::controller_layout::get_command_from_mouse
 
331
  ( bear::input::mouse::mouse_code button ) const
 
332
{
 
333
  return m_command.find_mouse_value(button, gui_command::null_command);
 
334
} // controller_layout::get_command_from_mouse()
 
335
 
 
336
/*----------------------------------------------------------------------------*/
 
337
/**
 
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
 
340
 *         with the command.
 
341
 */
 
342
bear::input::mouse::mouse_code
 
343
ptb::controller_layout::get_mouse_from_command
 
344
( gui_command::value_type a ) const
 
345
{
 
346
  return m_command.find_mouse(a);
 
347
} // controller_layout::get_mouse_from_command()
201
348
 
202
349
/*----------------------------------------------------------------------------*/
203
350
/**
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.
206
354
 */
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
209
357
{
210
358
  unsigned int ref = 0;
211
359
  unsigned int prev = 0;
216
364
      {
217
365
        if ( str[current] == 'a' )
218
366
          {
219
 
            result.push_back( str.substr(ref, prev - ref) );
 
367
            result += str.substr(ref, prev - ref);
220
368
            current = append_action_string(result, str, current);
221
369
          }
222
370
        else
223
 
          result.push_back( str.substr(ref, current - ref + 1) );
 
371
          result += str.substr(ref, current - ref + 1);
224
372
 
225
373
        ref = current + 1;
226
374
        prev = ref;
233
381
      }
234
382
 
235
383
  if ( ref < str.size() )
236
 
    result.push_back( str.substr(ref) );
 
384
    result += str.substr(ref);
237
385
} // controller_layout::escape_action_sequence()
238
386
 
239
387
/*----------------------------------------------------------------------------*/
243
391
 */
244
392
void ptb::controller_layout::load( std::istream& f )
245
393
{
246
 
  unsigned int n;
247
 
  unsigned int joy;
248
 
  bear::input::key_code key_code;
249
 
  bear::input::joystick::joy_code joy_code;
250
 
  bear::input::mouse::mouse_code mouse_code;
251
 
  int action;
252
 
 
253
 
  m_keyboard.clear();
254
 
  m_joystick.clear();
255
 
  m_mouse.clear();
256
 
 
257
 
  f >> n;
258
 
 
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;
262
 
 
263
 
  f >> n;
264
 
 
265
 
  for (unsigned int i=0; i!=n; ++i)
266
 
    if ( f >> joy >> joy_code >> action )
267
 
      {
268
 
        if ( joy >= bear::input::joystick::number_of_joysticks() )
269
 
          claw::logger << claw::log_warning << "Invalid joystick index: "
270
 
                       << joy << std::endl;
271
 
        else
272
 
          m_joystick[ bear::input::joystick_button(joy, joy_code) ] =
273
 
            (ptb::player_action::value_type)action;
274
 
      }
275
 
 
276
 
  f >> n;
277
 
 
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;
 
396
 
 
397
  m_action.clear();
 
398
  m_command.clear();
 
399
 
 
400
  for ( it=config.section_begin(s_action_keyboard_section);
 
401
        it!=config.section_end(s_action_keyboard_section); ++it )
 
402
    set_action_key
 
403
      ( bear::input::keyboard::get_key_named
 
404
        (config(s_action_keyboard_section, *it)),
 
405
        player_action::from_string(*it) );
 
406
 
 
407
  for ( it=config.section_begin(s_action_joystick_section);
 
408
        it!=config.section_end(s_action_joystick_section); ++it )
 
409
    set_action_joystick
 
410
      ( bear::input::joystick_button::get_button_named
 
411
        (config(s_action_joystick_section, *it)),
 
412
        player_action::from_string(*it) );
 
413
 
 
414
  for ( it=config.section_begin(s_action_mouse_section);
 
415
        it!=config.section_end(s_action_mouse_section); ++it )
 
416
    set_action_mouse
 
417
      ( bear::input::mouse::get_button_named
 
418
        (config(s_action_mouse_section, *it)),
 
419
        player_action::from_string(*it) );
 
420
 
 
421
  for ( it=config.section_begin(s_command_keyboard_section);
 
422
        it!=config.section_end(s_command_keyboard_section); ++it )
 
423
    set_command_key
 
424
      ( bear::input::keyboard::get_key_named
 
425
        (config(s_command_keyboard_section, *it)),
 
426
        gui_command::from_string(*it) );
 
427
 
 
428
  for ( it=config.section_begin(s_command_joystick_section);
 
429
        it!=config.section_end(s_command_joystick_section); ++it )
 
430
    set_command_joystick
 
431
      ( bear::input::joystick_button::get_button_named
 
432
        (config(s_command_joystick_section, *it)),
 
433
        gui_command::from_string(*it) );
 
434
 
 
435
  for ( it=config.section_begin(s_command_mouse_section);
 
436
        it!=config.section_end(s_command_mouse_section); ++it )
 
437
    set_command_mouse
 
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()
282
442
 
283
443
/*----------------------------------------------------------------------------*/
287
447
 */
288
448
void ptb::controller_layout::save( std::ostream& f ) const
289
449
{
290
 
  keyboard_map::const_iterator it_k;
291
 
  joystick_map::const_iterator it_j;
292
 
  mouse_map::const_iterator it_m;
293
 
 
294
 
  f << m_keyboard.size() << std::endl;
295
 
 
296
 
  for (it_k=m_keyboard.begin(); it_k!=m_keyboard.end(); ++it_k)
297
 
    f << it_k->first << " " << it_k->second << std::endl;
298
 
 
299
 
  f << m_joystick.size() << std::endl;
300
 
 
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;
304
 
 
305
 
  for (it_m=m_mouse.begin(); it_m!=m_mouse.end(); ++it_m)
306
 
    f << it_m->first << " " << it_m->second << std::endl;
 
450
  save_actions(f);
 
451
  save_commands(f);
307
452
} // controller_layout::save()
308
453
 
309
454
/*----------------------------------------------------------------------------*/
310
455
/**
 
456
 * \brief Remove a button from the controller.
 
457
 * \param b The button to remove.
 
458
 */
 
459
void ptb::controller_layout::remove( const bear::input::controller_button& b )
 
460
{
 
461
  m_action.remove(b);
 
462
  m_command.remove(b);
 
463
} // controller_layout::remove()
 
464
 
 
465
/*----------------------------------------------------------------------------*/
 
466
/**
311
467
 * \brief Remove an action from the controller.
312
468
 * \param key The keyboard key of the action to remove.
313
469
 */
314
 
void
315
 
 ptb::controller_layout::remove_key( bear::input::key_code key )
 
470
void ptb::controller_layout::remove_key( bear::input::key_code key )
316
471
{
317
 
  m_keyboard.erase(key);
 
472
  m_action.remove_key(key);
 
473
  m_command.remove_key(key);
318
474
} // controller_layout::remove_key()
319
475
 
320
476
/*----------------------------------------------------------------------------*/
326
482
void ptb::controller_layout::remove_joy
327
483
( unsigned int joy, bear::input::joystick::joy_code key )
328
484
{
329
 
  m_joystick.erase( bear::input::joystick_button(joy, key) );
 
485
  m_action.remove_joy( joy, key );
 
486
  m_command.remove_joy( joy, key );
330
487
} // controller_layout::remove_joy()
331
488
 
332
489
/*----------------------------------------------------------------------------*/
337
494
void
338
495
ptb::controller_layout::remove_mouse( bear::input::mouse::mouse_code button )
339
496
{
340
 
  m_mouse.erase(button);
 
497
  m_action.remove_mouse(button);
 
498
  m_command.remove_mouse(button);
341
499
} // controller_layout::remove_mouse()
342
500
 
343
501
/*----------------------------------------------------------------------------*/
346
504
 */
347
505
bool ptb::controller_layout::empty() const
348
506
{
349
 
  return m_joystick.empty() && m_keyboard.empty() && m_mouse.empty();
 
507
  return m_action.empty() && m_command.empty();
350
508
} // controller_layout::empty()
351
509
 
352
510
/*----------------------------------------------------------------------------*/
353
511
/**
354
512
 * \brief Convert the "\\ai" escape sequence.
355
 
 * \param result (in/out) The string table to which we append the action.
 
513
 * \param result (in/out) The string to which we append the action.
356
514
 * \param str The string to convert.
357
515
 * \param current The position of the parameters of the 'a' in the \\a
358
516
 *        sequence.
359
517
 * \return The position of the last character read.
360
518
 */
361
519
unsigned int ptb::controller_layout::append_action_string
362
 
( std::vector<std::string>& result, const std::string& str,
363
 
  unsigned int current ) const
 
520
( std::string& result, const std::string& str, unsigned int current ) const
364
521
{
365
522
  std::string::size_type pos = str.find_first_of(';', current);
366
523
  bool ok = false;
380
537
    }
381
538
 
382
539
  if (!ok)
383
 
    result.push_back("%a");
 
540
    result += "%a";
384
541
 
385
542
  return current;
386
543
} // controller_layout::append_action_string()
389
546
/**
390
547
 * \brief Append to a string the string corresponding to the action of a given
391
548
 *        player.
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.
396
553
 */
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
399
556
{
400
557
  bool result = true;
401
558
 
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 );
405
562
 
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 )
409
566
    {
410
567
      std::ostringstream oss;
411
568
      oss << joy.joystick_index << ' ';
412
569
 
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());
416
572
    }
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);
419
575
  else
420
576
    result = false;
421
577
 
424
580
 
425
581
/*----------------------------------------------------------------------------*/
426
582
/**
427
 
 * \brief Remove an action from all controllers.
428
 
 * \param a The action to remove.
429
 
 */
430
 
void ptb::controller_layout::remove_action( player_action::value_type a )
431
 
{
432
 
  bear::input::key_code key = find_key( a );
433
 
 
434
 
  while ( key != bear::input::keyboard::kc_not_a_key )
435
 
    {
436
 
      remove_key(key);
437
 
      key = find_key( a );
438
 
    }
439
 
 
440
 
  bear::input::joystick_button joy = find_joystick( a );
441
 
 
442
 
  while ( joy.button != bear::input::joystick::jc_invalid )
443
 
    {
444
 
      remove_joy(joy.joystick_index, joy.button);
445
 
      joy = find_joystick( a );
446
 
    }
447
 
 
448
 
  bear::input::mouse::mouse_code mouse = find_mouse( a );
449
 
 
450
 
  while ( mouse != bear::input::mouse::mc_invalid )
451
 
    {
452
 
      remove_mouse(mouse);
453
 
      find_mouse( a );
454
 
    }
455
 
} // controller_layout::remove_action()
 
583
 * \brief Save the actions in a stream.
 
584
 * \param f The stream in which we write.
 
585
 */
 
586
void ptb::controller_layout::save_actions( std::ostream& f ) const
 
587
{
 
588
  claw::configuration_file config;
 
589
 
 
590
  player_action::value_type a;
 
591
 
 
592
  for (a = player_action::min_value; a!=player_action::max_value; ++a)
 
593
    {
 
594
      bear::input::key_code key = get_key_from_action( a );
 
595
 
 
596
      if ( key != bear::input::keyboard::kc_not_a_key )
 
597
        config.set_value
 
598
          ( s_action_keyboard_section, player_action::to_string(a),
 
599
            bear::input::keyboard::get_name_of(key) );
 
600
    }
 
601
 
 
602
  for (a = player_action::min_value; a!=player_action::max_value; ++a)
 
603
    {
 
604
      bear::input::joystick_button joy = get_joystick_from_action( a );
 
605
 
 
606
      if ( joy.button != bear::input::joystick::jc_invalid )
 
607
        config.set_value
 
608
          ( s_action_joystick_section, player_action::to_string(a),
 
609
            bear::input::joystick_button::get_name_of(joy) );
 
610
    }
 
611
 
 
612
  for (a = player_action::min_value; a!=player_action::max_value; ++a)
 
613
    {
 
614
      bear::input::mouse::mouse_code mouse = get_mouse_from_action( a );
 
615
 
 
616
      if ( mouse != bear::input::mouse::mc_invalid )
 
617
        config.set_value
 
618
          ( s_action_mouse_section, player_action::to_string(a),
 
619
            bear::input::mouse::get_name_of(mouse) );
 
620
    }
 
621
 
 
622
  config.save(f);
 
623
} // controller_layout::save_actions()
 
624
 
 
625
/*----------------------------------------------------------------------------*/
 
626
/**
 
627
 * \brief Save the commands in a stream.
 
628
 * \param f The stream in which we write.
 
629
 */
 
630
void ptb::controller_layout::save_commands( std::ostream& f ) const
 
631
{
 
632
  claw::configuration_file config;
 
633
 
 
634
  gui_command::value_type a;
 
635
 
 
636
  for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
 
637
    {
 
638
      bear::input::key_code key = get_key_from_command( a );
 
639
 
 
640
      if ( key != bear::input::keyboard::kc_not_a_key )
 
641
        config.set_value
 
642
          ( s_command_keyboard_section, gui_command::to_string(a),
 
643
            bear::input::keyboard::get_name_of(key) );
 
644
    }
 
645
 
 
646
  for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
 
647
    {
 
648
      bear::input::joystick_button joy = get_joystick_from_command( a );
 
649
 
 
650
      if ( joy.button != bear::input::joystick::jc_invalid )
 
651
        config.set_value
 
652
          ( s_command_joystick_section, gui_command::to_string(a),
 
653
            bear::input::joystick_button::get_name_of(joy) );
 
654
    }
 
655
 
 
656
  for (a = gui_command::min_value; a<=gui_command::max_value; ++a)
 
657
    {
 
658
      bear::input::mouse::mouse_code mouse = get_mouse_from_command( a );
 
659
 
 
660
      if ( mouse != bear::input::mouse::mc_invalid )
 
661
        config.set_value
 
662
          ( s_command_mouse_section, gui_command::to_string(a),
 
663
            bear::input::mouse::get_name_of(mouse) );
 
664
    }
 
665
 
 
666
  config.save(f);
 
667
} // controller_layout::save_commands()