Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

make

File size: 5.2 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_VIEW1 = -1;
41int KeyMapper::PEV_VIEW2 = -1;
42int KeyMapper::PEV_VIEW3 = -1;
43int KeyMapper::PEV_VIEW4 = -1;
44int KeyMapper::PEV_VIEW5 = -1;
45
46
47orxKeyMapping map[] = { {&KeyMapper::PEV_UP, "Up"},
48                        {&KeyMapper::PEV_DOWN, "Down"},
49                        {&KeyMapper::PEV_LEFT, "Left"},
50                        {&KeyMapper::PEV_RIGHT, "Right"},
51                        {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"},
52                        {&KeyMapper::PEV_STRAFE_RIGTH, "StrafeRight"},
53                       
54                        {&KeyMapper::PEV_FIRE1, "Fire"},
55                        {&KeyMapper::PEV_FIRE1, "Fire1"},
56                        {&KeyMapper::PEV_FIRE2, "Fire2"},
57
58                        {&KeyMapper::PEV_VIEW0, "view0"},
59                        {&KeyMapper::PEV_VIEW1, "view1"},
60                        {&KeyMapper::PEV_VIEW2, "view2"},
61                        {&KeyMapper::PEV_VIEW3, "view3"},
62                        {&KeyMapper::PEV_VIEW4, "view4"},
63                        {&KeyMapper::PEV_VIEW5, "view5"} 
64                        {NULL, NULL}};
65
66
67/**
68   \brief standard constructor
69   \todo this constructor is not jet implemented - do it
70*/
71KeyMapper::KeyMapper () 
72{
73   this->setClassID(CL_KEY_MAPPER, "KeyMapper"); 
74   this->keyAliases = NULL;
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  // remove old bindings if present
99  if( this->keyAliases != NULL)
100    {
101      free (this->keyAliases);
102      this->keyAliases = NULL;
103    }
104 
105  // create parser
106  IniParser parser(fileName);
107  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
108    {
109      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
110      return;
111    }
112  // allocate empty lookup table
113  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
114 
115  char namebuf[256];
116  char valuebuf[256];
117  memset (namebuf, 0, 256);
118  memset (valuebuf, 0, 256);
119  int* index;
120 
121  while( parser.nextVar (namebuf, valuebuf) != -1)
122    {
123      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
124      index = nameToIndex (valuebuf);
125
126      switch( index[0])
127        {
128        case 0:
129          PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
130          strcpy (this->keyAliases->keys[index[1]], namebuf);
131          break;
132        case 1:
133          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
134          strcpy (this->keyAliases->buttons[index[1]], namebuf);
135          break;
136        default:
137          break;
138        }
139      memset (namebuf, 0, 256);
140      memset (valuebuf, 0, 256);
141    }
142
143
144  // PARSE MISC SECTION
145  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
146    {
147      PRINTF(1)("Could not find key bindings in %s\n", fileName);
148      return;
149    }
150
151  while( parser.nextVar (namebuf, valuebuf) != -1)
152    {
153      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
154      index = nameToIndex (valuebuf);     
155
156      switch( index[0])
157        {
158        case 0:
159          PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
160          strcpy (keyAliases->keys[index[1]], namebuf);
161          break;
162        case 1:
163          PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
164          strcpy (keyAliases->buttons[index[1]], namebuf);
165          break;
166        default:
167          break;
168        }
169      memset (namebuf, 0, 256);
170      memset (valuebuf, 0, 256);
171    }
172  this->mapKeys();
173}
174
175
176
177int* KeyMapper::nameToIndex (char* name)
178{
179  coord[0] = -1;
180  coord[1] = -1;
181  int c;
182  if( (c = keynameToSDLK (name)) != -1)
183    {
184      coord[1] = c;
185      coord[0] = 0;
186    }
187  if( (c = buttonnameToSDLB (name)) != -1)
188    {
189      coord[1] = c;
190      coord[0] = 1;
191    }
192  return coord;
193}
194
195
196
197void KeyMapper::mapKeys()
198{
199  for(int i = 0; i < N_STD_KEYS; ++i)
200    {
201      if( !strcmp (this->keyAliases->keys[i], "Up")) PEV_UP = i;
202
203    }
204  PRINTF(0)("fire = %i\n", PEV_FIRE1);
205}
206
207
208void KeyMapper::debug()
209{
210  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
211  PRINT(0)("Command 'up' got SDL key-ref nr %i \n", keynameToSDLK("UP"));
212  PRINT(0)("Command 'down' got SDL key-ref nr %i \n", (this->nameToIndex("DOWN"))[1]);
213  PRINT(0)("Command 'right' got SDL key-ref nr %i \n", (this->nameToIndex("RIGHT"))[1]);
214  PRINT(0)("Command 'left' got SDL key-ref nr %i \n", (this->nameToIndex("LEFT"))[1]);
215
216  //PRINT(0)("=======================================================\n");       
217}
Note: See TracBrowser for help on using the repository browser.