Revision: 20397
          
http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20397
Author:   campbellbarton
Date:     2009-05-25 08:24:23 +0200 (Mon, 25 May 2009)

Log Message:
-----------
BGE Joystick Hat Bugfix
bug reported by blenderage on blenderartist (found other bugs too).

- "All Hat Events" didnt work.
- Multiple hats didnt work
- use a menu with direction names rather then have the user guess. disallow 
zero as a direction.
- Allow up to 4 hats (was 2).
- Python api was clamping the axis to 2, maximum is currently JOYAXIS_MAX - 16
- New python attributes hatValues and hatSingle, match axis functions.
- Use SDL Axis events to fill in the axis and hat array rather then filling in 
every axis with SDL_JoystickGetAxis for each axis event.

Modified Paths:
--------------
    trunk/blender/source/blender/src/buttons_logic.c
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h
    trunk/blender/source/gameengine/PyDoc/GameTypes.py

Modified: trunk/blender/source/blender/src/buttons_logic.c
===================================================================
--- trunk/blender/source/blender/src/buttons_logic.c    2009-05-25 03:28:35 UTC 
(rev 20396)
+++ trunk/blender/source/blender/src/buttons_logic.c    2009-05-25 06:24:23 UTC 
(rev 20397)
@@ -1534,13 +1534,14 @@
                        else if (joy->type == SENS_JOY_HAT)
                        {
                                uiDefButI(block, NUM, 1, "Number:", xco+10, 
yco-68, 0.46 * (width-20), 19,
-                               &joy->hat, 1, 2.0, 100, 0,
+                               &joy->hat, 1, 4.0, 100, 0,
                                "Specify which hat to use");
                                
                                if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
-                                       uiDefButI(block, NUM, 1, "Direction:", 
xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
-                                       &joy->hatf, 0, 12, 100, 0,
-                                       "Specify hat direction");
+                                       str = 
"Direction%t|Up%x1|Down%x4|Left%x8|Right%x2|%l|Up/Right%x3|Down/Left%x12|Up/Left%x9|Down/Right%x6";
 
+                                       uiDefButI(block, MENU, B_NOP, str, 
xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
+                                       &joy->hatf, 2.0, 31, 0, 0,
+                                       "The direction of the hat, use 'All 
Events' to recieve events on any direction");
                                }
                        }
                        else { /* (joy->type == SENS_JOY_AXIS_SINGLE)*/
@@ -2752,12 +2753,12 @@
                        uiEmboss((float)xco, (float)yco-ysize, 
(float)xco+width, (float)yco, 1); 
                        uiDefIDPoinBut(block, test_obpoin_but, ID_OB, 1, "OB:", 
        xco+40, yco-44, (width-80), 19, &(parAct->ob), "Set this object as 
parent"); 
                        uiBlockBeginAlign(block);
-                       uiDefButBitI(block, TOGN, ACT_PARENT_COMPOUND, B_REDR,
+                       uiDefButBitS(block, TOGN, ACT_PARENT_COMPOUND, B_REDR,
                                  "Compound",
                                  xco + 40, yco - 64, (width - 80)/2, 19, 
&parAct->flag,
                                  0.0, 0.0, 0, 0,
                                  "Add this object shape to the parent shape 
(only if the parent shape is already compound)");
-                       uiDefButBitI(block, TOGN, ACT_PARENT_GHOST, B_REDR,
+                       uiDefButBitS(block, TOGN, ACT_PARENT_GHOST, B_REDR,
                                  "Ghost",
                                  xco + 40 + ((width - 80)/2), yco - 64, (width 
- 80)/2, 19, &parAct->flag,
                                  0.0, 0.0, 0, 0,

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp 
2009-05-25 03:28:35 UTC (rev 20396)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp 
2009-05-25 06:24:23 UTC (rev 20397)
@@ -42,7 +42,6 @@
        m_axismax(-1),
        m_buttonmax(-1),
        m_hatmax(-1),
-       m_hatdir(-2),
        m_isinit(0),
        m_istrig_axis(0),
        m_istrig_button(0),
@@ -50,6 +49,10 @@
 {
        for(int i=0; i<JOYAXIS_MAX; i++)
                m_axis_array[i]= 0;
+       
+       for(int i=0; i<JOYHAT_MAX; i++)
+               m_hat_array[i]= 0;
+       
 #ifndef DISABLE_SDL
        m_private = new PrivateData();
 #endif
@@ -184,22 +187,11 @@
 }
 
 
-bool SCA_Joystick::aHatIsPositive(int dir)
+bool SCA_Joystick::aHatIsPositive(int hatnum, int dir)
 {
-       bool result;
-       int res = pGetHat(dir);
-       res == dir? result = true : result = false;
-       return result;
+       return (GetHat(hatnum)==dir) ? true : false;
 }
 
-int SCA_Joystick::pGetHat(int direction)
-{
-       if(direction == m_hatdir){
-               return m_hatdir;
-       }
-       return 0;
-}
-
 int SCA_Joystick::GetNumberOfAxes()
 {
        return m_axismax;
@@ -248,8 +240,11 @@
                if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX;            
/* very unlikely */
                else if (m_axismax < 0) m_axismax = 0;
                
+               if (m_hatmax > JOYHAT_MAX) m_hatmax= JOYHAT_MAX;                
        /* very unlikely */
+               else if(m_hatmax<0) m_hatmax= 0;
+               
                if(m_buttonmax<0) m_buttonmax= 0;
-               if(m_hatmax<0) m_hatmax= 0;
+               
        }
        return true;
 #endif
@@ -278,15 +273,6 @@
        return 0;
 }
 
-void SCA_Joystick::pFillAxes()
-{
-#ifndef DISABLE_SDL
-       for(int i=0; i<m_axismax; i++)
-               m_axis_array[i]= SDL_JoystickGetAxis(m_private->m_joystick, i);
-#endif
-}
-
-
 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
 {
 #ifndef DISABLE_SDL

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h   
2009-05-25 03:28:35 UTC (rev 20396)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h   
2009-05-25 06:24:23 UTC (rev 20397)
@@ -53,28 +53,20 @@
        int                             m_joyindex;
 
        /* 
-        *support for 2 axes 
+        *support for JOYAXIS_MAX axes (in pairs)
         */
        int m_axis_array[JOYAXIS_MAX];
+
+       /* 
+        *support for JOYHAT_MAX hats (each is a direction)
+        */
+       int m_hat_array[JOYHAT_MAX];    
        
        /*
         * Precision or range of the axes
         */
        int                     m_prec;
 
-       /*
-        * multiple axis values stored here
-        */
-       int                     m_axisnum;
-       int                     m_axisvalue;
-
-       /*
-        * max # of axes avail
-        */
-       /*disabled
-       int                     m_axismax;
-       */
-
        /* 
         *      button values stored here 
         */
@@ -88,18 +80,6 @@
        int                     m_buttonmax;
        int                     m_hatmax;
        
-       /*
-        * hat values stored here 
-        */
-       int                     m_hatnum;
-       int                     m_hatdir;
-
-       /*
-
-        * max # of hats avail
-               disabled
-       int                     m_hatmax;
-        */
        /* is the joystick initialized ?*/
        bool                    m_isinit;
 
@@ -136,7 +116,6 @@
        /*
         * fills the axis mnember values 
         */
-       void pFillAxes(void);
        void pFillButtons(void);
 
        /*
@@ -149,11 +128,6 @@
         */
        int pGetAxis(int axisnum, int udlr);
 
-       /*
-        * gets the current hat direction
-        */
-       int pGetHat(int direction);
-
        SCA_Joystick(short int index);
 
        ~SCA_Joystick();
@@ -175,7 +149,7 @@
        bool aAnyButtonReleaseIsPositive(void);
        bool aButtonPressIsPositive(int button);
        bool aButtonReleaseIsPositive(int button);
-       bool aHatIsPositive(int dir);
+       bool aHatIsPositive(int hatnum, int dir);
 
        /*
         * precision is default '3200' which is overridden by input
@@ -191,8 +165,8 @@
                return m_buttonnum;
        }
 
-       int GetHat(void){
-               return m_hatdir;
+       int GetHat(int index){
+               return m_hat_array[index];
        }
 
        int GetThreshold(void){

Modified: 
trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h    
2009-05-25 03:28:35 UTC (rev 20396)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h    
2009-05-25 06:24:23 UTC (rev 20397)
@@ -40,6 +40,7 @@
 
 #define JOYINDEX_MAX                   8
 #define JOYAXIS_MAX                            16
+#define JOYHAT_MAX                             4
 
 #define JOYAXIS_RIGHT          0
 #define JOYAXIS_UP                     1

Modified: 
trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp   
2009-05-25 03:28:35 UTC (rev 20396)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp   
2009-05-25 06:24:23 UTC (rev 20397)
@@ -35,17 +35,20 @@
 #ifndef DISABLE_SDL
 void SCA_Joystick::OnAxisMotion(SDL_Event* sdl_event)
 {
-       pFillAxes();
-       m_axisnum       = sdl_event->jaxis.axis;
-       m_axisvalue = sdl_event->jaxis.value;
+       if(sdl_event->jaxis.axis >= JOYAXIS_MAX)
+               return;
+       
+       m_axis_array[sdl_event->jaxis.axis]= sdl_event->jaxis.value;
        m_istrig_axis = 1;
 }
 
 
 void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
 {
-       m_hatdir = sdl_event->jhat.value;
-       m_hatnum = sdl_event->jhat.hat;
+       if(sdl_event->jhat.hat >= JOYAXIS_MAX)
+               return;
+
+       m_hat_array[sdl_event->jhat.hat]= sdl_event->jhat.value;
        m_istrig_hat = 1;
 }
 

Modified: trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp    
2009-05-25 03:28:35 UTC (rev 20396)
+++ trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp    
2009-05-25 06:24:23 UTC (rev 20397)
@@ -198,35 +198,22 @@
        case KX_JOYSENSORMODE_HAT:
                {
                /* what is what!
-                       numberof = m_hat  -- max 2
+                       numberof = m_hat  -- max 4
                        direction= m_hatf -- max 12
                        */
                        
                        if (!js->IsTrigHat() && !reset) /* No events from SDL? 
- dont bother */
                                return false;
                        
-                       if(m_hat == 1){
-                               if(js->aHatIsPositive(m_hatf)){
-                                       m_istrig = 1;
+                       if((m_bAllEvents && js->GetHat(m_hat-1)) || 
js->aHatIsPositive(m_hat-1, m_hatf)) {
+                               m_istrig = 1;
+                               result = true;
+                       }else{
+                               if(m_istrig){
+                                       m_istrig = 0;
                                        result = true;
-                               }else{
-                                       if(m_istrig){
-                                               m_istrig = 0;
-                                               result = true;
-                                       }
                                }
                        }
-                       if(m_hat == 2){
-                               if(js->aHatIsPositive(m_hatf)){
-                                       m_istrig = 1;
-                                       result = true;
-                               }else{
-                                       if(m_istrig){
-                                               m_istrig = 0;
-                                               result = true;
-                                       }
-                               }
-                       }
                        break;
                }
                /* test for ball anyone ?*/
@@ -331,6 +318,8 @@
        
KX_PYATTRIBUTE_INT_LIST_RW_CHECK("hat",0,12,true,SCA_JoystickSensor,m_hat,2,CheckHat),
        KX_PYATTRIBUTE_RO_FUNCTION("axisValues",        SCA_JoystickSensor, 
pyattr_get_axis_values),
        KX_PYATTRIBUTE_RO_FUNCTION("axisSingle", SCA_JoystickSensor, 
pyattr_get_axis_single),
+       KX_PYATTRIBUTE_RO_FUNCTION("hatValues", SCA_JoystickSensor, 
pyattr_get_hat_values),
+       KX_PYATTRIBUTE_RO_FUNCTION("hatSingle", SCA_JoystickSensor, 
pyattr_get_hat_single),
        KX_PYATTRIBUTE_RO_FUNCTION("numAxis",           SCA_JoystickSensor, 
pyattr_get_num_axis),
        KX_PYATTRIBUTE_RO_FUNCTION("numButtons",        SCA_JoystickSensor, 
pyattr_get_num_buttons),
        KX_PYATTRIBUTE_RO_FUNCTION("numHats",           SCA_JoystickSensor, 
pyattr_get_num_hats),
@@ -617,6 +606,29 @@
        return PyInt_FromLong(joy->GetAxisPosition(self->m_axis-1));
 }
 
+PyObject* SCA_JoystickSensor::pyattr_get_hat_values(void *self_v, const 
KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
+       SCA_Joystick *joy = 
self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
+       
+       int hat_index= joy->GetNumberOfHats();
+       PyObject *list= PyList_New(hat_index);
+       
+       while(hat_index--) {
+               PyList_SET_ITEM(list, hat_index, 
PyInt_FromLong(joy->GetHat(hat_index)));
+       }
+       
+       return list;
+}
+
+PyObject* SCA_JoystickSensor::pyattr_get_hat_single(void *self_v, const 
KX_PYATTRIBUTE_DEF *attrdef)
+{
+       SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);
+       SCA_Joystick *joy = 
self->m_pJoystickMgr->GetJoystickDevice(self->m_joyindex);
+       
+       return PyInt_FromLong(joy->GetHat(self->m_hat-1));
+}
+
 PyObject* SCA_JoystickSensor::pyattr_get_num_axis(void *self_v, const 
KX_PYATTRIBUTE_DEF *attrdef)
 {
        SCA_JoystickSensor* self= static_cast<SCA_JoystickSensor*>(self_v);

Modified: trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h      
2009-05-25 03:28:35 UTC (rev 20396)

@@ Diff output truncated at 10240 characters. @@

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to