Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaned and finished up the key mapper class

File size: 4.5 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}
76
77
78/**
79   \brief standard deconstructor
80
81*/
82KeyMapper::~KeyMapper () 
83{
84  // delete what has to be deleted here
85}
86
87\
88/**
89   \brief loads new key bindings from a file
90   \param filename: The path and name of the file to load the bindings from
91*/
92void KeyMapper::loadKeyBindings (const char* fileName)
93{
94  FILE* stream;
95 
96  PRINTF(4)("Loading key bindings from %s\n", fileName);
97 
98  // create parser
99  IniParser parser(fileName);
100  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
101    {
102      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
103      return;
104    }
105  // allocate empty lookup table
106 
107  char namebuf[256];
108  char valuebuf[256];
109  memset (namebuf, 0, 256);
110  memset (valuebuf, 0, 256);
111  int* index;
112 
113  while( parser.nextVar (namebuf, valuebuf) != -1)
114    {
115      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
116      index = nameToIndex (valuebuf);
117      this->mapKeys(namebuf, index[1]);
118
119      /*
120      switch( index[0])
121        {
122        case 0:
123          this->mapKeys(namebuf, index[1]);
124          break;
125        case 1:
126          this->mapKeys(namebuf, index[1]);
127          break;
128        default:
129          break;
130        }
131      */
132      memset (namebuf, 0, 256);
133      memset (valuebuf, 0, 256);
134    }
135
136
137  // PARSE MISC SECTION
138  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
139    {
140      PRINTF(1)("Could not find key bindings in %s\n", fileName);
141      return;
142    }
143
144  while( parser.nextVar (namebuf, valuebuf) != -1)
145    {
146      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
147      index = nameToIndex (valuebuf);     
148      this->mapKeys(namebuf, index[1]);
149      /*
150      switch( index[0])
151        {
152        case 0:
153          this->mapKeys(namebuf, index[1]);
154          break;
155        case 1:
156        this->mapKeys(namebuf, index[1]);
157          break;
158        default:
159          break;
160        }
161      */
162      memset (namebuf, 0, 256);
163      memset (valuebuf, 0, 256);
164    }
165}
166
167
168
169int* KeyMapper::nameToIndex (char* name)
170{
171  coord[0] = -1;
172  coord[1] = -1;
173  int c;
174  if( (c = keynameToSDLK (name)) != -1)
175    {
176      coord[1] = c;
177      coord[0] = 0;
178    }
179  if( (c = buttonnameToSDLB (name)) != -1)
180    {
181      coord[1] = c;
182      coord[0] = 1;
183    }
184  return coord;
185}
186
187
188
189void KeyMapper::mapKeys(char* name, int keyID)
190{
191  for(int i = 0; map[i].pValue != NULL; ++i )
192    {
193      if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;}
194    }
195}
196
197
198void KeyMapper::debug()
199{
200  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
201
202  for(int i = 0; map[i].pValue != NULL; ++i)
203    {
204      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
205    }
206
207  //PRINT(0)("=======================================================\n");       
208}
Note: See TracBrowser for help on using the repository browser.