Added Joystick sensor (from snailrose)
authorKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>
Sun, 23 Jan 2005 01:36:29 +0000 (01:36 +0000)
committerKester Maddock <Christopher.Maddock.1@uni.massey.ac.nz>
Sun, 23 Jan 2005 01:36:29 +0000 (01:36 +0000)
20 files changed:
source/blender/blenkernel/intern/sca.c
source/blender/blenloader/intern/writefile.c
source/blender/makesdna/DNA_sensor_types.h
source/blender/src/buttons_logic.c
source/gameengine/Converter/KX_ConvertSensors.cpp
source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp [new file with mode: 0644]
source/gameengine/GameLogic/Joystick/SCA_Joystick.h [new file with mode: 0644]
source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h [new file with mode: 0644]
source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp [new file with mode: 0644]
source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h [new file with mode: 0644]
source/gameengine/GameLogic/Makefile
source/gameengine/GameLogic/SCA_EventManager.h
source/gameengine/GameLogic/SCA_JoystickManager.cpp [new file with mode: 0644]
source/gameengine/GameLogic/SCA_JoystickManager.h [new file with mode: 0644]
source/gameengine/GameLogic/SCA_JoystickSensor.cpp [new file with mode: 0644]
source/gameengine/GameLogic/SCA_JoystickSensor.h [new file with mode: 0644]
source/gameengine/GameLogic/SConscript
source/gameengine/Ketsji/KX_Scene.cpp
source/gameengine/Ketsji/KX_Scene.h
source/gameengine/Ketsji/SConscript

index fdcf998d1d344f0bf536c930babe6045de3a3027..e772a3bcdb49f10ae7ab88ef5b8dddcf9069b0a6 100644 (file)
@@ -172,6 +172,9 @@ void init_sensor(bSensor *sens)
        case SENS_MESSAGE:
                sens->data= MEM_callocN(sizeof(bMessageSensor), "messagesens");
                break;
+       case SENS_JOYSTICK:
+               sens->data= MEM_callocN(sizeof(bJoystickSensor), "joysticksens");
+               break;
        default:
                ; /* this is very severe... I cannot make any memory for this        */
                /* logic brick...                                                    */
index 3232f12f13e954ac74df93cf3468baf6ee99f966..e5b6177aea4fc877e8074a06894f99ad4bb703a8 100644 (file)
@@ -474,6 +474,9 @@ static void write_sensors(WriteData *wd, ListBase *lb)
                case SENS_MESSAGE:
                        writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
                        break;
+               case SENS_JOYSTICK:
+                       writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
+                       break;
                default:
                        ; /* error: don't know how to write this file */
                }
index 9c4d3c1ca2040d1527315f6720d9417de146f309..c56fe81ca1926b814b2566a7c92c6498ff32f264 100644 (file)
@@ -151,6 +151,19 @@ typedef struct bSensor {
        int pad;
 } bSensor;
 
+typedef struct bJoystickSensor {
+       char name[32];
+       short type;
+       short pad;
+       int axis;
+       int axisf;
+       int button;
+       int buttonf;
+       int hat;
+       int hatf;
+       int precision;
+} bJoystickSensor;
+
 /* bMouseSensor->type: uses blender event defines */
 
 /* propertysensor->type */
@@ -186,7 +199,7 @@ typedef struct bSensor {
 #define SENS_RANDOM     8
 #define SENS_RAY        9
 #define SENS_MESSAGE   10
-
+#define SENS_JOYSTICK  11
 /* sensor->flag */
 #define SENS_SHOW              1
 #define SENS_DEL               2
@@ -196,7 +209,7 @@ typedef struct bSensor {
 /* sensor->pulse */
 #define SENS_PULSE_CONT        0
 #define SENS_PULSE_REPEAT      1
-#define SENS_PULSE_ONCE        2
+//#define SENS_PULSE_ONCE      2
 #define SENS_NEG_PULSE_MODE 4
 
 /* sensor->suppress */
@@ -224,5 +237,19 @@ typedef struct bSensor {
 #define BL_SENS_MOUSE_MOVEMENT       8
 #define BL_SENS_MOUSE_MOUSEOVER      16
 
+#define SENS_JOY_BUTTON                0
+#define SENS_JOY_BUTTON_PRESSED        0
+#define SENS_JOY_BUTTON_RELEASED       1
+
+#define SENS_JOY_AXIS                  1
+#define SENS_JOY_X_AXIS                0
+#define SENS_JOY_Y_AXIS                1
+#define SENS_JOY_NEG_X_AXIS            2
+#define SENS_JOY_NEG_Y_AXIS            3
+#define SENS_JOY_PRECISION             4
+
+#define SENS_JOY_HAT                   2
+#define SENS_JOY_HAT_DIR               0
+
 #endif
 
index d70f4cd9fd6c2a380a098192557742abad26f7db..61c57ea8596f2981c1eb69d1c87844b58ae00868 100644 (file)
@@ -640,6 +640,8 @@ static char *sensor_name(int type)
                return "Ray";
        case SENS_MESSAGE:
                return "Message";
+       case SENS_JOYSTICK:
+               return "Joystick";
        }
        return "unknown";
 }
@@ -649,7 +651,7 @@ static char *sensor_pup(void)
        /* the number needs to match defines in game.h */
        return "Sensors %t|Always %x0|Keyboard %x3|Mouse %x5|"
                "Touch %x1|Collision %x6|Near %x2|Radar %x7|"
-               "Property %x4|Random %x8|Ray %x9|Message %x10";
+               "Property %x4|Random %x8|Ray %x9|Message %x10|Joystick %x11";
 }
 
 static char *controller_name(int type)
@@ -928,6 +930,7 @@ static int get_col_sensor(int type)
        case SENS_RANDOM:               return TH_BUT_NEUTRAL;
        case SENS_RAY:                  return TH_BUT_SETTING1;
        case SENS_MESSAGE:              return TH_BUT_SETTING2;
+       case SENS_JOYSTICK:             return TH_BUT_NEUTRAL;
        default:                                return TH_BUT_NEUTRAL;
        }
 }
