Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: now shooting and weapon change work again

File size: 5.0 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;
39int KeyMapper::PEV_PREVIOUS_WEAPON = -1;
40int KeyMapper::PEV_NEXT_WEAPON = -1;
41
42int KeyMapper::PEV_VIEW0 = -1;
43int KeyMapper::PEV_VIEW1 = -1;
44int KeyMapper::PEV_VIEW2 = -1;
45int KeyMapper::PEV_VIEW3 = -1;
46int KeyMapper::PEV_VIEW4 = -1;
47int KeyMapper::PEV_VIEW5 = -1; 
48
49int KeyMapper::PEV_NEXT_WORLD = -1;
50int KeyMapper::PEV_PREVIOUS_WORLD = -1;
51
52int KeyMapper::PEV_PAUSE = -1;
53int KeyMapper::PEV_QUIT = -1;
54
55
56
57
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   \brief standard constructor
89   \todo this constructor is not jet implemented - do it
90*/
91KeyMapper::KeyMapper () 
92{
93   this->setClassID(CL_KEY_MAPPER, "KeyMapper"); 
94}
95
96
97/**
98   \brief standard deconstructor
99
100*/
101KeyMapper::~KeyMapper () 
102{
103  // delete what has to be deleted here
104}
105
106\
107/**
108   \brief loads new key bindings from a file
109   \param filename: The path and name of the file to load the bindings from
110*/
111void KeyMapper::loadKeyBindings (const char* fileName)
112{
113  FILE* stream;
114 
115  PRINTF(4)("Loading key bindings from %s\n", fileName);
116 
117  // create parser
118  IniParser parser(fileName);
119  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
120    {
121      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
122      return;
123    }
124  // allocate empty lookup table
125 
126  char namebuf[256];
127  char valuebuf[256];
128  memset (namebuf, 0, 256);
129  memset (valuebuf, 0, 256);
130  int* index;
131 
132  while( parser.nextVar (namebuf, valuebuf) != -1)
133    {
134      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
135      index = nameToIndex (valuebuf);
136      this->mapKeys(namebuf, index[1]);
137
138      /*
139      switch( index[0])
140        {
141        case 0:
142          this->mapKeys(namebuf, index[1]);
143          break;
144        case 1:
145          this->mapKeys(namebuf, index[1]);
146          break;
147        default:
148          break;
149        }
150      */
151      memset (namebuf, 0, 256);
152      memset (valuebuf, 0, 256);
153    }
154
155
156  // PARSE MISC SECTION
157  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
158    {
159      PRINTF(1)("Could not find key bindings in %s\n", fileName);
160      return;
161    }
162
163  while( parser.nextVar (namebuf, valuebuf) != -1)
164    {
165      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
166      index = nameToIndex (valuebuf);     
167      this->mapKeys(namebuf, index[1]);
168      /*
169      switch( index[0])
170        {
171        case 0:
172          this->mapKeys(namebuf, index[1]);
173          break;
174        case 1:
175        this->mapKeys(namebuf, index[1]);
176          break;
177        default:
178          break;
179        }
180      */
181      memset (namebuf, 0, 256);
182      memset (valuebuf, 0, 256);
183    }
184}
185
186
187
188int* KeyMapper::nameToIndex (char* name)
189{
190  coord[0] = -1;
191  coord[1] = -1;
192  int c;
193  if( (c = keynameToSDLK (name)) != -1)
194    {
195      coord[1] = c;
196      coord[0] = 0;
197    }
198  if( (c = buttonnameToSDLB (name)) != -1)
199    {
200      coord[1] = c;
201      coord[0] = 1;
202    }
203  return coord;
204}
205
206
207
208void KeyMapper::mapKeys(char* name, int keyID)
209{
210  for(int i = 0; map[i].pValue != NULL; ++i )
211    {
212      if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;}
213      PRINTF(0)("Mapping %s to id %i\n", name, keyID);
214    }
215}
216
217
218void KeyMapper::debug()
219{
220  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
221
222  for(int i = 0; map[i].pValue != NULL; ++i)
223    {
224      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
225    }
226
227  //PRINT(0)("=======================================================\n");       
228}
Note: See TracBrowser for help on using the repository browser.