input.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00016 #include "input.h"
00017 #include "video.h"
00018 #include "script.h"
00019 #include "script_read.h"
00020 #include "mode_manager.h"
00021 #include "system.h"
00022 #include "quit.h"
00023 #include "pause.h"
00024 #include "battle.h"
00025 
00026 using namespace std;
00027 
00028 using namespace hoa_utils;
00029 using namespace hoa_video;
00030 using namespace hoa_script;
00031 using namespace hoa_mode_manager;
00032 using namespace hoa_system;
00033 using namespace hoa_quit;
00034 using namespace hoa_pause;
00035 using namespace hoa_input::private_input;
00036 
00037 using namespace hoa_battle;
00038 using namespace hoa_battle::private_battle;
00039 
00040 template<> hoa_input::GameInput* Singleton<hoa_input::GameInput>::_singleton_reference = NULL;
00041 
00042 namespace hoa_input {
00043 
00044 GameInput* InputManager = NULL;
00045 bool INPUT_DEBUG = false;
00046 
00047 //To determine if we are in battle mode when pausing
00048 bool in_battle_mode = false;
00049 
00050 void TEMP_HandlePause()
00051 {
00052   if (ModeManager->GetGameType() == MODE_MANAGER_BATTLE_MODE)
00053   {
00054     current_battle->FreezeTimers();
00055     in_battle_mode = true;
00056   }
00057   else if (in_battle_mode)
00058   {
00059     current_battle->UnFreezeTimers();
00060     in_battle_mode = false;
00061   }
00062 }
00063 
00064 // Initializes class members
00065 GameInput::GameInput() {
00066   if (INPUT_DEBUG) cout << "INPUT: GameInput constructor invoked" << endl;
00067   _any_key_press      = false;
00068   _any_key_release    = false;
00069   _up_state             = false;
00070   _up_press             = false;
00071   _up_release           = false;
00072   _down_state           = false;
00073   _down_press           = false;
00074   _down_release         = false;
00075   _left_state           = false;
00076   _left_press           = false;
00077   _left_release         = false;
00078   _right_state          = false;
00079   _right_press          = false;
00080   _right_release        = false;
00081   _confirm_state        = false;
00082   _confirm_press        = false;
00083   _confirm_release      = false;
00084   _cancel_state         = false;
00085   _cancel_press         = false;
00086   _cancel_release       = false;
00087   _menu_state           = false;
00088   _menu_press           = false;
00089   _menu_release         = false;
00090   _swap_state           = false;
00091   _swap_press           = false;
00092   _swap_release         = false;
00093   _right_select_state   = false;
00094   _right_select_press   = false;
00095   _right_select_release = false;
00096   _left_select_state    = false;
00097   _left_select_press    = false;
00098   _left_select_release  = false;
00099 
00100   _joyaxis_x_first      = true;
00101   _joyaxis_y_first      = true;
00102   _joystick.js          = NULL;
00103 }
00104 
00105 
00106 GameInput::~GameInput() {
00107   if (INPUT_DEBUG) cout << "INPUT: GameInput destructor invoked" << endl;
00108 
00109   // If a joystick is open, close it before exiting
00110   if (_joystick.js != NULL) {
00111     SDL_JoystickClose(_joystick.js);
00112   }
00113 }
00114 
00115 // Initialize singleton pointers and key/joystick systems.
00116 bool GameInput::SingletonInitialize() {
00117   // Initialize the SDL joystick subsystem
00118   if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) != 0) {
00119     cerr << "INPUT ERROR: failed to initailize the SDL joystick subsystem" << endl;
00120     return false;
00121   }
00122 
00123   // Loads saved settings to setup the key and joystick configurations
00124   string in_filename = "dat/config/settings.lua";
00125   ReadScriptDescriptor input_map_data;
00126   if (input_map_data.OpenFile(in_filename) == false) {
00127     cerr << "INPUT ERROR: failed to open data file for reading: "
00128          << in_filename << endl;
00129     return false;
00130   }
00131 
00132   input_map_data.OpenTable("key_settings");
00133   _key.up           = static_cast<SDLKey>(input_map_data.ReadInt("up"));
00134   _key.down         = static_cast<SDLKey>(input_map_data.ReadInt("down"));
00135   _key.left         = static_cast<SDLKey>(input_map_data.ReadInt("left"));
00136   _key.right        = static_cast<SDLKey>(input_map_data.ReadInt("right"));
00137   _key.confirm      = static_cast<SDLKey>(input_map_data.ReadInt("confirm"));
00138   _key.cancel       = static_cast<SDLKey>(input_map_data.ReadInt("cancel"));
00139   _key.menu         = static_cast<SDLKey>(input_map_data.ReadInt("menu"));
00140   _key.swap         = static_cast<SDLKey>(input_map_data.ReadInt("swap"));
00141   _key.left_select  = static_cast<SDLKey>(input_map_data.ReadInt("left_select"));
00142   _key.right_select = static_cast<SDLKey>(input_map_data.ReadInt("right_select"));
00143   _key.pause        = static_cast<SDLKey>(input_map_data.ReadInt("pause"));
00144   input_map_data.CloseTable();
00145 
00146   if (input_map_data.IsErrorDetected()) {
00147     cerr << "INPUT ERROR: failure while trying to retrieve key map "
00148       << "information from file: " << in_filename << endl;
00149     cerr << input_map_data.GetErrorMessages() << endl;
00150     return false;
00151   }
00152 
00153   input_map_data.OpenTable("joystick_settings");
00154   _joystick.joy_index    = static_cast<int32>(input_map_data.ReadInt("index"));
00155   _joystick.confirm      = static_cast<uint8>(input_map_data.ReadInt("confirm"));
00156   _joystick.cancel       = static_cast<uint8>(input_map_data.ReadInt("cancel"));
00157   _joystick.menu         = static_cast<uint8>(input_map_data.ReadInt("menu"));
00158   _joystick.swap         = static_cast<uint8>(input_map_data.ReadInt("swap"));
00159   _joystick.left_select  = static_cast<uint8>(input_map_data.ReadInt("left_select"));
00160   _joystick.right_select = static_cast<uint8>(input_map_data.ReadInt("right_select"));
00161   _joystick.pause        = static_cast<uint8>(input_map_data.ReadInt("pause"));
00162   _joystick.quit         = static_cast<uint8>(input_map_data.ReadInt("quit"));
00163   input_map_data.CloseTable();
00164 
00165   if (input_map_data.IsErrorDetected()) {
00166     cerr << "INPUT: an error occured while trying to retrieve joystick mapping information "
00167       << "from file: " << in_filename << endl;
00168     cerr << input_map_data.GetErrorMessages() << endl;
00169     return false;
00170   }
00171   input_map_data.CloseFile();
00172 
00173   // Attempt to initialize and setup the joystick system
00174   if (SDL_NumJoysticks() == 0) { // No joysticks found
00175     SDL_JoystickEventState(SDL_IGNORE);
00176     SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
00177   }
00178   else { // At least one joystick exists
00179     SDL_JoystickEventState(SDL_ENABLE);
00180     // TODO: need to allow user to specify which joystick to open, if multiple exist
00181     _joystick.js = SDL_JoystickOpen(_joystick.joy_index);
00182   }
00183 
00184   return true;
00185 }
00186 
00187 
00188 // Loads the default key settings from the lua file and sets them back
00189 bool GameInput::RestoreDefaultKeys() {
00190   // Load the settings file
00191   string in_filename = "dat/config/settings.lua";
00192   ReadScriptDescriptor settings_file;
00193   if (!settings_file.OpenFile(in_filename)) {
00194     cerr << "INPUT ERROR: failed to open data file for reading: " << in_filename << endl;
00195     return false;
00196   }
00197 
00198   // Load all default keys from the table
00199   settings_file.OpenTable("key_defaults");
00200   _key.up           = static_cast<SDLKey>(settings_file.ReadInt("up"));
00201   _key.down         = static_cast<SDLKey>(settings_file.ReadInt("down"));
00202   _key.left         = static_cast<SDLKey>(settings_file.ReadInt("left"));
00203   _key.right        = static_cast<SDLKey>(settings_file.ReadInt("right"));
00204   _key.confirm      = static_cast<SDLKey>(settings_file.ReadInt("confirm"));
00205   _key.cancel       = static_cast<SDLKey>(settings_file.ReadInt("cancel"));
00206   _key.menu         = static_cast<SDLKey>(settings_file.ReadInt("menu"));
00207   _key.swap         = static_cast<SDLKey>(settings_file.ReadInt("swap"));
00208   _key.left_select  = static_cast<SDLKey>(settings_file.ReadInt("left_select"));
00209   _key.right_select = static_cast<SDLKey>(settings_file.ReadInt("right_select"));
00210   _key.pause        = static_cast<SDLKey>(settings_file.ReadInt("pause"));
00211   settings_file.CloseTable();
00212 
00213   settings_file.CloseFile();
00214 
00215   return true;
00216 }
00217 
00218 
00219 // Loads the default joystick settings from the lua file and sets them back
00220 bool GameInput::RestoreDefaultJoyButtons()
00221 {
00222   // Load the settings file
00223   string in_filename = "dat/config/settings.lua";
00224   ReadScriptDescriptor settings_file;
00225   if (settings_file.OpenFile(in_filename) == false) {
00226     cerr << "INPUT ERROR: failed to open data file for reading: " << in_filename << endl;
00227     return false;
00228   }
00229 
00230   // Load all default buttons from the table
00231   settings_file.OpenTable("joystick_defaults");
00232   _joystick.confirm      = static_cast<uint8>(settings_file.ReadInt("confirm"));
00233   _joystick.cancel       = static_cast<uint8>(settings_file.ReadInt("cancel"));
00234   _joystick.menu         = static_cast<uint8>(settings_file.ReadInt("menu"));
00235   _joystick.swap         = static_cast<uint8>(settings_file.ReadInt("swap"));
00236   _joystick.left_select  = static_cast<uint8>(settings_file.ReadInt("left_select"));
00237   _joystick.right_select = static_cast<uint8>(settings_file.ReadInt("right_select"));
00238   _joystick.pause        = static_cast<uint8>(settings_file.ReadInt("pause"));
00239   settings_file.CloseTable();
00240 
00241   settings_file.CloseFile();
00242 
00243   return true;
00244 }
00245 
00246 
00247 // Checks if any keyboard key or joystick button is pressed
00248 bool GameInput::AnyKeyPress() {
00249   return _any_key_press;
00250 }
00251 
00252 
00253 // Checks if any keyboard key or joystick button is released
00254 bool GameInput::AnyKeyRelease() {
00255   return _any_key_release;
00256 }
00257 
00258 
00259 
00260 void GameInput::TogglePause(){
00261   // If the current game mode is PauseMode, unpause the game
00262   if (ModeManager->GetGameType() == MODE_MANAGER_PAUSE_MODE) {
00263     ModeManager->Pop();
00264     TEMP_HandlePause();
00265   }
00266   // Otherwise, make PauseMode the active game mode
00267   else {
00268     PauseMode *PM = new PauseMode();
00269 
00270     TEMP_HandlePause();
00271     ModeManager->Push(PM);
00272   }
00273 }
00274 
00275 
00276 
00277 // Handles all of the event processing for the game.
00278 void GameInput::EventHandler() {
00279   SDL_Event event; // Holds the game event
00280 
00281   // Reset all of the press and release flags so that they don't get detected twice.
00282   _any_key_press   = false;
00283   _any_key_release = false;
00284 
00285   _up_press             = false;
00286   _up_release           = false;
00287   _down_press           = false;
00288   _down_release         = false;
00289   _left_press           = false;
00290   _left_release         = false;
00291   _right_press          = false;
00292   _right_release        = false;
00293   _confirm_press        = false;
00294   _confirm_release      = false;
00295   _cancel_press         = false;
00296   _cancel_release       = false;
00297   _menu_press           = false;
00298   _menu_release         = false;
00299   _swap_press           = false;
00300   _swap_release         = false;
00301   _right_select_press   = false;
00302   _right_select_release = false;
00303   _left_select_press    = false;
00304   _left_select_release  = false;
00305 
00306   // Loops until there are no remaining events to process
00307   while (SDL_PollEvent(&event)) {
00308     if (event.type == SDL_QUIT) {
00309       // Quit the game without question if the active game mode is BootMode or QuitMode
00310       if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE
00311         || ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00312         SystemManager->ExitGame();
00313       }
00314       // Otherwise, we push QuitMode onto the stack
00315       else {
00316         QuitMode *QM = new QuitMode();
00317         TEMP_HandlePause();
00318         ModeManager->Push(QM);
00319       }
00320       return;
00321     }
00322     // Check if the window was iconified/minimized or restored
00323     else if (event.type == SDL_ACTIVEEVENT) {
00324       // TEMP: pausing the game on a context switch between another application proved to
00325       // be rather annoying. The code which did this is commented out below. I think it would
00326       // be better if instead the application yielded for a certain amount of time when the
00327       // application looses context.
00328       
00329 //      if (event.active.state & SDL_APPACTIVE) {
00330 //        if (event.active.gain == 0) { // Window was iconified/minimized
00331 //          // Check if the game is in pause mode. Otherwise the player might put pause on,
00332 //          // minimize the window and then the pause is off.
00333 //          if (ModeManager->GetGameType() != MODE_MANAGER_PAUSE_MODE) {
00334 //            TogglePause();
00335 //          }
00336 //        }
00337 //        else if (ModeManager->GetGameType() == MODE_MANAGER_PAUSE_MODE) { // Window was restored
00338 //          TogglePause();
00339 //        }
00340 //      }
00341 //      else if (event.active.state & SDL_APPINPUTFOCUS) {
00342 //        if (event.active.gain == 0) { // Window lost keyboard focus (another application was made active)
00343 //          // Check if the game is in pause mode. Otherwise the player might put pause on,
00344 //          // minimize the window and then the pause is off.
00345 //          if (ModeManager->GetGameType() != MODE_MANAGER_PAUSE_MODE) {
00346 //            TogglePause();
00347 //          }
00348 //        }
00349 //        else if (ModeManager->GetGameType() == MODE_MANAGER_PAUSE_MODE) { // Window gain keyboard focus (not sure)
00350 //          TogglePause();
00351 //        }
00352 //      }
00353       break;
00354     }
00355     else if (event.type == SDL_KEYUP || event.type == SDL_KEYDOWN) {
00356       _KeyEventHandler(event.key);
00357       break;
00358     }
00359     else {
00360       _JoystickEventHandler(event);
00361       break;
00362     }
00363   } // while (SDL_PollEvent(&event)
00364 
00365   // Compare the current and previous peak joystick axis values to detect movement events
00366   if (_joystick.js != NULL) {
00367     // ******************************* X-Axis Movment *************************************
00368     // Check for a x-axis boundary change from left to center or right
00369     if ((_joystick.x_previous_peak <= -JOYAXIS_THRESHOLD) &&
00370         (_joystick.x_current_peak > -JOYAXIS_THRESHOLD)) {
00371       _left_state = false;
00372       _left_release = true;
00373       // Check for a right x-axis boundary change
00374       if (_joystick.x_current_peak >= JOYAXIS_THRESHOLD) {
00375         _right_state = true;
00376         _right_press = true;
00377       }
00378     }
00379     // Check for a x-axis boundary change from right to center or left
00380     else if ((_joystick.x_previous_peak >= JOYAXIS_THRESHOLD) &&
00381              (_joystick.x_current_peak < JOYAXIS_THRESHOLD)) {
00382       _right_state = false;
00383       _right_release = true;
00384       // Check for a left x-axis boundary change
00385       if (_joystick.x_current_peak <= -JOYAXIS_THRESHOLD) {
00386         _left_state = true;
00387         _left_press = true;
00388       }
00389     }
00390     // Check for a x-axis boundary change from center to left
00391     else if ((_joystick.x_current_peak <= -JOYAXIS_THRESHOLD) &&
00392              (_joystick.x_previous_peak > -JOYAXIS_THRESHOLD) &&
00393              (_joystick.x_previous_peak < JOYAXIS_THRESHOLD)) {
00394       _left_state = true;
00395       _left_press = true;
00396     }
00397     // Check for a x-axis boundary change from center to right
00398     else if ((_joystick.x_current_peak >= JOYAXIS_THRESHOLD) &&
00399              (_joystick.x_previous_peak > -JOYAXIS_THRESHOLD) &&
00400              (_joystick.x_previous_peak > JOYAXIS_THRESHOLD)) {
00401       _right_state = true;
00402       _right_press = true;
00403     }
00404 
00405     // ******************************* Y-Axis Movment *************************************
00406     // Check for a y-axis boundary change from up to center or down
00407     if ((_joystick.y_previous_peak <= -JOYAXIS_THRESHOLD) &&
00408         (_joystick.y_current_peak > -JOYAXIS_THRESHOLD)) {
00409       _up_state = false;
00410       _up_release = true;
00411       // Check for a down y-axis boundary change
00412       if (_joystick.y_current_peak >= JOYAXIS_THRESHOLD) {
00413         _down_state = true;
00414         _down_press = true;
00415       }
00416     }
00417     // Check for a y-axis boundary change from down to center or up
00418     else if ((_joystick.y_previous_peak >= JOYAXIS_THRESHOLD) &&
00419              (_joystick.y_current_peak < JOYAXIS_THRESHOLD)) {
00420       _down_state = false;
00421       _down_release = true;
00422       // Check for an up y-axis boundary change
00423       if (_joystick.y_current_peak <= -JOYAXIS_THRESHOLD) {
00424         _up_state = true;
00425         _up_press = true;
00426       }
00427     }
00428     // Check for a y-axis boundary change from center to up
00429     else if ((_joystick.y_current_peak <= -JOYAXIS_THRESHOLD) &&
00430              (_joystick.y_previous_peak > -JOYAXIS_THRESHOLD) &&
00431              (_joystick.y_previous_peak < JOYAXIS_THRESHOLD)) {
00432       _up_state = true;
00433       _up_press = true;
00434     }
00435     // Check for a x-axis boundary change from center to down
00436     else if ((_joystick.y_current_peak >= JOYAXIS_THRESHOLD) &&
00437              (_joystick.y_previous_peak > -JOYAXIS_THRESHOLD) &&
00438              (_joystick.y_previous_peak < JOYAXIS_THRESHOLD)) {
00439       _down_state = true;
00440       _down_press = true;
00441     }
00442 
00443     // Save previous peak values for the next iteration of event processing
00444     _joystick.x_previous_peak = _joystick.x_current_peak;
00445     _joystick.y_previous_peak = _joystick.y_current_peak;
00446 
00447     // Reset first axis motion detectors for next event processing loop
00448     _joyaxis_x_first = true;
00449     _joyaxis_y_first = true;
00450   } // (_joystick.js != NULL)
00451 } // void GameInput::EventHandler()
00452 
00453 
00454 
00455 // Handles all keyboard events for the game
00456 void GameInput::_KeyEventHandler(SDL_KeyboardEvent& key_event) {
00457   if (key_event.type == SDL_KEYDOWN) { // Key was pressed
00458     
00459     _any_key_press = true;
00460 
00461     if (key_event.keysym.mod & KMOD_CTRL || key_event.keysym.sym == SDLK_LCTRL || key_event.keysym.sym == SDLK_RCTRL) { // CTRL key was held down
00462 
00463       _any_key_press = false; // CTRL isn't "any key"! :)
00464 
00465       if (key_event.keysym.sym == SDLK_a) {
00466         // Toggle the display of advanced video engine information
00467         VideoManager->ToggleAdvancedDisplay();
00468       }
00469       else if (key_event.keysym.sym == SDLK_f) {
00470         // Toggle between full-screen and windowed mode
00471         if (INPUT_DEBUG) cout << "Toggle fullscreen!" << endl;
00472         VideoManager->ToggleFullscreen();
00473         VideoManager->ApplySettings();
00474         return;
00475       }
00476       else if (key_event.keysym.sym == SDLK_q) {
00477         // Quit the game without question if the current game mode is BootMode or QuitMode
00478         if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE
00479           || ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00480           SystemManager->ExitGame();
00481         }
00482         // Otherwise, enter QuitMode
00483         else {
00484           QuitMode *QM = new QuitMode();
00485           TEMP_HandlePause();
00486           ModeManager->Push(QM);
00487         }
00488       }
00489       else if (key_event.keysym.sym == SDLK_r) {
00490         VideoManager->ToggleFPS();
00491         return;
00492       }
00493       else if (key_event.keysym.sym == SDLK_s) {
00494         // Take a screenshot of the current game
00495         VideoManager->MakeScreenshot();
00496         return;
00497       }
00498       else if (key_event.keysym.sym == SDLK_t) {
00499         // Display and cycle through the texture sheets
00500         VideoManager->DEBUG_NextTexSheet();
00501         return;
00502       }
00503       
00504       //return;
00505     } // endif CTRL pressed
00506 
00507     if (key_event.keysym.sym == SDLK_ESCAPE) // Same story as on Ctrl-Q
00508     {
00509       // Quit the game without question if the current game mode is BootMode
00510       if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE) {
00511           SystemManager->ExitGame();
00512       }
00513       else {
00514         // Cancel QuitMode if it is the active game mode
00515         if(ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00516           ModeManager->Pop();
00517           TEMP_HandlePause();
00518         }
00519         // Otherwise, enter QuitMode
00520         else {
00521           QuitMode *QM = new QuitMode();
00522           TEMP_HandlePause();
00523           ModeManager->Push(QM);
00524         }
00525       }
00526     }
00527 
00528     // Note: a switch-case statement won't work here because Key.up is not an
00529     // integer value the compiler will whine and cry about it ;_;
00530     if (key_event.keysym.sym == _key.up) {
00531       _up_state = true;
00532       _up_press = true;
00533       return;
00534     }
00535     else if (key_event.keysym.sym == _key.down) {
00536       _down_state = true;
00537       _down_press = true;
00538       return;
00539     }
00540     else if (key_event.keysym.sym == _key.left) {
00541       _left_state = true;
00542       _left_press = true;
00543       return;
00544     }
00545     else if (key_event.keysym.sym == _key.right) {
00546       _right_state = true;
00547       _right_press = true;
00548       return;
00549     }
00550     else if (key_event.keysym.sym == _key.confirm) {
00551       _confirm_state = true;
00552       _confirm_press = true;
00553       return;
00554     }
00555     else if (key_event.keysym.sym == _key.cancel) {
00556       _cancel_state = true;
00557       _cancel_press = true;
00558       return;
00559     }
00560     else if (key_event.keysym.sym == _key.menu) {
00561       _menu_state = true;
00562       _menu_press = true;
00563       return;
00564     }
00565     else if (key_event.keysym.sym == _key.swap) {
00566       _swap_state = true;
00567       _swap_press = true;
00568       return;
00569     }
00570     else if (key_event.keysym.sym == _key.left_select) {
00571       _left_select_state = true;
00572       _left_select_press = true;
00573       return;
00574     }
00575     else if (key_event.keysym.sym == _key.right_select) {
00576       _right_select_state = true;
00577       _right_select_press = true;
00578       return;
00579     }
00580     else if (key_event.keysym.sym == _key.pause) {
00581       // Don't pause if the current game mode is BootMode or QuitMode
00582       if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE
00583         || ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00584         return;
00585       }
00586       TogglePause();
00587     }
00588   }
00589 
00590   else { // Key was released
00591 
00592     _any_key_press = false;
00593     _any_key_release = true;
00594 
00595     //if (key_event.keysym.mod & KMOD_CTRL) // Don't recognize a key release if ctrl is down
00596     //  return;
00597 
00598     if (key_event.keysym.sym == _key.up) {
00599       _up_state = false;
00600       _up_release = true;
00601       return;
00602     }
00603     else if (key_event.keysym.sym == _key.down) {
00604       _down_state = false;
00605       _down_release = true;
00606       return;
00607     }
00608     else if (key_event.keysym.sym == _key.left) {
00609       _left_state = false;
00610       _left_release = true;
00611       return;
00612     }
00613     else if (key_event.keysym.sym == _key.right) {
00614       _right_state = false;
00615       _right_release = true;
00616       return;
00617     }
00618     else if (key_event.keysym.sym == _key.confirm) {
00619       _confirm_state = false;
00620       _confirm_release = true;
00621       return;
00622     }
00623     else if (key_event.keysym.sym == _key.cancel) {
00624       _cancel_state = false;
00625       _cancel_release = true;
00626       return;
00627     }
00628     else if (key_event.keysym.sym == _key.menu) {
00629       _menu_state = false;
00630       _menu_release = true;
00631       return;
00632     }
00633     else if (key_event.keysym.sym == _key.swap) {
00634       _swap_state = false;
00635       _swap_release = true;
00636       return;
00637     }
00638     else if (key_event.keysym.sym == _key.left_select) {
00639       _left_select_state = false;
00640       _left_select_release = true;
00641       return;
00642     }
00643     else if (key_event.keysym.sym == _key.right_select) {
00644       _right_select_state = false;
00645       _right_select_release = true;
00646       return;
00647     }
00648   }
00649 } // void GameInput::_KeyEventHandler(SDL_KeyboardEvent& key_event)
00650 
00651 
00652 // Handles all joystick events for the game
00653 void GameInput::_JoystickEventHandler(SDL_Event& js_event) {
00654   if (js_event.type == SDL_JOYAXISMOTION) {
00655     if (js_event.jaxis.axis == 0) { // X-axis motion
00656       if (_joyaxis_x_first == true) {
00657         _joystick.x_current_peak = js_event.jaxis.value;
00658         _joyaxis_x_first = false;
00659       }
00660       else if (abs(js_event.jaxis.value) > abs(_joystick.x_current_peak)) {
00661         _joystick.x_current_peak = js_event.jaxis.value;
00662       }
00663     }
00664     else { // Y-axis motion
00665       if (_joyaxis_y_first == true) {
00666         _joystick.y_current_peak = js_event.jaxis.value;
00667         _joyaxis_y_first = false;
00668       }
00669       if (abs(js_event.jaxis.value) > abs(_joystick.y_current_peak)) {
00670         _joystick.y_current_peak = js_event.jaxis.value;
00671       }
00672     }
00673   } // if (js_event.type == SDL_JOYAXISMOTION)
00674   else if (js_event.type == SDL_JOYBUTTONDOWN) {
00675 
00676     _any_key_press = true;
00677 
00678     if (js_event.jbutton.button == _joystick.confirm) {
00679       _confirm_state = true;
00680       _confirm_press = true;
00681       return;
00682     }
00683     else if (js_event.jbutton.button == _joystick.cancel) {
00684       _cancel_state = true;
00685       _cancel_press = true;
00686       return;
00687     }
00688     else if (js_event.jbutton.button == _joystick.menu) {
00689       _menu_state = true;
00690       _menu_press = true;
00691       return;
00692     }
00693     else if (js_event.jbutton.button == _joystick.swap) {
00694       _swap_state = true;
00695       _swap_press = true;
00696       return;
00697     }
00698     else if (js_event.jbutton.button == _joystick.left_select) {
00699       _left_select_state = true;
00700       _left_select_press = true;
00701       return;
00702     }
00703     else if (js_event.jbutton.button == _joystick.right_select) {
00704       _right_select_state = true;
00705       _right_select_press = true;
00706       return;
00707     }
00708     else if (js_event.jbutton.button == _joystick.pause) {
00709       // Don't pause if the current game mode is BootMode or QuitMode
00710       if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE
00711         || ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00712         return;
00713       }
00714       // If the current game mode is PauseMode, unpause the game
00715       else if (ModeManager->GetGameType() == MODE_MANAGER_PAUSE_MODE) {
00716         ModeManager->Pop();
00717         TEMP_HandlePause();
00718       }
00719       // Otherwise, make PauseMode the active game mode
00720       else {
00721         PauseMode *PM = new PauseMode();
00722 
00723         TEMP_HandlePause();
00724         ModeManager->Push(PM);
00725       }
00726       return;
00727     }
00728     else if (js_event.jbutton.button == _joystick.quit) {
00729       // Quit the game without question if the current game mode is BootMode or QuitMode
00730       if (ModeManager->GetGameType() == MODE_MANAGER_BOOT_MODE
00731         || ModeManager->GetGameType() == MODE_MANAGER_QUIT_MODE) {
00732         SystemManager->ExitGame();
00733       }
00734       // Otherwise, enter QuitMode
00735       else {
00736         QuitMode *QM = new QuitMode();
00737         TEMP_HandlePause();
00738         ModeManager->Push(QM);
00739       }
00740       return;
00741     }
00742   } // else if (js_event.type == JOYBUTTONDOWN)
00743   else if (js_event.type == SDL_JOYBUTTONUP) {
00744 
00745     _any_key_press = false;
00746     _any_key_release = true;
00747 
00748     if (js_event.jbutton.button == _joystick.confirm) {
00749       _confirm_state = false;
00750       _confirm_release = true;
00751       return;
00752     }
00753     else if (js_event.jbutton.button == _joystick.cancel) {
00754       _cancel_state = false;
00755       _cancel_release = true;
00756       return;
00757     }
00758     else if (js_event.jbutton.button == _joystick.menu) {
00759       _menu_state = false;
00760       _menu_release = true;
00761       return;
00762     }
00763     else if (js_event.jbutton.button == _joystick.swap) {
00764       _swap_state = false;
00765       _swap_release = true;
00766       return;
00767     }
00768     else if (js_event.jbutton.button == _joystick.left_select) {
00769       _left_select_state = false;
00770       _left_select_release = true;
00771       return;
00772     }
00773     else if (js_event.jbutton.button == _joystick.right_select) {
00774       _right_select_state = false;
00775       _right_select_release = true;
00776       return;
00777     }
00778   } // else if (js_event.type == JOYBUTTONUP)
00779 
00780   // SDL_JOYBALLMOTION and SDL_JOYHATMOTION are ignored for now. Should we process them?
00781 
00782 } // void GameInput::_JoystickEventHandler(SDL_Event& js_event)
00783 
00784 
00785 // Sets a new key over an older one. If the same key is used elsewhere, the older one is removed
00786 void GameInput::_SetNewKey(SDLKey & old_key, SDLKey new_key)
00787 {
00788   if (_key.up == new_key)  // up key used already
00789   {
00790     _key.up = old_key;
00791     old_key = new_key;
00792     return;
00793   }
00794   if (_key.down == new_key)  // down key used already
00795   {
00796     _key.down = old_key;
00797     old_key = new_key;
00798     return;
00799   }
00800   if (_key.left == new_key)  // left key used already
00801   {
00802     _key.left = old_key;
00803     old_key = new_key;
00804     return;
00805   }
00806   if (_key.right == new_key)  // right key used already
00807   {
00808     _key.right = old_key;
00809     old_key = new_key;
00810     return;
00811   }
00812   if (_key.confirm == new_key)  // confirm key used already
00813   {
00814     _key.confirm = old_key;
00815     old_key = new_key;
00816     return;
00817   }
00818   if (_key.cancel == new_key)  // cancel key used already
00819   {
00820     _key.cancel = old_key;
00821     old_key = new_key;
00822     return;
00823   }
00824   if (_key.menu == new_key)  // menu key used already
00825   {
00826     _key.menu = old_key;
00827     old_key = new_key;
00828     return;
00829   }
00830   if (_key.swap == new_key)  // swap key used already
00831   {
00832     _key.swap = old_key;
00833     old_key = new_key;
00834     return;
00835   }
00836   if (_key.left_select == new_key)  // left_select key used already
00837   {
00838     _key.left_select = old_key;
00839     old_key = new_key;
00840     return;
00841   }
00842   if (_key.right_select == new_key)  // right_select key used already
00843   {
00844     _key.right_select = old_key;
00845     old_key = new_key;
00846     return;
00847   }
00848   if (_key.pause == new_key)  // pause key used already
00849   {
00850     _key.pause = old_key;
00851     old_key = new_key;
00852     return;
00853   }
00854 
00855   old_key = new_key; // Otherwise simply overwrite the old value
00856 } // end GameInput::_SetNewKey(SDLKey & old_key, SDLKey new_key)
00857 
00858 
00859 // Sets a new joystick button over an older one. If the same button is used elsewhere, the older one is removed
00860 void GameInput::_SetNewJoyButton(uint8 & old_button, uint8 new_button)
00861 {
00862   if (_joystick.confirm == new_button)  // confirm button used already
00863   {
00864     _joystick.confirm = old_button;
00865     old_button = new_button;
00866     return;
00867   }
00868   if (_joystick.cancel == new_button)  // cancel button used already
00869   {
00870     _joystick.cancel = old_button;
00871     old_button = new_button;
00872     return;
00873   }
00874   if (_joystick.menu == new_button)  // menu button used already
00875   {
00876     _joystick.menu = old_button;
00877     old_button = new_button;
00878     return;
00879   }
00880   if (_joystick.swap == new_button)  // swap button used already
00881   {
00882     _joystick.swap = old_button;
00883     old_button = new_button;
00884     return;
00885   }
00886   if (_joystick.left_select == new_button)  // left_select button used already
00887   {
00888     _joystick.left_select = old_button;
00889     old_button = new_button;
00890     return;
00891   }
00892   if (_joystick.right_select == new_button)  // right_select button used already
00893   {
00894     _joystick.right_select = old_button;
00895     old_button = new_button;
00896     return;
00897   }
00898   if (_joystick.pause == new_button)  // pause button used already
00899   {
00900     _joystick.pause = old_button;
00901     old_button = new_button;
00902     return;
00903   }
00904 
00905   old_button = new_button; // Otherwise simply overwrite the old value
00906 } // end GameInput::_SetNewJoyButton(uint8 & old_button, uint8 new_button)
00907 
00908 
00909 } // namespace hoa_input

Generated on Fri Jul 6 23:11:16 2007 for Hero of Allacrost by  doxygen 1.5.1