joystick button off by one
[supertux.git] / src / control / joystickkeyboardcontroller.cpp
index d154f15..c62153d 100644 (file)
@@ -1,7 +1,7 @@
 //  $Id$
-// 
+//
 //  SuperTux
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
 //
 //  This program is free software; you can redistribute it and/or
 //  modify it under the terms of the GNU General Public License
 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 //  GNU General Public License for more details.
-// 
+//
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-//  02111-1307, USA.
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
 #include <config.h>
 
 #include <sstream>
 #include "joystickkeyboardcontroller.hpp"
-#include "msg.hpp"
+#include "log.hpp"
 #include "gui/menu.hpp"
 #include "gettext.hpp"
 #include "lisp/lisp.hpp"
@@ -71,6 +71,8 @@ JoystickKeyboardController::JoystickKeyboardController()
   keymap.insert(std::make_pair(SDLK_RETURN, MENU_SELECT));
   keymap.insert(std::make_pair(SDLK_KP_ENTER, MENU_SELECT));
   keymap.insert(std::make_pair(SDLK_CARET, CONSOLE));
+  keymap.insert(std::make_pair(SDLK_DELETE, PEEK_LEFT));
+  keymap.insert(std::make_pair(SDLK_END, PEEK_RIGHT));
   
   int joystick_count = SDL_NumJoysticks();
   min_joybuttons = -1;
@@ -79,12 +81,12 @@ JoystickKeyboardController::JoystickKeyboardController()
     SDL_Joystick* joystick = SDL_JoystickOpen(i);
     bool good = true;
     if(SDL_JoystickNumButtons(joystick) < 2) {
-      msg_warning << "Joystick " << i << " has less than 2 buttons" << std::endl;
+      log_warning << "Joystick " << i << " has less than 2 buttons" << std::endl;
       good = false;
     }
     if(SDL_JoystickNumAxes(joystick) < 2
        && SDL_JoystickNumHats(joystick) == 0) {
-      msg_warning << "Joystick " << i << " has less than 2 axes and no hat" << std::endl;
+      log_warning << "Joystick " << i << " has less than 2 axes and no hat" << std::endl;
       good = false;
     }
     if(!good) {
@@ -109,13 +111,28 @@ JoystickKeyboardController::JoystickKeyboardController()
   
   joy_button_map.insert(std::make_pair(0, JUMP));
   joy_button_map.insert(std::make_pair(1, ACTION));
-  // map the last 2 buttons to menu and pause
-  if(min_joybuttons > 2)
-    joy_button_map.insert(std::make_pair(min_joybuttons-1, PAUSE_MENU));
-  // map all remaining joystick buttons to MENU_SELECT
-  for(int i = 2; i < max_joybuttons; ++i) {
-    if(i != min_joybuttons-1)
-      joy_button_map.insert(std::make_pair(i, MENU_SELECT));
+  // 6 or more Buttons
+  if( min_joybuttons > 5 ){
+    joy_button_map.insert(std::make_pair( 4, PEEK_LEFT));
+    joy_button_map.insert(std::make_pair( 5, PEEK_RIGHT));
+    // 8 or more
+    if(min_joybuttons > 7)
+      joy_button_map.insert(std::make_pair(min_joybuttons-1, PAUSE_MENU));
+    // map all remaining joystick buttons to MENU_SELECT
+    for(int i = 2; i < max_joybuttons; ++i) {
+      if( i != min_joybuttons-1 && i !=4  && i!= 5 )
+        joy_button_map.insert(std::make_pair(i, MENU_SELECT));
+    }
+    
+  } else {
+    // map the last 2 buttons to menu and pause
+    if(min_joybuttons > 2)
+      joy_button_map.insert(std::make_pair(min_joybuttons-1, PAUSE_MENU));
+    // map all remaining joystick buttons to MENU_SELECT
+    for(int i = 2; i < max_joybuttons; ++i) {
+      if(i != min_joybuttons-1)
+        joy_button_map.insert(std::make_pair(i, MENU_SELECT));
+    }
   }
 
   // some joysticks or SDL seem to produce some bogus events after being opened
@@ -153,7 +170,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
         map->get("key", key);
         map->get("control", control);
         if(key < SDLK_FIRST || key >= SDLK_LAST) {
-          msg_warning << "Invalid key '" << key << "' in keymap" << std::endl;
+          log_warning << "Invalid key '" << key << "' in keymap" << std::endl;
           continue;
         }
 
@@ -163,12 +180,12 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
             break;
         }
         if(controlNames[i] == 0) {
-          msg_warning << "Invalid control '" << control << "' in keymap" << std::endl;
+          log_warning << "Invalid control '" << control << "' in keymap" << std::endl;
           continue;
         }
         keymap.insert(std::make_pair((SDLKey) key, (Control) i));
       } else {
-        msg_warning << "Invalid lisp element '" << iter.item() << "' in keymap" << std::endl;
+        log_warning << "Invalid lisp element '" << iter.item() << "' in keymap" << std::endl;
       }
     }
   }
