Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/util/event/key_mapper.cc @ 4452

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

orxonox/trunk: key_mapper.* commented and reviewed

File size: 5.6 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/**
107   \brief loads new key bindings from a file
108   \param filename: The path and name of the file to load the bindings from
109*/
110void KeyMapper::loadKeyBindings (const char* fileName)
111{
112  FILE* stream;
113 
114  PRINTF(4)("Loading key bindings from %s\n", fileName);
115 
116  // create parser
117  IniParser parser(fileName);
118  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
119    {
120      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
121      return;
122    }
123  // allocate empty lookup table
124 
125  char namebuf[256];
126  char valuebuf[256];
127  memset (namebuf, 0, 256);
128  memset (valuebuf, 0, 256);
129  int* index;
130 
131  while( parser.nextVar (namebuf, valuebuf) != -1)
132    {
133      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
134      index = nameToIndex (valuebuf);
135      this->mapKeys(namebuf, index[1]);
136
137      /*
138      switch( index[0])
139        {
140        case 0:
141          this->mapKeys(namebuf, index[1]);
142          break;
143        case 1:
144          this->mapKeys(namebuf, index[1]);
145          break;
146        default:
147          break;
148        }
149      */
150      memset (namebuf, 0, 256);
151      memset (valuebuf, 0, 256);
152    }
153
154
155  // PARSE MISC SECTION
156  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
157    {
158      PRINTF(1)("Could not find key bindings in %s\n", fileName);
159      return;
160    }
161
162  while( parser.nextVar (namebuf, valuebuf) != -1)
163    {
164      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
165      index = nameToIndex (valuebuf);     
166      this->mapKeys(namebuf, index[1]);
167      /*
168      switch( index[0])
169        {
170        case 0:
171          this->mapKeys(namebuf, index[1]);
172          break;
173        case 1:
174        this->mapKeys(namebuf, index[1]);
175          break;
176        default:
177          break;
178        }
179      */
180      memset (namebuf, 0, 256);
181      memset (valuebuf, 0, 256);
182    }
183}
184
185
186/**
187   \brief this function looks up name to key index
188   \param the name of the button
189*/
190int* KeyMapper::nameToIndex (char* name)
191{
192  coord[0] = -1;
193  coord[1] = -1;
194  int c;
195  if( (c = keynameToSDLK (name)) != -1)
196    {
197      coord[1] = c;
198      coord[0] = 0;
199    }
200  if( (c = buttonnameToSDLB (name)) != -1)
201    {
202      coord[1] = c;
203      coord[0] = 1;
204    }
205  return coord;
206}
207
208
209/**
210   \brief the function maps name to key ids
211   \param name of the key
212   \param id of the key
213*/
214void KeyMapper::mapKeys(char* name, int keyID)
215{
216  for(int i = 0; map[i].pValue != NULL; ++i )
217    {
218      if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;}
219      PRINTF(0)("Mapping %s to id %i\n", name, keyID);
220    }
221}
222
223
224/**
225   \brief this function gives some debug information about the key mapper class
226*/
227void KeyMapper::debug()
228{
229  PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
230  for(int i = 0; map[i].pValue != NULL; ++i)
231    {
232      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
233    }
234  PRINT(0)("=======================================================\n");       
235}
Note: See TracBrowser for help on using the repository browser.