@@ -980,6 +983,8 @@ static short draw_sensorbuttons(bSensor *sens, uiBlock *block, short xco, short
        bRandomSensor    *randomSensor = NULL;
        bRaySensor       *raySens      = NULL;
        bMessageSensor   *mes          = NULL;
+       bJoystickSensor  *joy              = NULL;
+
        short ysize;
        char *str;
        
@@ -1299,6 +1304,64 @@ static short draw_sensorbuttons(bSensor *sens, uiBlock *block, short xco, short
                        yco -= ysize;
                        break;
                }
+               case SENS_JOYSTICK:
+               {
+
+                       ysize =  72;
+                       
+                       glRects(xco, yco-ysize, xco+width, yco);
+                       uiEmboss((float)xco, (float)yco-ysize, (float)xco+width, (float)yco, 1);
+                       
+                       /* line 1: header */
+                       draw_default_sensor_header(sens, block, xco, yco, width);
+
+                       joy= sens->data;
+                       
+
+                       str= "Type %t|Button %x0|Axis %x1|Hat%x2"; 
+                       uiDefButS(block, MENU, B_REDR, str, xco+10, yco-44, 0.6 * (width-20), 19,
+                               &joy->type, 0, 31, 0, 0,
+                               "The type of event this joystick sensor is triggered on.");
+                       
+                       if(joy->type == SENS_JOY_BUTTON)
+                       {
+                               uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
+                               &joy->button, 0, 18, 100, 0,
+                               "Specify which button to use");
+                               
+                               str = "Type %t|Pressed %x0|Released %x1"; 
+                               uiDefButI(block, MENU, B_REDR, str, xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
+                               &joy->buttonf, 2.0, 31, 0, 0,
+                               "Button pressed or released.");
+                       }
+                       else if(joy->type == SENS_JOY_AXIS)
+                       {
+                               uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
+                               &joy->axis, 1, 2.0, 100, 0,
+                               "Specify which axis to use");
+
+                               uiDefButI(block, NUM, 1, "Threshold:", xco+10 + 0.6 * (width-20),yco-44, 0.4 * (width-20), 19,
+                               &joy->precision, 0, 32768.0, 100, 0,
+                               "Specify the precision of the axis");
+
+                               str = "Type %t|Up Axis %x1 |Down Axis %x3|Left Axis %x2|Right Axis %x0"; 
+                               uiDefButI(block, MENU, B_REDR, str, xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
+                               &joy->axisf, 2.0, 31, 0, 0,
+                               "The direction of the axis");
+                       }
+                       else
+                       {
+                               uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
+                               &joy->hat, 1, 2.0, 100, 0,
+                               "Specify which hat to use");
+                               
+                               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");
+                       }
+                       yco-= ysize;
+                       break;
+               }
        }
        
        uiBlockSetEmboss(block, UI_EMBOSSM);
index 8818762bfabef18373cf5684006111db1982d6db..396d633043c97d257d41abae52a0735403cfe695 100644 (file)
@@ -65,7 +65,7 @@ probably misplaced */
 #include "KX_NearSensor.h"
 #include "KX_RadarSensor.h"
 #include "KX_MouseFocusSensor.h"
-
+#include "SCA_JoystickSensor.h"
 #include "KX_NetworkMessageSensor.h"
 
 #include "SCA_PropertySensor.h"
@@ -548,7 +548,7 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                        // this sumoObject is not deleted by a gameobj, so delete it ourself
                                        // later (memleaks)!
                                        MT_Scalar smallmargin = 0.0;
-                                       MT_Scalar largemargin = 0.1;
+                                       MT_Scalar largemargin = 0.0;
                                        
                                        bool bFindMaterial = false;
                                        gamesensor = new KX_RadarSensor(
@@ -609,6 +609,62 @@ void BL_ConvertSensors(struct Object* blenderobject,
                                                gamesensor = new SCA_RandomSensor(eventmgr, gameobj, randomSeed);
                                        }
                                }
