[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19575] trunk/blender/source: BGE Joystick Sensor

Campbell Barton ideasman42 at gmail.com
Tue Apr 7 08:23:45 CEST 2009


Revision: 19575
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19575
Author:   campbellbarton
Date:     2009-04-07 08:23:45 +0200 (Tue, 07 Apr 2009)

Log Message:
-----------
BGE Joystick Sensor
- Raised limit of 2 axis to 4 axis pairs (4==8 joysticks axis pairs)
- Added a new Joystick Sensor type "Single Axis", so you can detect horizontal or vertical movement, rather then just Up/Down/Left/Right
- added Python attribute "axisSingle" so you can get the value from the selected axis (rather then getting it out of the axis list)
- renamed Py attribute "axisPosition" to "axisValues" (was never in a release)

If we need to increase the axis limit again just change JOYAXIS_MAX and the button limits.

Modified Paths:
--------------
    trunk/blender/source/blender/makesdna/DNA_sensor_types.h
    trunk/blender/source/blender/src/buttons_logic.c
    trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
    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/SCA_JoystickSensor.py

Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2009-04-07 05:01:42 UTC (rev 19574)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2009-04-07 06:23:45 UTC (rev 19575)
@@ -166,7 +166,8 @@
 	char type;
 	char joyindex;
 	short flag;
-	int axis;
+	short axis;
+	short axis_single;
 	int axisf;
 	int button;
 	int hat;
@@ -255,21 +256,23 @@
 
 #define SENS_JOY_ANY_EVENT		1
 
-#define SENS_JOY_BUTTON		0
+#define SENS_JOY_BUTTON		0			/* axis type */
 #define SENS_JOY_BUTTON_PRESSED	0
 #define SENS_JOY_BUTTON_RELEASED	1
 
-#define SENS_JOY_AXIS			1
+#define SENS_JOY_AXIS			1		/* axis type */
 #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			2		/* axis type */
 #define SENS_JOY_HAT_DIR		0
 
+#define SENS_JOY_AXIS_SINGLE	3		/* axis type */
 
+
 #define SENS_DELAY_REPEAT		1
 // should match JOYINDEX_MAX in SCA_JoystickDefines.h */
 #define SENS_JOY_MAXINDEX		8

Modified: trunk/blender/source/blender/src/buttons_logic.c
===================================================================
--- trunk/blender/source/blender/src/buttons_logic.c	2009-04-07 05:01:42 UTC (rev 19574)
+++ trunk/blender/source/blender/src/buttons_logic.c	2009-04-07 06:23:45 UTC (rev 19575)
@@ -1457,32 +1457,33 @@
 			&joy->joyindex, 0, SENS_JOY_MAXINDEX-1, 100, 0,
 			"Specify which joystick to use");			
 
-			str= "Type %t|Button %x0|Axis %x1|Hat%x2"; 
+			str= "Type %t|Button %x0|Axis %x1|Single Axis %x3|Hat%x2"; 
 			uiDefButC(block, MENU, B_REDR, str, xco+87, yco-44, 0.26 * (width-20), 19,
 				&joy->type, 0, 31, 0, 0,
 				"The type of event this joystick sensor is triggered on.");
 			
