src/control/joystickkeyboardcontroller.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
00003 //                2007 Ingo Ruhnke <grumbel@gmx.de>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #include "control/joystickkeyboardcontroller.hpp"
00019 
00020 #include <iostream>
00021 
00022 #include "lisp/list_iterator.hpp"
00023 #include "gui/menu_manager.hpp"
00024 #include "supertux/console.hpp"
00025 #include "supertux/gameconfig.hpp"
00026 #include "supertux/menu/menu_storage.hpp"
00027 #include "supertux/menu/joystick_menu.hpp"
00028 #include "supertux/menu/keyboard_menu.hpp"
00029 #include "util/gettext.hpp"
00030 #include "util/writer.hpp"
00031 
00032 JoystickKeyboardController::JoystickKeyboardController() :
00033   controller(),
00034   keymap(),
00035   joy_button_map(),
00036   joy_axis_map(),
00037   joy_hat_map(),
00038   joysticks(),
00039   name(),
00040   dead_zone(),
00041   min_joybuttons(),
00042   max_joybuttons(),
00043   max_joyaxis(),
00044   max_joyhats(),
00045   hat_state(0),
00046   jump_with_up_joy(),
00047   jump_with_up_kbd(),
00048   wait_for_key(-1), 
00049   wait_for_joystick(-1)
00050 {
00051   controller = new Controller;
00052 
00053   // initialize default keyboard map
00054   keymap[SDLK_LEFT]     = Controller::LEFT;
00055   keymap[SDLK_RIGHT]    = Controller::RIGHT;
00056   keymap[SDLK_UP]       = Controller::UP;
00057   keymap[SDLK_DOWN]     = Controller::DOWN;
00058   keymap[SDLK_SPACE]    = Controller::JUMP;
00059   keymap[SDLK_LCTRL]    = Controller::ACTION;
00060   keymap[SDLK_LALT]     = Controller::ACTION;
00061   keymap[SDLK_ESCAPE]   = Controller::PAUSE_MENU;
00062   keymap[SDLK_p]        = Controller::PAUSE_MENU;
00063   keymap[SDLK_PAUSE]    = Controller::PAUSE_MENU;
00064   keymap[SDLK_RETURN]   = Controller::MENU_SELECT;
00065   keymap[SDLK_KP_ENTER] = Controller::MENU_SELECT;
00066   keymap[SDLK_CARET]    = Controller::CONSOLE;
00067   keymap[SDLK_DELETE]   = Controller::PEEK_LEFT;
00068   keymap[SDLK_PAGEDOWN] = Controller::PEEK_RIGHT;
00069   keymap[SDLK_HOME]     = Controller::PEEK_UP;
00070   keymap[SDLK_END]      = Controller::PEEK_DOWN;
00071 
00072   jump_with_up_joy = false;
00073   jump_with_up_kbd = false;
00074 
00075   updateAvailableJoysticks();
00076 
00077   dead_zone = 1000;
00078 
00079   // Default joystick button configuration
00080   bind_joybutton(0, 0, Controller::JUMP);
00081   bind_joybutton(0, 1, Controller::ACTION);
00082   // 6 or more Buttons
00083   if( min_joybuttons > 5 ){
00084     bind_joybutton(0, 4, Controller::PEEK_LEFT);
00085     bind_joybutton(0, 5, Controller::PEEK_RIGHT);
00086     // 8 or more
00087     if(min_joybuttons > 7)
00088       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
00089   } else {
00090     // map the last 2 buttons to menu and pause
00091     if(min_joybuttons > 2)
00092       bind_joybutton(0, min_joybuttons-1, Controller::PAUSE_MENU);
00093     // map all remaining joystick buttons to MENU_SELECT
00094     for(int i = 2; i < max_joybuttons; ++i) {
00095       if(i != min_joybuttons-1)
00096         bind_joybutton(0, i, Controller::MENU_SELECT);
00097     }
00098   }
00099 
00100   // Default joystick axis configuration
00101   bind_joyaxis(0, -1, Controller::LEFT);
00102   bind_joyaxis(0, 1, Controller::RIGHT);
00103   bind_joyaxis(0, -2, Controller::UP);
00104   bind_joyaxis(0, 2, Controller::DOWN);
00105 }
00106 
00107 JoystickKeyboardController::~JoystickKeyboardController()
00108 {
00109   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
00110       i != joysticks.end(); ++i) {
00111     if(*i != 0)
00112       SDL_JoystickClose(*i);
00113   }
00114   delete controller;
00115 }
00116 
00117 void
00118 JoystickKeyboardController::updateAvailableJoysticks()
00119 {
00120   for(std::vector<SDL_Joystick*>::iterator i = joysticks.begin();
00121       i != joysticks.end(); ++i) {
00122     if(*i != 0)
00123       SDL_JoystickClose(*i);
00124   }
00125   joysticks.clear();
00126   
00127   SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
00128   SDL_InitSubSystem(SDL_INIT_JOYSTICK);
00129 
00130   int joystick_count = SDL_NumJoysticks();
00131   min_joybuttons = -1;
00132   max_joybuttons = -1;
00133   max_joyaxis    = -1;
00134   max_joyhats    = -1;
00135 
00136   if( joystick_count > 0 ){
00137     for(int i = 0; i < joystick_count; ++i) {
00138       SDL_Joystick* joystick = SDL_JoystickOpen(i);
00139       bool good = true;
00140       if(SDL_JoystickNumButtons(joystick) < 2) {
00141         log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 buttons" << std::endl;
00142         good = false;
00143       }
00144       if(SDL_JoystickNumAxes(joystick) < 2
00145          && SDL_JoystickNumHats(joystick) == 0) {
00146         log_info << "Joystick " << i << ": " << SDL_JoystickName(i) << " has less than 2 axes and no hat" << std::endl;
00147         good = false;
00148       }
00149       if(!good) {
00150         SDL_JoystickClose(joystick);
00151         continue;
00152       }
00153 
00154       if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
00155         min_joybuttons = SDL_JoystickNumButtons(joystick);
00156 
00157       if(SDL_JoystickNumButtons(joystick) > max_joybuttons)
00158         max_joybuttons = SDL_JoystickNumButtons(joystick);
00159 
00160       if(SDL_JoystickNumAxes(joystick) > max_joyaxis)
00161         max_joyaxis = SDL_JoystickNumAxes(joystick);
00162 
00163       if(SDL_JoystickNumHats(joystick) > max_joyhats)
00164         max_joyhats = SDL_JoystickNumHats(joystick);
00165 
00166       joysticks.push_back(joystick);
00167     }
00168   }
00169 
00170   // some joysticks or SDL seem to produce some bogus events after being opened
00171   Uint32 ticks = SDL_GetTicks();
00172   while(SDL_GetTicks() - ticks < 200) {
00173     SDL_Event event;
00174     SDL_PollEvent(&event);
00175   }
00176 }
00177 
00178 Controller*
00179 JoystickKeyboardController::get_main_controller()
00180 {
00181   return controller;
00182 }
00183 
00184 void
00185 JoystickKeyboardController::read(const Reader& lisp)
00186 {
00187   const lisp::Lisp* keymap_lisp = lisp.get_lisp("keymap");
00188   if(keymap_lisp) {
00189     keymap.clear();
00190     keymap_lisp->get("jump-with-up", jump_with_up_kbd);
00191     lisp::ListIterator iter(keymap_lisp);
00192     while(iter.next()) {
00193       if(iter.item() == "map") {
00194         int key = -1;
00195         std::string control;
00196         const lisp::Lisp* map = iter.lisp();
00197         map->get("key", key);
00198         map->get("control", control);
00199         if(key < SDLK_FIRST || key >= SDLK_LAST) {
00200           log_info << "Invalid key '" << key << "' in keymap" << std::endl;
00201           continue;
00202         }
00203 
00204         int i = 0;
00205         for(i = 0; Controller::controlNames[i] != 0; ++i) {
00206           if(control == Controller::controlNames[i])
00207             break;
00208         }
00209         if(Controller::controlNames[i] == 0) {
00210           log_info << "Invalid control '" << control << "' in keymap" << std::endl;
00211           continue;
00212         }
00213         keymap[SDLKey(key)] = Control(i);
00214       }
00215     }
00216   }
00217 
00218   const lisp::Lisp* joystick_lisp = lisp.get_lisp("joystick");
00219   if(joystick_lisp) {
00220     joystick_lisp->get("dead-zone", dead_zone);
00221     joystick_lisp->get("jump-with-up", jump_with_up_joy);
00222     lisp::ListIterator iter(joystick_lisp);
00223     while(iter.next()) {
00224       if(iter.item() == "map") {
00225         int button = -1;
00226         int axis   = 0;
00227         int hat    = -1;
00228         std::string control;
00229         const lisp::Lisp* map = iter.lisp();
00230 
00231         map->get("control", control);
00232         int i = 0;
00233         for(i = 0; Controller::controlNames[i] != 0; ++i) {
00234           if(control == Controller::controlNames[i])
00235             break;
00236         }
00237         if(Controller::controlNames[i] == 0) {
00238           log_info << "Invalid control '" << control << "' in buttonmap" << std::endl;
00239           continue;
00240         }
00241 
00242         if (map->get("button", button)) {
00243           if(button < 0 || button >= max_joybuttons) {
00244             log_info << "Invalid button '" << button << "' in buttonmap" << std::endl;
00245             continue;
00246           }
00247           bind_joybutton(0, button, Control(i));
00248         }
00249 
00250         if (map->get("axis",   axis)) {
00251           if (axis == 0 || abs(axis) > max_joyaxis) {
00252             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
00253             continue;
00254           }
00255           bind_joyaxis(0, axis, Control(i));
00256         }
00257 
00258         if (map->get("hat",   hat)) {
00259           if (hat != SDL_HAT_UP   &&
00260               hat != SDL_HAT_DOWN &&
00261               hat != SDL_HAT_LEFT &&
00262               hat != SDL_HAT_RIGHT) {
00263             log_info << "Invalid axis '" << axis << "' in axismap" << std::endl;
00264             continue;
00265           } else {
00266             bind_joyhat(0, hat, Control(i));
00267           }
00268         }
00269       }
00270     }
00271   }
00272 }
00273 
00274 void
00275 JoystickKeyboardController::write(Writer& writer)
00276 {
00277   writer.start_list("keymap");
00278   writer.write("jump-with-up", jump_with_up_kbd);
00279   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
00280     writer.start_list("map");
00281     writer.write("key", (int) i->first);
00282     writer.write("control", Controller::controlNames[i->second]);
00283     writer.end_list("map");
00284   }
00285   writer.end_list("keymap");
00286 
00287   writer.start_list("joystick");
00288   writer.write("dead-zone", dead_zone);
00289   writer.write("jump-with-up", jump_with_up_joy);
00290 
00291   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end();
00292       ++i) {
00293     writer.start_list("map");
00294     writer.write("button", i->first.second);
00295     writer.write("control", Controller::controlNames[i->second]);
00296     writer.end_list("map");
00297   }
00298 
00299   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00300     writer.start_list("map");
00301     writer.write("hat", i->first.second);
00302     writer.write("control", Controller::controlNames[i->second]);
00303     writer.end_list("map");
00304   }
00305 
00306   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00307     writer.start_list("map");
00308     writer.write("axis", i->first.second);
00309     writer.write("control", Controller::controlNames[i->second]);
00310     writer.end_list("map");
00311   }
00312 
00313   writer.end_list("joystick");
00314 }
00315 
00316 void
00317 JoystickKeyboardController::update()
00318 {
00319   controller->update();
00320 }
00321 
00322 void
00323 JoystickKeyboardController::reset()
00324 {
00325   controller->reset();
00326 }
00327 
00328 void
00329 JoystickKeyboardController::set_joy_controls(Control id, bool value)
00330 {
00331   if (jump_with_up_joy && id == Controller::UP)
00332     controller->set_control(Controller::JUMP, value);
00333 
00334   controller->set_control(id, value);
00335 }
00336 
00337 void
00338 JoystickKeyboardController::process_event(const SDL_Event& event)
00339 {
00340   switch(event.type) {
00341     case SDL_KEYUP:
00342     case SDL_KEYDOWN:
00343       process_key_event(event.key);
00344       break;
00345 
00346     case SDL_JOYAXISMOTION:
00347       process_axis_event(event.jaxis);
00348       break;
00349 
00350     case SDL_JOYHATMOTION:
00351       process_hat_event(event.jhat);
00352       break;
00353 
00354     case SDL_JOYBUTTONDOWN:
00355     case SDL_JOYBUTTONUP:
00356       process_button_event(event.jbutton);
00357       break;
00358 
00359     default:
00360       break;
00361   }
00362 }
00363 
00364 void
00365 JoystickKeyboardController::process_button_event(const SDL_JoyButtonEvent& jbutton)
00366 {
00367   if(wait_for_joystick >= 0) 
00368   {
00369     if(jbutton.state == SDL_PRESSED)
00370     {
00371       bind_joybutton(jbutton.which, jbutton.button, (Control)wait_for_joystick);
00372       MenuStorage::get_joystick_options_menu()->update();
00373       reset();
00374       wait_for_joystick = -1;
00375     }
00376   } 
00377   else 
00378   {
00379     ButtonMap::iterator i = joy_button_map.find(std::make_pair(jbutton.which, jbutton.button));
00380     if(i == joy_button_map.end()) {
00381       log_debug << "Unmapped joybutton " << (int)jbutton.button << " pressed" << std::endl;
00382     } else {
00383       set_joy_controls(i->second, (jbutton.state == SDL_PRESSED));
00384     }
00385   }
00386 }
00387 
00388 void
00389 JoystickKeyboardController::process_axis_event(const SDL_JoyAxisEvent& jaxis)
00390 {
00391   if (wait_for_joystick >= 0)
00392   {
00393     if (abs(jaxis.value) > dead_zone) {
00394       if (jaxis.value < 0)
00395         bind_joyaxis(jaxis.which, -(jaxis.axis + 1), Control(wait_for_joystick));
00396       else
00397         bind_joyaxis(jaxis.which, jaxis.axis + 1, Control(wait_for_joystick));
00398 
00399       MenuStorage::get_joystick_options_menu()->update();
00400       wait_for_joystick = -1;
00401     }
00402   }
00403   else
00404   {
00405     // Split the axis into left and right, so that both can be
00406     // mapped separately (needed for jump/down vs up/down)
00407     int axis = jaxis.axis + 1;
00408 
00409     AxisMap::iterator left  = joy_axis_map.find(std::make_pair(jaxis.which, -axis));
00410     AxisMap::iterator right = joy_axis_map.find(std::make_pair(jaxis.which, axis));
00411 
00412     if(left == joy_axis_map.end()) {
00413       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
00414     } else {
00415       if (jaxis.value < -dead_zone)
00416         set_joy_controls(left->second,  true);
00417       else if (jaxis.value > dead_zone)
00418         set_joy_controls(left->second, false);
00419       else
00420         set_joy_controls(left->second, false);
00421     }
00422 
00423     if(right == joy_axis_map.end()) {
00424       // std::cout << "Unmapped joyaxis " << (int)jaxis.axis << " moved" << std::endl;
00425     } else {
00426       if (jaxis.value < -dead_zone)
00427         set_joy_controls(right->second, false);
00428       else if (jaxis.value > dead_zone)
00429         set_joy_controls(right->second, true);
00430       else
00431         set_joy_controls(right->second, false);
00432     }
00433   }
00434 }
00435 
00436 void
00437 JoystickKeyboardController::process_hat_event(const SDL_JoyHatEvent& jhat)
00438 {
00439   Uint8 changed = hat_state ^ jhat.value;
00440 
00441   if (wait_for_joystick >= 0)
00442   {
00443     if (changed & SDL_HAT_UP && jhat.value & SDL_HAT_UP)
00444       bind_joyhat(jhat.which, SDL_HAT_UP, Control(wait_for_joystick));
00445 
00446     if (changed & SDL_HAT_DOWN && jhat.value & SDL_HAT_DOWN)
00447       bind_joyhat(jhat.which, SDL_HAT_DOWN, Control(wait_for_joystick));
00448 
00449     if (changed & SDL_HAT_LEFT && jhat.value & SDL_HAT_LEFT)
00450       bind_joyhat(jhat.which, SDL_HAT_LEFT, Control(wait_for_joystick));
00451 
00452     if (changed & SDL_HAT_RIGHT && jhat.value & SDL_HAT_RIGHT)
00453       bind_joyhat(jhat.which, SDL_HAT_RIGHT, Control(wait_for_joystick));
00454 
00455     MenuStorage::get_joystick_options_menu()->update();
00456     wait_for_joystick = -1;
00457   }
00458   else
00459   {
00460     if (changed & SDL_HAT_UP)
00461     {
00462       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_UP));
00463       if (it != joy_hat_map.end())
00464         set_joy_controls(it->second, jhat.value & SDL_HAT_UP);
00465     }
00466 
00467     if (changed & SDL_HAT_DOWN)
00468     {
00469       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_DOWN));
00470       if (it != joy_hat_map.end())
00471         set_joy_controls(it->second, jhat.value & SDL_HAT_DOWN);
00472     }
00473 
00474     if (changed & SDL_HAT_LEFT)
00475     {
00476       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_LEFT));
00477       if (it != joy_hat_map.end())
00478         set_joy_controls(it->second, jhat.value & SDL_HAT_LEFT);
00479     }
00480 
00481     if (changed & SDL_HAT_RIGHT)
00482     {
00483       HatMap::iterator it = joy_hat_map.find(std::make_pair(jhat.which, SDL_HAT_RIGHT));
00484       if (it != joy_hat_map.end())
00485         set_joy_controls(it->second, jhat.value & SDL_HAT_RIGHT);
00486     }
00487   }
00488 
00489   hat_state = jhat.value;
00490 }
00491 
00492 void
00493 JoystickKeyboardController::process_key_event(const SDL_KeyboardEvent& event)
00494 {
00495   KeyMap::iterator key_mapping = keymap.find(event.keysym.sym);
00496 
00497   // if console key was pressed: toggle console
00498   if ((key_mapping != keymap.end()) && (key_mapping->second == Controller::CONSOLE)) {
00499     if (event.type == SDL_KEYDOWN) 
00500       Console::instance->toggle();
00501   } else {
00502     if (Console::instance->hasFocus()) {
00503       // if console is open: send key there
00504       process_console_key_event(event);
00505     } else if (MenuManager::current()) {
00506       // if menu mode: send key there
00507       process_menu_key_event(event);
00508     } else if(key_mapping == keymap.end()) {
00509       // default action: update controls
00510       //log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl;
00511     } else {
00512       Control control = key_mapping->second;
00513       bool value = (event.type == SDL_KEYDOWN);
00514       controller->set_control(control, value);
00515       if (jump_with_up_kbd && control == Controller::UP){
00516         controller->set_control(Controller::JUMP, value);
00517       }
00518     }
00519   }
00520 }
00521 
00522 void
00523 JoystickKeyboardController::process_console_key_event(const SDL_KeyboardEvent& event)
00524 {
00525   if (event.type != SDL_KEYDOWN) return;
00526 
00527   switch (event.keysym.sym) {
00528     case SDLK_RETURN:
00529       Console::instance->enter();
00530       break;
00531     case SDLK_BACKSPACE:
00532       Console::instance->backspace();
00533       break;
00534     case SDLK_TAB:
00535       Console::instance->autocomplete();
00536       break;
00537     case SDLK_PAGEUP:
00538       Console::instance->scroll(-1);
00539       break;
00540     case SDLK_PAGEDOWN:
00541       Console::instance->scroll(+1);
00542       break;
00543     case SDLK_HOME:
00544       Console::instance->move_cursor(-65535);
00545       break;
00546     case SDLK_END:
00547       Console::instance->move_cursor(+65535);
00548       break;
00549     case SDLK_UP:
00550       Console::instance->show_history(-1);
00551       break;
00552     case SDLK_DOWN:
00553       Console::instance->show_history(+1);
00554       break;
00555     case SDLK_LEFT:
00556       Console::instance->move_cursor(-1);
00557       break;
00558     case SDLK_RIGHT:
00559       Console::instance->move_cursor(+1);
00560       break;
00561     default:
00562       int c = event.keysym.unicode;
00563       if ((c >= 32) && (c <= 126)) {
00564         Console::instance->input((char)c);
00565       }
00566       break;
00567   }
00568 }
00569 
00570 void
00571 JoystickKeyboardController::process_menu_key_event(const SDL_KeyboardEvent& event)
00572 {
00573   // wait for key mode?
00574   if(wait_for_key >= 0) {
00575     if(event.type == SDL_KEYUP)
00576       return;
00577 
00578     if(event.keysym.sym != SDLK_ESCAPE
00579        && event.keysym.sym != SDLK_PAUSE) {
00580       bind_key(event.keysym.sym, Control(wait_for_key));
00581     }
00582     reset();
00583     MenuStorage::get_key_options_menu()->update();
00584     wait_for_key = -1;
00585     return;
00586   }
00587   if(wait_for_joystick >= 0) {
00588     if(event.keysym.sym == SDLK_ESCAPE) {
00589       reset();
00590       MenuStorage::get_joystick_options_menu()->update();
00591       wait_for_joystick = -1;
00592     }
00593     return;
00594   }
00595 
00596   Control control;
00597   /* we use default keys when the menu is open (to avoid problems when
00598    * redefining keys to invalid settings
00599    */
00600   switch(event.keysym.sym) {
00601     case SDLK_UP:
00602       control = Controller::UP;
00603       break;
00604     case SDLK_DOWN:
00605       control = Controller::DOWN;
00606       break;
00607     case SDLK_LEFT:
00608       control = Controller::LEFT;
00609       break;
00610     case SDLK_RIGHT:
00611       control = Controller::RIGHT;
00612       break;
00613     case SDLK_SPACE:
00614     case SDLK_RETURN:
00615     case SDLK_KP_ENTER:
00616       control = Controller::MENU_SELECT;
00617       break;
00618     case SDLK_ESCAPE:
00619     case SDLK_PAUSE:
00620       control = Controller::PAUSE_MENU;
00621       break;
00622     default:
00623       return;
00624       break;
00625   }
00626 
00627   controller->set_control(control, (event.type == SDL_KEYDOWN));
00628 }
00629 
00630 void
00631 JoystickKeyboardController::unbind_joystick_control(Control control)
00632 {
00633   // remove all previous mappings for that control
00634   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); /* no ++i */) {
00635     if(i->second == control)
00636       joy_axis_map.erase(i++);
00637     else
00638       ++i;
00639   }
00640 
00641   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); /* no ++i */) {
00642     if(i->second == control)
00643       joy_button_map.erase(i++);
00644     else
00645       ++i;
00646   }
00647 
00648   for(HatMap::iterator i = joy_hat_map.begin();  i != joy_hat_map.end(); /* no ++i */) {
00649     if(i->second == control)
00650       joy_hat_map.erase(i++);
00651     else
00652       ++i;
00653   }
00654 }
00655 
00656 void
00657 JoystickKeyboardController::bind_joyaxis(JoyId joy_id, int axis, Control control)
00658 {
00659   // axis isn't the SDL axis number, but axisnumber + 1 with sign
00660   // changed depending on if the positive or negative end is to be
00661   // used (negative axis 0 becomes -1, positive axis 2 becomes +3,
00662   // etc.)
00663 
00664   unbind_joystick_control(control);
00665 
00666   // add new mapping
00667   joy_axis_map[std::make_pair(joy_id, axis)] = control;
00668 }
00669 
00670 void
00671 JoystickKeyboardController::bind_joyhat(JoyId joy_id, int dir, Control c)
00672 {
00673   unbind_joystick_control(c);
00674 
00675   // add new mapping
00676   joy_hat_map[std::make_pair(joy_id, dir)] = c;
00677 }
00678 
00679 void
00680 JoystickKeyboardController::bind_joybutton(JoyId joy_id, int button, Control control)
00681 {
00682   unbind_joystick_control(control);
00683 
00684   // add new mapping
00685   joy_button_map[std::make_pair(joy_id, button)] = control;
00686 }
00687 
00688 void
00689 JoystickKeyboardController::bind_key(SDLKey key, Control control)
00690 {
00691   // remove all previous mappings for that control and for that key
00692   for(KeyMap::iterator i = keymap.begin();
00693       i != keymap.end(); /* no ++i */) {
00694     if(i->second == control) {
00695       KeyMap::iterator e = i;
00696       ++i;
00697       keymap.erase(e);
00698     } else {
00699       ++i;
00700     }
00701   }
00702 
00703   KeyMap::iterator i = keymap.find(key);
00704   if(i != keymap.end())
00705     keymap.erase(i);
00706 
00707   // add new mapping
00708   keymap[key] = control;
00709 }
00710 
00711 void
00712 JoystickKeyboardController::print_joystick_mappings()
00713 {
00714   std::cout << "Joystick Mappings" << std::endl;
00715   std::cout << "-----------------" << std::endl;
00716   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00717     std::cout << "Axis: " << i->first.second << " -> " << i->second << std::endl;
00718   }
00719 
00720   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
00721     std::cout << "Button: " << i->first.second << " -> " << i->second << std::endl;
00722   }
00723 
00724   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00725     std::cout << "Hat: " << i->first.second << " -> " << i->second << std::endl;
00726   }
00727   std::cout << std::endl;
00728 }
00729 
00730 SDLKey
00731 JoystickKeyboardController::reversemap_key(Control c)
00732 {
00733   for(KeyMap::iterator i = keymap.begin(); i != keymap.end(); ++i) {
00734     if(i->second == c)
00735       return i->first;
00736   }
00737 
00738   return SDLK_UNKNOWN;
00739 }
00740 
00741 int
00742 JoystickKeyboardController::reversemap_joyaxis(Control c)
00743 {
00744   for(AxisMap::iterator i = joy_axis_map.begin(); i != joy_axis_map.end(); ++i) {
00745     if(i->second == c)
00746       return i->first.second;
00747   }
00748 
00749   return 0;
00750 }
00751 
00752 int
00753 JoystickKeyboardController::reversemap_joybutton(Control c)
00754 {
00755   for(ButtonMap::iterator i = joy_button_map.begin(); i != joy_button_map.end(); ++i) {
00756     if(i->second == c)
00757       return i->first.second;
00758   }
00759 
00760   return -1;
00761 }
00762 
00763 int
00764 JoystickKeyboardController::reversemap_joyhat(Control c)
00765 {
00766   for(HatMap::iterator i = joy_hat_map.begin(); i != joy_hat_map.end(); ++i) {
00767     if(i->second == c)
00768       return i->first.second;
00769   }
00770 
00771   return -1;
00772 }
00773 
00774 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1