[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16957] trunk/blender: joystick update

Campbell Barton ideasman42 at gmail.com
Tue Oct 7 07:09:24 CEST 2008


Revision: 16957
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16957
Author:   campbellbarton
Date:     2008-10-07 07:09:24 +0200 (Tue, 07 Oct 2008)

Log Message:
-----------
joystick update
* use SDL events to trigger the sensor, trigger was being forced every tick. removed workaround for this problem.
* added "All Events" option, similar to all keys in the keyboard sensor.
  This means every event from the joystick will trigger the sensor, however only events from the selected type (axis/button/hat) is used to set the positive state of the sensor.
* Added python function sens_joy.GetButtonValues(), returns a list of pressed button indicies.
* Removed pressed/released option for joystick buttons, it was the same as the invert option.

Modified Paths:
--------------
    trunk/blender/intern/SoundSystem/sdl/SND_SDLCDDevice.cpp
    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_JoystickEvents.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.h

Modified: trunk/blender/intern/SoundSystem/sdl/SND_SDLCDDevice.cpp
===================================================================
--- trunk/blender/intern/SoundSystem/sdl/SND_SDLCDDevice.cpp	2008-10-07 00:10:17 UTC (rev 16956)
+++ trunk/blender/intern/SoundSystem/sdl/SND_SDLCDDevice.cpp	2008-10-07 05:09:24 UTC (rev 16957)
@@ -41,7 +41,9 @@
 #include "SND_SDLCDDevice.h"
 #include "SoundDefines.h"
 
+#ifndef DISABLE_SDL
 #include <SDL.h>
+#endif
 
 SND_SDLCDDevice::SND_SDLCDDevice() :
 	m_cdrom(NULL),

Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-10-07 00:10:17 UTC (rev 16956)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-10-07 05:09:24 UTC (rev 16957)
@@ -163,12 +163,12 @@
 
 typedef struct bJoystickSensor {
 	char name[32];
-	short type;
-	short joyindex;
+	char type;
+	char joyindex;
+	short flag;
 	int axis;
 	int axisf;
 	int button;
-	int buttonf;
 	int hat;
 	int hatf;
 	int precision;
@@ -252,6 +252,8 @@
 #define BL_SENS_MOUSE_MOUSEOVER      16
 #define BL_SENS_MOUSE_MOUSEOVER_ANY	 32
 
+#define SENS_JOY_ANY_EVENT		1
+
 #define SENS_JOY_BUTTON		0
 #define SENS_JOY_BUTTON_PRESSED	0
 #define SENS_JOY_BUTTON_RELEASED	1
@@ -266,6 +268,7 @@
 #define SENS_JOY_HAT			2
 #define SENS_JOY_HAT_DIR		0
 
+
 #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	2008-10-07 00:10:17 UTC (rev 16956)
+++ trunk/blender/source/blender/src/buttons_logic.c	2008-10-07 05:09:24 UTC (rev 16957)
@@ -1447,29 +1447,33 @@
 
 			joy= sens->data;
 
-			uiDefButS(block, NUM, 1, "Index:", xco+10, yco-44, 0.6 * (width-120), 19,
+			uiDefButC(block, NUM, 1, "Index:", xco+10, yco-44, 0.33 * (width-20), 19,
 			&joy->joyindex, 0, SENS_JOY_MAXINDEX-1, 100, 0,
 			"Specify which joystick to use");			
 
 			str= "Type %t|Button %x0|Axis %x1|Hat%x2"; 
-			uiDefButS(block, MENU, B_REDR, str, xco+87, yco-44, 0.6 * (width-150), 19,
+			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.");
 			
+			uiDefButBitS(block, TOG, SENS_JOY_ANY_EVENT, B_REDR,
+				(joy->flag & SENS_JOY_ANY_EVENT) ? "All Events" : "All",
+				xco+10 + 0.5 * (width-20), yco-68, ((joy->flag & SENS_JOY_ANY_EVENT) ? 0.5 : 0.098) * (width-20), 19,
+				&joy->flag, 0, 0, 0, 0,
+				"Trigger from all events of the current (axis/button/hat)");
+			
+			
 			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.");
+				if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
+					uiDefButI(block, NUM, 1, "Number:", xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
+					&joy->button, 0, 18, 100, 0,
+					"Specify which button to use");
+				}
 			}
 			else if(joy->type == SENS_JOY_AXIS)
 			{
-				uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
+				uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
 				&joy->axis, 1, 2.0, 100, 0,
 				"Specify which axis to use");
 
@@ -1477,20 +1481,24 @@
 				&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");
+				if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
+					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,
+				uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (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");
+				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");
+				}
 			}
 			yco-= ysize;
 			break;

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-10-07 00:10:17 UTC (rev 16956)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-10-07 05:09:24 UTC (rev 16957)
@@ -685,7 +685,6 @@
 					int axis	=0;
 					int axisf	=0;
 					int button	=0;
-					int buttonf =0; 
 					int hat		=0; 
 					int hatf	=0;
 					int prec	=0;
@@ -700,7 +699,6 @@
 						break;
 					case SENS_JOY_BUTTON:
 						button	= bjoy->button;
