Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: key_mapper.* commented and reviewed

File size: 5.6 KB
RevLine 
[4367]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
[4388]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.
[4367]17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT
20
[4386]21#include "key_mapper.h"
[4367]22
[4386]23#include "ini_parser.h"
[4400]24#include "key_names.h"
[4386]25
[4367]26using namespace std;
27
28
[4452]29/* initialize all variables to a reasonable value*/
30int KeyMapper::PEV_UP                = EV_UNKNOWN;
31int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
32int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
33int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
34int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
35int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
[4386]36
[4452]37int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
38int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
39int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
40int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
[4386]41
[4452]42int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
43int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
44int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
45int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
46int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
47int KeyMapper::PEV_VIEW5             = EV_UNKNOWN; 
[4386]48
[4452]49int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
50int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
[4386]51
[4452]52int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
53int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
[4386]54
[4410]55
56
[4452]57//! this is the mapping array from names to ids: enter all orxonox.conf keys here
[4400]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"},
[4401]63                        {&KeyMapper::PEV_STRAFE_RIGHT, "StrafeRight"},
[4400]64                       
65                        {&KeyMapper::PEV_FIRE1, "Fire"},
66                        {&KeyMapper::PEV_FIRE1, "Fire1"},
67                        {&KeyMapper::PEV_FIRE2, "Fire2"},
[4412]68                        {&KeyMapper::PEV_NEXT_WEAPON, "Next"},
69                        {&KeyMapper::PEV_PREVIOUS_WEAPON, "Prev"},
[4386]70
[4412]71
[4400]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"},
[4410]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"},
[4400]84                        {NULL, NULL}};
85
86
[4452]87
[4367]88/**
89   \brief standard constructor
90*/
91KeyMapper::KeyMapper () 
92{
93   this->setClassID(CL_KEY_MAPPER, "KeyMapper"); 
94}
95
96
97/**
98   \brief standard deconstructor
99*/
100KeyMapper::~KeyMapper () 
101{
102}
[4369]103
[4452]104
105
[4369]106/**
107   \brief loads new key bindings from a file
108   \param filename: The path and name of the file to load the bindings from
109*/
110void KeyMapper::loadKeyBindings (const char* fileName)
111{
112  FILE* stream;
113 
114  PRINTF(4)("Loading key bindings from %s\n", fileName);
115 
116  // create parser
117  IniParser parser(fileName);
118  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
119    {
120      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
121      return;
122    }
123  // allocate empty lookup table
124 
125  char namebuf[256];
126  char valuebuf[256];
127  memset (namebuf, 0, 256);
128  memset (valuebuf, 0, 256);
129  int* index;
130 
131  while( parser.nextVar (namebuf, valuebuf) != -1)
132    {
[4399]133      PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
[4398]134      index = nameToIndex (valuebuf);
[4403]135      this->mapKeys(namebuf, index[1]);
[4369]136
[4403]137      /*
[4369]138      switch( index[0])
139        {
140        case 0:
[4402]141          this->mapKeys(namebuf, index[1]);
[4369]142          break;
143        case 1:
[4403]144          this->mapKeys(namebuf, index[1]);
[4369]145          break;
146        default:
147          break;
148        }
[4403]149      */
[4369]150      memset (namebuf, 0, 256);
151      memset (valuebuf, 0, 256);
152    }
153
154
155  // PARSE MISC SECTION
156  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
157    {
158      PRINTF(1)("Could not find key bindings in %s\n", fileName);
159      return;
160    }
161
162  while( parser.nextVar (namebuf, valuebuf) != -1)
163    {
[4399]164      PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
[4386]165      index = nameToIndex (valuebuf);     
[4403]166      this->mapKeys(namebuf, index[1]);
167      /*
[4369]168      switch( index[0])
169        {
170        case 0:
[4403]171          this->mapKeys(namebuf, index[1]);
[4369]172          break;
173        case 1:
[4403]174        this->mapKeys(namebuf, index[1]);
[4369]175          break;
176        default:
177          break;
178        }
[4403]179      */
[4369]180      memset (namebuf, 0, 256);
181      memset (valuebuf, 0, 256);
182    }
183}
[4386]184
185
[4452]186/**
187   \brief this function looks up name to key index
188   \param the name of the button
189*/
[4386]190int* KeyMapper::nameToIndex (char* name)
191{
192  coord[0] = -1;
193  coord[1] = -1;
194  int c;
195  if( (c = keynameToSDLK (name)) != -1)
196    {
197      coord[1] = c;
198      coord[0] = 0;
199    }
200  if( (c = buttonnameToSDLB (name)) != -1)
201    {
202      coord[1] = c;
203      coord[0] = 1;
204    }
205  return coord;
206}
[4389]207
208
[4452]209/**
210   \brief the function maps name to key ids
211   \param name of the key
212   \param id of the key
213*/
[4402]214void KeyMapper::mapKeys(char* name, int keyID)
[4399]215{
[4402]216  for(int i = 0; map[i].pValue != NULL; ++i )
[4399]217    {
[4402]218      if( !strcmp (name, map[i].pName)) { *map[i].pValue = keyID; break;}
[4411]219      PRINTF(0)("Mapping %s to id %i\n", name, keyID);
[4399]220    }
221}
222
223
[4452]224/**
225   \brief this function gives some debug information about the key mapper class
226*/
[4389]227void KeyMapper::debug()
228{
[4452]229  PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
[4403]230  for(int i = 0; map[i].pValue != NULL; ++i)
231    {
232      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
233    }
[4452]234  PRINT(0)("=======================================================\n");       
[4389]235}
Note: See TracBrowser for help on using the repository browser.