Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/event/key_mapper.cc @ 4864

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

orxonox/trunk: merging minor nice fixes from laptop

File size: 6.4 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 "globals.h"
24#include "ini_parser.h"
25#include "key_names.h"
26
27using namespace std;
28
29
30/* initialize all variables to a reasonable value*/
31int KeyMapper::PEV_UP                = EV_UNKNOWN;
32int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
33int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
34int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
35int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
36int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
37
38int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
39int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
40int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
41int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
42
43int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
44int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
45int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
46int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
47int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
48int KeyMapper::PEV_VIEW5             = EV_UNKNOWN;
49
50int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
51int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
52
53int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
54int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
55
56
57
58//! this is the mapping array from names to ids: enter all orxonox.conf keys here
59/** @todo use globals.h for this.... everything is done there for those Options,
60 * and you do not have to care about The namings, as they might change
61 */
62orxKeyMapping map[] = {
63  {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP},
64  {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN},
65  {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT},
66  {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT},
67  {&KeyMapper::PEV_STRAFE_LEFT,          "StrafeLeft"},
68  {&KeyMapper::PEV_STRAFE_RIGHT,         "StrafeRight"},
69
70  {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE},
71  {&KeyMapper::PEV_FIRE1,                "Fire1"},
72  {&KeyMapper::PEV_FIRE2,                "Fire2"},
73  {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON},
74  {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON},
75
76
77  {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0},
78  {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1},
79  {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2},
80  {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3},
81  {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4},
82  {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5},
83
84  {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD},
85  {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD},
86
87  {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE},
88  {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT},
89  {NULL, NULL}
90};
91
92
93
94/**
95 *  standard constructor
96*/
97KeyMapper::KeyMapper ()
98{
99   this->setClassID(CL_KEY_MAPPER, "KeyMapper");
100}
101
102
103/**
104 *  standard deconstructor
105*/
106KeyMapper::~KeyMapper ()
107{
108}
109
110
111/**
112 *  loads new key bindings from a file
113 * @param filename: The path and name of the file to load the bindings from
114*/
115void KeyMapper::loadKeyBindings (const char* fileName)
116{
117  FILE* stream;
118
119  PRINTF(4)("Loading key bindings from %s\n", fileName);
120
121  // create parser
122  IniParser parser(fileName);
123  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
124    {
125      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
126      return;
127    }
128  // allocate empty lookup table
129
130  char namebuf[256];
131  char valuebuf[256];
132  memset (namebuf, 0, 256);
133  memset (valuebuf, 0, 256);
134  int* index;
135
136  while( parser.nextVar (namebuf, valuebuf) != -1)
137    {
138      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
139      index = nameToIndex (valuebuf);
140      this->mapKeys(namebuf, index[1]);
141
142      /*
143      switch( index[0])
144        {
145        case 0:
146          this->mapKeys(namebuf, index[1]);
147          break;
148        case 1:
149          this->mapKeys(namebuf, index[1]);
150          break;
151        default:
152          break;
153        }
154      */
155      memset (namebuf, 0, 256);
156      memset (valuebuf, 0, 256);
157    }
158
159
160  // PARSE MISC SECTION
161  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
162    {
163      PRINTF(1)("Could not find key bindings in %s\n", fileName);
164      return;
165    }
166
167  while( parser.nextVar (namebuf, valuebuf) != -1)
168    {
169      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
170      index = nameToIndex (valuebuf);
171      this->mapKeys(namebuf, index[1]);
172      /*
173      switch( index[0])
174        {
175        case 0:
176          this->mapKeys(namebuf, index[1]);
177          break;
178        case 1:
179        this->mapKeys(namebuf, index[1]);
180          break;
181        default:
182          break;
183        }
184      */
185      memset (namebuf, 0, 256);
186      memset (valuebuf, 0, 256);
187    }
188}
189
190
191/**
192 *  this function looks up name to key index
193 * @param the name of the button
194*/
195int* KeyMapper::nameToIndex (char* name)
196{
197  coord[0] = -1;
198  coord[1] = -1;
199  int c;
200  if( (c = keynameToSDLK (name)) != -1)
201    {
202      coord[1] = c;
203      coord[0] = 0;
204    }
205  if( (c = buttonnameToSDLB (name)) != -1)
206    {
207      coord[1] = c;
208      coord[0] = 1;
209    }
210  return coord;
211}
212
213
214/**
215 *  the function maps name to key ids
216 * @param name of the key
217 * @param id of the key
218*/
219void KeyMapper::mapKeys(char* name, int keyID)
220{
221  for(int i = 0; map[i].pValue != NULL; ++i )
222    {
223      if( !strcmp (name, map[i].pName))
224      {
225        *map[i].pValue = keyID;
226        PRINTF(0)("Mapping %s to id %i\n", name, keyID);
227        break;
228      }
229    }
230}
231
232
233/**
234 *  this function gives some debug information about the key mapper class
235*/
236void KeyMapper::debug()
237{
238  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
239  for(int i = 0; map[i].pValue != NULL; ++i)
240    {
241      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
242    }
243  PRINT(0)("=======================================================\n");
244}
Note: See TracBrowser for help on using the repository browser.