Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: implemented a new kind of ini-parser
this parser first reads the file, and then one can search through without accessing the file anymore

File size: 5.7 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"))
124  {
125    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
126    return;
127  }
128  int* index;
129
130  while(iniParser->nextVar())
131  {
132    PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
133    index = nameToIndex (iniParser->getCurrentValue());
134    this->mapKeys(iniParser->getCurrentName(), index[1]);
135  }
136
137
138  // PARSE MISC SECTION
139  if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
140  {
141    PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
142    return;
143  }
144
145  while( iniParser->nextVar())
146  {
147    PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
148    index = nameToIndex (iniParser->getCurrentValue());
149    this->mapKeys(iniParser->getCurrentName(), index[1]);
150  }
151}
152
153/**
154 *  this function looks up name to key index
155 * @param the name of the button
156*/
157int* KeyMapper::nameToIndex (const char* name)
158{
159  coord[0] = -1;
160  coord[1] = -1;
161  int c;
162  if( (c = keynameToSDLK (name)) != -1)
163    {
164      coord[1] = c;
165      coord[0] = 0;
166    }
167  if( (c = buttonnameToSDLB (name)) != -1)
168    {
169      coord[1] = c;
170      coord[0] = 1;
171    }
172  return coord;
173}
174
175
176/**
177 *  the function maps name to key ids
178 * @param name of the key
179 * @param id of the key
180*/
181void KeyMapper::mapKeys(const char* name, int keyID)
182{
183  for(int i = 0; map[i].pValue != NULL; ++i )
184    {
185      if( !strcmp (name, map[i].pName))
186      {
187        *map[i].pValue = keyID;
188        PRINTF(0)("Mapping %s to id %i\n", name, keyID);
189        break;
190      }
191    }
192}
193
194
195/**
196 *  this function gives some debug information about the key mapper class
197*/
198void KeyMapper::debug()
199{
200  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
201  for(int i = 0; map[i].pValue != NULL; ++i)
202    {
203      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
204    }
205  PRINT(0)("=======================================================\n");
206}
Note: See TracBrowser for help on using the repository browser.