Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/lib/event/key_mapper.cc @ 10761

Last change on this file since 10761 was 10698, checked in by snellen, 18 years ago

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

File size: 8.8 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 "orxonox_globals.h"
26#include "parser/ini_parser/ini_parser.h"
27#include "parser/preferences/preferences.h"
28#include "key_names.h"
29#include "event_def.h"
30#include "debug.h"
31
32
33
34ObjectListDefinition(KeyMapper);
35
36/* initialize all variables to a reasonable value*/
37int KeyMapper::PEV_FORWARD           = EV_UNKNOWN;
38int KeyMapper::PEV_BACKWARD          = EV_UNKNOWN;
39int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
40int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
41int KeyMapper::PEV_UP                = EV_UNKNOWN;
42int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
43int KeyMapper::PEV_ROLL_LEFT         = EV_UNKNOWN;
44int KeyMapper::PEV_ROLL_RIGHT        = EV_UNKNOWN;
45int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
46int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
47int KeyMapper::PEV_JUMP              = EV_UNKNOWN;
48int KeyMapper::PEV_ACTION            = EV_UNKNOWN;
49int KeyMapper::PEV_CROUCH            = EV_UNKNOWN;
50
51int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
52int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
53int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
54int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
55
56int KeyMapper::PEV_CHANGE_SHIP       = EV_UNKNOWN;
57
58int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
59int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
60int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
61int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
62int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
63int KeyMapper::PEV_VIEW5             = EV_UNKNOWN;
64
65int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
66int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
67
68int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
69int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
70
71
72
73//! this is the mapping array from names to ids: enter all orxonox.conf keys here
74/** @todo use globals.h for this.... everything is done there for those Options,
75 * and you do not have to care about The namings, as they might change
76 */
77KeyMapper::KeyMapping KeyMapper::map[] = {
78      {&KeyMapper::PEV_FORWARD,              CONFIG_NAME_PLAYER_FORWARD,         SDLK_w},
79      {&KeyMapper::PEV_BACKWARD,             CONFIG_NAME_PLAYER_BACKWARD,        SDLK_s},
80      {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP,              SDLK_r},
81      {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN,            SDLK_f},
82      {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT,            SDLK_a},
83      {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT,           SDLK_d},
84      {&KeyMapper::PEV_ROLL_RIGHT,           CONFIG_NAME_PLAYER_ROLL_LEFT,       SDLK_z},
85      {&KeyMapper::PEV_ROLL_LEFT,            CONFIG_NAME_PLAYER_ROLL_RIGHT,      SDLK_c},
86      {&KeyMapper::PEV_STRAFE_LEFT,          CONFIG_NAME_PLAYER_STRAFE_LEFT,     SDLK_q},
87      {&KeyMapper::PEV_STRAFE_RIGHT,         CONFIG_NAME_PLAYER_STRAFE_RIGHT,    SDLK_e},
88      {&KeyMapper::PEV_JUMP,                 CONFIG_NAME_PLAYER_JUMP,            SDLK_SPACE},
89      {&KeyMapper::PEV_ACTION,               CONFIG_NAME_PLAYER_ACTION,          SDLK_u},
90      {&KeyMapper::PEV_CROUCH,               CONFIG_NAME_PLAYER_CROUCH,          SDLK_LCTRL},
91
92
93      {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE,            EV_MOUSE_BUTTON_LEFT},
94      {&KeyMapper::PEV_FIRE2,                CONFIG_NAME_PLAYER_FIRE2,           EV_MOUSE_BUTTON_RIGHT},
95      {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON,     EV_MOUSE_BUTTON_WHEELUP},
96      {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON,     EV_MOUSE_BUTTON_WHEELDOWN},
97
98      {&KeyMapper::PEV_CHANGE_SHIP,          CONFIG_NAME_PLAYER_CHANGE_SHIP,     SDLK_g},
99
100
101      {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0,                  SDLK_1},
102      {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1,                  SDLK_2},
103      {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2,                  SDLK_3},
104      {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3,                  SDLK_4},
105      {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4,                  SDLK_5},
106      {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5,                  SDLK_6},
107
108      {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD,             SDLK_x},
109      {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD,             SDLK_z},
110
111      {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE,                  SDLK_p},
112      {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT,                   SDLK_ESCAPE},
113      {NULL, "", 0}
114    };
115
116
117
118/**
119 *  standard constructor
120*/
121KeyMapper::KeyMapper ()
122{
123  this->registerObject(this, KeyMapper::_objectList);
124}
125
126
127/**
128 *  standard deconstructor
129*/
130KeyMapper::~KeyMapper ()
131{}
132
133
134/**
135 *  loads new key bindings from a file
136 * @param filename: The path and name of the file to load the bindings from
137*/
138void KeyMapper::loadKeyBindings (const std::string& fileName)
139{
140  IniParser parser(fileName);
141  this->loadKeyBindings(&parser);
142}
143
144void KeyMapper::loadKeyBindings(IniParser* iniParser)
145{
146  IniParser::Section* section = iniParser->getSection (CONFIG_SECTION_CONTROL);
147  if( section == NULL)
148  {
149    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n");
150    return;
151  }
152  int* index;
153
154
155  for(IniParser::Entry::iterator entry = section->begin();
156      entry != section->end();
157      ++entry)
158  {
159    PRINTF(3)("Keys: Parsing %s, %s now.\n", (*entry).name().c_str(), (*entry).value().c_str());
160    // map the name to an sdl index
161    index = nameToIndex ((*entry).value());
162    // map the index to a internal name
163    this->mapKeys((*entry).name(), index);
164  }
165
166
167  // PARSE MISC SECTION
168  //   if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
169  //   {
170  //     PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
171  //     return;
172  //   }
173  //
174  //   iniParser->firstVar();
175  //   while( iniParser->getCurrentName() != "" )
176  //   {
177  //     PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
178  //     index = nameToIndex (iniParser->getCurrentValue());
179  //     this->mapKeys(iniParser->getCurrentName(), index);
180  //     iniParser->nextVar();
181  //   }
182}
183
184void KeyMapper::loadKeyBindings()
185{
186  if( !Preferences::getInstance()->sectionExists(CONFIG_SECTION_CONTROL))
187  {
188    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n");
189    return;
190  }
191  int* index;
192
193  std::list<std::string> keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_CONTROL);
194  for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ )
195  {
196    PRINTF(3)("Keys: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "").c_str());
197    // map the name to an sdl index
198    index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, ""));
199    // map the index to a internal name
200    this->mapKeys(*it, index);
201  }
202}
203
204/**
205 *  this function looks up name to key index
206 * @param the name of the button
207*/
208int* KeyMapper::nameToIndex (const std::string& name)
209{
210  coord[0] = -1;
211  coord[1] = -1;
212  int c;
213  if( (c = keynameToSDLK (name)) != -1)
214  {
215    coord[1] = c;
216    coord[0] = 0;
217  }
218  if( (c = buttonnameToSDLB (name)) != -1)
219  {
220    coord[1] = c;
221    coord[0] = 1;
222  }
223  return coord;
224}
225
226
227/**
228 *  the function maps name to key ids
229 * @param name of the key
230 * @param id of the key
231*/
232void KeyMapper::mapKeys(const std::string& name, int* index)
233{
234  for( int i = 0; map[i].pValue != NULL; ++i )
235  {
236    if( name == map[i].pName)
237    {
238      if( index[0] == 0)
239      {
240        *map[i].pValue = index[1];
241        PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLKToKeyname(index[1]).c_str(), index[1]);
242        break;
243      }
244      else
245      {
246        *map[i].pValue = index[1];
247        PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLBToButtonname(index[1]).c_str(), index[1]);
248        break;
249      }
250    }
251  }
252}
253
254
255/**
256 *  this function gives some debug information about the key mapper class
257*/
258void KeyMapper::debug()
259{
260  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
261  for(int i = 0; map[i].pValue != NULL; ++i)
262  {
263    PRINT(0)("%s = %i\n",map[i].pName.c_str(), *map[i].pValue);
264  }
265  PRINT(0)("=======================================================\n");
266}
Note: See TracBrowser for help on using the repository browser.