Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4582 was 4582, checked in by patrick, 19 years ago

orxonox/trunk: fixed a small bug in the event handling system which caused to display a msg multiple times

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