Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 4390 was 4389, checked in by patrick, 20 years ago

orxonox/trunk: working on keymapping, there are some problems in genericaly map unknown keys to well undefined functions… which is not very surprising :)

File size: 4.4 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 "keynames.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;
44
45
46
47/**
48   \brief standard constructor
49   \todo this constructor is not jet implemented - do it
50*/
51KeyMapper::KeyMapper () 
52{
53   this->setClassID(CL_KEY_MAPPER, "KeyMapper"); 
54   this->keyAliases = NULL;
55}
56
57
58/**
59   \brief standard deconstructor
60
61*/
62KeyMapper::~KeyMapper () 
63{
64  // delete what has to be deleted here
65}
66
67\
68/**
69   \brief loads new key bindings from a file
70   \param filename: The path and name of the file to load the bindings from
71*/
72void KeyMapper::loadKeyBindings (const char* fileName)
73{
74  FILE* stream;
75 
76  PRINTF(4)("Loading key bindings from %s\n", fileName);
77 
78  // remove old bindings if present
79  if( this->keyAliases != NULL)
80    {
81      free (this->keyAliases);
82      this->keyAliases = NULL;
83    }
84 
85  // create parser
86  IniParser parser(fileName);
87  if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1)
88    {
89      PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName);
90      return;
91    }
92  // allocate empty lookup table
93  this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings));
94 
95  char namebuf[256];
96  char valuebuf[256];
97  memset (namebuf, 0, 256);
98  memset (valuebuf, 0, 256);
99  int* index;
100 
101  while( parser.nextVar (namebuf, valuebuf) != -1)
102    {
103      //index = nameToIndex (valuebuf);
104      int c;
105      if( (c = keynameToSDLK (valuebuf)) != -1)
106        {
107          index[1] = c; index[0] = 0;
108        }
109      if( (c = buttonnameToSDLB (valuebuf)) != -1)
110        {
111          index[1] = c; index[0] = 1;
112        }
113
114      switch( index[0])
115        {
116        case 0:
117          PRINTF(0)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
118          strcpy (this->keyAliases->keys[index[1]], namebuf);
119          break;
120        case 1:
121          PRINTF(0)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
122          strcpy (this->keyAliases->buttons[index[1]], namebuf);
123          break;
124        default:
125          break;
126        }
127      memset (namebuf, 0, 256);
128      memset (valuebuf, 0, 256);
129    }
130
131
132  // PARSE MISC SECTION
133  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
134    {
135      PRINTF(1)("Could not find key bindings in %s\n", fileName);
136      return;
137    }
138
139  while( parser.nextVar (namebuf, valuebuf) != -1)
140    {
141      index = nameToIndex (valuebuf);     
142
143      switch( index[0])
144        {
145        case 0:
146          PRINTF(0)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
147          strcpy (keyAliases->keys[index[1]], namebuf);
148          break;
149        case 1:
150          PRINTF(0)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
151          strcpy (keyAliases->buttons[index[1]], namebuf);
152          break;
153        default:
154          break;
155        }
156      memset (namebuf, 0, 256);
157      memset (valuebuf, 0, 256);
158    }
159}
160
161
162
163int* KeyMapper::nameToIndex (char* name)
164{
165  coord[0] = -1;
166  coord[1] = -1;
167  int c;
168  if( (c = keynameToSDLK (name)) != -1)
169    {
170      coord[1] = c;
171      coord[0] = 0;
172    }
173  if( (c = buttonnameToSDLB (name)) != -1)
174    {
175      coord[1] = c;
176      coord[0] = 1;
177    }
178  return coord;
179}
180
181
182void KeyMapper::debug()
183{
184  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
185  PRINT(0)("Command 'up' got SDL key-ref nr %i \n", (this->nameToIndex("UP"))[0]);
186  PRINT(0)("Command 'down' got SDL key-ref nr %i \n", (this->nameToIndex("DOWN"))[0]);
187  PRINT(0)("Command 'right' got SDL key-ref nr %i \n", (this->nameToIndex("RIGHT"))[0]);
188  PRINT(0)("Command 'left' got SDL key-ref nr %i \n", (this->nameToIndex("LEFT"))[0]);
189
190  //PRINT(0)("=======================================================\n");       
191}
Note: See TracBrowser for help on using the repository browser.