Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/lib/event/key_mapper.cc @ 6768

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

orxonox/trunk:
merged spaceshipcontrol back to the trunk
merged with command:
svn merge -r5915:HEAD spaceshipcontrol/ ../trunk/
no conflicts (nice work guys :)

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