input.h

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 
00036 #ifndef __INPUT_HEADER__
00037 #define __INPUT_HEADER__
00038 
00039 #include "utils.h"
00040 #include "defs.h"
00041 
00043 namespace hoa_input {
00044 
00046 extern GameInput* InputManager;
00047 
00049 extern bool INPUT_DEBUG;
00050 
00051 /* \brief Handles pausing/unpausing for battle mode (special case)
00052 * \note Placed here so it's visible from QuitMode as well
00053 */
00054 void TEMP_HandlePause();
00055 
00056 
00058 namespace private_input {
00059 
00061 const int16 JOYAXIS_THRESHOLD = 8192;
00062 
00069 class KeyState {
00070 public:
00075   SDLKey up;
00076   SDLKey down;
00077   SDLKey left;
00078   SDLKey right;
00079   SDLKey confirm;
00080   SDLKey cancel;
00081   SDLKey menu;
00082   SDLKey swap;
00083   SDLKey left_select;
00084   SDLKey right_select;
00085   SDLKey pause;
00087 }; // class KeyState
00088 
00099 class JoystickState {
00100 public:
00102   SDL_Joystick *js;
00103 
00105   int32 joy_index;
00106 
00108 
00112 
00113   uint8 confirm;
00114   uint8 cancel;
00115   uint8 menu;
00116   uint8 swap;
00117   uint8 left_select;
00118   uint8 right_select;
00119   uint8 pause;
00120   uint8 quit;
00122 
00124 
00128 
00129   int16 x_previous_peak;
00130   int16 y_previous_peak;
00132 
00134 
00138 
00139   int16 x_current_peak;
00140   int16 y_current_peak;
00142 
00143   friend class GameInput;
00144 }; // class JoystickState
00145 
00146 } // namespace private_input
00147 
00202 class GameInput : public hoa_utils::Singleton<GameInput> {
00203   friend class hoa_utils::Singleton<GameInput>;
00204 
00205 private:
00206   GameInput();
00207 
00209   private_input::KeyState _key;
00210 
00212   private_input::JoystickState _joystick;
00213 
00214 
00216   bool _any_key_press;
00217 
00219   bool _any_key_release;
00220 
00225   bool _up_state;
00226   bool _down_state;
00227   bool _left_state;
00228   bool _right_state;
00229   bool _confirm_state;
00230   bool _cancel_state;
00231   bool _menu_state;
00232   bool _swap_state;
00233   bool _left_select_state;
00234   bool _right_select_state;
00236 
00241   bool _up_press;
00242   bool _down_press;
00243   bool _left_press;
00244   bool _right_press;
00245   bool _confirm_press;
00246   bool _cancel_press;
00247   bool _menu_press;
00248   bool _swap_press;
00249   bool _left_select_press;
00250   bool _right_select_press;
00252 
00257   bool _up_release;
00258   bool _down_release;
00259   bool _left_release;
00260   bool _right_release;
00261   bool _confirm_release;
00262   bool _cancel_release;
00263   bool _menu_release;
00264   bool _swap_release;
00265   bool _left_select_release;
00266   bool _right_select_release;
00268 
00273   bool _joyaxis_x_first;
00274   bool _joyaxis_y_first;
00276 
00280   void _KeyEventHandler(SDL_KeyboardEvent& key_event);
00284   void _JoystickEventHandler(SDL_Event& js_event);
00285 
00290   void _SetNewKey(SDLKey & old_key, SDLKey new_key);
00291 
00296   void _SetNewJoyButton(uint8 & old_button, uint8 new_button);
00297 public:
00298   ~GameInput();
00299 
00300   bool SingletonInitialize ();
00301 
00305   bool RestoreDefaultKeys();
00306 
00310   bool RestoreDefaultJoyButtons();
00311 
00315   bool AnyKeyPress();
00316 
00320   bool AnyKeyRelease();
00321 
00322   void TogglePause();
00332   void EventHandler();
00333 
00338   bool UpState() 
00339     { return _up_state; }
00340   bool DownState() 
00341     { return _down_state; }
00342   bool LeftState() 
00343     { return _left_state; }
00344   bool RightState() 
00345     { return _right_state; }
00346   bool ConfirmState() 
00347     { return _confirm_state; }
00348   bool CancelState() 
00349     { return _cancel_state; }
00350   bool MenuState() 
00351     { return _menu_state; }
00352   bool SwapState() 
00353     { return _swap_state; }
00354   bool LeftSelectState() 
00355     { return _left_select_state; }
00356   bool RightSelectState() 
00357     { return _right_select_state; }
00359 
00364   bool UpPress() 
00365     { return _up_press; }
00366   bool DownPress() 
00367     { return _down_press; }
00368   bool LeftPress() 
00369     { return _left_press; }
00370   bool RightPress() 
00371     { return _right_press; }
00372   bool ConfirmPress() 
00373     { return _confirm_press; }
00374   bool CancelPress() 
00375     { return _cancel_press; }
00376   bool MenuPress() 
00377     { return _menu_press; }
00378   bool SwapPress() 
00379     { return _swap_press; }
00380   bool LeftSelectPress() 
00381     { return _left_select_press; }
00382   bool RightSelectPress() 
00383     { return _right_select_press; }
00385 
00390   bool UpRelease() 
00391     { return _up_release; }
00392   bool DownRelease() 
00393     { return _down_release; }
00394   bool LeftRelease() 
00395     { return _left_release; }
00396   bool RightRelease() 
00397     { return _right_release; }
00398   bool ConfirmRelease() 
00399     { return _confirm_release; }
00400   bool CancelRelease() 
00401     { return _cancel_release; }
00402   bool MenuRelease() 
00403     { return _menu_release; }
00404   bool SwapRelease() 
00405     { return _swap_release; }
00406   bool LeftSelectRelease() 
00407     { return _left_select_release; }
00408   bool RightSelectRelease() 
00409     { return _right_select_release; }
00411 
00416   std::string GetUpKeyName() const { return SDL_GetKeyName(_key.up); }
00417   std::string GetDownKeyName() const { return SDL_GetKeyName(_key.down); }
00418   std::string GetLeftKeyName() const { return SDL_GetKeyName(_key.left); }
00419   std::string GetRightKeyName() const { return SDL_GetKeyName(_key.right); }
00420   std::string GetConfirmKeyName() const { return SDL_GetKeyName(_key.confirm); }
00421   std::string GetCancelKeyName() const { return SDL_GetKeyName(_key.cancel); }
00422   std::string GetMenuKeyName() const { return SDL_GetKeyName(_key.menu); }
00423   std::string GetSwapKeyName() const { return SDL_GetKeyName(_key.swap); }
00424   std::string GetLeftSelectKeyName() const { return SDL_GetKeyName(_key.left_select); }
00425   std::string GetRightSelectKeyName() const { return SDL_GetKeyName(_key.right_select); }
00426   std::string GetPauseKeyName() const { return SDL_GetKeyName(_key.pause); }
00428 
00433   int32 GetConfirmJoy() const { return _joystick.confirm; }
00434   int32 GetCancelJoy() const { return _joystick.cancel; }
00435   int32 GetMenuJoy() const { return _joystick.menu; }
00436   int32 GetSwapJoy() const { return _joystick.swap; }
00437   int32 GetLeftSelectJoy() const { return _joystick.left_select; }
00438   int32 GetRightSelectJoy() const { return _joystick.right_select; }
00439   int32 GetPauseJoy() const { return _joystick.pause; }
00441 
00446   void SetUpKey(const SDLKey & key) { _SetNewKey(_key.up, key); }
00447   void SetDownKey(const SDLKey & key) { _SetNewKey(_key.down, key); }
00448   void SetLeftKey(const SDLKey & key) { _SetNewKey(_key.left, key); }
00449   void SetRightKey(const SDLKey & key) { _SetNewKey(_key.right, key); }
00450   void SetConfirmKey(const SDLKey & key) { _SetNewKey(_key.confirm, key); }
00451   void SetCancelKey(const SDLKey & key) { _SetNewKey(_key.cancel, key); }
00452   void SetMenuKey(const SDLKey & key) { _SetNewKey(_key.menu, key); }
00453   void SetSwapKey(const SDLKey & key) { _SetNewKey(_key.swap, key); }
00454   void SetLeftSelectKey(const SDLKey & key) { _SetNewKey(_key.left_select, key); }
00455   void SetRightSelectKey(const SDLKey & key) { _SetNewKey(_key.right_select, key); }
00456   void SetPauseKey(const SDLKey & key) { _SetNewKey(_key.pause, key); }
00458 
00463   void SetConfirmJoy(uint8 button) { _SetNewJoyButton(_joystick.confirm, button); }
00464   void SetCancelJoy(uint8 button) { _SetNewJoyButton(_joystick.cancel, button); }
00465   void SetMenuJoy(uint8 button) { _SetNewJoyButton(_joystick.menu, button); }
00466   void SetSwapJoy(uint8 button) { _SetNewJoyButton(_joystick.swap, button); }
00467   void SetLeftSelectJoy(uint8 button) { _SetNewJoyButton(_joystick.left_select, button); }
00468   void SetRightSelectJoy(uint8 button) { _SetNewJoyButton(_joystick.right_select, button); }
00469   void SetPauseJoy(uint8 button) { _SetNewJoyButton(_joystick.pause, button); }
00471 
00476   int32 GetUpKey() const { return _key.up; }
00477   int32 GetDownKey() const { return _key.down; }
00478   int32 GetLeftKey() const { return _key.left; }
00479   int32 GetRightKey() const { return _key.right; }
00480   int32 GetConfirmKey() const { return _key.confirm; }
00481   int32 GetCancelKey() const { return _key.cancel; }
00482   int32 GetMenuKey() const { return _key.menu; }
00483   int32 GetSwapKey() const { return _key.swap; }
00484   int32 GetLeftSelectKey() const { return _key.left_select; }
00485   int32 GetRightSelectKey() const { return _key.right_select; }
00486   int32 GetPauseKey() const { return _key.pause; }
00488 
00489 }; // class GameInput
00490 
00491 } // namespace hoa_input
00492 
00493 #endif

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