Merged back changes from 0.3.x branch
[supertux.git] / src / control / joystickkeyboardcontroller.cpp
index 0fb4d9e..5e2455a 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 "log.hpp"
 #include "gui/menu.hpp"
 #include "gettext.hpp"
 #include "lisp/lisp.hpp"
 #include "lisp/list_iterator.hpp"
 #include "game_session.hpp"
+#include "console.hpp"
+#include "gameconfig.hpp"
 
 class JoystickKeyboardController::JoystickMenu : public Menu
 {
@@ -50,13 +53,11 @@ public:
   virtual void menu_action(MenuItem* item);
   JoystickKeyboardController* controller;
 };
-  
+
 JoystickKeyboardController::JoystickKeyboardController()
   : wait_for_key(-1), wait_for_joybutton(-1), key_options_menu(0),
     joystick_options_menu(0)
 {
-  memset(last_keys, 0, sizeof(last_keys));
-
   // initialize default keyboard map
   keymap.insert(std::make_pair(SDLK_LEFT, LEFT));
   keymap.insert(std::make_pair(SDLK_RIGHT, RIGHT));
@@ -67,10 +68,13 @@ JoystickKeyboardController::JoystickKeyboardController()
   keymap.insert(std::make_pair(SDLK_LALT, ACTION));
   keymap.insert(std::make_pair(SDLK_ESCAPE, PAUSE_MENU));
   keymap.insert(std::make_pair(SDLK_p, PAUSE_MENU));
-  keymap.insert(std::make_pair(SDLK_PAUSE, PAUSE_MENU));  
+  keymap.insert(std::make_pair(SDLK_PAUSE, PAUSE_MENU));
   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;
   max_joybuttons = -1;
@@ -78,19 +82,19 @@ JoystickKeyboardController::JoystickKeyboardController()
     SDL_Joystick* joystick = SDL_JoystickOpen(i);
     bool good = true;
     if(SDL_JoystickNumButtons(joystick) < 2) {
-      std::cerr << "Joystick " << i << " has less than 2 buttons.\n";
+      log_info << "Joystick " << i << " has less than 2 buttons" << std::endl;
       good = false;
     }
     if(SDL_JoystickNumAxes(joystick) < 2
        && SDL_JoystickNumHats(joystick) == 0) {
-      std::cerr << "Joystick " << i << " has less than 2 axes and no hat.\n";
+      log_info << "Joystick " << i << " has less than 2 axes and no hat" << std::endl;
       good = false;
     }
     if(!good) {
       SDL_JoystickClose(joystick);
       continue;
     }
-    
+
     if(min_joybuttons < 0 || SDL_JoystickNumButtons(joystick) < min_joybuttons)
       min_joybuttons = SDL_JoystickNumButtons(joystick);
     if(SDL_JoystickNumButtons(joystick) > max_joybuttons) {
@@ -105,16 +109,31 @@ JoystickKeyboardController::JoystickKeyboardController()
   joyaxis_y = 1;
   dead_zone_x = 1000;
   dead_zone_y = 1000;
-  
+
   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
@@ -152,7 +171,7 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
         map->get("key", key);
         map->get("control", control);
         if(key < SDLK_FIRST || key >= SDLK_LAST) {
-          std::cerr << "Invalid key '" << key << "' in keymap.\n";
+          log_info << "Invalid key '" << key << "' in keymap" << std::endl;
           continue;
         }
 
@@ -162,12 +181,12 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
             break;
         }
         if(controlNames[i] == 0) {
-          std::cerr << "Invalid control '" << control << "' in keymap.\n";
+          log_info << "Invalid control '" << control << "' in keymap" << std::endl;
           continue;
         }
         keymap.insert(std::make_pair((SDLKey) key, (Control) i));
       } else {
-        std::cerr << "Invalid lisp element '" << iter.item() << "' in keymap.\n";
+        log_info << "Invalid lisp element '" << iter.item() << "' in keymap" << std::endl;
       }
     }
   }