@@ -189,7 +206,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
         map->get("button", button);
         map->get("control", control);
         if(button < 0 || button >= max_joybuttons) {
-          msg_warning << "Invalid button '" << button << "' in buttonmap" << std::endl;
+          log_warning << "Invalid button '" << button << "' in buttonmap" << std::endl;
           continue;
         }
         
@@ -199,7 +216,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
             break;
         }                                                                           
         if(controlNames[i] == 0) {
-          msg_warning << "Invalid control '" << control << "' in buttonmap" << std::endl;
+          log_warning << "Invalid control '" << control << "' in buttonmap" << std::endl;
           continue;
         }
         reset_joybutton(button, (Control) i);
@@ -321,7 +338,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
 
       ButtonMap::iterator i = joy_button_map.find(event.jbutton.button);
       if(i == joy_button_map.end()) {
-        msg_debug << "Unmapped joybutton " << (int)event.jbutton.button << " pressed" << std::endl;
+        log_debug << "Unmapped joybutton " << (int)event.jbutton.button << " pressed" << std::endl;
         return;
       }
       
@@ -361,7 +378,7 @@ JoystickKeyboardController::process_key_event(const SDL_Event& event)
 
   // default action: update controls
   if(key_mapping == keymap.end()) {
-    msg_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl;
+    log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl;
     return;
   }
   Control control = key_mapping->second;
@@ -392,6 +409,12 @@ JoystickKeyboardController::process_console_key_event(const SDL_Event& event)
     case SDLK_END:
       Console::instance->scroll(+65535);
       break;
+    case SDLK_UP:
+      Console::instance->show_history(-1);
+      break;
+    case SDLK_DOWN:
+      Console::instance->show_history(+1);
+      break;
     default:
       int c = event.key.keysym.unicode;
       if ((c >= 32) && (c <= 126)) {
@@ -563,14 +586,16 @@ JoystickKeyboardController::KeyboardMenu::KeyboardMenu(
     JoystickKeyboardController* _controller)
   : controller(_controller)
 {
-    add_label(_("Keyboard Setup"));
+    add_label(_("Setup Keyboard"));
     add_hl();
     add_controlfield(Controller::UP, _("Up"));
     add_controlfield(Controller::DOWN, _("Down"));
     add_controlfield(Controller::LEFT, _("Left"));
     add_controlfield(Controller::RIGHT, _("Right"));
     add_controlfield(Controller::JUMP, _("Jump"));
-    add_controlfield(Controller::ACTION, _("Shoot/Run"));
+    add_controlfield(Controller::ACTION, _("Action"));
+    add_controlfield(Controller::PEEK_LEFT, _("Peek Left"));
+    add_controlfield(Controller::PEEK_RIGHT, _("Peek Right"));
     add_hl();
     add_back(_("Back"));
     update();
@@ -638,6 +663,10 @@ JoystickKeyboardController::KeyboardMenu::update()
     controller->reversemap_key(Controller::JUMP)));
   get_item_by_id((int) Controller::ACTION).change_input(get_key_name(
     controller->reversemap_key(Controller::ACTION)));
+  get_item_by_id((int) Controller::PEEK_LEFT).change_input(get_key_name(
+    controller->reversemap_key(Controller::PEEK_LEFT)));
+  get_item_by_id((int) Controller::PEEK_RIGHT).change_input(get_key_name(
+    controller->reversemap_key(Controller::PEEK_RIGHT)));
 }
 
 //---------------------------------------------------------------------------
@@ -646,12 +675,14 @@ JoystickKeyboardController::JoystickMenu::JoystickMenu(
   JoystickKeyboardController* _controller)
   : controller(_controller)
 {
-  add_label(_("Joystick Setup"));
+  add_label(_("Setup Joystick"));
   add_hl();
   if(controller->joysticks.size() > 0) {
     add_controlfield(Controller::JUMP, _("Jump"));
-    add_controlfield(Controller::ACTION, _("Shoot/Run"));
+    add_controlfield(Controller::ACTION, _("Action"));
     add_controlfield(Controller::PAUSE_MENU, _("Pause/Menu"));
+    add_controlfield(Controller::PEEK_LEFT, _("Peek Left"));
+    add_controlfield(Controller::PEEK_RIGHT, _("Peek Right"));
   } else {
     add_deactive(-1, _("No Joysticks found"));
   }
@@ -695,5 +726,9 @@ JoystickKeyboardController::JoystickMenu::update()
     controller->reversemap_joybutton(Controller::ACTION)));
   get_item_by_id((int) Controller::PAUSE_MENU).change_input(get_button_name(
     controller->reversemap_joybutton(Controller::PAUSE_MENU)));
+  get_item_by_id((int) Controller::PEEK_LEFT).change_input(get_button_name(
+    controller->reversemap_joybutton(Controller::PEEK_LEFT)));
+  get_item_by_id((int) Controller::PEEK_RIGHT).change_input(get_button_name(
+    controller->reversemap_joybutton(Controller::PEEK_RIGHT)));
 }