[SDL2] - New Game Controller API - Need Help Refactoring Code

Hi,

We are switching from old joystick API to new game controller API.
We need help with refactoring below source code to make it much smaller.
Any help would be appreciated, thanks!

Jesse “JeZxLee”

if (UseNewGamepadAPI == true)
{
    if (JoystickSetupProcess == JoySetupNotStarted)
    {
        for (int index = 0; index < 4; index++)
        {
            if (JoystickDisabled[index] == false)
            {
                if ( GamepadUP[index] == SDL_CONTROLLER_BUTTON_DPAD_UP
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_UP) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = UP;
                }
                else if ( GamepadUP[index] == SDL_CONTROLLER_BUTTON_DPAD_DOWN
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = UP;
                }
                else if ( GamepadUP[index] == SDL_CONTROLLER_BUTTON_DPAD_LEFT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = UP;
                }
                else if ( GamepadUP[index] == SDL_CONTROLLER_BUTTON_DPAD_RIGHT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = UP;
                }

                else if ( GamepadDOWN[index] == SDL_CONTROLLER_BUTTON_DPAD_UP
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_UP) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = DOWN;
                }
                else if ( GamepadDOWN[index] == SDL_CONTROLLER_BUTTON_DPAD_DOWN
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = DOWN;
                }
                else if ( GamepadDOWN[index] == SDL_CONTROLLER_BUTTON_DPAD_LEFT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = DOWN;
                }
                else if ( GamepadDOWN[index] == SDL_CONTROLLER_BUTTON_DPAD_RIGHT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1 )
                {
                    JoystickDirectionVertical[JoystickOne+index] = DOWN;
                }

                else if ( GamepadLEFT[index] == SDL_CONTROLLER_BUTTON_DPAD_UP
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_UP) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = LEFT;
                }
                else if ( GamepadLEFT[index] == SDL_CONTROLLER_BUTTON_DPAD_DOWN
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = LEFT;
                }
                else if ( GamepadLEFT[index] == SDL_CONTROLLER_BUTTON_DPAD_LEFT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = LEFT;
                }
                else if ( GamepadLEFT[index] == SDL_CONTROLLER_BUTTON_DPAD_RIGHT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = LEFT;
                }

                else if ( GamepadRIGHT[index] == SDL_CONTROLLER_BUTTON_DPAD_UP
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_UP) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = RIGHT;
                }
                else if ( GamepadRIGHT[index] == SDL_CONTROLLER_BUTTON_DPAD_DOWN
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_DOWN) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = RIGHT;
                }
                else if ( GamepadRIGHT[index] == SDL_CONTROLLER_BUTTON_DPAD_LEFT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_LEFT) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = RIGHT;
                }
                else if ( GamepadRIGHT[index] == SDL_CONTROLLER_BUTTON_DPAD_RIGHT
                && SDL_GameControllerGetButton(GamepadController[index], SDL_CONTROLLER_BUTTON_DPAD_RIGHT) == 1 )
                {
                    JoystickDirectionHorizontal[JoystickOne+index] = RIGHT;
                }

Nevermind…figured it out:

    if (UseNewGamepadAPI == true)
    {
        if (JoystickSetupProcess == JoySetupNotStarted)
        {
            for (int index = 0; index < 4; index++)
            {
                if (JoystickDisabled[index] == false)
                {
                    for ( int indexTwo = SDL_CONTROLLER_BUTTON_DPAD_UP; indexTwo < (SDL_CONTROLLER_BUTTON_DPAD_RIGHT+1); indexTwo++ )
                    {
                        if (  GamepadUP[index] == indexTwo
                        && SDL_GameControllerGetButton( GamepadController[index], (SDL_GameControllerButton)indexTwo ) == 1  )
                        {
                            JoystickDirectionVertical[JoystickOne+index] = UP;
                        }
                        else if (  GamepadDOWN[index] == indexTwo
                        && SDL_GameControllerGetButton( GamepadController[index], (SDL_GameControllerButton)indexTwo ) == 1  )
                        {
                            JoystickDirectionVertical[JoystickOne+index] = DOWN;
                        }
                        else if (  GamepadLEFT[index] == indexTwo
                        && SDL_GameControllerGetButton( GamepadController[index], (SDL_GameControllerButton)indexTwo ) == 1  )
                        {
                            JoystickDirectionHorizontal[JoystickOne+index] = LEFT;
                        }
                        else if (  GamepadRIGHT[index] == indexTwo
                        && SDL_GameControllerGetButton( GamepadController[index], (SDL_GameControllerButton)indexTwo ) == 1  )
                        {
                            JoystickDirectionHorizontal[JoystickOne+index] = RIGHT;
                        }
                     }