test/controllermap.c
changeset 10745 7461fcef6ae2
parent 10737 3406a0f8b041
child 10747 da1f4eeac65a
equal deleted inserted replaced
10744:f42f107a6035 10745:7461fcef6ae2
   195 }
   195 }
   196 
   196 
   197 static void
   197 static void
   198 SetCurrentBinding(int iBinding)
   198 SetCurrentBinding(int iBinding)
   199 {
   199 {
       
   200     int iIndex;
   200     SDL_GameControllerExtendedBind *pBinding;
   201     SDL_GameControllerExtendedBind *pBinding;
   201 
   202 
   202     if (iBinding < 0) {
   203     if (iBinding < 0) {
   203         return;
   204         return;
   204     }
   205     }
   211     s_iCurrentBinding = iBinding;
   212     s_iCurrentBinding = iBinding;
   212 
   213 
   213     pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
   214     pBinding = &s_arrBindings[s_arrBindingOrder[s_iCurrentBinding]];
   214     SDL_zerop(pBinding);
   215     SDL_zerop(pBinding);
   215 
   216 
   216     SDL_memset(s_arrAxisState, 0, s_nNumAxes*sizeof(*s_arrAxisState));
   217     for (iIndex = 0; iIndex < s_nNumAxes; ++iIndex) {
       
   218         s_arrAxisState[iIndex].m_nFarthestValue = s_arrAxisState[iIndex].m_nStartingValue;
       
   219     }
   217 
   220 
   218     s_unPendingAdvanceTime = 0;
   221     s_unPendingAdvanceTime = 0;
       
   222 }
       
   223 
       
   224 static SDL_bool
       
   225 BBindingContainsBinding(const SDL_GameControllerExtendedBind *pBindingA, const SDL_GameControllerExtendedBind *pBindingB)
       
   226 {
       
   227     if (pBindingA->bindType != pBindingB->bindType)
       
   228     {
       
   229         return SDL_FALSE;
       
   230     }
       
   231     switch (pBindingA->bindType)
       
   232     {
       
   233     case SDL_CONTROLLER_BINDTYPE_AXIS:
       
   234         if (pBindingA->value.axis.axis != pBindingB->value.axis.axis) {
       
   235             return SDL_FALSE;
       
   236         }
       
   237         {
       
   238             int minA = SDL_min(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
       
   239             int maxA = SDL_max(pBindingA->value.axis.axis_min, pBindingA->value.axis.axis_max);
       
   240             int minB = SDL_min(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
       
   241             int maxB = SDL_max(pBindingB->value.axis.axis_min, pBindingB->value.axis.axis_max);
       
   242             return (minA <= minB && maxA >= maxB);
       
   243         }
       
   244         /* Not reached */
       
   245     default:
       
   246         return SDL_memcmp(pBindingA, pBindingB, sizeof(*pBindingA)) == 0;
       
   247     }
   219 }
   248 }
   220 
   249 
   221 static void
   250 static void
   222 ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
   251 ConfigureBinding(const SDL_GameControllerExtendedBind *pBinding)
   223 {
   252 {
   225     int iIndex;
   254     int iIndex;
   226     int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
   255     int iCurrentElement = s_arrBindingOrder[s_iCurrentBinding];
   227 
   256 
   228     /* Do we already have this binding? */
   257     /* Do we already have this binding? */
   229     for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   258     for (iIndex = 0; iIndex < SDL_arraysize(s_arrBindings); ++iIndex) {
   230         if (SDL_memcmp(pBinding, &s_arrBindings[iIndex], sizeof(*pBinding)) == 0) {
   259         pCurrent = &s_arrBindings[iIndex];
       
   260         if (BBindingContainsBinding(pCurrent, pBinding)) {
   231             if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
   261             if (iIndex == SDL_CONTROLLER_BUTTON_A && iCurrentElement != SDL_CONTROLLER_BUTTON_B) {
   232                 /* Skip to the next binding */
   262                 /* Skip to the next binding */
   233                 SetCurrentBinding(s_iCurrentBinding + 1);
   263                 SetCurrentBinding(s_iCurrentBinding + 1);
   234                 return;
   264                 return;
   235             }
   265             }
   261             return;
   291             return;
   262         }
   292         }
   263 
   293 
   264         bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
   294         bNativeAxis = (iCurrentElement >= SDL_CONTROLLER_BUTTON_MAX);
   265         bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
   295         bCurrentAxis = (pCurrent->bindType == SDL_CONTROLLER_BINDTYPE_AXIS);
   266         if (bNativeAxis == bCurrentAxis) {
   296         if (bNativeAxis == bCurrentAxis &&
       
   297             (pBinding->bindType != SDL_CONTROLLER_BINDTYPE_AXIS ||
       
   298              pBinding->value.axis.axis != pCurrent->value.axis.axis)) {
   267             /* We already have a binding of the type we want, ignore the new one */
   299             /* We already have a binding of the type we want, ignore the new one */
   268             return;
   300             return;
   269         }
   301         }
   270     }
   302     }
   271 
   303 
   410                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
   442                     nCurrentDistance = SDL_abs(nValue - pAxisState->m_nStartingValue);
   411                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
   443                     nFarthestDistance = SDL_abs(pAxisState->m_nFarthestValue - pAxisState->m_nStartingValue);
   412                     if (nCurrentDistance > nFarthestDistance) {
   444                     if (nCurrentDistance > nFarthestDistance) {
   413                         pAxisState->m_nFarthestValue = nValue;
   445                         pAxisState->m_nFarthestValue = nValue;
   414                     }
   446                     }
   415                     if (nCurrentDistance <= 8000 && nFarthestDistance >= 20000) {
   447                     if (nFarthestDistance >= 20000 && nCurrentDistance <= 10000) {
   416                         /* We've gone out and back, let's bind this axis */
   448                         /* We've gone out far enough and started to come back, let's bind this axis */
   417                         SDL_GameControllerExtendedBind binding;
   449                         SDL_GameControllerExtendedBind binding;
   418                         SDL_zero(binding);
   450                         SDL_zero(binding);
   419                         binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   451                         binding.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
   420                         binding.value.axis.axis = event.jaxis.axis;
   452                         binding.value.axis.axis = event.jaxis.axis;
   421                         binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);
   453                         binding.value.axis.axis_min = StandardizeAxisValue(pAxisState->m_nStartingValue);