Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: key mapping works just fine

File size: 5.1 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
30int KeyMapper::PEV_UP = -1;
31int KeyMapper::PEV_DOWN = -1;
32int KeyMapper::PEV_LEFT = -1;
33int KeyMapper::PEV_RIGHT = -1;
34int KeyMapper::PEV_STRAFE_LEFT = -1;
35int KeyMapper::PEV_STRAFE_RIGHT = -1;
36
37int KeyMapper::PEV_FIRE1 = -1;
38int KeyMapper::PEV_FIRE2 = -1;
39
40int KeyMapper::PEV_VIEW0 = -1;
41int KeyMapper::PEV_VIEW1 = -1;
42int KeyMapper::PEV_VIEW2 = -1;
43int KeyMapper::PEV_VIEW3 = -1;
44int KeyMapper::PEV_VIEW4 = -1;
45int KeyMapper::PEV_VIEW5 = -1;
46
47
48orxKeyMapping map[] = { {&KeyMapper::PEV_UP, "Up"},
49                        {&KeyMapper::PEV_DOWN, "Down"},
50                        {&KeyMapper::PEV_LEFT, "Left"},
51                        {&KeyMapper::PEV_RIGHT, "Right"},
52                        {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"},
53                        {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"},
54                       
55                        {&KeyMapper::PEV_FIRE1, "Fire"},
56                        {&KeyMapper::PEV_FIRE1, "Fire1"},
57                        {&KeyMapper::PEV_FIRE2, "Fire2"},
58
59                        {&KeyMapper::PEV_VIEW0, "view0"},
60                        {&KeyMapper::PEV_VIEW1, "view1"},
61                        {&KeyMapper::PEV_VIEW2, "view2"},
62                        {&KeyMapper::PEV_VIEW3, "view3"},
63                        {&KeyMapper::PEV_VIEW4, "view4"},
64                        {&KeyMapper::PEV_VIEW5, "view5"},                       
65                        {NULL, NULL}};
66
67
68/**
69   \brief standard constructor
70   \todo this constructor is not jet implemented - do it
71*/
72KeyMapper::KeyMapper () 
73{
74   this->setClassID(CL_KEY_MAPPER, "KeyMapper"); 
75   this->keyAliases = NULL;
76}
77
78
79/**
80   \brief standard deconstructor
81
82*/
83KeyMapper::~KeyMapper () 
84{
85  // delete what has to be deleted here
86}
87
88\
89/**
90   \brief loads new key bindings from a file
91   \param filename: The path and name of the file to load the bindings from
92*/
93void KeyMapper::loadKeyBindings (const char* fileName)
94{
95  FILE* stream;
96 
97  PRINTF(4)("Loading key bindings from %s\n", fileName);
98 
99  // remove old bindings if present
100  if( this->keyAliases != NULL)
101    {
102      free (this->keyAliases);
103      this->keyAliases = NULL;
104    }
105 
106  // create parser
107  IniParser parser(fileName);
108  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
109    {
110      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
111      return;
112    }
113  // allocate empty lookup table
114  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
115 
116  char namebuf[256];
117  char valuebuf[256];
118  memset (namebuf, 0, 256);
119  memset (valuebuf, 0, 256);
120  int* index;
121 
122  while( parser.nextVar (namebuf, valuebuf) != -1)
123    {
124      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
125      index = nameToIndex (valuebuf);
126
127      switch( index[0])
128        {
129        case 0:
130          PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
131          strcpy (this->keyAliases->keys[index[1]], namebuf);
132          this->mapKeys(namebuf, index[1]);
133          break;
134        case 1:
135          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
136          strcpy (this->keyAliases->buttons[index[1]], namebuf);
137          break;
138        default:
139          break;
140        }
141      memset (namebuf, 0, 256);
142      memset (valuebuf, 0, 256);
143    }
144
145
146  // PARSE MISC SECTION
147  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
148    {
149      PRINTF(1)("Could not find key bindings in %s\n", fileName);
150      return;
151    }
152
153  while( parser.nextVar (namebuf, valuebuf) != -1)
154    {
155      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
156      index = nameToIndex (valuebuf);     
157
158      switch( index[0])
159        {
160        case 0:
161          PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
162          strcpy (keyAliases->keys[index[1]], namebuf);
163          break;
164        case 1:
165          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
166          strcpy (keyAliases->buttons[index[1]], namebuf);
167          break;
168        default:
169          break;
170        }
171      memset (namebuf, 0, 256);
172      memset (valuebuf, 0, 256);
173    }
174}
175
176
177
178int* KeyMapper::nameToIndex (char* name)
179{
180  coord[0] = -1;
181  coord[1] = -1;
182  int c;
183  if( (c = keynameToSDLK (name)) != -1)
184    {
185      coord[1] = c;
186      coord[0] = 0;
187    }
188  if( (c = buttonnameToSDLB (name)) != -1)
189    {
190      coord[1] = c;
191      coord[0] = 1;
192    }
193  return coord;
194}
195
196
197
198void KeyMapper::mapKeys(char* name, int keyID)
199{
200  for(int i = 0; map[i].pValue != NULL; ++i )
201    {
202      if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;}
203    }
204}
205
206
207void KeyMapper::debug()
208{
209  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
210  PRINT(0)("PEV_UP = %i\n", PEV_UP);
211  PRINT(0)("PEV_DOWN = %i\n", PEV_DOWN);
212  PRINT(0)("PEV_LEFT = %i\n", PEV_LEFT);
213  PRINT(0)("PEV_RIGHT = %i\n", PEV_RIGHT);
214
215  //PRINT(0)("=======================================================\n");       
216}
Note: See TracBrowser for help on using the repository browser.