- Main Page
- Classes
- Files

- File List
- File Members

# C:/Users/Public/AGS Final Folder/BFLETCHER D4010283/Source/Game/Source/InputHandler.cpp

Go to the documentation of this file.

    00001 #include "InputHandler.h"
    00002
    00003 #include "EventManager.h"
    00004 #include "InputEvents.h"
    00005 #include "ApplicationEvents.h"
    00006
    00007 #include "GameException.h"
    00008
    00009 // #ifdef _GP2X
    00010 // #include <linux/ioctl.h>
    00011 // #include <unistd.h>
    00012 // #include <sys/types.h>
    00013 // #include <sys/stat.h>
    00014 // #include <fcntl.h>
    00015 // #endif
    00016
    00017 #include "DebugUtil.h"
    00018
    00019 template<> InputHandler* TSingleton<InputHandler>::m_pInstance = NULL;
    00020
    00021 InputHandler::InputHandler()
    00022 #ifdef _GP2X
    00023 : m_pJoystick(0)
    00024 #endif
    00025 {
    00026         for(int i = 0; i < eNumInputKeys; i++)
    00027                 m_keyArray[i] = false;
    00028 }
    00029
    00030 InputHandler::~InputHandler()
    00031 {
    00032
    00033 }
    00034
    00035 void InputHandler::Update()
    00036 {
    00037 // #ifdef _GP2X
    00038 //      int fd = open("/dev/GPIO", O_RDWR | O_NDELAY);
    00039 //      if(fd < 0)
    00040 //      {
    00041 //              DebugUtil::outputDebugText("GPIO Open failed!\n");
    00042 //              return; //Throw exception
    00043 //      }
    00044 //
    00045 //      DebugUtil::outputDebugText("GPIO Open succeeded!\n");
    00046 //
    00047 //      unsigned long buff = 0;
    00048 //      if (read(fd, &buff, 4) != 4)
    00049 //      {
    00050 //              DebugUtil::outputDebugText("GPIO Read failed!\n");
    00051 //              return; //throw exception
    00052 //      }
    00053 //
    00054 //      DebugUtil::outputDebugText("GPIO Read succeeded!\n");
    00055 //
    00056 //      close(fd);
    00057 //
    00058 //      RegisterKeyValue(eKeyUp, (buff & GP2X_VK_UP));
    00059 //      RegisterKeyValue(eKeyDown, (buff & GP2X_VK_DOWN));
    00060 //      RegisterKeyValue(eKeyLeft, (buff & GP2X_VK_LEFT));
    00061 //      RegisterKeyValue(eKeyRight, (buff & GP2X_VK_RIGHT));
    00062 //      RegisterKeyValue(eKeyButtonStart, (buff & GP2X_VK_START));
    00063 //      RegisterKeyValue(eKeyButtonSelect, (buff & GP2X_VK_SELECT));
    00064 //      RegisterKeyValue(eKeyButtonVolumeUp, (buff & GP2X_VK_VOL_UP));
    00065 //      RegisterKeyValue(eKeyButtonVolumeDown, (buff & GP2X_VK_VOL_DOWN));
    00066 //      RegisterKeyValue(eKeyButtonA, (buff & GP2X_VK_FA));
    00067 //      RegisterKeyValue(eKeyButtonB, (buff & GP2X_VK_FB));
    00068 //      RegisterKeyValue(eKeyButtonX, (buff & GP2X_VK_FX));
    00069 //      RegisterKeyValue(eKeyButtonY, (buff & GP2X_VK_FY));
    00070 //      RegisterKeyValue(eKeyButtonL, (buff & GP2X_VK_FL));
    00071 //      RegisterKeyValue(eKeyButtonR, (buff & GP2X_VK_FR));
    00072 //
    00073 // #endif
    00074
    00075
    00076 #ifdef _GP2X
    00077         m_pJoystick = SDL_JoystickOpen(0);
    00078         if(!m_pJoystick)
    00079                 DebugUtil::outputDebugText("ERROR WITH JOYSTICK!!\n");
    00080
    00081         SDL_Event event;
    00082         while(SDL_PollEvent(&event))
    00083         {
    00084                 DebugUtil::outputDebugText("event found in gp2x joystick thing: %d\n", event.jbutton.button);
    00085                 if(event.type == SDL_JOYBUTTONDOWN || event.type == SDL_JOYBUTTONUP)
    00086                 {
    00087                         eInputKey keyType = ConvertGP2XToEnum(event.jbutton.button);
    00088
    00089                         DebugUtil::outputDebugText("Joystick Button ID Found: %d\n", (int)keyType);
    00090                         RegisterKeyValue(keyType, (event.type == SDL_JOYBUTTONDOWN));
    00091                 }
    00092         }
    00093
    00094         return;
    00095 #endif
    00096
    00097 #ifdef _WIN32
    00098         SDL_Event event;
    00099         while (SDL_PollEvent(&event))
    00100         {
    00101                 if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
    00102                 {
    00103                         eInputKey keyType = eKeyUndefined;
    00104                         switch(event.key.keysym.sym)
    00105                         {
    00106                                 case SDLK_UP:
    00107                                         keyType = eKeyUp;
    00108                                         break;
    00109                                 case SDLK_DOWN:
    00110                                         keyType = eKeyDown;
    00111                                         break;
    00112                                 case SDLK_LEFT:
    00113                                         keyType = eKeyLeft;
    00114                                         break;
    00115                                 case SDLK_RIGHT:
    00116                                         keyType = eKeyRight;
    00117                                         break;
    00118                                 case SDLK_x:
    00119                                         keyType = eKeyButtonX;
    00120                                         break;
    00121                                 case SDLK_s:
    00122                                         keyType = eKeyButtonY;
    00123                                         break;
    00124                                 case SDLK_c:
    00125                                         keyType = eKeyButtonA;
    00126                                         break;
    00127                                 case SDLK_d:
    00128                                         keyType = eKeyButtonB;
    00129                                         break;
    00130                                 case SDLK_w:
    00131                                         keyType = eKeyButtonL;
    00132                                         break;
    00133                                 case SDLK_e:
    00134                                         keyType = eKeyButtonR;
    00135                                         break;
    00136                                 case SDLK_m:
    00137                                         keyType = eKeyButtonStart;
    00138                                         break;
    00139                                 case SDLK_n:
    00140                                         keyType = eKeyButtonSelect;
    00141                                         break;
    00142                                 case SDLK_PAGEUP:
    00143                                         keyType = eKeyButtonVolumeUp;
    00144                                         break;
    00145                                 case SDLK_PAGEDOWN:
    00146                                         keyType = eKeyButtonVolumeDown;
    00147                                         break;
    00148                         }
    00149
    00150                         RegisterKeyValue(keyType, (event.type == SDL_KEYDOWN));
    00151                 }
    00152                 if(event.type == SDL_QUIT)
    00153                 {
    00154                         ApplicationQuitEvent* pEvent = new ApplicationQuitEvent();
    00155                         EventManager::getSingleton()->TriggerEvent(pEvent);
    00156                 }
    00157         }
    00158 #endif
    00159 }
    00160
    00161 bool InputHandler::IsKeyDown(const eInputKey &key)
    00162 {
    00163 #ifdef _GP2X
    00164         int button = ConvertEnumToGP2X(key);
    00165
    00166         SDL_JoystickUpdate();
    00167
    00168         if(button)
    00169                 return (bool)SDL_JoystickGetButton(m_pJoystick, button);
    00170         else
    00171                 return 0;
    00172 #else
    00173         if(key > eNumInputKeys)
    00174         {
    00175                 throw GameException(eMinorError, "InputHandler: Key requested in IsKeyDown beyond range: %d", (int)key);
    00176                 return false; //Throw exception
    00177         }
    00178
    00179         return m_keyArray[key];
    00180 #endif
    00181 }
    00182
    00183 void InputHandler::RegisterKeyValue(const eInputKey &key, const bool &bIsDown)
    00184 {
    00185         if(key == eKeyUndefined)
    00186                 return;
    00187
    00188         if(m_keyArray[key] != bIsDown)
    00189         {
    00190                 m_keyArray[key] = bIsDown;
    00191                 ButtonInputEvent* pEvent = new ButtonInputEvent((int)key, bIsDown);
    00192                 DebugUtil::outputDebugText("Button Event sent to event manager: %d || %s\n", (int)key, pEvent->GetEventType().getStr().c_str());
    00193                 EventManager::getSingleton()->TriggerEvent(pEvent);
    00194         }
    00195 }
    00196
    00197 #ifdef _GP2X
    00198 eInputKey InputHandler::ConvertGP2XToEnum(const Uint8 &button)
    00199 {
    00200         eInputKey keyType = eKeyUndefined;
    00201         switch(button)
    00202         {
    00203                 case GP2X_BUTTON_UP:
    00204                         keyType = eKeyUp;
    00205                         break;
    00206                 case GP2X_BUTTON_DOWN:
    00207                         keyType = eKeyDown;
    00208                         break;
    00209                 case GP2X_BUTTON_LEFT:
    00210                         keyType = eKeyLeft;
    00211                         break;
    00212                 case GP2X_BUTTON_UPLEFT:
    00213                         keyType = eKeyUpLeft;
    00214                         break;
    00215                 case GP2X_BUTTON_DOWNLEFT:
    00216                         keyType = eKeyDownLeft;
    00217                         break;
    00218                 case GP2X_BUTTON_RIGHT:
    00219                         keyType = eKeyRight;
    00220                         break;
    00221                 case GP2X_BUTTON_UPRIGHT:
    00222                         keyType = eKeyUpRight;
    00223                         break;
    00224                 case GP2X_BUTTON_DOWNRIGHT:
    00225                         keyType = eKeyDownRight;
    00226                         break;
    00227
    00228                 case GP2X_BUTTON_X:
    00229                         keyType = eKeyButtonX;
    00230                         break;
    00231                 case GP2X_BUTTON_Y:
    00232                         keyType = eKeyButtonY;
    00233                         break;
    00234                 case GP2X_BUTTON_A:
    00235                         keyType = eKeyButtonA;
    00236                         break;
    00237                 case GP2X_BUTTON_B:
    00238                         keyType = eKeyButtonB;
    00239                         break;
    00240
    00241                 case GP2X_BUTTON_L:
    00242                         keyType = eKeyButtonL;
    00243                         break;
    00244                 case GP2X_BUTTON_R:
    00245                         keyType = eKeyButtonR;
    00246                         break;
    00247
    00248                 case GP2X_BUTTON_START:
    00249                         keyType = eKeyButtonStart;
    00250                         break;
    00251                 case GP2X_BUTTON_SELECT:
    00252                         keyType = eKeyButtonSelect;
    00253                         break;
    00254
    00255                 case GP2X_BUTTON_VOLUP:
    00256                         keyType = eKeyButtonVolumeUp;
    00257                         break;
    00258                 case GP2X_BUTTON_VOLDOWN:
    00259                         keyType = eKeyButtonVolumeDown;
    00260                         break;
    00261         }
    00262
    00263         return keyType;
    00264 }
    00265
    00266 int InputHandler::ConvertEnumToGP2X(const eInputKey &keyType)
    00267 {
    00268         int buttonVal = 0;
    00269         switch(keyType)
    00270         {
    00271                 case eKeyUp:
    00272                         buttonVal = GP2X_BUTTON_UP;
    00273                         break;
    00274                 case eKeyUpLeft:
    00275                         buttonVal = GP2X_BUTTON_UPLEFT;
    00276                         break;
    00277                 case eKeyUpRight:
    00278                         buttonVal = GP2X_BUTTON_UPRIGHT;
    00279                         break;
    00280                 case eKeyDown:
    00281                         buttonVal = GP2X_BUTTON_DOWN;
    00282                         break;
    00283                 case eKeyDownLeft:
    00284                         buttonVal = GP2X_BUTTON_DOWNLEFT;
    00285                         break;
    00286                 case eKeyDownRight:
    00287                         buttonVal = GP2X_BUTTON_DOWNRIGHT;
    00288                         break;
    00289                 case eKeyLeft:
    00290                         buttonVal = GP2X_BUTTON_LEFT;
    00291                         break;
    00292                 case eKeyRight:
    00293                         buttonVal = GP2X_BUTTON_RIGHT;
    00294                         break;
    00295
    00296                 case eKeyButtonA:
    00297                         buttonVal = GP2X_BUTTON_A;
    00298                         break;
    00299                 case eKeyButtonB:
    00300                         buttonVal = GP2X_BUTTON_B;
    00301                         break;
    00302                 case eKeyButtonX:
    00303                         buttonVal = GP2X_BUTTON_X;
    00304                         break;
    00305                 case eKeyButtonY:
    00306                         buttonVal = GP2X_BUTTON_Y;
    00307                         break;
    00308                 case eKeyButtonL:
    00309                         buttonVal = GP2X_BUTTON_L;
    00310                         break;
    00311                 case eKeyButtonR:
    00312                         buttonVal = GP2X_BUTTON_R;
    00313                         break;
    00314
    00315                 case eKeyButtonStart:
    00316                         buttonVal = GP2X_BUTTON_START;
    00317                         break;
    00318                 case eKeyButtonSelect:
    00319                         buttonVal = GP2X_BUTTON_SELECT;
    00320                         break;
    00321
    00322                 case eKeyButtonVolumeUp:
    00323                         buttonVal = GP2X_BUTTON_VOLUP;
    00324                         break;
    00325                 case eKeyButtonVolumeDown:
    00326                         buttonVal = GP2X_BUTTON_VOLDOWN;
    00327                         break;
    00328         }
    00329
    00330         return buttonVal;
    00331 }
    00332
    00333 #endif