Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10679 was 10638, checked in by snellen, 18 years ago

Merged inputdevice-branch back to trunk: New ScriptTrigger (ActionTrigger) available

File size: 8.7 KB
RevLine 
[4582]1/*
[4367]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
[4582]14
[4388]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
[5915]23#include "event_def.h"
24
[10618]25#include "orxonox_globals.h"
[5944]26#include "parser/ini_parser/ini_parser.h"
[9869]27#include "parser/preferences/preferences.h"
[4400]28#include "key_names.h"
[7661]29#include "event_def.h"
[5075]30#include "debug.h"
[4386]31
[5915]32
[4367]33
[9869]34ObjectListDefinition(KeyMapper);
[4367]35
[4452]36/* initialize all variables to a reasonable value*/
[6997]37int KeyMapper::PEV_FORWARD           = EV_UNKNOWN;
38int KeyMapper::PEV_BACKWARD          = EV_UNKNOWN;
[4452]39int KeyMapper::PEV_LEFT              = EV_UNKNOWN;
40int KeyMapper::PEV_RIGHT             = EV_UNKNOWN;
[6998]41int KeyMapper::PEV_UP                = EV_UNKNOWN;
42int KeyMapper::PEV_DOWN              = EV_UNKNOWN;
[5978]43int KeyMapper::PEV_ROLL_LEFT         = EV_UNKNOWN;
44int KeyMapper::PEV_ROLL_RIGHT        = EV_UNKNOWN;
[4452]45int KeyMapper::PEV_STRAFE_LEFT       = EV_UNKNOWN;
46int KeyMapper::PEV_STRAFE_RIGHT      = EV_UNKNOWN;
[8724]47int KeyMapper::PEV_JUMP              = EV_UNKNOWN;
[10638]48int KeyMapper::PEV_ACTION            = EV_UNKNOWN;
[4386]49
[4452]50int KeyMapper::PEV_FIRE1             = EV_UNKNOWN;
51int KeyMapper::PEV_FIRE2             = EV_UNKNOWN;
52int KeyMapper::PEV_PREVIOUS_WEAPON   = EV_UNKNOWN;
53int KeyMapper::PEV_NEXT_WEAPON       = EV_UNKNOWN;
[4386]54
[6998]55int KeyMapper::PEV_CHANGE_SHIP       = EV_UNKNOWN;
56
[4452]57int KeyMapper::PEV_VIEW0             = EV_UNKNOWN;
58int KeyMapper::PEV_VIEW1             = EV_UNKNOWN;
59int KeyMapper::PEV_VIEW2             = EV_UNKNOWN;
60int KeyMapper::PEV_VIEW3             = EV_UNKNOWN;
61int KeyMapper::PEV_VIEW4             = EV_UNKNOWN;
[4582]62int KeyMapper::PEV_VIEW5             = EV_UNKNOWN;
[4386]63
[4452]64int KeyMapper::PEV_NEXT_WORLD        = EV_UNKNOWN;
65int KeyMapper::PEV_PREVIOUS_WORLD    = EV_UNKNOWN;
[4386]66
[4452]67int KeyMapper::PEV_PAUSE             = EV_UNKNOWN;
68int KeyMapper::PEV_QUIT              = EV_UNKNOWN;
[4386]69
[4410]70
71
[4452]72//! this is the mapping array from names to ids: enter all orxonox.conf keys here
[4833]73/** @todo use globals.h for this.... everything is done there for those Options,
[4834]74 * and you do not have to care about The namings, as they might change
[4833]75 */
[7661]76KeyMapper::KeyMapping KeyMapper::map[] = {
[9880]77      {&KeyMapper::PEV_FORWARD,              CONFIG_NAME_PLAYER_FORWARD,         SDLK_w},
78      {&KeyMapper::PEV_BACKWARD,             CONFIG_NAME_PLAYER_BACKWARD,        SDLK_s},
79      {&KeyMapper::PEV_UP,                   CONFIG_NAME_PLAYER_UP,              SDLK_r},
80      {&KeyMapper::PEV_DOWN,                 CONFIG_NAME_PLAYER_DOWN,            SDLK_f},
81      {&KeyMapper::PEV_LEFT,                 CONFIG_NAME_PLAYER_LEFT,            SDLK_a},
82      {&KeyMapper::PEV_RIGHT,                CONFIG_NAME_PLAYER_RIGHT,           SDLK_d},
83      {&KeyMapper::PEV_ROLL_RIGHT,           CONFIG_NAME_PLAYER_ROLL_LEFT,       SDLK_z},
84      {&KeyMapper::PEV_ROLL_LEFT,            CONFIG_NAME_PLAYER_ROLL_RIGHT,      SDLK_c},
[10638]85      {&KeyMapper::PEV_STRAFE_LEFT,          CONFIG_NAME_PLAYER_STRAFE_LEFT,     SDLK_q},
86      {&KeyMapper::PEV_STRAFE_RIGHT,         CONFIG_NAME_PLAYER_STRAFE_RIGHT,    SDLK_e},
87      {&KeyMapper::PEV_JUMP,                 CONFIG_NAME_PLAYER_JUMP,            SDLK_SPACE},
88      {&KeyMapper::PEV_ACTION,               CONFIG_NAME_PLAYER_ACTION,          SDLK_u},
[4386]89
[9880]90      {&KeyMapper::PEV_FIRE1,                CONFIG_NAME_PLAYER_FIRE,            EV_MOUSE_BUTTON_LEFT},
[10638]91      {&KeyMapper::PEV_FIRE2,                CONFIG_NAME_PLAYER_FIRE2,           EV_MOUSE_BUTTON_RIGHT},
[9880]92      {&KeyMapper::PEV_NEXT_WEAPON,          CONFIG_NAME_PLAYER_NEXT_WEAPON,     EV_MOUSE_BUTTON_WHEELUP},
93      {&KeyMapper::PEV_PREVIOUS_WEAPON,      CONFIG_NAME_PLAYER_PREV_WEAPON,     EV_MOUSE_BUTTON_WHEELDOWN},
[4412]94
[9880]95      {&KeyMapper::PEV_CHANGE_SHIP,          CONFIG_NAME_PLAYER_CHANGE_SHIP,     SDLK_g},
[4410]96
[6998]97
[9880]98      {&KeyMapper::PEV_VIEW0,                CONFIG_NAME_VIEW0,                  SDLK_1},
99      {&KeyMapper::PEV_VIEW1,                CONFIG_NAME_VIEW1,                  SDLK_2},
100      {&KeyMapper::PEV_VIEW2,                CONFIG_NAME_VIEW2,                  SDLK_3},
101      {&KeyMapper::PEV_VIEW3,                CONFIG_NAME_VIEW3,                  SDLK_4},
102      {&KeyMapper::PEV_VIEW4,                CONFIG_NAME_VIEW4,                  SDLK_5},
103      {&KeyMapper::PEV_VIEW5,                CONFIG_NAME_VIEW5,                  SDLK_6},
[4410]104
[9880]105      {&KeyMapper::PEV_NEXT_WORLD,           CONFIG_NAME_NEXT_WORLD,             SDLK_x},
106      {&KeyMapper::PEV_PREVIOUS_WORLD,       CONFIG_NAME_PREV_WORLD,             SDLK_z},
[4582]107
[9880]108      {&KeyMapper::PEV_PAUSE,                CONFIG_NAME_PAUSE,                  SDLK_p},
109      {&KeyMapper::PEV_QUIT,                 CONFIG_NAME_QUIT,                   SDLK_ESCAPE},
110      {NULL, "", 0}
111    };
[4400]112
113
[4452]114
[4367]115/**
[4836]116 *  standard constructor
[4367]117*/
[4582]118KeyMapper::KeyMapper ()
[4367]119{
[9869]120  this->registerObject(this, KeyMapper::_objectList);
[4367]121}
122
123
124/**
[4836]125 *  standard deconstructor
[4367]126*/
[4582]127KeyMapper::~KeyMapper ()
[9880]128{}
[4369]129
[4452]130
[4369]131/**
[4836]132 *  loads new key bindings from a file
133 * @param filename: The path and name of the file to load the bindings from
[4369]134*/
[7221]135void KeyMapper::loadKeyBindings (const std::string& fileName)
[4369]136{
[4866]137  IniParser parser(fileName);
138  this->loadKeyBindings(&parser);
139}
[4582]140
[4866]141void KeyMapper::loadKeyBindings(IniParser* iniParser)
142{
[9880]143  IniParser::Section* section = iniParser->getSection (CONFIG_SECTION_CONTROL);
144  if( section == NULL)
[4866]145  {
[7661]146    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n");
[4866]147    return;
148  }
[4369]149  int* index;
[4582]150
[9880]151
152  for(IniParser::Entry::iterator entry = section->begin();
153      entry != section->end();
154      ++entry)
[4866]155  {
[9880]156    PRINTF(3)("Keys: Parsing %s, %s now.\n", (*entry).name().c_str(), (*entry).value().c_str());
[5474]157    // map the name to an sdl index
[9880]158    index = nameToIndex ((*entry).value());
[5474]159    // map the index to a internal name
[9880]160    this->mapKeys((*entry).name(), index);
[4866]161  }
[4369]162
163
164  // PARSE MISC SECTION
[9880]165  //   if( !iniParser->getSection (CONFIG_SECTION_MISC_KEYS))
166  //   {
167  //     PRINTF(1)("Could not find key bindings" CONFIG_SECTION_MISC_KEYS "\n");
168  //     return;
169  //   }
170  //
171  //   iniParser->firstVar();
172  //   while( iniParser->getCurrentName() != "" )
173  //   {
174  //     PRINTF(3)("MISC: Parsing %s, %s now.\n", iniParser->getCurrentName(), iniParser->getCurrentValue());
175  //     index = nameToIndex (iniParser->getCurrentValue());
176  //     this->mapKeys(iniParser->getCurrentName(), index);
177  //     iniParser->nextVar();
178  //   }
[4369]179}
[4386]180
[7256]181void KeyMapper::loadKeyBindings()
182{
[7661]183  if( !Preferences::getInstance()->sectionExists(CONFIG_SECTION_CONTROL))
[7256]184  {
[7661]185    PRINTF(1)("Could not find key bindings " CONFIG_SECTION_CONTROL "\n");
[7256]186    return;
187  }
188  int* index;
189
[7661]190  std::list<std::string> keys = Preferences::getInstance()->listKeys(CONFIG_SECTION_CONTROL);
[7256]191  for ( std::list<std::string>::const_iterator it = keys.begin(); it!=keys.end(); it++ )
192  {
[7661]193    PRINTF(3)("Keys: Parsing %s, %s now.\n", it->c_str(), Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, "").c_str());
[7256]194    // map the name to an sdl index
[7661]195    index = nameToIndex (Preferences::getInstance()->getString(CONFIG_SECTION_CONTROL, *it, ""));
[7256]196    // map the index to a internal name
197    this->mapKeys(*it, index);
198  }
199}
200
[4452]201/**
[4836]202 *  this function looks up name to key index
203 * @param the name of the button
[4452]204*/
[7221]205int* KeyMapper::nameToIndex (const std::string& name)
[4386]206{
207  coord[0] = -1;
208  coord[1] = -1;
209  int c;
[9880]210  if( (c = keynameToSDLK (name)) != -1)
211  {
212    coord[1] = c;
213    coord[0] = 0;
214  }
215  if( (c = buttonnameToSDLB (name)) != -1)
216  {
217    coord[1] = c;
218    coord[0] = 1;
219  }
[4386]220  return coord;
221}
[4389]222
223
[4452]224/**
[4836]225 *  the function maps name to key ids
226 * @param name of the key
227 * @param id of the key
[4452]228*/
[7221]229void KeyMapper::mapKeys(const std::string& name, int* index)
[4399]230{
[5474]231  for( int i = 0; map[i].pValue != NULL; ++i )
[9880]232  {
233    if( name == map[i].pName)
[4399]234    {
[9880]235      if( index[0] == 0)
[4582]236      {
[9880]237        *map[i].pValue = index[1];
238        PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLKToKeyname(index[1]).c_str(), index[1]);
239        break;
[4582]240      }
[9880]241      else
242      {
243        *map[i].pValue = index[1];
244        PRINTF(4)("Mapping %s to '%s' (id %i)\n", name.c_str(), SDLBToButtonname(index[1]).c_str(), index[1]);
245        break;
246      }
[4399]247    }
[9880]248  }
[4399]249}
250
251
[4452]252/**
[4836]253 *  this function gives some debug information about the key mapper class
[4452]254*/
[4389]255void KeyMapper::debug()
256{
[4582]257  PRINT(0)("\n==========================| KeyMapper::debug() |===\n");
[4403]258  for(int i = 0; map[i].pValue != NULL; ++i)
[9880]259  {
260    PRINT(0)("%s = %i\n",map[i].pName.c_str(), *map[i].pValue);
261  }
[4582]262  PRINT(0)("=======================================================\n");
[4389]263}
Note: See TracBrowser for help on using the repository browser.