@@ -188,17 +207,17 @@ JoystickKeyboardController::read(const lisp::Lisp& lisp)
         map->get("button", button);
         map->get("control", control);
         if(button < 0 || button >= max_joybuttons) {
-          std::cerr << "Invalid button '" << button << "' in buttonmap.\n";
+          log_info << "Invalid button '" << button << "' in buttonmap" << std::endl;
           continue;
         }
-        
+
         int i = 0;
         for(i = 0; controlNames[i] != 0; ++i) {
           if(control == controlNames[i])
             break;
-        }                                                                           
+        }
         if(controlNames[i] == 0) {
-          std::cerr << "Invalid control '" << control << "' in buttonmap.\n";
+          log_info << "Invalid control '" << control << "' in buttonmap" << std::endl;
           continue;
         }
         reset_joybutton(button, (Control) i);
@@ -231,15 +250,13 @@ JoystickKeyboardController::write(lisp::Writer& writer)
     writer.write_string("control", controlNames[i->second]);
     writer.end_list("map");
   }
-  writer.end_list("joystick");  
+  writer.end_list("joystick");
 }
 
 void
 JoystickKeyboardController::reset()
 {
   Controller::reset();
-  for(size_t i = 0; i < sizeof(last_keys); ++i)
-      last_keys[i] = 0;
 }
 
 void
@@ -248,31 +265,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
   switch(event.type) {
     case SDL_KEYUP:
     case SDL_KEYDOWN:
-      // remember ascii keys for cheat codes...
-      if(event.type == SDL_KEYDOWN && 
-          (event.key.keysym.unicode & 0xFF80) == 0) {
-        memmove(last_keys, last_keys+1, sizeof(last_keys)-1);
-        last_keys[sizeof(last_keys)-1] = event.key.keysym.unicode;
-        if(GameSession::current())
-          GameSession::current()->try_cheats();
-      }
-                       
-      // menu mode?
-      if(Menu::current()) { // menu mode
-        process_menu_key_event(event);
-        return;
-      } else {
-        // normal mode, find key in keymap
-        KeyMap::iterator i = keymap.find(event.key.keysym.sym);
-        if(i == keymap.end()) {
-#ifdef DEBUG
-          std::cerr << "Pressed key without mapping.\n";
-#endif
-          return;
-        }
-        Control control = i->second;
-        controls[control] = event.type == SDL_KEYDOWN ? true : false;
-      }
+      process_key_event(event);
       break;
 
     case SDL_JOYAXISMOTION:
@@ -304,7 +297,7 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
     case SDL_JOYHATMOTION:
       if(!use_hat)
         break;
-      
+
       if(event.jhat.value & SDL_HAT_UP) {
         controls[UP] = true;
         controls[DOWN] = false;
@@ -346,15 +339,11 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
 
       ButtonMap::iterator i = joy_button_map.find(event.jbutton.button);
       if(i == joy_button_map.end()) {
-#ifdef DEBUG
-        std::cerr << "Unmapped joybutton " << (int) event.jbutton.button
-          << " pressed.\n";
-#endif
+        log_debug << "Unmapped joybutton " << (int)event.jbutton.button << " pressed" << std::endl;
         return;
       }
-      
-      controls[i->second] =
-        event.type == SDL_JOYBUTTONDOWN ? true : false;
+
+      controls[i->second] = (event.type == SDL_JOYBUTTONDOWN);
       break;
     }
 
@@ -364,6 +353,78 @@ JoystickKeyboardController::process_event(const SDL_Event& event)
 }
 
 void