-						buttonf	= bjoy->buttonf;
 						joysticktype  = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
 						break;
 					case SENS_JOY_HAT:
@@ -719,8 +717,9 @@
 						joysticktype,
 						axis,axisf,
 						prec,
-						button,buttonf,
-						hat,hatf);
+						button,
+						hat,hatf,
+						(bjoy->flag & SENS_JOY_ANY_EVENT));
 				} 
 				else
 				{

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2008-10-07 00:10:17 UTC (rev 16956)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2008-10-07 05:09:24 UTC (rev 16957)
@@ -24,7 +24,9 @@
  *
  * ***** END GPL LICENSE BLOCK *****
  */
+#ifndef DISABLE_SDL
 #include <SDL.h>
+#endif
 
 #include "SCA_Joystick.h"
 #include "SCA_JoystickPrivate.h"
@@ -40,7 +42,10 @@
 	m_buttonnum(-2),
 	m_hatdir(-2),
 	m_isinit(0),
-	m_istrig(0)
+	m_istrig(0),
+	m_axismax(-1),
+	m_buttonmax(-1),
+	m_hatmax(-1)
 {
 #ifndef DISABLE_SDL
 	m_private = new PrivateData();
@@ -116,12 +121,19 @@
 }
 
 
+bool SCA_Joystick::aAnyAxisIsPositive(int axis)
+{
+	bool result;
+	int res = pAxisTest(axis);
+	res > m_prec? result = true: result = false;
+	return result;
+}
+
 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;
 }
 
@@ -131,7 +143,6 @@
 	bool result;
 	int res = pGetAxis(axis,0);
 	res < -m_prec? result = true : result = false;
-	m_istrig = result;
 	return result;
 }
 
@@ -141,7 +152,6 @@
 	bool result;
 	int res = pGetAxis(axis,1);
 	res < -m_prec ? result = true : result = false;
-	m_istrig = result;
 	return result;
 }
 
@@ -151,11 +161,19 @@
 	bool result;
 	int res = pGetAxis(axis,0);
 	res > m_prec ? result = true:result = false;
-	m_istrig = result;
 	return result;
 }
 
+bool SCA_Joystick::aAnyButtonPressIsPositive(void)
+{
+	return (m_buttonnum==-2) ? false : true;
+}
 
+bool SCA_Joystick::aAnyButtonReleaseIsPositive(void)
+{
+	return (m_buttonnum==-2) ? true : false;
+}
+
 bool SCA_Joystick::aButtonPressIsPositive(int button)
 {
 #ifdef DISABLE_SDL
@@ -163,7 +181,6 @@
 #else
 	bool result;
 	SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
-	m_istrig = result;
 	return result;
 #endif
 }
@@ -176,7 +193,6 @@
 #else
 	bool result;
 	SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
-	m_istrig = result;
 	return result;
 #endif
 }
@@ -187,27 +203,9 @@
 	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){
@@ -218,52 +216,19 @@
 
 int SCA_Joystick::GetNumberOfAxes()
 {
-#ifdef DISABLE_SDL
-	return -1;
-#else
-	int number;
-	if(m_isinit){
-		if(m_private->m_joystick){
-			number = SDL_JoystickNumAxes(m_private->m_joystick);
-			return number;
-		}
-	}
-	return -1;
-#endif
+	return m_axismax;
 }
 
 
 int SCA_Joystick::GetNumberOfButtons()
 {
-#ifdef DISABLE_SDL
-	return -1;
-#else
-	int number;
-	if(m_isinit){
-		if(m_private->m_joystick){
-			number = SDL_JoystickNumButtons(m_private->m_joystick);
-			return number;
-		}
-	}
-	return -1;
-#endif
+	return m_buttonmax;
 }
 
 
 int SCA_Joystick::GetNumberOfHats()
 {
-#ifdef DISABLE_SDL
-	return -1;
-#else
-	int number;
-	if(m_isinit){
-		if(m_private->m_joystick){
-			number = SDL_JoystickNumHats(m_private->m_joystick);
-			return number;
-		}
-	}
-	return -1;
-#endif
+	return m_hatmax;
 }
 
 bool SCA_Joystick::CreateJoystickDevice(void)
@@ -280,9 +245,14 @@
 
 		m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
 		SDL_JoystickEventState(SDL_ENABLE);
-	
-		echo("Joystick " << m_joyindex << " initialized");
 		m_isinit = true;
+		
+		echo("Joystick " << m_joyindex << " initialized");
+		
+		/* must run after being initialized */
+		m_axismax =		SDL_JoystickNumAxes(m_private->m_joystick);
+		m_buttonmax =	SDL_JoystickNumButtons(m_private->m_joystick);
+		m_hatmax =		SDL_JoystickNumHats(m_private->m_joystick);
 	}
 	return true;
 #endif
@@ -314,10 +284,10 @@
 void SCA_Joystick::pFillAxes()
 {
 #ifndef DISABLE_SDL
-	if(GetNumberOfAxes() == 1){
+	if(m_axismax == 1){
 		m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
 		m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
-	}else if(GetNumberOfAxes() > 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);
@@ -340,3 +310,13 @@
 	return 0;
 }

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list