Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/trunk/src/lib/event/key_mapper.cc @ 5026

Last change on this file since 5026 was 5015, checked in by bensch, 19 years ago

orxonox/trunk: robust IniParser

File size: 5.8 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 "globals.h"
24#include "ini_parser.h"
25#include "key_names.h"
26
27using namespace std;
28
29
30/* initialize all variables to a reasonable value*/
31int KeyMapper::PEV_UP                = EV_UNKNOWN;
32int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
33int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
34int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
35int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
36int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
37
38int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
39int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
40int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
41int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
42
43int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
44int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
45int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
46int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
47int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
48int KeyMapper::PEV_VIEW5             = EV_UNKNOWN;
49
50int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
51int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
52
53int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
54int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
55
56
57
58//! this is the mapping array from names to ids: enter all orxonox.conf keys here
59/** @todo use globals.h for this.... everything is done there for those Options,
60 * and you do not have to care about The namings, as they might change
61 */
62orxKeyMapping map[] = {
63  {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP},
64  {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN},
65  {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT},
66  {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT},
67  {&KeyMapper::PEV_STRAFE_LEFT,          "StrafeLeft"},
68  {&KeyMapper::PEV_STRAFE_RIGHT,         "StrafeRight"},
69
70  {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE},
71  {&KeyMapper::PEV_FIRE1,                "Fire1"},
72  {&KeyMapper::PEV_FIRE2,                "Fire2"},
73  {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON},
74  {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON},
75
76
77  {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0},
78  {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1},
79  {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2},
80  {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3},
81  {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4},
82  {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5},
83
84  {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD},
85  {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD},
86
87  {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE},
88  {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT},
89  {NULL, NULL}
90};
91
92
93
94/**
95 *  standard constructor
96*/
97KeyMapper::KeyMapper ()
98{
99   this->setClassID(CL_KEY_MAPPER, "KeyMapper");
100}
101
102
103/**
104 *  standard deconstructor
105*/
106KeyMapper::~KeyMapper ()
107{
108}
109
110
111/**
112 *  loads new key bindings from a file
113 * @param filename: The path and name of the file to load the bindings from
114*/
115void KeyMapper::loadKeyBindings (const char* fileName)
116{
117  IniParser parser(fileName);
118  this->loadKeyBindings(&parser);
119}
120
121void KeyMapper::loadKeyBindings(IniParser* iniParser)
122{
123  if( !iniParser->getSection (CONFIG_SECTION_PLAYER "1"))
124  {
125    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1\n");
126    return;
127  }
128  int* index;
129
130  iniParser->getFirstVar();
131  while(iniParser->getCurrentName())
132  {
133    PRINTF(3)("Keys: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
134    index = nameToIndex (iniParser->getCurrentValue());
135    this->mapKeys(iniParser->getCurrentName(), index[1]);
136    iniParser->nextVar();
137  }
138
139
140  // PARSE MISC SECTION
141  if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
142  {
143    PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
144    return;
145  }
146
147  iniParser->getFirstVar();
148  while(iniParser->getCurrentName())
149  {
150    PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
151    index = nameToIndex (iniParser->getCurrentValue());
152    this->mapKeys(iniParser->getCurrentName(), index[1]);
153    iniParser->nextVar();
154  }
155}
156
157/**
158 *  this function looks up name to key index
159 * @param the name of the button
160*/
161int* KeyMapper::nameToIndex (const char* name)
162{
163  coord[0] = -1;
164  coord[1] = -1;
165  int c;
166  if( (c = keynameToSDLK (name)) != -1)
167    {
168      coord[1] = c;
169      coord[0] = 0;
170    }
171  if( (c = buttonnameToSDLB (name)) != -1)
172    {
173      coord[1] = c;
174      coord[0] = 1;
175    }
176  return coord;
177}
178
179
180/**
181 *  the function maps name to key ids
182 * @param name of the key
183 * @param id of the key
184*/
185void KeyMapper::mapKeys(const char* name, int keyID)
186{
187  for(int i = 0; map[i].pValue != NULL; ++i )
188    {
189      if( !strcmp (name, map[i].pName))
190      {
191        *map[i].pValue = keyID;
192        PRINTF(0)("Mapping %s to id %i\n", name, keyID);
193        break;
194      }
195    }
196}
197
198
199/**
200 *  this function gives some debug information about the key mapper class
201*/
202void KeyMapper::debug()
203{
204  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
205  for(int i = 0; map[i].pValue != NULL; ++i)
206    {
207      PRINT(0)("%s = %i\n",map[i].pName, *map[i].pValue);
208    }
209  PRINT(0)("=======================================================\n");
210}
Note: See TracBrowser for help on using the repository browser.