Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: moved the keynames strucure to a place closer to util/event, some small tests

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      PRINTF(0)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf);
104      index = nameToIndex (valuebuf);
105
106      switch( index[0])
107        {
108        case 0:
109          PRINTF(0)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
110          strcpy (this->keyAliases->keys[index[1]], namebuf);
111          break;
112        case 1:
113          PRINTF(0)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
114          strcpy (this->keyAliases->buttons[index[1]], namebuf);
115          break;
116        default:
117          break;
118        }
119      memset (namebuf, 0, 256);
120      memset (valuebuf, 0, 256);
121    }
122
123
124  // PARSE MISC SECTION
125  if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1)
126    {
127      PRINTF(1)("Could not find key bindings in %s\n", fileName);
128      return;
129    }
130
131  while( parser.nextVar (namebuf, valuebuf) != -1)
132    {
133      PRINTF(0)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf);
134      index = nameToIndex (valuebuf);     
135
136      switch( index[0])
137        {
138        case 0:
139          PRINTF(0)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf);
140          strcpy (keyAliases->keys[index[1]], namebuf);
141          break;
142        case 1:
143          PRINTF(0)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf);
144          strcpy (keyAliases->buttons[index[1]], namebuf);
145          break;
146        default:
147          break;
148        }
149      memset (namebuf, 0, 256);
150      memset (valuebuf, 0, 256);
151    }
152}
153
154
155
156int* KeyMapper::nameToIndex (char* name)
157{
158  coord[0] = -1;
159  coord[1] = -1;
160  int c;
161  if( (c = keynameToSDLK (name)) != -1)
162    {
163      coord[1] = c;
164      coord[0] = 0;
165    }
166  if( (c = buttonnameToSDLB (name)) != -1)
167    {
168      coord[1] = c;
169      coord[0] = 1;
170    }
171  return coord;
172}
173
174
175void KeyMapper::debug()
176{
177  //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); 
178  PRINT(0)("Command 'up' got SDL key-ref nr %i \n", keynameToSDLK("UP"));
179  PRINT(0)("Command 'down' got SDL key-ref nr %i \n", (this->nameToIndex("DOWN"))[1]);
180  PRINT(0)("Command 'right' got SDL key-ref nr %i \n", (this->nameToIndex("RIGHT"))[1]);
181  PRINT(0)("Command 'left' got SDL key-ref nr %i \n", (this->nameToIndex("LEFT"))[1]);
182
183  //PRINT(0)("=======================================================\n");       
184}
Note: See TracBrowser for help on using the repository browser.