/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Patrick Boenzli co-programmer: Christian Meyer This code was inspired by the command_node.cc code from Christian Meyer in revision 4386 and earlier. */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_EVENT #include "key_mapper.h" #include "ini_parser.h" #include "key_names.h" using namespace std; int KeyMapper::PEV_UP = -1; int KeyMapper::PEV_DOWN = -1; int KeyMapper::PEV_LEFT = -1; int KeyMapper::PEV_RIGHT = -1; int KeyMapper::PEV_STRAFE_LEFT = -1; int KeyMapper::PEV_STRAFE_RIGHT = -1; int KeyMapper::PEV_FIRE1 = -1; int KeyMapper::PEV_FIRE2 = -1; int KeyMapper::PEV_VIEW1 = -1; int KeyMapper::PEV_VIEW2 = -1; int KeyMapper::PEV_VIEW3 = -1; int KeyMapper::PEV_VIEW4 = -1; int KeyMapper::PEV_VIEW5 = -1; orxKeyMapping map[] = { {&KeyMapper::PEV_UP, "Up"}, {&KeyMapper::PEV_DOWN, "Down"}, {&KeyMapper::PEV_LEFT, "Left"}, {&KeyMapper::PEV_RIGHT, "Right"}, {&KeyMapper::PEV_STRAFE_LEFT, "StrafeLeft"}, {&KeyMapper::PEV_STRAFE_RIGTH, "StrafeRight"}, {&KeyMapper::PEV_FIRE1, "Fire"}, {&KeyMapper::PEV_FIRE1, "Fire1"}, {&KeyMapper::PEV_FIRE2, "Fire2"}, {&KeyMapper::PEV_VIEW0, "view0"}, {&KeyMapper::PEV_VIEW1, "view1"}, {&KeyMapper::PEV_VIEW2, "view2"}, {&KeyMapper::PEV_VIEW3, "view3"}, {&KeyMapper::PEV_VIEW4, "view4"}, {&KeyMapper::PEV_VIEW5, "view5"} {NULL, NULL}}; /** \brief standard constructor \todo this constructor is not jet implemented - do it */ KeyMapper::KeyMapper () { this->setClassID(CL_KEY_MAPPER, "KeyMapper"); this->keyAliases = NULL; } /** \brief standard deconstructor */ KeyMapper::~KeyMapper () { // delete what has to be deleted here } \ /** \brief loads new key bindings from a file \param filename: The path and name of the file to load the bindings from */ void KeyMapper::loadKeyBindings (const char* fileName) { FILE* stream; PRINTF(4)("Loading key bindings from %s\n", fileName); // remove old bindings if present if( this->keyAliases != NULL) { free (this->keyAliases); this->keyAliases = NULL; } // create parser IniParser parser(fileName); if( parser.getSection (CONFIG_SECTION_PLAYER "1") == -1) { PRINTF(1)("Could not find key bindings " CONFIG_SECTION_PLAYER"1 in %s\n", fileName); return; } // allocate empty lookup table this->keyAliases = (KeyBindings*) calloc (1, sizeof (KeyBindings)); char namebuf[256]; char valuebuf[256]; memset (namebuf, 0, 256); memset (valuebuf, 0, 256); int* index; while( parser.nextVar (namebuf, valuebuf) != -1) { PRINTF(3)("Keys: Parsing %s, %s now.\n", namebuf, valuebuf); index = nameToIndex (valuebuf); switch( index[0]) { case 0: PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); strcpy (this->keyAliases->keys[index[1]], namebuf); break; case 1: PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); strcpy (this->keyAliases->buttons[index[1]], namebuf); break; default: break; } memset (namebuf, 0, 256); memset (valuebuf, 0, 256); } // PARSE MISC SECTION if( parser.getSection (CONFIG_SECTION_MISC_KEYS) == -1) { PRINTF(1)("Could not find key bindings in %s\n", fileName); return; } while( parser.nextVar (namebuf, valuebuf) != -1) { PRINTF(3)("MISC: Parsing %s, %s now.\n", namebuf, valuebuf); index = nameToIndex (valuebuf); switch( index[0]) { case 0: PRINTF(3)("Key binding %d(%s) set to %s\n", index[1], SDLKToKeyname( index[1]), namebuf); strcpy (keyAliases->keys[index[1]], namebuf); break; case 1: PRINTF(3)("Button binding %d(%s) set to %s\n", index[1], SDLBToButtonname( index[1]), namebuf); strcpy (keyAliases->buttons[index[1]], namebuf); break; default: break; } memset (namebuf, 0, 256); memset (valuebuf, 0, 256); } this->mapKeys(); } int* KeyMapper::nameToIndex (char* name) { coord[0] = -1; coord[1] = -1; int c; if( (c = keynameToSDLK (name)) != -1) { coord[1] = c; coord[0] = 0; } if( (c = buttonnameToSDLB (name)) != -1) { coord[1] = c; coord[0] = 1; } return coord; } void KeyMapper::mapKeys() { for(int i = 0; i < N_STD_KEYS; ++i) { if( !strcmp (this->keyAliases->keys[i], "Up")) PEV_UP = i; } PRINTF(0)("fire = %i\n", PEV_FIRE1); } void KeyMapper::debug() { //PRINT(0)("\n==========================| KeyMapper::debug() |===\n"); PRINT(0)("Command 'up' got SDL key-ref nr %i \n", keynameToSDLK("UP")); PRINT(0)("Command 'down' got SDL key-ref nr %i \n", (this->nameToIndex("DOWN"))[1]); PRINT(0)("Command 'right' got SDL key-ref nr %i \n", (this->nameToIndex("RIGHT"))[1]); PRINT(0)("Command 'left' got SDL key-ref nr %i \n", (this->nameToIndex("LEFT"))[1]); //PRINT(0)("=======================================================\n"); }