[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15333] trunk/blender/source: BGE patch: Add level option on sensor and fix sensor reset.

Benoit Bolsee benoit.bolsee at online.be
Mon Jun 23 22:27:42 CEST 2008


Revision: 15333
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15333
Author:   ben2610
Date:     2008-06-23 22:26:48 +0200 (Mon, 23 Jun 2008)

Log Message:
-----------
BGE patch: Add level option on sensor and fix sensor reset.

Level option is now available on all sensors but is only implemented on 
mouse and keyboard sensors. The purpose of that option is to make
the sensor react on level rather than edge by default. It's only
applicable to state engine system when there is a state transition:
the sensor will generate a pulse if the condition is met from the
start of the state. Normally, the keyboard sensor generate a pulse
only when the key is pressed and not when the key is already pressed.
This patch allows to select this behavior.
The second part of the patch corrects the reset method for sensors
with inverted output.

Modified Paths:
--------------
    trunk/blender/source/blender/makesdna/DNA_sensor_types.h
    trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.cpp
    trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
    trunk/blender/source/gameengine/Ketsji/KX_RaySensor.cpp

Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2008-06-23 20:26:48 UTC (rev 15333)
@@ -144,7 +144,7 @@
 
 	/* just add here, to avoid align errors... */
 	short invert; /* Whether or not to invert the output. */
-	short freq2;  /* The negative pulsing frequency? Not used anymore... */
+	short level;  /* Whether the sensor is level base (edge by default) */
 	int pad;
 } bSensor;
 

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -251,6 +251,7 @@
 	bool neg_pulsemode = false;
 	int frequency = 0;
 	bool invert = false;
+	bool level = false;
 	
 	while(sens)
 	{
@@ -263,7 +264,8 @@
 		
 		frequency = sens->freq;
 		invert    = !(sens->invert == 0);
-		
+		level     = !(sens->level == 0);
+
 		switch (sens->type)
 		{
 		case  SENS_ALWAYS:
@@ -711,6 +713,7 @@
 									 neg_pulsemode, 
 									 frequency);
 			gamesensor->SetInvert(invert);
+			gamesensor->SetLevel(level);
 			gamesensor->SetName(STR_String(sens->name));			
 			
 			gameobj->AddSensor(gamesensor);

Modified: trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -55,6 +55,7 @@
 	m_links = 0;
 	m_suspended = false;
 	m_invert = false;
+	m_level = false;
 	m_pos_ticks = 0;
 	m_neg_ticks = 0;
 	m_pos_pulsemode = false;
@@ -95,7 +96,11 @@
 	m_invert = inv;
 }
 
+void SCA_ISensor::SetLevel(bool lvl) {
+	m_level = lvl;
+}
 
+
 float SCA_ISensor::GetNumber() {
 	return IsPositiveTrigger();
 }
@@ -177,6 +182,10 @@
 	 METH_VARARGS, GetInvert_doc},
 	{"setInvert", (PyCFunction) SCA_ISensor::sPySetInvert, 
 	 METH_VARARGS, SetInvert_doc},
+	{"getLevel", (PyCFunction) SCA_ISensor::sPyGetLevel, 
+	 METH_VARARGS, GetLevel_doc},
+	{"setLevel", (PyCFunction) SCA_ISensor::sPySetLevel, 
+	 METH_VARARGS, SetLevel_doc},
 	{NULL,NULL} //Sentinel
 };
 
@@ -328,6 +337,31 @@
 	Py_Return;
 }
 
+char SCA_ISensor::GetLevel_doc[] = 
+"getLevel()\n"
+"\tReturns whether this sensor is a level detector or a edge detector.\n"
+"\tIt makes a difference only in case of logic state transition (state actuator).\n"
+"\tA level detector will immediately generate a pulse if the condition for the\n"
+"\tdetector is met when entering the state. A edge detector will wait for an off-on\n"
+"\ttransition to occur.\n"
+"\tOnly some sensors implement this feature: keyboard.\n";
+PyObject* SCA_ISensor::PyGetLevel(PyObject* self, PyObject* args, PyObject* kwds)
+{
+	return BoolToPyArg(m_level);
+}
+
+char SCA_ISensor::SetLevel_doc[] = 
+"setLevel(level?)\n"
+"\t- level?: Detect level instead of edge? (KX_TRUE, KX_FALSE)\n"
+"\tSet whether to detect level or edge transition when entering a state.\n";
+PyObject* SCA_ISensor::PySetLevel(PyObject* self, PyObject* args, PyObject* kwds)
+{
+	int pyarg = 0;
+	if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; }
+	m_level = PyArgToBool(pyarg);
+	Py_Return;
+}
+
 char SCA_ISensor::GetUseNegPulseMode_doc[] = 
 "getUseNegPulseMode()\n"
 "\tReturns whether negative pulse mode is active.\n";

Modified: trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h	2008-06-23 20:26:48 UTC (rev 15333)
@@ -61,6 +61,9 @@
 	/** invert the output signal*/
 	bool m_invert;
 
