Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/shared_lib/src/lib/event/key_mapper.cc @ 7285

Last change on this file since 7285 was 7256, checked in by bensch, 19 years ago

orxonox/trunk: merged the preferences back to the trunk
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/preferences . -r7233:HEAD
no conflicts… nice work

File size: 8.2 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11   ### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: Christian Meyer
14
15   This code was inspired by the command_node.cc code from Christian Meyer in revision
16   4386 and earlier.
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
20
21#include "key_mapper.h"
22
23#include "event_def.h"
24
25#include "globals.h"
26#include "parser/ini_parser/ini_parser.h"
27#include "util/preferences.h"
28#include "key_names.h"
29#include "debug.h"
30
31
32using namespace std;
33
34
35/* initialize all variables to a reasonable value*/
36int KeyMapper::PEV_FORWARD           = EV_UNKNOWN;
37int KeyMapper::PEV_BACKWARD          = EV_UNKNOWN;
38int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
39int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
40int KeyMapper::PEV_UP                = EV_UNKNOWN;
41int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
42int KeyMapper::PEV_ROLL_LEFT         = EV_UNKNOWN;
43int KeyMapper::PEV_ROLL_RIGHT        = EV_UNKNOWN;
44int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
45int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
46
47int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
48int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
49int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
50int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
51
52int KeyMapper::PEV_CHANGE_SHIP       = EV_UNKNOWN;
53
54int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
55int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
56int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
57int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
58int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
59int KeyMapper::PEV_VIEW5             = EV_UNKNOWN;
60
61int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
62int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
63
64int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
65int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
66
67
68
69//! this is the mapping array from names to ids: enter all orxonox.conf keys here
70/** @todo use globals.h for this.... everything is done there for those Options,
71 * and you do not have to care about The namings, as they might change
72 */
73orxKeyMapping map[] = {
74  {&KeyMapper::PEV_FORWARD,              CONFIG_NAME_PLAYER_FORWARD},
75  {&KeyMapper::PEV_BACKWARD,             CONFIG_NAME_PLAYER_BACKWARD},
76  {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP},
77  {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN},
78  {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT},
79  {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT},
80  {&KeyMapper::PEV_ROLL_LEFT,            CONFIG_NAME_PLAYER_ROLL_RIGHT},
81  {&KeyMapper::PEV_ROLL_RIGHT,           CONFIG_NAME_PLAYER_ROLL_LEFT},
82  {&KeyMapper::PEV_STRAFE_LEFT,          "StrafeLeft"},
83  {&KeyMapper::PEV_STRAFE_RIGHT,         "StrafeRight"},
84
85  {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE},
86  {&KeyMapper::PEV_FIRE1,                "Fire1"},
87  {&KeyMapper::PEV_FIRE2,                "Fire2"},
88  {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON},
89  {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON},
90
91  {&KeyMapper::PEV_CHANGE_SHIP,          CONFIG_NAME_PLAYER_CHANGE_SHIP},
92
93
94  {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0},
95  {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1},
96  {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2},
97  {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3},
98  {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4},
99  {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5},
100
101  {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD},
102  {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD},
103
104  {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE},
105  {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT},
106  {NULL, NULL}
107};
108
109
110
111/**
112 *  standard constructor
113*/
114KeyMapper::KeyMapper ()
115{
116   this->setClassID(CL_KEY_MAPPER, "KeyMapper");
117}
118
119
120/**
121 *  standard deconstructor
122*/
123KeyMapper::~KeyMapper ()
124{
125}
126
127
128/**
129 *  loads new key bindings from a file
130 * @param filename: The path and name of the file to load the bindings from
131*/
132void KeyMapper::loadKeyBindings (const std::string& fileName)
133{
134  IniParser parser(fileName);
135  this->loadKeyBindings(&parser);
136}
137
138void KeyMapper::loadKeyBindings(IniParser* iniParser)
139{
140  if( !iniParser->getSection (CONFIG_SECTION_PLAYER "1"))
141  {
142    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
143    return;
144  }
145  int* index;
146
147  iniParser->firstVar();
148  while( iniParser->getCurrentName() != "" )
149  {
150    PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
151    // map the name to an sdl index
152    index = nameToIndex (iniParser->getCurrentValue());
153    // map the index to a internal name
154    this->mapKeys(iniParser->getCurrentName(), index);
155    iniParser->nextVar();
156  }
157
158
159  // PARSE MISC SECTION
160  if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
161  {
162    PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
163    return;
164  }
165
166  iniParser->firstVar();
167  while( iniParser->getCurrentName() != "" )
168  {
169    PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
170    index = nameToIndex (iniParser->getCurrentValue());
171    this->mapKeys(iniParser->getCurrentName(), index);
172    iniParser->nextVar();
173  }
174}
175
176void KeyMapper::loadKeyBindings()
177{
178  if( !Preferences::getInstance()->sectionExists(CONFIG_SECTION_PLAYER "1"))
179  {
180    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
181    return;
182  }
183  int* index;
184
185  std::list<std::string> keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_PLAYER "1");
186  for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ )
187  {
188    PRINTF(3)("Keys: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_PLAYER "1", *it, "").c_str());
189    // map the name to an sdl index
190    index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_PLAYER "1", *it, ""));
191    // map the index to a internal name
192    this->mapKeys(*it, index);
193  }
194
195
196  // PARSE MISC SECTION
197  if( !Preferences::getInstance()->sectionExists (CONFIG_SECTION_MISC_KEYS))
198  {
199    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_MISC_KEYS "\n");
200    return;
201  }
202
203  keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_MISC_KEYS);
204  for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ )
205  {
206    PRINTF(3)("MISC: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_MISC_KEYS, *it, "").c_str());
207    index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_MISC_KEYS, *it, ""));
208    this->mapKeys(*it, index);
209  }
210}
211
212/**
213 *  this function looks up name to key index
214 * @param the name of the button
215*/
216int* KeyMapper::nameToIndex (const std::string& name)
217{
218  coord[0] = -1;
219  coord[1] = -1;
220  int c;
221  if( (c = keynameToSDLK (name)) != -1) {
222      coord[1] = c;
223      coord[0] = 0;
224    }
225  if( (c = buttonnameToSDLB (name)) != -1) {
226      coord[1] = c;
227      coord[0] = 1;
228    }
229  return coord;
230}
231
232
233/**
234 *  the function maps name to key ids
235 * @param name of the key
236 * @param id of the key
237*/
238void KeyMapper::mapKeys(const std::string& name, int* index)
239{
240  for( int i = 0; map[i].pValue != NULL; ++i )
241    {
242      if( name == map[i].pName)
243      {
244        if( index[0] == 0)
245        {
246          *map[i].pValue = index[1];
247          PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLKToKeyname(index[1]), index[1]);
248          break;
249        }
250        else {
251          *map[i].pValue = index[1];
252          PRINTF(4)("Mapping %s to '%s' (id %i)\n", name, SDLBToButtonname(index[1]), index[1]);
253          break;
254        }
255      }
256    }
257}
258
259
260/**
261 *  this function gives some debug information about the key mapper class
262*/
263void KeyMapper::debug()
264{
265  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
266  for(int i = 0; map[i].pValue != NULL; ++i)
267    {
268      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
269    }
270  PRINT(0)("=======================================================\n");
271}
Note: See TracBrowser for help on using the repository browser.