src/interface/keyboard.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  *  Wormux is a convivial mass murder game.
00003  *  Copyright (C) 2001-2004 Lawrence Azzoug.
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 2 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, write to the Free Software
00017  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
00018  ******************************************************************************
00019  * Keyboard managment.
00020  *****************************************************************************/
00021 
00022 #include "keyboard.h"
00023 #include <sstream>
00024 #include <iostream>
00025 #include "cursor.h"
00026 #include "game_msg.h"
00027 #include "interface.h"
00028 #include "../include/action.h"
00029 #include "../include/app.h"
00030 #include "../game/config.h"
00031 #include "../game/game.h"
00032 #include "../game/game_loop.h"
00033 #include "../game/game_mode.h"
00034 #include "../game/time.h"
00035 #include "../graphic/video.h"
00036 #include "../include/action_handler.h"
00037 #include "../include/constant.h"
00038 #include "../map/camera.h"
00039 #include "../team/macro.h"
00040 #include "../character/move.h"
00041 #include "../tool/i18n.h"
00042 #include "../tool/math_tools.h"
00043 #include "../sound/jukebox.h"
00044 #include "../map/camera.h"
00045 #include "../weapon/weapon.h"
00046 #include "../weapon/weapons_list.h"
00047 #include "../network/network.h"
00048 
00049 // Vitesse du definalement au clavier
00050 #define SCROLL_CLAVIER 20 // ms
00051 
00052 Keyboard::Keyboard()
00053 {
00054   //Disable repeated events when a key is kept down
00055   SDL_EnableKeyRepeat(0,0);
00056 }
00057 
00058 void Keyboard::Reset()
00059 {
00060   for (int i = Action::ACTION_FIRST; i != Action::ACTION_LAST; i++)
00061     PressedKeys[i] = false ;
00062 }
00063 
00064 void Keyboard::SetKeyAction(int key, Action::Action_t at)
00065 {
00066   layout[key] = at;
00067 }
00068 
00069 // Get the key associated to an action.
00070 int Keyboard::GetKeyAssociatedToAction(Action::Action_t at)
00071 {
00072   std::map<int, Action::Action_t>::iterator it;
00073   for (it= layout.begin(); it != layout.end(); it++) {
00074     if (it->second == at) {
00075       return it->first;
00076     }
00077   }
00078   return 0;
00079 }
00080 
00081 
00082 void Keyboard::HandleKeyEvent( const SDL_Event *event)
00083 {
00084   //Handle input text for Chat session in Network game
00085   //While player writes, it cannot control the game.
00086   if(GameLoop::GetInstance()->chatsession.CheckInput()){
00087     GameLoop::GetInstance()->chatsession.HandleKey(event);
00088     return;
00089   }
00090 
00091   std::map<int, Action::Action_t>::iterator it = layout.find(event->key.keysym.sym);
00092 
00093   if ( it == layout.end() )
00094     return;
00095 
00096   Action::Action_t action = it->second;
00097 
00098   //We can perform the next actions, only if the player is played localy:
00099   if(ActiveTeam().IsLocal())
00100   {
00101 
00102     if(action <= Action::ACTION_NEXT_CHARACTER)
00103       {
00104         switch (action) {
00105 //           case Action::ACTION_ADD:
00106 //        if (lance_grenade.time < 15)
00107 //          lance_grenade.time ++;
00108 //          break ;
00109 
00110 //           case Action::ACTION_SUBSTRACT:
00111 //      if (lance_grenade.time > 1)
00112 //          lance_grenade.time --;
00113 //        break ;
00114           default:
00115             break ;
00116         }
00117       }
00118 
00119      Key_Event_t event_type;
00120      switch( event->type)
00121        {
00122         case SDL_KEYDOWN:
00123           event_type = KEY_PRESSED;break;
00124         case SDL_KEYUP:
00125           event_type = KEY_RELEASED;break;
00126         default:
00127           return;
00128        }
00129     if(event_type == KEY_PRESSED)
00130       HandleKeyPressed(action);
00131     if(event_type == KEY_RELEASED)
00132       HandleKeyReleased(action);
00133 
00134     if ((ActiveTeam().GetWeapon().override_keys &&
00135         ActiveTeam().GetWeapon().IsActive()) || ActiveTeam().GetWeapon().force_override_keys)
00136       {
00137         ActiveTeam().AccessWeapon().HandleKeyEvent(action, event_type);
00138         return ;
00139       }
00140     ActiveCharacter().HandleKeyEvent( action, event_type);
00141   }
00142   else
00143   {
00144     Key_Event_t event_type;
00145     switch( event->type)
00146     {
00147       case SDL_KEYDOWN: event_type = KEY_PRESSED;break;
00148       case SDL_KEYUP: event_type = KEY_RELEASED;break;
00149       default: return;
00150     }
00151     //Current player is on the network
00152     if(event_type == KEY_RELEASED)
00153       HandleKeyReleased(action);
00154   }
00155 }
00156 
00157 // Handle a pressed key
00158 void Keyboard::HandleKeyPressed (const Action::Action_t &action)
00159 {
00160   PressedKeys[action] = true ;
00161 
00162   if (GameLoop::GetInstance()->ReadState() == GameLoop::PLAYING &&
00163       ActiveTeam().GetWeapon().CanChangeWeapon())
00164     {
00165       int weapon_sort = -1;
00166 
00167       switch(action) {
00168         case Action::ACTION_WEAPONS1:
00169           weapon_sort = 1;
00170           break;
00171 
00172         case Action::ACTION_WEAPONS2:
00173           weapon_sort = 2;
00174           break;
00175 
00176         case Action::ACTION_WEAPONS3:
00177           weapon_sort = 3;
00178           break;
00179 
00180         case Action::ACTION_WEAPONS4:
00181           weapon_sort = 4;
00182           break;
00183 
00184         case Action::ACTION_WEAPONS5:
00185           weapon_sort = 5;
00186           break;
00187 
00188         case Action::ACTION_WEAPONS6:
00189           weapon_sort = 6;
00190           break;
00191 
00192         case Action::ACTION_WEAPONS7:
00193           weapon_sort = 7;
00194           break;
00195 
00196         case Action::ACTION_WEAPONS8:
00197           weapon_sort = 8;
00198           break;
00199 
00200         case Action::ACTION_NEXT_CHARACTER:
00201           if (GameMode::GetInstance()->AllowCharacterSelection()) {
00202             Action * next_character = new Action(Action::ACTION_NEXT_CHARACTER);
00203             next_character->StoreActiveCharacter();
00204             ActiveTeam().NextCharacter();
00205             next_character->StoreActiveCharacter();
00206             ActionHandler::GetInstance()->NewAction(next_character);
00207           }
00208           return ;
00209 
00210         default:
00211           break ;
00212       }
00213 
00214       if ( weapon_sort > 0 )
00215         {
00216           Weapon::Weapon_type weapon;
00217           if (Config::GetInstance()->GetWeaponsList()->GetWeaponBySort(weapon_sort, weapon))
00218             ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_CHANGE_WEAPON, weapon));
00219 
00220           return;
00221         }
00222     }
00223 }
00224 
00225 // Handle a released key
00226 void Keyboard::HandleKeyReleased (const Action::Action_t &action)
00227 {
00228   PressedKeys[action] = false ;
00229 
00230   // We manage here only actions which are active on KEY_RELEASED event.
00231   Interface * interface = Interface::GetInstance();
00232 
00233   switch(action) // Convert to int to avoid a warning
00234   {
00235     case Action::ACTION_QUIT:
00236       Game::GetInstance()->SetEndOfGameStatus( true );
00237       return;
00238 
00239     case Action::ACTION_PAUSE:
00240       ActionHandler::GetInstance()->NewAction(new Action(Action::ACTION_PAUSE));
00241       return;
00242 
00243     case Action::ACTION_FULLSCREEN:
00244       AppWormux::GetInstance()->video.ToggleFullscreen();
00245       return;
00246     case Action::ACTION_CHAT:
00247     if(network.IsConnected())
00248       GameLoop::GetInstance()->chatsession.ShowInput();
00249     return;
00250     case Action::ACTION_CENTER:
00251 
00252       CharacterCursor::GetInstance()->FollowActiveCharacter();
00253       camera.FollowObject (&ActiveCharacter(), true, true, true);
00254       return;
00255 
00256     case Action::ACTION_TOGGLE_INTERFACE:
00257       interface->EnableDisplay (!interface->IsDisplayed());
00258       return;
00259     default:
00260       return;
00261   }
00262 
00263   if( ! ActiveTeam().IsLocal())
00264     return;
00265 
00266   switch(action) {
00267     case Action::ACTION_TOGGLE_WEAPONS_MENUS:
00268       interface->weapons_menu.SwitchDisplay();
00269       return;
00270 
00271     default:
00272       break ;
00273   }
00274 }
00275 
00276 // Refresh keys which are still pressed.
00277 void Keyboard::Refresh()
00278 {
00279   //Treat KEY_REFRESH events:
00280   for (int i = Action::ACTION_FIRST; i < Action::ACTION_LAST; i++)
00281     if(PressedKeys[i])
00282       {
00283         if (ActiveTeam().GetWeapon().override_keys &&
00284             ActiveTeam().GetWeapon().IsActive())
00285           {
00286             ActiveTeam().AccessWeapon().HandleKeyEvent(static_cast<Action::Action_t>(i), KEY_REFRESH);
00287           }
00288         else
00289           {
00290             ActiveCharacter().HandleKeyEvent(static_cast<Action::Action_t>(i),KEY_REFRESH);
00291           }
00292       }
00293 }
00294 
00295 void Keyboard::TestCamera()
00296 {
00297 }
00298 

Generated on Mon Jan 1 13:10:58 2007 for Wormux by  doxygen 1.4.7