test/controllermap.c
changeset 10824 e854440318d2
parent 10752 c9d3ff50dfcf
child 10930 badc1f1411ef
     1.1 --- a/test/controllermap.c	Fri Jan 20 08:13:23 2017 -0800
     1.2 +++ b/test/controllermap.c	Fri Jan 20 16:40:11 2017 -0800
     1.3 @@ -131,6 +131,8 @@
     1.4  
     1.5      } value;
     1.6  
     1.7 +    SDL_bool committed;
     1.8 +
     1.9  } SDL_GameControllerExtendedBind;
    1.10  
    1.11  static SDL_GameControllerExtendedBind s_arrBindings[BINDING_COUNT];
    1.12 @@ -234,6 +236,9 @@
    1.13          if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
    1.14              return SDL_FALSE;
    1.15          }
    1.16 +        if (!pBindingA->committed) {
    1.17 +            return SDL_FALSE;
    1.18 +        }
    1.19          {
    1.20              int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    1.21              int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
    1.22 @@ -275,6 +280,23 @@
    1.23          }
    1.24      }
    1.25  
    1.26 +#ifdef DEBUG_CONTROLLERMAP
    1.27 +    switch ( pBinding->bindType )
    1.28 +    {
    1.29 +    case SDL_CONTROLLER_BINDTYPE_NONE:
    1.30 +            break;
    1.31 +    case SDL_CONTROLLER_BINDTYPE_BUTTON:
    1.32 +            SDL_Log("Configuring button binding for button %d\n", pBinding->value.button);
    1.33 +            break;
    1.34 +    case SDL_CONTROLLER_BINDTYPE_AXIS:
    1.35 +            SDL_Log("Configuring axis binding for axis %d %d/%d committed = %s\n", pBinding->value.axis.axis, pBinding->value.axis.axis_min, pBinding->value.axis.axis_max, pBinding->committed ? "true" : "false");
    1.36 +            break;
    1.37 +    case SDL_CONTROLLER_BINDTYPE_HAT:
    1.38 +            SDL_Log("Configuring hat binding for hat %d %d\n", pBinding->value.hat.hat, pBinding->value.hat.hat_mask);
    1.39 +            break;
    1.40 +    }
    1.41 +#endif /* DEBUG_CONTROLLERMAP */
    1.42 +
    1.43      /* Should the new binding override the existing one? */
    1.44      pCurrent = &s_arrBindings[iCurrentElement];
    1.45      if (pCurrent->bindType != SDL_CONTROLLER_BINDTYPE_NONE) {
    1.46 @@ -286,7 +308,7 @@
    1.47                         iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_LEFT ||
    1.48                         iCurrentElement == SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
    1.49          bCurrentDPad = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_HAT);
    1.50 -        if (bNativeDPad == bCurrentDPad) {
    1.51 +        if (bNativeDPad && bCurrentDPad) {
    1.52              /* We already have a binding of the type we want, ignore the new one */
    1.53              return;
    1.54          }
    1.55 @@ -303,7 +325,11 @@
    1.56  
    1.57      *pCurrent = *pBinding;
    1.58  
    1.59 -    s_unPendingAdvanceTime = SDL_GetTicks();
    1.60 +    if (pBinding->committed) {
    1.61 +        s_unPendingAdvanceTime = SDL_GetTicks();
    1.62 +    } else {
    1.63 +        s_unPendingAdvanceTime = 0;
    1.64 +    }
    1.65  }
    1.66  
    1.67  static SDL_bool
    1.68 @@ -451,15 +477,22 @@
    1.69                      nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
    1.70                      if (nCurrentDistance > nFarthestDistance) {
    1.71                          pAxisState->m_nFarthestValue = nValue;
    1.72 +                        nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
    1.73                      }
    1.74 -                    if (nFarthestDistance >= 16000 && nCurrentDistance <= 10000) {
    1.75 -                        /* We've gone out far enough and started to come back, let's bind this axis */
    1.76 +
    1.77 +#ifdef DEBUG_CONTROLLERMAP
    1.78 +                    SDL_Log("AXIS %d nValue %d nCurrentDistance %d nFarthestDistance %d\n", event.jaxis.axis, nValue, nCurrentDistance, nFarthestDistance);
    1.79 +#endif
    1.80 +                    if (nFarthestDistance >= 16000) {
    1.81 +                        /* If we've gone out far enough and started to come back, let's bind this axis */
    1.82 +                        SDL_bool bCommitBinding = (nCurrentDistance <= 10000) ? SDL_TRUE : SDL_FALSE;
    1.83                          SDL_GameControllerExtendedBind binding;
    1.84                          SDL_zero(binding);
    1.85                          binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
    1.86                          binding.value.axis.axis = event.jaxis.axis;
    1.87                          binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
    1.88                          binding.value.axis.axis_max = StandardizeAxisValue(pAxisState->m_nFarthestValue);
    1.89 +                        binding.committed = bCommitBinding;
    1.90                          ConfigureBinding(&binding);
    1.91                      }
    1.92                  }
    1.93 @@ -468,10 +501,15 @@
    1.94                  if (event.jhat.which == nJoystickID) {
    1.95                      if (event.jhat.value != SDL_HAT_CENTERED) {
    1.96                          SDL_GameControllerExtendedBind binding;
    1.97 +
    1.98 +#ifdef DEBUG_CONTROLLERMAP
    1.99 +                        SDL_Log("HAT %d %d\n", event.jhat.hat, event.jhat.value);
   1.100 +#endif
   1.101                          SDL_zero(binding);
   1.102                          binding.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
   1.103                          binding.value.hat.hat = event.jhat.hat;
   1.104                          binding.value.hat.hat_mask = event.jhat.value;
   1.105 +                        binding.committed = SDL_TRUE;
   1.106                          ConfigureBinding(&binding);
   1.107                      }
   1.108                  }
   1.109 @@ -481,9 +519,14 @@
   1.110              case SDL_JOYBUTTONDOWN:
   1.111                  if (event.jbutton.which == nJoystickID) {
   1.112                      SDL_GameControllerExtendedBind binding;
   1.113 +
   1.114 +#ifdef DEBUG_CONTROLLERMAP
   1.115 +                    SDL_Log("BUTTON %d\n", event.jbutton.button);
   1.116 +#endif
   1.117                      SDL_zero(binding);
   1.118                      binding.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
   1.119                      binding.value.button = event.jbutton.button;
   1.120 +                    binding.committed = SDL_TRUE;
   1.121                      ConfigureBinding(&binding);
   1.122                  }
   1.123                  break;