+JoystickKeyboardController::process_key_event(const SDL_Event& event)
+{
+  KeyMap::iterator key_mapping = keymap.find(event.key.keysym.sym);
+
+  // if console key was pressed: toggle console
+  if ((key_mapping != keymap.end()) && (key_mapping->second == CONSOLE)) {
+    if (event.type != SDL_KEYDOWN) return;
+    Console::instance->toggle();
+    return;
+  }
+
+  // if console is open: send key there
+  if (Console::instance->hasFocus()) {
+    process_console_key_event(event);
+    return;
+  }
+
+  // if menu mode: send key there
+  if (Menu::current()) {
+    process_menu_key_event(event);
+    return;
+  }
+
+  // default action: update controls
+  if(key_mapping == keymap.end()) {
+    log_debug << "Key " << event.key.keysym.sym << " is unbound" << std::endl;
+    return;
+  }
+  Control control = key_mapping->second;
+  controls[control] = (event.type == SDL_KEYDOWN);
+}
+
+void
+JoystickKeyboardController::process_console_key_event(const SDL_Event& event)
+{
+  if (event.type != SDL_KEYDOWN) return;
+
+  switch (event.key.keysym.sym) {
+    case SDLK_RETURN:
+      Console::instance->input << std::endl;
+      break;
+    case SDLK_BACKSPACE:
+      Console::instance->backspace();
+      break;
+    case SDLK_TAB:
+      Console::instance->autocomplete();
+      break;
+    case SDLK_PAGEUP:
+      Console::instance->scroll(-1);
+      break;
+    case SDLK_PAGEDOWN:
+      Console::instance->scroll(+1);
+      break;
+    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)) {
+       Console::instance->input << (char)c;
+      }
+      break;
+  }
+}
+
+void
 JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
 {
   // wait for key mode?
@@ -371,7 +432,7 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
     if(event.type == SDL_KEYUP)
       return;
 
-    if(event.key.keysym.sym != SDLK_ESCAPE                      
+    if(event.key.keysym.sym != SDLK_ESCAPE
         && event.key.keysym.sym != SDLK_PAUSE) {
       reset_key(event.key.keysym.sym, (Control) wait_for_key);
     }
@@ -379,7 +440,7 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
     key_options_menu->update();
     wait_for_key = -1;
     return;
-  } 
+  }
   if(wait_for_joybutton >= 0) {
     if(event.key.keysym.sym == SDLK_ESCAPE) {
       reset();
@@ -388,7 +449,7 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
     }
     return;
   }
+
   Control control;
   /* we use default keys when the menu is open (to avoid problems when
    * redefining keys to invalid settings
@@ -420,7 +481,7 @@ JoystickKeyboardController::process_menu_key_event(const SDL_Event& event)
       break;
   }
 
-  controls[control] = event.type == SDL_KEYDOWN ? true : false;
+  controls[control] = (event.type == SDL_KEYDOWN);
 }
 
 void
@@ -519,37 +580,25 @@ JoystickKeyboardController::get_joystick_options_menu()
   return joystick_options_menu;
 }
 
-bool
-JoystickKeyboardController::check_cheatcode(const std::string& cheatcode)
-{
-  if(cheatcode.size() > sizeof(last_keys)) {
-#ifdef DEBUG
-    std::cerr << "Cheat Code too long.\n";
-#endif
-    return false;
-  }
-
-  for(size_t i = 0; i < cheatcode.size(); ++i) {
-    if(last_keys[sizeof(last_keys)-1 - i] != cheatcode[cheatcode.size()-1-i])
-      return false;
-  }
-  return true;
-}
-
 //----------------------------------------------------------------------------
 
 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"));
+    if (config->console_enabled) {
+      add_controlfield(Controller::CONSOLE, _("Console"));
+    }
     add_hl();
     add_back(_("Back"));
     update();
@@ -617,6 +666,14 @@ 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)));
+  if (config->console_enabled) {
+    get_item_by_id((int) Controller::CONSOLE).change_input(get_key_name(
+      controller->reversemap_key(Controller::CONSOLE)));
+  }
 }
 
 //---------------------------------------------------------------------------
@@ -625,12 +682,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"));
   }
@@ -647,7 +706,7 @@ JoystickKeyboardController::JoystickMenu::get_button_name(int button)
 {
   if(button < 0)
     return _("None");
-    
+
   std::ostringstream name;
   name << "Button " << button;
   return name.str();
@@ -674,5 +733,8 @@ 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)));
 }
-