-			if (joy->flag & SENS_JOY_ANY_EVENT) {
-				switch (joy->type) {
-				case SENS_JOY_AXIS:	
-					str = "All Axis Events";
-					break;
-				case SENS_JOY_BUTTON:	
-					str = "All Button Events";
-					break;
-				default:
-					str = "All Hat Events";
-					break;
+			if (joy->type != SENS_JOY_AXIS_SINGLE) {
+				if (joy->flag & SENS_JOY_ANY_EVENT) {
+					switch (joy->type) {
+					case SENS_JOY_AXIS:	
+						str = "All Axis Events";
+						break;
+					case SENS_JOY_BUTTON:	
+						str = "All Button Events";
+						break;
+					default:
+						str = "All Hat Events";
+						break;
+					}
+				} else {
+					str = "All";
 				}
-			} else {
-				str = "All";
+				
+				uiDefButBitS(block, TOG, SENS_JOY_ANY_EVENT, B_REDR, str,
+					xco+10 + 0.475 * (width-20), yco-68, ((joy->flag & SENS_JOY_ANY_EVENT) ? 0.525 : 0.12) * (width-20), 19,
+					&joy->flag, 0, 0, 0, 0,
+					"Triggered by all events on this joysticks current type (axis/button/hat)");
 			}
-			
-			uiDefButBitS(block, TOG, SENS_JOY_ANY_EVENT, B_REDR, str,
-				xco+10 + 0.475 * (width-20), yco-68, ((joy->flag & SENS_JOY_ANY_EVENT) ? 0.525 : 0.12) * (width-20), 19,
-				&joy->flag, 0, 0, 0, 0,
-				"Triggered by all events on this joysticks current type (axis/button/hat)");
-			
 			if(joy->type == SENS_JOY_BUTTON)
 			{
 				if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
@@ -1493,8 +1494,8 @@
 			}
 			else if(joy->type == SENS_JOY_AXIS)
 			{
-				uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
-				&joy->axis, 1, 2.0, 100, 0,
+				uiDefButS(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
+				&joy->axis, 1, 4.0, 100, 0,
 				"Specify which axis pair to use, 1 is useually the main direction input.");
 
 				uiDefButI(block, NUM, 1, "Threshold:", xco+10 + 0.6 * (width-20),yco-44, 0.4 * (width-20), 19,
@@ -1508,7 +1509,7 @@
 					"The direction of the axis, use 'All Events' to recieve events on any direction");
 				}
 			}
-			else
+			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,
@@ -1520,6 +1521,15 @@
 					"Specify hat direction");
 				}
 			}
+			else { /* (joy->type == SENS_JOY_AXIS_SINGLE)*/
+				uiDefButS(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
+				&joy->axis_single, 1, 8.0, 100, 0,
+				"Specify a single axis (verticle/horizontal/other) to detect");
+				
+				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");
+			}
 			yco-= ysize;
 			break;
 		}

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2009-04-07 05:01:42 UTC (rev 19574)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2009-04-07 06:23:45 UTC (rev 19575)
@@ -708,6 +708,11 @@
 						hatf	= bjoy->hatf;
 						joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_HAT;
 						break;
+					case SENS_JOY_AXIS_SINGLE:
+						axis	= bjoy->axis_single;
+						prec	= bjoy->precision;
+						joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_AXIS_SINGLE;
+						break;
 					default:
 						printf("Error: bad case statement\n");
 						break;

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2009-04-07 05:01:42 UTC (rev 19574)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2009-04-07 06:23:45 UTC (rev 19575)
@@ -36,21 +36,19 @@
 SCA_Joystick::SCA_Joystick(short int index)
 	:
 	m_joyindex(index),
-	m_axis10(0),
-	m_axis11(0),
-	m_axis20(0),
-	m_axis21(0),
 	m_prec(3200),
 	m_buttonnum(-2),
 	m_axismax(-1),
-	m_hatdir(-2),
 	m_buttonmax(-1),
 	m_hatmax(-1),
+	m_hatdir(-2),
 	m_isinit(0),
 	m_istrig_axis(0),
 	m_istrig_button(0),
 	m_istrig_hat(0)
 {
+	for(int i=0; i<JOYAXIS_MAX; i++)
+		m_axis_array[i]= 0;
 #ifndef DISABLE_SDL
 	m_private = new PrivateData();
 #endif
@@ -125,49 +123,32 @@
 }
 
 
-bool SCA_Joystick::aAnyAxisIsPositive(int axis)
+bool SCA_Joystick::aAxisPairIsPositive(int axis)
 {
-	bool result;
-	int res = pAxisTest(axis);
-	res > m_prec? result = true: result = false;
-	return result;
+	return (pAxisTest(axis) > m_prec) ? true:false;
 }
 
-bool SCA_Joystick::aRightAxisIsPositive(int axis)
+bool SCA_Joystick::aAxisPairDirectionIsPositive(int axis, int dir)
 {
-	bool result;
-	int res = pGetAxis(axis,1);
-	res > m_prec? result = true: result = false;
-	return result;
-}
 
+	int res;
 
