src/supertux/menu/joystick_menu.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 "supertux/menu/joystick_menu.hpp"
00019 
00020 #include <sstream>
00021 
00022 #include "util/gettext.hpp"
00023 
00024 namespace{
00025   const int SCAN_JOYSTICKS = Controller::CONTROLCOUNT + 1;
00026 }
00027 
00028 JoystickMenu::JoystickMenu(JoystickKeyboardController* _controller) :
00029   controller(_controller)
00030 {
00031   recreateMenu();
00032 }
00033 
00034 JoystickMenu::~JoystickMenu()
00035 {}
00036 
00037 void
00038 JoystickMenu::recreateMenu()
00039 {
00040   clear();
00041   add_label(_("Setup Joystick"));
00042   add_hl();
00043   if(controller->joysticks.size() > 0) {
00044     add_controlfield(Controller::UP,          _("Up"));
00045     add_controlfield(Controller::DOWN,        _("Down"));
00046     add_controlfield(Controller::LEFT,        _("Left"));
00047     add_controlfield(Controller::RIGHT,       _("Right"));
00048     add_controlfield(Controller::JUMP,        _("Jump"));
00049     add_controlfield(Controller::ACTION,      _("Action"));
00050     add_controlfield(Controller::PAUSE_MENU,  _("Pause/Menu"));
00051     add_controlfield(Controller::PEEK_LEFT,   _("Peek Left"));
00052     add_controlfield(Controller::PEEK_RIGHT,  _("Peek Right"));
00053     add_controlfield(Controller::PEEK_UP,     _("Peek Up"));
00054     add_controlfield(Controller::PEEK_DOWN,   _("Peek Down"));
00055 
00056     add_toggle(Controller::CONTROLCOUNT, _("Jump with Up"), controller->jump_with_up_joy);
00057   } else {
00058     add_inactive(-1, _("No Joysticks found"));
00059   }
00060   add_inactive(-1,"");
00061   add_entry(SCAN_JOYSTICKS, _("Scan for Joysticks"));
00062 
00063   //Show Joysticks currently activated:
00064   for(std::vector<SDL_Joystick*>::iterator i = controller->joysticks.begin();
00065       i != controller->joysticks.end(); ++i) {
00066     if(*i != 0)
00067       add_inactive(-1, SDL_JoystickName(SDL_JoystickIndex(*i)) );
00068   }
00069 
00070   add_hl();
00071   add_back(_("Back"));
00072   update();
00073 }
00074 
00075 std::string
00076 JoystickMenu::get_button_name(int button)
00077 {
00078   if(button < 0)
00079     return _("None");
00080 
00081   std::ostringstream name;
00082   name << "Button " << button;
00083   return name.str();
00084 }
00085 
00086 void
00087 JoystickMenu::menu_action(MenuItem* item)
00088 {
00089   if (item->id >= 0 && item->id < Controller::CONTROLCOUNT) {
00090     item->change_input(_("Press Button"));
00091     controller->wait_for_joystick = item->id;
00092   } else if (item->id == Controller::CONTROLCOUNT) {
00093     controller->jump_with_up_joy = item->toggled;
00094   } else if( item->id == SCAN_JOYSTICKS) {
00095     controller->updateAvailableJoysticks();
00096     recreateMenu();
00097   }
00098 }
00099 
00100 void
00101 JoystickMenu::update_menu_item(Controller::Control id)
00102 {
00103   int button  = controller->reversemap_joybutton(id);
00104   int axis    = controller->reversemap_joyaxis(id);
00105   int hat_dir = controller->reversemap_joyhat(id);
00106 
00107   if (button != -1) {
00108     get_item_by_id((int)id).change_input(get_button_name(button));
00109   } else if (axis != 0) {
00110     std::ostringstream name;
00111 
00112     name << "Axis ";
00113 
00114     if (axis < 0)
00115       name << "-";
00116     else
00117       name << "+";
00118 
00119     if (abs(axis) == 1)
00120       name << "X";
00121     else if (abs(axis) == 2)
00122       name << "Y";
00123     else if (abs(axis) == 2)
00124       name << "X2";
00125     else if (abs(axis) == 3)
00126       name << "Y2";
00127     else
00128       name << abs(axis);
00129 
00130     get_item_by_id((int)id).change_input(name.str());
00131   } else if (hat_dir != -1) {
00132     std::string name;
00133 
00134     switch (hat_dir)
00135     {
00136       case SDL_HAT_UP:
00137         name = "Hat Up";
00138         break;
00139 
00140       case SDL_HAT_DOWN:
00141         name = "Hat Down";
00142         break;
00143 
00144       case SDL_HAT_LEFT:
00145         name = "Hat Left";
00146         break;
00147 
00148       case SDL_HAT_RIGHT:
00149         name = "Hat Right";
00150         break;
00151 
00152       default:
00153         name = "Unknown hat_dir";
00154         break;
00155     }
00156 
00157     get_item_by_id((int)id).change_input(name);
00158   } else {
00159     get_item_by_id((int)id).change_input("None");
00160   }
00161 }
00162 
00163 void
00164 JoystickMenu::update()
00165 {
00166   if(controller->joysticks.size() == 0)
00167     return;
00168 
00169   update_menu_item(Controller::UP);
00170   update_menu_item(Controller::DOWN);
00171   update_menu_item(Controller::LEFT);
00172   update_menu_item(Controller::RIGHT);
00173 
00174   update_menu_item(Controller::JUMP);
00175   update_menu_item(Controller::ACTION);
00176   update_menu_item(Controller::PAUSE_MENU);
00177   update_menu_item(Controller::PEEK_LEFT);
00178   update_menu_item(Controller::PEEK_RIGHT);
00179   update_menu_item(Controller::PEEK_UP);
00180   update_menu_item(Controller::PEEK_DOWN);
00181 
00182   get_item_by_id(Controller::CONTROLCOUNT).toggled = controller->jump_with_up_joy;
00183 }
00184 
00185 /* EOF */

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