Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: fixed a compile error, sorry for the last commit without message…

File size: 5.3 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          break;
133        case 1:
134          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
135          strcpy (this->keyAliases->buttons[index[1]], namebuf);
136          break;
137        default:
138          break;
139        }
140      memset (namebuf, 0, 256);
141      memset (valuebuf, 0, 256);
142    }
143
144
145  // PARSE MISC SECTION
146  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
147    {
148      PRINTF(1)("Could not find key bindings in %s\n", fileName);
149      return;
150    }
151
152  while( parser.nextVar (namebuf, valuebuf) != -1)
153    {
154      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
155      index = nameToIndex (valuebuf);     
156
157      switch( index[0])
158        {
159        case 0:
160          PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
161          strcpy (keyAliases->keys[index[1]], namebuf);
162          break;
163        case 1:
164          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
165          strcpy (keyAliases->buttons[index[1]], namebuf);
166          break;
167        default:
168          break;
169        }
170      memset (namebuf, 0, 256);
171      memset (valuebuf, 0, 256);
172    }
173  this->mapKeys();
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()
199{
200  for(int i = 0; i < N_STD_KEYS; ++i)
201    {
202      if( !strcmp (this->keyAliases->keys[i], "Up")) PEV_UP = i;
203
204    }
205  PRINTF(0)("fire = %i\n", PEV_FIRE1);
206}
207
208
209void KeyMapper::debug()
210{
211  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
212  PRINT(0)("Command 'up' got SDL key-ref nr %i \n", keynameToSDLK("UP"));
213  PRINT(0)("Command 'down' got SDL key-ref nr %i \n", (this->nameToIndex("DOWN"))[1]);
214  PRINT(0)("Command 'right' got SDL key-ref nr %i \n", (this->nameToIndex("RIGHT"))[1]);
215  PRINT(0)("Command 'left' got SDL key-ref nr %i \n", (this->nameToIndex("LEFT"))[1]);
216
217  //PRINT(0)("=======================================================\n");       
218}
Note: See TracBrowser for help on using the repository browser.