test/controllermap.c
changeset 10745 7461fcef6ae2
parent 10737 3406a0f8b041
child 10747 da1f4eeac65a
     1.1 --- a/test/controllermap.c	Tue Jan 03 00:44:05 2017 -0800
     1.2 +++ b/test/controllermap.c	Tue Jan 03 23:39:28 2017 -0800
     1.3 @@ -197,6 +197,7 @@
     1.4  static void
     1.5  SetCurrentBinding(int iBinding)
     1.6  {
     1.7 +    int iIndex;
     1.8      SDL_GameControllerExtendedBind *pBinding;
     1.9  
    1.10      if (iBinding < 0) {
    1.11 @@ -213,11 +214,39 @@
    1.12      pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
    1.13      SDL_zerop(pBinding);
    1.14  
    1.15 -    SDL_memset(s_arrAxisState, 0, s_nNumAxes*sizeof(*s_arrAxisState));
    1.16 +    for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
    1.17 +        s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
    1.18 +    }
    1.19  
    1.20      s_unPendingAdvanceTime = 0;
    1.21  }
    1.22  
    1.23 +static SDL_bool
    1.24 +BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
    1.25 +{
    1.26 +    if (pBindingA->bindType != pBindingB->bindType)
    1.27 +    {
    1.28 +        return SDL_FALSE;
    1.29 +    }
    1.30 +    switch (pBindingA->bindType)
    1.31 +    {
    1.32 +    case SDL_CONTROLLER_BINDTYPE_AXIS:
    1.33 +        if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
    1.34 +            return SDL_FALSE;
    1.35 +        }
    1.36 +        {
    1.37 +            int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    1.38 +            int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    1.39 +            int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
    1.40 +            int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
    1.41 +            return (minA <= minB && maxA >= maxB);
    1.42 +        }
    1.43 +        /* Not reached */
    1.44 +    default:
    1.45 +        return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
    1.46 +    }
    1.47 +}
    1.48 +
    1.49  static void
    1.50  ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
    1.51  {
    1.52 @@ -227,7 +256,8 @@
    1.53  
    1.54      /* Do we already have this binding? */
    1.55      for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
    1.56 -        if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
    1.57 +        pCurrent = &s_arrBindings[iIndex];
    1.58 +        if (BBindingContainsBinding(pCurrent, pBinding)) {
    1.59              if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
    1.60                  /* Skip to the next binding */
    1.61                  SetCurrentBinding(s_iCurrentBinding + 1);
    1.62 @@ -263,7 +293,9 @@
    1.63  
    1.64          bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
    1.65          bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
    1.66 -        if (bNativeAxis == bCurrentAxis) {
    1.67 +        if (bNativeAxis == bCurrentAxis &&
    1.68 +            (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
    1.69 +             pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
    1.70              /* We already have a binding of the type we want, ignore the new one */
    1.71              return;
    1.72          }
    1.73 @@ -412,8 +444,8 @@
    1.74                      if (nCurrentDistance > nFarthestDistance) {
    1.75                          pAxisState->m_nFarthestValue = nValue;
    1.76                      }
    1.77 -                    if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
    1.78 -                        /* We've gone out and back, let's bind this axis */
    1.79 +                    if (nFarthestDistance >= 20000 && nCurrentDistance <= 10000) {
    1.80 +                        /* We've gone out far enough and started to come back, let's bind this axis */
    1.81                          SDL_GameControllerExtendedBind binding;
    1.82                          SDL_zero(binding);
    1.83                          binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;