Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Iniparser is passed on to the EvenetHandler over orxonox

File size: 6.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 "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  IniParser parser(fileName);
118  this->loadKeyBindings(&parser);
119}
120
121void KeyMapper::loadKeyBindings(IniParser* iniParser)
122{
123  if( iniParser->getSection (CONFIG_SECTION_PLAYER "1") == -1)
124  {
125    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
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( iniParser->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( iniParser->getSection (CONFIG_SECTION_MISC_KEYS) == -1)
162  {
163    PRINTF(1)("Could not find key bindings\n");
164    return;
165  }
166
167  while( iniParser->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 *  this function looks up name to key index
192 * @param the name of the button
193*/
194int* KeyMapper::nameToIndex (char* name)
195{
196  coord[0] = -1;
197  coord[1] = -1;
198  int c;
199  if( (c = keynameToSDLK (name)) != -1)
200    {
201      coord[1] = c;
202      coord[0] = 0;
203    }
204  if( (c = buttonnameToSDLB (name)) != -1)
205    {
206      coord[1] = c;
207      coord[0] = 1;
208    }
209  return coord;
210}
211
212
213/**
214 *  the function maps name to key ids
215 * @param name of the key
216 * @param id of the key
217*/
218void KeyMapper::mapKeys(char* name, int keyID)
219{
220  for(int i = 0; map[i].pValue != NULL; ++i )
221    {
222      if( !strcmp (name, map[i].pName))
223      {
224        *map[i].pValue = keyID;
225        PRINTF(0)("Mapping %s to id %i\n", name, keyID);
226        break;
227      }
228    }
229}
230
231
232/**
233 *  this function gives some debug information about the key mapper class
234*/
235void KeyMapper::debug()
236{
237  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
238  for(int i = 0; map[i].pValue != NULL; ++i)
239    {
240      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
241    }
242  PRINT(0)("=======================================================\n");
243}
Note: See TracBrowser for help on using the repository browser.