+                               break;
+                       }
+               case SENS_JOYSTICK:
+                       {
+                               int joysticktype = SCA_JoystickSensor::KX_JOYSENSORMODE_NODEF;
+                               
+                               bJoystickSensor* bjoy = (bJoystickSensor*) sens->data;
+                               
+                               SCA_JoystickManager *eventmgr 
+                                       = (SCA_JoystickManager*) logicmgr->FindEventManager(SCA_EventManager::JOY_EVENTMGR);
+                               if (eventmgr) 
+                               {
+                                       int axis        =0;
+                                       int axisf       =0;
+                                       int button      =0;
+                                       int buttonf =0; 
+                                       int hat         =0; 
+                                       int hatf        =0;
+                                       int prec        =0;
+                                       
+                                       switch(bjoy->type)
+                                       {
+                                       case SENS_JOY_AXIS:
+                                               axis    = bjoy->axis;
+                                               axisf   = bjoy->axisf;
+                                               prec    = bjoy->precision;
+                                               joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_AXIS;
+                                               break;
+                                       case SENS_JOY_BUTTON:
+                                               button  = bjoy->button;
+                                               buttonf = bjoy->buttonf;
+                                               joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
+                                               break;
+                                       case SENS_JOY_HAT:
+                                               hat             = bjoy->hat;
+                                               hatf    = bjoy->hatf;
+                                               joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_HAT;
+                                               break;
+                                       default:
+                                               printf("Error: bad case statement\n");
+                                               break;
+                                       }
+                                       gamesensor = new SCA_JoystickSensor(
+                                               eventmgr,
+                                               gameobj,
+                                               joysticktype,
+                                               axis,axisf,
+                                               prec,
+                                               button,buttonf,
+                                               hat,hatf);
+                               } 
+                               else
+                               {
+                                       printf("Error there was a problem finding the event manager\n");
+                               }
+
                                break;
                        }
                default:
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp b/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
new file mode 100644 (file)
index 0000000..7d1d4b2
--- /dev/null
@@ -0,0 +1,355 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#include <SDL.h>
+#include "SCA_Joystick.h"
+#include "SCA_JoystickPrivate.h"
+
+SCA_Joystick::SCA_Joystick()
+       :
+       m_axis10(0),
+       m_axis11(0),
+       m_axis20(0),
+       m_axis21(0),
+       m_prec(3200),
+       m_buttonnum(-2),
+       m_hatdir(-2),
+       m_isinit(0),
+       m_istrig(0)
+{
+       m_private = new PrivateData();
+}
+
+SCA_Joystick::~SCA_Joystick()
+{
+       delete m_private;
+}
+
+bool SCA_Joystick::CreateJoystickDevice()
+{
+       bool init = false;
+       init = pCreateJoystickDevice();
+       return init;
+}
+
+
+void SCA_Joystick::DestroyJoystickDevice()
+{
+       if(m_isinit)
+               pDestroyJoystickDevice();
+}
+
+void SCA_Joystick::HandleEvents()
+{
+       if(m_isinit)
+       {
+               if(SDL_PollEvent(&m_private->m_event))
+               {
+                       switch(m_private->m_event.type)
+                       {
+                       case SDL_JOYAXISMOTION: {HANDLE_AXISMOTION(OnAxisMotion);break;}
+                       case SDL_JOYHATMOTION:  {HANDLE_HATMOTION(OnHatMotion);  break;}
+                       case SDL_JOYBUTTONUP:   {HANDLE_BUTTONUP(OnButtonUp);    break;}
+                       case SDL_JOYBUTTONDOWN: {HANDLE_BUTTONDOWN(OnButtonDown);break;}
+                       case SDL_JOYBALLMOTION: {HANDLE_BALLMOTION(OnBallMotion);break;}
+                       default:                                {HANDLE_NOEVENT(OnNothing);      break;}
+                       }
+               }
+       }
+}
+
+
+void SCA_Joystick::cSetPrecision(int val)
+{
+       m_prec = val;
+}
+
+bool SCA_Joystick::aRightAxisIsPositive(int axis)
+{
+       bool result;
+       int res = pGetAxis(axis,1);
+       res > m_prec? result = true: result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aUpAxisIsPositive(int axis)
+{
+       bool result;
+       int res = pGetAxis(axis,0);
+       res < -m_prec? result = true : result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aLeftAxisIsPositive(int axis)
+{
+       bool result;
+       int res = pGetAxis(axis,1);
+       res < -m_prec ? result = true : result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aDownAxisIsPositive(int axis)
+{
+       bool result;
+       int res = pGetAxis(axis,0);
+       res > m_prec ? result = true:result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aButtonPressIsPositive(int button)
+{
+       bool result;
+       SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aButtonReleaseIsPositive(int button)
+{
+       bool result;
+       SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
+       m_istrig = result;
+       return result;
+}
+
+
+bool SCA_Joystick::aHatIsPositive(int dir)
+{
+       bool result;
+       int res = pGetHat(dir);
+       res == dir? result = true : result = false;
+       m_istrig = result;
+       return result;
+}
+
+
+int SCA_Joystick::pGetButtonPress(int button)
+{
+       if(button == m_buttonnum)
+               return m_buttonnum;
+       return -2;
+
+}
+
+
+int SCA_Joystick::pGetButtonRelease(int button)
+{
+       if(button == m_buttonnum)
+               return m_buttonnum;
+       return -2;
+}
+
+
+int SCA_Joystick::pGetHat(int direction)
+{
+       if(direction == m_hatdir){
+               return m_hatdir;
+       }
+       return 0;
+}
+
+
+bool SCA_Joystick::GetJoyAxisMotion()
+{
+       bool result = false;
+       if(m_isinit){
+               if(SDL_PollEvent(&m_private->m_event)){
+                       switch(m_private->m_event.type)
+                       {
+                       case SDL_JOYAXISMOTION:
+                               result = true;
+                               break;
+                       }
+               }
+       }
+       return result;
+}
+
+
+bool SCA_Joystick::GetJoyButtonPress()
+{
+       bool result = false;
+       if(m_isinit){
+               if(SDL_PollEvent(&m_private->m_event)){
+                       switch(m_private->m_event.type)
+                       {
+                       case SDL_JOYBUTTONDOWN:
+                               result = true;
+                               break;
+                       }
+               }
+       }
+       return result;
+}
+
+
+bool SCA_Joystick::GetJoyButtonRelease()
+{
+       bool result = false;
+       if(m_isinit)
+       {
+               if(SDL_PollEvent(&m_private->m_event)){
+                       switch(m_private->m_event.type)
+                       {
+                       case SDL_JOYBUTTONUP:
+                               result = true;
+                               break;
+                       }
+               }
+       }
+       return result;
+}
+
+
+bool SCA_Joystick::GetJoyHatMotion()
+{
+       bool result = false;
+       if(m_isinit){
+               if(SDL_PollEvent(&m_private->m_event)){
+                       switch(m_private->m_event.type)
+                       {
+                       case SDL_JOYHATMOTION:
+                               result = true;
+                               break;
+                       }
+               }
+       }
+       return 0;
+}
+
+
+int SCA_Joystick::GetNumberOfAxes()
+{
+       int number;
+       if(m_isinit){
+               if(m_private->m_joystick){
+                       number = SDL_JoystickNumAxes(m_private->m_joystick);
+                       return number;
+               }
+       }
+       return -1;
+}
+
+
+int SCA_Joystick::GetNumberOfButtons()
+{
+       int number;
+       if(m_isinit){
+               if(m_private->m_joystick){
+                       number = SDL_JoystickNumButtons(m_private->m_joystick);
+                       return number;
+               }
+       }
+       return -1;
+}
+
+int SCA_Joystick::GetNumberOfHats()
+{
+       int number;
+       if(m_isinit){
+               if(m_private->m_joystick){
+                       number = SDL_JoystickNumHats(m_private->m_joystick);
+                       return number;
+               }
+       }
+       return -1;
+}
+
+
+bool SCA_Joystick::pCreateJoystickDevice()
+{
+       if(m_isinit == false){
+               if(SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO ) == -1 ){
+                       echo("Error-Initializing-SDL: " << SDL_GetError());
+                       return false;
+               }
+               if(SDL_NumJoysticks() > 0){
+                       for(int i=0; i<SDL_NumJoysticks();i++){
+                               m_private->m_joystick = SDL_JoystickOpen(i);
+                               SDL_JoystickEventState(SDL_ENABLE);
+                               m_numjoys = i;
+                       }
+                       echo("Joystick-initialized");
+                       m_isinit = true;
+                       return true;
+               }else{
+                       echo("Joystick-Error: " << SDL_NumJoysticks() << " avaiable joystick(s)");
+                       return false;
+               }
+       }
+       return false;
+}
+
+
+void SCA_Joystick::pDestroyJoystickDevice()
+{
+       echo("Closing-");
+       for(int i=0; i<SDL_NumJoysticks(); i++){
+               if(SDL_JoystickOpened(i)){
+                       SDL_JoystickClose(m_private->m_joystick);
+               }
+       }
+       SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO );
+}
+
+void SCA_Joystick::pFillAxes()
+{
+       if(GetNumberOfAxes() == 1){
+               m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
+               m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
+       }else if(GetNumberOfAxes() > 1){
+               m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
+               m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
+               m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
+               m_axis21 = SDL_JoystickGetAxis(m_private->m_joystick, 3);
+       }else{
+               m_axis10 = 0;m_axis11 = 0;
+               m_axis20 = 0;m_axis21 = 0;
+       }
+}
+
+int SCA_Joystick::pGetAxis(int axisnum, int udlr)
+{
+       if(axisnum == 1 && udlr == 1)return m_axis10; //u/d
+       if(axisnum == 1 && udlr == 0)return m_axis11; //l/r
+       if(axisnum == 2 && udlr == 0)return m_axis20; //...
+       if(axisnum == 2 && udlr == 1)return m_axis21;
+       return 0;
+}
+
diff --git a/source/gameengine/GameLogic/Joystick/SCA_Joystick.h b/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
new file mode 100644 (file)
index 0000000..413dbfa
--- /dev/null
@@ -0,0 +1,195 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#ifndef _SCA_JOYSTICK_H_
+#define _SCA_JOYSTICK_H_
+
+#include "SCA_JoystickDefines.h"
+
+using namespace std;
+
+/*
+ * Basic Joystick class
+ */
+class SCA_Joystick
+{
+       class PrivateData;
+       PrivateData             *m_private;
+       int                             m_joyindex;
+       /*!
+        * the number of avail joysticks 
+        */
+       int                     m_numjoys;
+       /* 
+        *support for 2 axes 
+        */
+       int m_axis10,m_axis11;
+       int m_axis20,m_axis21;
+       /* 
+        * 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 
+        */
+       int                     m_buttonnum;
+       /*
+        * max # of buttons avail
+       */
+       int                     m_buttonmax;
+        /* 
+        * 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;
+       
+       /* is triggered */
+       bool                    m_istrig;
+       /*
+        * Open the joystick
+        */
+       bool pCreateJoystickDevice(void);
+       /*
+        * Close the joystick
+        */
+       void pDestroyJoystickDevice(void);
+       
+       /*
+        * event callbacks
+        */
+       void OnAxisMotion(void);
+       void OnHatMotion(void);
+       void OnButtonUp(void);
+       void OnButtonDown(void);
+       void OnNothing(void);
+       void OnBallMotion(void){}
+       /*
+        * fills the axis mnember values 
+        */
+       void pFillAxes(void);
+
+       void pFillButtons(void);
+       /*
+        * returns m_axis10,m_axis11...
+        */
+       int pGetAxis(int axisnum, int udlr);
+       /*
+        * gets the current button
+        */
+       int pGetButtonPress(int button);
+       /*
+        * returns if no button is pressed
+        */
+       int pGetButtonRelease(int button);
+       /*
+        * gets the current hat direction
+        */
+       int pGetHat(int direction);
+       
+public:
+       SCA_Joystick();
+       ~SCA_Joystick();
+       
+       bool CreateJoystickDevice(void);
+       void DestroyJoystickDevice(void);
+       void HandleEvents();
+       /*
+        */
+       bool aUpAxisIsPositive(int axis);
+       bool aDownAxisIsPositive(int axis);
+       bool aLeftAxisIsPositive(int axis);
+       bool aRightAxisIsPositive(int axis);
+       bool aButtonPressIsPositive(int button);
+       bool aButtonReleaseIsPositive(int button);
+       bool aHatIsPositive(int dir);
+       /*
+        * precision is default '3200' which is overridden by input
+        */
+       void cSetPrecision(int val);
+
+       int GetAxis10(void){
+               return m_axis10;
+       }
+       int GetAxis11(void){
+               return m_axis11;
+       }
+       int GetAxis20(void){
+               return m_axis20;
+       }
+       int GetAxis21(void){
+               return m_axis21;
+       }
+       int GetButton(void){
+               return m_buttonnum;
+       }
+       int GetHat(void){
+               return m_hatdir;
+       }
+       int GetThreshold(void){
+               return m_prec;
+       }
+       bool IsTrig(void){
+               return m_istrig;
+       }
+       
+       /*
+        * returns true if an event is being processed
+        */
+       bool GetJoyAxisMotion(void);
+       bool GetJoyButtonPress(void);
+       bool GetJoyButtonRelease(void);
+       bool GetJoyHatMotion(void);
+       /*
+        * returns the # of...
+        */
+       int GetNumberOfAxes(void);
+       int GetNumberOfButtons(void);
+       int GetNumberOfHats(void);
+       
+};
+
+#endif
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickDefines.h
new file mode 100644 (file)
index 0000000..c4ddd5b
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#ifndef _SCA_JOYSTICKDEFINES_H_
+#define _SCA_JOYSTICKDEFINES_H_
+
+#ifdef main
+#undef main
+#endif
+
+#ifndef _DEBUG
+#define echo(x)
+#else
+#include <iostream>
+#define echo(x) std::cout << x << std::endl;
+#endif
+
+/* function callbacks */
+#define HANDLE_AXISMOTION(fn)  ((fn)(), 0L)
+#define HANDLE_HATMOTION(fn)   ((fn)(), 0L)
+#define HANDLE_BUTTONUP(fn)    ((fn)(), 0L)
+#define HANDLE_BUTTONDOWN(fn)  ((fn)(), 0L)
+#define HANDLE_BALLMOTION(fn)  ((fn)(), 0L)
+#define HANDLE_NOEVENT(fn)             ((fn)(), 0L)
+
+#endif
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp b/source/gameengine/GameLogic/Joystick/SCA_JoystickEvents.cpp
new file mode 100644 (file)
index 0000000..90bb728
--- /dev/null
@@ -0,0 +1,73 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#include <SDL.h>
+#include "SCA_Joystick.h"
+#include "SCA_JoystickPrivate.h"
+
+void SCA_Joystick::OnAxisMotion(void)
+{
+       pFillAxes();
+       m_axisnum       = m_private->m_event.jaxis.axis;
+       m_axisvalue = m_private->m_event.jaxis.value;
+       m_istrig = 1;
+}
+
+
+void SCA_Joystick::OnHatMotion(void)
+{
+       m_hatdir = m_private->m_event.jhat.value;
+       m_hatnum = m_private->m_event.jhat.hat;
+       m_istrig = 1;
+}
+
+
+void SCA_Joystick::OnButtonUp(void)
+{
+       m_buttonnum = -2;
+}
+
+
+void SCA_Joystick::OnButtonDown(void)
+{
+       m_buttonmax = GetNumberOfButtons();
+       if(m_private->m_event.jbutton.button >= 1 || m_private->m_event.jbutton.button <= m_buttonmax)
+       {
+               m_istrig = 1;
+               m_buttonnum = m_private->m_event.jbutton.button;
+       }
+}
+
+
+void SCA_Joystick::OnNothing(void)
+{
+       m_istrig = 0;
+}
+
diff --git a/source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h b/source/gameengine/GameLogic/Joystick/SCA_JoystickPrivate.h
new file mode 100644 (file)
index 0000000..0843574
--- /dev/null
@@ -0,0 +1,52 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): snailrose.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#ifndef __SCA_JOYSTICKPRIVATE_H__
+#define __SCA_JOYSTICKPRIVATE_H__
+#include "SCA_Joystick.h"
+
+class SCA_Joystick::PrivateData
+{
+public:
+       /*
+        * SDL events structure 
+        */
+       SDL_Event               m_event;
+       /*
+        * The Joystick
+        */
+       SDL_Joystick*   m_joystick;
+
+       PrivateData()
+       : m_joystick(NULL)
+       {
+       }
+};
+#endif
index 520be0d89e952f36f8e03831e6c968e3aac57416..07611ceffd67e1af75ecd64c269950bb505e31d9 100644 (file)
@@ -42,7 +42,8 @@ CPPFLAGS += -I../Expressions
 CPPFLAGS += -I$(NAN_STRING)/include    
 CPPFLAGS += -I$(NAN_MOTO)/include
 CPPFLAGS += -I$(NAN_PYTHON)/include/python$(NAN_PYTHON_VERSION)
+CPPFLAGS += -I$(NAN_SDL)
 
 CPPFLAGS += -I../../kernel/gen_system
 
-
+CCSRCS += Joystick/SCA_Joystick.cpp Joystick/SCA_JoystickEvents.cpp
index 92bda9df17092970542c4f3c36666c47f5d82863..e15e19b239b095cb89783dfa513e0e7d8cb5bef9 100644 (file)
@@ -51,7 +51,8 @@ public:
                RANDOM_EVENTMGR,
                RAY_EVENTMGR,
                RADAR_EVENTMGR,
-               NETWORK_EVENTMGR
+               NETWORK_EVENTMGR,
+               JOY_EVENTMGR
        };
 
        SCA_EventManager(EVENT_MANAGER_TYPE mgrtype);
diff --git a/source/gameengine/GameLogic/SCA_JoystickManager.cpp b/source/gameengine/GameLogic/SCA_JoystickManager.cpp
new file mode 100644 (file)
index 0000000..3637883
--- /dev/null
@@ -0,0 +1,81 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+#include "SCA_JoystickSensor.h"
+#include "SCA_JoystickManager.h"
+#include "SCA_LogicManager.h"
+#include <vector>
+#include "SCA_ISensor.h"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+using namespace std;
+
+SCA_JoystickManager::SCA_JoystickManager(class SCA_LogicManager* logicmgr)
+       : SCA_EventManager(JOY_EVENTMGR),
+       m_logicmgr(logicmgr)
+{
+       Joystick.CreateJoystickDevice();
+}
+SCA_JoystickManager::~SCA_JoystickManager()
+{
+       Joystick.DestroyJoystickDevice();
+}
+
+
+void SCA_JoystickManager::NextFrame(double curtime,double deltatime)
+{
+       for (unsigned int i = 0; i < m_sensors.size(); i++)
+       {
+               SCA_JoystickSensor* joysensor = (SCA_JoystickSensor*) m_sensors[i];
+               if(!joysensor->IsSuspended())
+               {
+                       Joystick.HandleEvents();
+                       joysensor->Activate(m_logicmgr, NULL);
+               }
+       }
+}
+
+
+
+void SCA_JoystickManager::RegisterSensor(SCA_ISensor* sensor)
+{
+       m_sensors.push_back(sensor);
+}
+
+SCA_Joystick SCA_JoystickManager::GetJoystickDevice()
+{
+       /* 
+        *Return the instance of SCA_Joystick for use 
+        */
+       return Joystick;
+}
diff --git a/source/gameengine/GameLogic/SCA_JoystickManager.h b/source/gameengine/GameLogic/SCA_JoystickManager.h
new file mode 100644 (file)
index 0000000..c6ca0a4
--- /dev/null
@@ -0,0 +1,56 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+#ifndef __JOYSTICKMANAGER_H_
+#define __JOYSTICKMANAGER_H_
+
+#include "SCA_EventManager.h"
+#include "Joystick/SCA_Joystick.h"
+#include <vector>
+
+using namespace std;
+class SCA_JoystickManager : public SCA_EventManager
+{
+       
+       class SCA_LogicManager* m_logicmgr;
+       /**
+        * SDL Joystick Class Instance
+        */
+       SCA_Joystick Joystick;
+public:
+       SCA_JoystickManager(class SCA_LogicManager* logicmgr);
+       virtual ~SCA_JoystickManager();
+       virtual void NextFrame(double curtime,double deltatime);
+       virtual void    RegisterSensor(SCA_ISensor* sensor);
+       SCA_Joystick GetJoystickDevice(void);
+
+};
+
+#endif
diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
new file mode 100644 (file)
index 0000000..d1a3546
--- /dev/null
@@ -0,0 +1,452 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+#include "SCA_JoystickManager.h"
+#include "SCA_JoystickSensor.h"
+
+#include "SCA_EventManager.h"
+#include "SCA_LogicManager.h"
+
+#include <iostream>
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
+                                                                          SCA_IObject* gameobj,
+                                                                          short int joymode,
+                                                                          int axis, int axisf,int prec,
+                                                                          int button, int buttonf,
+                                                                          int hat, int hatf,
+                                                                          PyTypeObject* T )
+                                                                          :SCA_ISensor(gameobj,eventmgr,T),
+                                                                          m_pJoystickMgr(eventmgr),
+                                                                          m_axis(axis),
+                                                                          m_axisf(axisf),
+                                                                          m_button(button),
+                                                                          m_buttonf(buttonf),
+                                                                          m_hat(hat),
+                                                                          m_hatf(hatf),
+                                                                          m_precision(prec),
+                                                                          m_joymode(joymode)
+{      
+/*
+std::cout << " axis "          << m_axis               << std::endl;
+std::cout << " axis flag "     << m_axisf              << std::endl;
+std::cout << " precision "     << m_precision  << std::endl;
+std::cout << " button "        << m_button     << std::endl;
+std::cout << " button flag "<< m_buttonf       << std::endl;
+std::cout << " hat "           << m_hat                << std::endl;
+std::cout << " hat flag "      << m_hatf               << std::endl;
+*/
+       m_istrig=0;
+}
+
+
+
+SCA_JoystickSensor::~SCA_JoystickSensor()
+{
+}
+
+
+
+CValue* SCA_JoystickSensor::GetReplica()
+{
+       CValue* replica = new SCA_JoystickSensor(*this);
+       // this will copy properties and so on...
+       CValue::AddDataToReplica(replica);
+       return replica;
+}
+
+
+
+bool SCA_JoystickSensor::IsPositiveTrigger()
+{ 
+       bool result =   m_istrig;
+       if (m_invert)
+               result = !result;
+       return result;
+}
+bool SCA_JoystickSensor::Evaluate(CValue* event)
+{
+       SCA_Joystick js = m_pJoystickMgr->GetJoystickDevice();
+
+       bool result = false;
+       
+       switch(m_joymode)
+       {
+       case KX_JOYSENSORMODE_AXIS:
+               {
+               /* what is what!
+                       m_axisf == 0 == right
+                       m_axisf == 1 == up
+                       m_axisf == 2 == left
+                       m_axisf == 3 == down
+                       numberof== m_axis  -- max 2
+                       */
+                       js.cSetPrecision(m_precision);
+                       if(m_axisf == 1){
+                               if(js.aUpAxisIsPositive(m_axis)){
+                                       m_istrig =1;
+                                       result = true;
+                               }else{
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       if(m_axisf == 3){
+                               if(js.aDownAxisIsPositive(m_axis)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else{
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       if(m_axisf == 2){
+                               if(js.aLeftAxisIsPositive(m_axis)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else{
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       if(m_axisf == 0){
+                               if(js.aRightAxisIsPositive(m_axis)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else{
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       break;
+               }
+       case KX_JOYSENSORMODE_BUTTON:
+               {
+               /* what is what!
+                       pressed  = m_buttonf == 0
+                       released = m_buttonf == 1
+                       m_button = the actual button in question
+                       */
+                       if(m_buttonf == 0){
+                               if(js.aButtonPressIsPositive(m_button)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else {
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       if(m_buttonf == 1){
+                               if(js.aButtonReleaseIsPositive(m_button)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else {
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }                       
+                       }
+                       break;
+               }
+       case KX_JOYSENSORMODE_HAT:
+               {
+               /* what is what!
+                       numberof = m_hat  -- max 2
+                       direction= m_hatf -- max 12
+                       */
+                       if(m_hat == 1){
+                               if(js.aHatIsPositive(m_hatf)){
+                                       m_istrig = 1;
+                                       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;
+                                       }
+                               }
+                       }
+                       /*
+                       if(m_hat == 3){
+                               if(js.aHatIsPositive(m_hatf)){
+                                       m_istrig = 1;
+                                       result = true;
+                               }else{
+                                       if(m_istrig){
+                                               m_istrig = 0;
+                                               result = true;
+                                       }
+                               }
+                       }
+                       */
+                       break;
+               }
+               /* test for ball anyone ?*/
+       default:
+               printf("Error invalid switch statement\n");
+               break;
+       }
+       if(!js.IsTrig()){
+               m_istrig = 0;
+       }
+       return result;
+}
+
+
+bool SCA_JoystickSensor::isValid(SCA_JoystickSensor::KX_JOYSENSORMODE m)
+{
+       bool res = false;
+       
+       res = ((m > KX_JOYSENSORMODE_NODEF) && (m < KX_JOYSENSORMODE_MAX));
+       
+       return res;
+}
+
+/* ------------------------------------------------------------------------- */
+/* Python functions                                                                                                             */
+/* ------------------------------------------------------------------------- */
+
+/* Integration hooks ------------------------------------------------------- */
+PyTypeObject SCA_JoystickSensor::Type = {
+       PyObject_HEAD_INIT(&PyType_Type)
+               0,
+               "SCA_JoystickSensor",
+               sizeof(SCA_JoystickSensor),
+               0,
+               PyDestructor,
+               0,
+               __getattr,
+               __setattr,
+               0, //&MyPyCompare,
+               __repr,
+               0, //&cvalue_as_number,
+               0,
+               0,
+               0,
+               0
+};
+
+PyParentObject SCA_JoystickSensor::Parents[] = {
+               &SCA_JoystickSensor::Type,
+               &SCA_ISensor::Type,
+               &SCA_ILogicBrick::Type,
+               &CValue::Type,
+               NULL
+};
+
+PyMethodDef SCA_JoystickSensor::Methods[] = {
+       {"getAxis",      (PyCFunction) SCA_JoystickSensor::sPyGetAxis,          METH_NOARGS,    GetAxis_doc},
+       {"setAxis",      (PyCFunction) SCA_JoystickSensor::sPySetAxis,          METH_VARARGS,   SetAxis_doc},
+       {"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetRealAxis,      METH_NOARGS,    GetRealAxis_doc},
+       {"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS,        GetThreshold_doc},
+       {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS,       SetThreshold_doc},
+       {"getButton",    (PyCFunction) SCA_JoystickSensor::sPyGetButton,        METH_NOARGS,    GetButton_doc},
+       {"setButton",    (PyCFunction) SCA_JoystickSensor::sPySetButton,        METH_VARARGS,   SetButton_doc},
+       {"getHat",               (PyCFunction) SCA_JoystickSensor::sPyGetHat,           METH_NOARGS,    GetHat_doc},
+       {"setHat",               (PyCFunction) SCA_JoystickSensor::sPySetHat,           METH_VARARGS,   SetHat_doc},
+       {"getNumAxes",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes,     METH_NOARGS,    NumberOfAxes_doc},
+       {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS,      NumberOfButtons_doc},
+       {"getNumHats",   (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats,     METH_NOARGS,    NumberOfHats_doc},
+       {NULL,NULL} //Sentinel
+};
+
+PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) {
+       _getattr_up(SCA_ISensor);
+}
+
+/* get axis  ---------------------------------------------------------- */
+char SCA_JoystickSensor::GetAxis_doc[] = 
+"getAxis\n"
+"\tReturns the current state of the axis.\n";
+PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self, 
+                                                                               PyObject* args, 
+                                                                               PyObject* kwds) {
+       return Py_BuildValue("[ii]",m_axis, m_axisf);
+}
+/* set axis  ---------------------------------------------------------- */
+char SCA_JoystickSensor::SetAxis_doc[] = 
+"setAxis\n"
+"\tSets the current state of the axis.\n";
+PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, 
+                                                                               PyObject* args, 
+                                                                               PyObject* kwds) {
+       
+       int axis,axisflag;
+       if(!PyArg_ParseTuple(args, "ii", &axis, &axisflag)){
+               return NULL;
+       }
+       m_axis = axis;
+       m_axisf = axisflag;
+       Py_Return;
+}
+/* get axis value ----------------------------------------------------- */
+char SCA_JoystickSensor::GetRealAxis_doc[] = 
+"getAxisValue\n"
+"\tReturns a list of the values for each axis .\n";
+PyObject* SCA_JoystickSensor::PyGetRealAxis( PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds) {
+       int a,b,c,d;
+       SCA_Joystick joy = m_pJoystickMgr->GetJoystickDevice();
+       a = joy.GetAxis10();
+       b = joy.GetAxis11();
+       c = joy.GetAxis20();
+       d = joy.GetAxis21();
+       return Py_BuildValue("[iiii]",a,b,c,d);
+}
+/* get threshold  ----------------------------------------------------- */
+char SCA_JoystickSensor::GetThreshold_doc[] = 
+"getThreshold\n"
+"\tReturns the threshold of the axis.\n";
+PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self, 
+                                                                                        PyObject* args, 
+                                                                                        PyObject* kwds) {
+       return Py_BuildValue("i", m_precision);
+}
+/* set threshold  ----------------------------------------------------- */
+char SCA_JoystickSensor::SetThreshold_doc[] = 
+"setThreshold\n"
+"\tSets the threshold of the axis.\n";
+PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, 
+                                                                                        PyObject* args, 
+                                                                                        PyObject* kwds) {
+       int thresh;
+       if(!PyArg_ParseTuple(args, "i", &thresh)){
+               return NULL;
+       }
+       m_precision = thresh;
+       Py_Return;
+}
+/* get button  -------------------------------------------------------- */
+char SCA_JoystickSensor::GetButton_doc[] = 
+"getButton\n"
+"\tReturns the currently pressed button.\n";
+PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self, 
+                                                                                 PyObject* args, 
+                                                                                 PyObject* kwds) {
+       return Py_BuildValue("[ii]",m_button, m_buttonf);
+}
+/* set button  -------------------------------------------------------- */
+char SCA_JoystickSensor::SetButton_doc[] = 
+"setButton\n"
+"\tSets the button the sensor reacts to.\n";
+PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, 
+                                                                                 PyObject* args, 
+                                                                                 PyObject* kwds) {
+       int button,buttonflag;
+       if(!PyArg_ParseTuple(args, "ii", &button, &buttonflag)){
+               return NULL;
+       }
+       m_button = button;
+       m_buttonf = buttonflag;
+       Py_Return;      
+}
+/* get hat     ----------------------------------------------------------- */
+char SCA_JoystickSensor::GetHat_doc[] = 
+"getHat\n"
+"\tReturns the current direction of the hat.\n";
+PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self, 
+                                                                          PyObject* args, 
+                                                                          PyObject* kwds) {
+       return Py_BuildValue("[ii]",m_hat, m_hatf);
+}
+/* set hat     ----------------------------------------------------------- */
+char SCA_JoystickSensor::SetHat_doc[] = 
+"setHat\n"
+"\tSets the hat the sensor reacts to.\n";
+PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, 
+                                                                          PyObject* args, 
+                                                                          PyObject* kwds) {
+       int hat,hatflag;
+       if(!PyArg_ParseTuple(args, "ii", &hat, &hatflag)){
+               return NULL;
+       }
+       m_hat = hat;
+       m_hatf = hatflag;
+       Py_Return;
+}
+/* get # of ----------------------------------------------------- */
+char SCA_JoystickSensor::NumberOfAxes_doc[] = 
+"getNumAxes\n"
+"\tReturns the number of axes .\n";
+PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds) {
+       int num;
+       SCA_Joystick joy = m_pJoystickMgr->GetJoystickDevice();
+       num = joy.GetNumberOfAxes();
+       return Py_BuildValue("i",num);
+}
+char SCA_JoystickSensor::NumberOfButtons_doc[] = 
+"getNumButtons\n"
+"\tReturns the number of buttons .\n";
+PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds) {
+       int num;
+       SCA_Joystick joy = m_pJoystickMgr->GetJoystickDevice();
+       num = joy.GetNumberOfButtons();
+       return Py_BuildValue("i",num);
+}
+char SCA_JoystickSensor::NumberOfHats_doc[] = 
+"getNumHats\n"
+"\tReturns the number of hats .\n";
+PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self, 
+                                                                                       PyObject* args, 
+                                                                                       PyObject* kwds) {
+       int num;
+       SCA_Joystick joy = m_pJoystickMgr->GetJoystickDevice();
+       num = joy.GetNumberOfHats();
+       return Py_BuildValue("i",num);
+}
diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.h b/source/gameengine/GameLogic/SCA_JoystickSensor.h
new file mode 100644 (file)
index 0000000..08af4b8
--- /dev/null
@@ -0,0 +1,127 @@
+/**
+ * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version. The Blender
+ * Foundation also sells licenses for use in proprietary software under
+ * the Blender License.  See http://www.blender.org/BL/ for information
+ * about this.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+ * All rights reserved.
+ *
+ * The Original Code is: all of this file.
+ *
+ * Contributor(s): none yet.
+ *
+ * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ */
+
+
+#ifndef __JOYSENSOR_H_
+#define __JOYSENSOR_H
+
+#include "SCA_ISensor.h"
+
+class SCA_JoystickSensor :public SCA_ISensor
+{
+       Py_Header;
+       class SCA_JoystickManager*      m_pJoystickMgr;
+       
+       /**
+        * Axis 1-or-2
+        */
+       int     m_axis;
+       /**
+        * Axis flag to find direction
+        */
+       int     m_axisf;
+       /**
+        * The actual button
+        */
+       int     m_button;
+       /**
+        * Flag for a pressed or released button
+        */
+       int     m_buttonf;
+       /**
+        * The actual hat
+        */
+       int     m_hat;
+       /**
+        * Flag to find direction 1-12
+        */
+       int     m_hatf;
+       /**
+        * The threshold value the axis acts opon
+        */
+       int     m_precision;
+       /**
+        * Is an event triggered ?
+        */
+       bool    m_istrig;
+       /**
+        * The mode to determine axis,button or hat
+        */
+       short int m_joymode;
+
+       enum KX_JOYSENSORMODE {
+               KX_JOYSENSORMODE_NODEF = 0,
+               KX_JOYSENSORMODE_AXIS,
+               KX_JOYSENSORMODE_BUTTON,
+               KX_JOYSENSORMODE_HAT,
+               KX_JOYSENSORMODE_MAX
+       };
+       bool isValid(KX_JOYSENSORMODE);
+
+public:
+       SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
+                                          SCA_IObject* gameobj,
+                                          short int joymode,
+                                          int axis, int axisf,int prec,
+                                          int button, int buttonf,
+                                          int hat, int hatf,
+                                          PyTypeObject* T=&Type );
+       virtual ~SCA_JoystickSensor();
+       virtual CValue* GetReplica();
+       
+       virtual bool Evaluate(CValue* event);
+       virtual bool IsPositiveTrigger();
+       
+       /* --------------------------------------------------------------------- */
+       /* Python interface ---------------------------------------------------- */
+       /* --------------------------------------------------------------------- */
+
+       virtual PyObject* _getattr(const STR_String& attr);
+
+       /* Axes*/
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,GetAxis);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,SetAxis);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,GetRealAxis);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,GetThreshold);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,SetThreshold);
+       /* Buttons */
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,GetButton);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,SetButton);
+       /* Hats */
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,GetHat);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,SetHat);
+       /* number of */
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,NumberOfAxes);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,NumberOfButtons);
+       KX_PYMETHOD_DOC(SCA_JoystickSensor,NumberOfHats);
+       
+};
+
+#endif
index f50e14e0af9bfc653f23e029c4e277255c2324b2..928667ea4c1d440703db12933a29a456a522481a 100755 (executable)
@@ -16,6 +16,8 @@ source_files = ['SCA_ANDController.cpp',
                 'SCA_IObject.cpp',
                 'SCA_IScene.cpp',
                 'SCA_ISensor.cpp',
+               'SCA_JoystickManager.cpp',
+               'SCA_JoystickSensor.cpp',
                 'SCA_KeyboardManager.cpp',
                 'SCA_KeyboardSensor.cpp',
                 'SCA_LogicManager.cpp',
@@ -30,7 +32,9 @@ source_files = ['SCA_ANDController.cpp',
                 'SCA_RandomEventManager.cpp',
                 'SCA_RandomNumberGenerator.cpp',
                 'SCA_RandomSensor.cpp',
-                'SCA_TimeEventManager.cpp']
+                'SCA_TimeEventManager.cpp',
+               'Joystick/SCA_Joystick.cpp',
+               'Joystick/SCA_JoystickEvents.cpp']
 
 sca_gamelogic_env.Append (CPPPATH=['.',
                                    '#/source/kernel/gen_system',
@@ -39,5 +43,6 @@ sca_gamelogic_env.Append (CPPPATH=['.',
                                    '#/intern/moto/include'])
 
 sca_gamelogic_env.Append (CPPPATH = user_options_dict['PYTHON_INCLUDE'])
+sca_gamelogic_env.Append (CPPPATH = user_options_dict['SDL_INCLUDE'])
 
 sca_gamelogic_env.Library (target='#'+user_options_dict['BUILD_DIR']+'/lib/SCA_GameLogic', source=source_files)
index 36e1596ee85d1cb01980f4c53794bceeb7622a11..a5b5369a8269023383f351bcffc52aec18d7242e 100644 (file)
@@ -39,6 +39,8 @@
 #pragma warning (disable : 4786)
 #endif //WIN32
 
+#include "MT_assert.h"
+
 #include "KX_KetsjiEngine.h"
 #include "RAS_IPolygonMaterial.h"
 #include "KX_Scene.h"
@@ -53,6 +55,7 @@
 #include "SCA_MouseManager.h"
 #include "SCA_PropertyEventManager.h"
 #include "KX_Camera.h"
+#include "SCA_JoystickManager.h"
 
 #include "RAS_MeshObject.h"
 #include "RAS_IRasterizer.h"
@@ -135,6 +138,8 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
        KX_RayEventManager* raymgr = new KX_RayEventManager(m_logicmgr);
 
        KX_NetworkEventManager* netmgr = new KX_NetworkEventManager(m_logicmgr, ndi);
+       
+       SCA_JoystickManager *joymgr     = new SCA_JoystickManager(m_logicmgr);
 
        m_logicmgr->RegisterEventManager(alwaysmgr);
        m_logicmgr->RegisterEventManager(propmgr);
@@ -144,9 +149,10 @@ KX_Scene::KX_Scene(class SCA_IInputDevice* keyboarddevice,
        m_logicmgr->RegisterEventManager(rndmgr);
        m_logicmgr->RegisterEventManager(raymgr);
        m_logicmgr->RegisterEventManager(netmgr);
+       m_logicmgr->RegisterEventManager(joymgr);
 
        m_soundScene = new SND_Scene(adi);
-       assert (m_networkDeviceInterface != NULL);
+       MT_assert (m_networkDeviceInterface != NULL);
        m_networkScene = new NG_NetworkScene(m_networkDeviceInterface);
        
        m_rootnode = NULL;
@@ -187,7 +193,7 @@ KX_Scene::~KX_Scene()
 
        if (m_euthanasyobjects)
                m_euthanasyobjects->Release();
-       
+
        if (m_logicmgr)
                delete m_logicmgr;
 
@@ -204,7 +210,6 @@ KX_Scene::~KX_Scene()
        {
                delete m_bucketmanager;
        }
-       
        Py_DECREF(m_attrlist);
 }
 
index 74161a4d5b8581f56ec3262581d0cd19f22307b8..27f234f52a693851f9ee8218b2ee0dff11ff3345 100644 (file)
@@ -83,7 +83,7 @@ class RAS_MaterialBucket;
 class RAS_IPolyMaterial;
 class RAS_IRasterizer;
 class RAS_IRenderTools;
-
+class SCA_JoystickManager;
 /**
  * The KX_Scene holds all data for an independent scene. It relates
  * KX_Objects to the specific objects in the modules.
index 948e0135eeff9df75b262a3780c8cd7c4a458471..35cfa47700ea48a76f8f67fcd62b6020c05989ae 100644 (file)
@@ -109,5 +109,6 @@ if sys.platform == 'win32':
        ketsji_env.Append (CXXFLAGS = ['/GR'])
        ketsji_env.Append ( CCFLAGS =['/Ox'])
 ketsji_env.Append (CPPPATH = user_options_dict['PYTHON_INCLUDE'])
+ketsji_env.Append (CPPPATH = user_options_dict['SDL_INCLUDE'])
 
 ketsji_env.Library (target='#'+user_options_dict['BUILD_DIR']+'/lib/KX_ketsji', source=source_files)