-bool SCA_Joystick::aUpAxisIsPositive(int axis)
-{
-	bool result;
-	int res = pGetAxis(axis,0);
-	res < -m_prec? result = true : result = false;
-	return result;
+	if (dir==JOYAXIS_UP || dir==JOYAXIS_DOWN)
+		res = pGetAxis(axis, 1);
+	else /* JOYAXIS_LEFT || JOYAXIS_RIGHT */
+		res = pGetAxis(axis, 0);
+	
+	if (dir==JOYAXIS_DOWN || dir==JOYAXIS_RIGHT)
+		return (res > m_prec) ? true : false;
+	else /* JOYAXIS_UP || JOYAXIS_LEFT */
+		return (res < -m_prec) ? true : false;
 }
 
-
-bool SCA_Joystick::aLeftAxisIsPositive(int axis)
+bool SCA_Joystick::aAxisIsPositive(int axis_single)
 {
-	bool result;
-	int res = pGetAxis(axis,1);
-	res < -m_prec ? result = true : result = false;
-	return result;
+	return abs(m_axis_array[axis_single]) > m_prec ? true:false;
 }
 
-
-bool SCA_Joystick::aDownAxisIsPositive(int axis)
-{
-	bool result;
-	int res = pGetAxis(axis,0);
-	res > m_prec ? result = true:result = false;
-	return result;
-}
-
 bool SCA_Joystick::aAnyButtonPressIsPositive(void)
 {
 	return (m_buttonnum==-2) ? false : true;
@@ -255,8 +236,12 @@
 		
 		/* must run after being initialized */
 		m_axismax =		SDL_JoystickNumAxes(m_private->m_joystick);
+		if (m_axismax > JOYAXIS_MAX) m_axismax= JOYAXIS_MAX;		/* very unlikely */
+		
 		m_buttonmax =	SDL_JoystickNumButtons(m_private->m_joystick);
 		m_hatmax =		SDL_JoystickNumHats(m_private->m_joystick);
+		
+		
 	}
 	return true;
 #endif
@@ -288,17 +273,8 @@
 void SCA_Joystick::pFillAxes()
 {
 #ifndef DISABLE_SDL
-	if(m_axismax == 1){
-		m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
-		m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
-	}else if(m_axismax > 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 = m_axis11 = m_axis20 = m_axis21 = 0;
-	}
+	for(int i=0; i<m_axismax; i++)
+		m_axis_array[i]= SDL_JoystickGetAxis(m_private->m_joystick, i);
 #endif
 }
 
@@ -306,10 +282,7 @@
 int SCA_Joystick::pGetAxis(int axisnum, int udlr)
 {
 #ifndef DISABLE_SDL
-	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 m_axis_array[(axisnum*2)+udlr];
 #endif
 	return 0;
 }
@@ -317,13 +290,9 @@
 int SCA_Joystick::pAxisTest(int axisnum)
 {
 #ifndef DISABLE_SDL
-	short i1,i2;
-	if(axisnum == 1) {
-		i1 = m_axis10;	i2 = m_axis11;
-	}
-	else if(axisnum == 2) {
-		i1 = m_axis20;	i2 = m_axis21;
-	}
+	short i1= m_axis_array[(axisnum*2)];
+	short i2= m_axis_array[(axisnum*2)+1];
+	
 	/* long winded way to do
 	 *   return MAX2(abs(i1), abs(i2))
 	 * avoid abs from math.h */
@@ -335,4 +304,3 @@
 	return 0;
 #endif
 }
-

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2009-04-07 05:01:42 UTC (rev 19574)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2009-04-07 06:23:45 UTC (rev 19575)
@@ -55,10 +55,8 @@
 	/* 
 	 *support for 2 axes 
 	 */
-
-	int m_axis10,m_axis11;
-	int m_axis20,m_axis21;
-
+	int m_axis_array[JOYAXIS_MAX];
+	
 	/*
 	 * Precision or range of the axes
 	 */
@@ -120,8 +118,11 @@
 	void OnButtonUp(SDL_Event *sdl_event);
 	void OnButtonDown(SDL_Event *sdl_event);
 	void OnNothing(SDL_Event *sdl_event);
+#if 0 /* not used yet */
 	void OnBallMotion(SDL_Event *sdl_event){}
 #endif
+		
+#endif
 	/*
 	 * Open the joystick
 	 */
@@ -139,12 +140,12 @@
 	void pFillButtons(void);
 
 	/*
-	 * returns m_axis10,m_axis11...
+	 * returns m_axis_array
 	 */
 
 	int pAxisTest(int axisnum);
 	/*
-	 * returns m_axis10,m_axis11...

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list