+	/** detect level instead of edge*/
+	bool m_level;
+
 	/** Sensor must ignore updates? */
 	bool m_suspended;
 
@@ -105,6 +108,8 @@
 	virtual void Delete() { Release(); }
 	/** Set inversion of pulses on or off. */
 	void SetInvert(bool inv);
+	/** set the level detection on or off */
+	void SetLevel(bool lvl);
 
 	void RegisterToManager();
 	virtual float GetNumber();
@@ -134,6 +139,8 @@
 	KX_PYMETHOD_DOC(SCA_ISensor,SetUseNegPulseMode);
 	KX_PYMETHOD_DOC(SCA_ISensor,GetInvert);
 	KX_PYMETHOD_DOC(SCA_ISensor,SetInvert);
+	KX_PYMETHOD_DOC(SCA_ISensor,GetLevel);
+	KX_PYMETHOD_DOC(SCA_ISensor,SetLevel);
 
 };
 

Modified: trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_JoystickSensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -69,7 +69,7 @@
 
 void SCA_JoystickSensor::Init()
 {
-	m_istrig=0;
+	m_istrig=(m_invert)?1:0;
 }
 
 SCA_JoystickSensor::~SCA_JoystickSensor()

Modified: trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -77,7 +77,7 @@
 	// by the state engine. It reinitializes the sensor as if it was just created.
 	// However, if the target key is pressed when the sensor is reactivated, it
 	// will not generated an event (see remark in Evaluate()).
-	m_val = 0;
+	m_val = (m_invert)?1:0;
 }
 
 CValue* SCA_KeyboardSensor::GetReplica()
@@ -176,10 +176,10 @@
 				{
 					if (m_val == 0)
 					{
-						//see comment below
-						//m_val = 1;
-						//result = true;
-						;
+						if (m_level) {
+							m_val = 1;
+							result = true;
+						}
 					}
 				} else
 				{
@@ -229,15 +229,11 @@
 					{
 						if (m_val == 0)
 						{
-							//hmm, this abnormal situation may occur in the following cases:
-							//- the key was pressed while the scene was suspended
-							//- this is a new scene and the key is active from the start
-							//In the second case, it's dangerous to activate the sensor
-							//(think of a key to go to next scene)
-							//What we really need is a edge/level flag in the key sensor
-							//m_val = 1;
-							//result = true;
-							;
+							if (m_level) 
+							{
+								m_val = 1;
+								result = true;
+							}
 						}
 					}
 				}

Modified: trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_MouseSensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -83,7 +83,7 @@
 
 void SCA_MouseSensor::Init()
 {
-	m_val = 0; /* stores the latest attribute */
+	m_val = (m_invert)?1:0; /* stores the latest attribute */
 }
 
 SCA_MouseSensor::~SCA_MouseSensor() 
@@ -168,10 +168,11 @@
 					{
 						if (m_val == 0)
 						{
-							//dangerous
-							//m_val = 1;
-							//result = true;
-							;
+							if (m_level)
+							{
+								m_val = 1;
+								result = true;
+							}
 						}
 					} else
 					{

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PropertySensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -54,7 +54,6 @@
 	  m_checkpropval(propval),
 	  m_checkpropmaxval(propmaxval),
 	  m_checkpropname(propname),
-	  m_lastresult(false),
 	  m_range_expr(NULL)
 {
 	//CParser pars;
@@ -78,6 +77,7 @@
 void SCA_PropertySensor::Init()
 {
 	m_recentresult = false;
+	m_lastresult = m_invert?true:false;
 }
 
 void SCA_PropertySensor::PrecalculateRangeExpression()

Modified: trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/Ketsji/KX_MouseFocusSensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -74,7 +74,7 @@
 
 void KX_MouseFocusSensor::Init()
 {
-	m_mouse_over_in_previous_frame = false;
+	m_mouse_over_in_previous_frame = (m_invert)?true:false;
 	m_positive_event = false;
 	m_hitObject = 0;
 }
@@ -92,13 +92,13 @@
 		obHasFocus = ParentObjectHasFocus();
 		
 		if (!obHasFocus) {
+			m_positive_event = false;
 			if (m_mouse_over_in_previous_frame) {
-					m_positive_event = false;
-					result = true;
+				result = true;
 			} 
 		} else {
+			m_positive_event = true;
 			if (!m_mouse_over_in_previous_frame) {
-				m_positive_event = true;
 				result = true;
 			} 
 		} 

Modified: trunk/blender/source/gameengine/Ketsji/KX_RaySensor.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_RaySensor.cpp	2008-06-23 18:09:51 UTC (rev 15332)
+++ trunk/blender/source/gameengine/Ketsji/KX_RaySensor.cpp	2008-06-23 20:26:48 UTC (rev 15333)
@@ -69,7 +69,7 @@
 
 void KX_RaySensor::Init()
 {
-	m_bTriggered = false;
+	m_bTriggered = (m_invert)?true:false;
 	m_rayHit = false;
 	m_hitObject = NULL;
 }





More information about the Bf-blender-cvs mailing list