[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20064] trunk/blender/source: BGE logic: new sensor "tap" option to generate automatically on/off pulses

Benoit Bolsee benoit.bolsee at online.be
Tue May 5 00:21:02 CEST 2009


Revision: 20064
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20064
Author:   ben2610
Date:     2009-05-05 00:21:02 +0200 (Tue, 05 May 2009)

Log Message:
-----------
BGE logic: new sensor "tap" option to generate automatically on/off pulses

When enabled, this option converts any positive trigger from the sensor
into a pair of positive+negative trigger, with the negative trigger sent
in the next frame. The negative trigger from the sensor are not passed
to the controller as the option automatically generates the negative triggers. 
>From the controller point of view, the sensor is positive only for 1 frame, 
even if the underlying sensor state remains positive.

The option interacts with the other sensor option in this way:
- Level option: tap option is mutually exclusive with level option. Both
  cannot be enabled at the same time.
- Invert option: tap option operates on the negative trigger of the 
  sensor, which are converted to positive trigger by the invert option.
  Hence, the controller will see the sensor positive for 1 frame when 
  the underlying sensor state turns negative. 
- Positive pulse option: tap option adds a negative trigger after each
  repeated positive pulse, unless the frequency option is 0, in which case
  positive pulse are generated on every frame as before, as long as the
  underlying sensor state is positive.
- Negative pulse option: this option is not compatible with tap option
  and is ignored when tap option is enabled.

Notes:
- Keyboard "All keys" is handled specially when tap option is set:
  There will be one pair of positive/negative trigger for each new 
  key press, regardless on how many keys are already pressed and there
  is no trigger when keys are released, regardless if keys are still
  pressed. 
  In case two keys are pressed in succesive frames, there will
  be 2 positive triggers and 1 negative trigger in the following frame.

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/SCA_ANDController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ExpressionController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ISensor.h
    trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_NANDController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_NORController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_ORController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_XNORController.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_XORController.cpp

Modified: trunk/blender/source/blender/makesdna/DNA_sensor_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/blender/makesdna/DNA_sensor_types.h	2009-05-04 22:21:02 UTC (rev 20064)
@@ -158,7 +158,8 @@
 	/* just add here, to avoid align errors... */
 	short invert; /* Whether or not to invert the output. */
 	short level;  /* Whether the sensor is level base (edge by default) */
-	int pad;
+	short tap;
+	short pad;
 } bSensor;
 
 typedef struct bJoystickSensor {

Modified: trunk/blender/source/blender/src/buttons_logic.c
===================================================================
--- trunk/blender/source/blender/src/buttons_logic.c	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/blender/src/buttons_logic.c	2009-05-04 22:21:02 UTC (rev 20064)
@@ -1026,6 +1026,19 @@
 	BIF_ThemeColorShade(col, medium?30:0);
 }
 
+
+static void verify_logicbutton_func(void *data1, void *data2)
+{
+	bSensor *sens= (bSensor*)data1;
+	
+	if(sens->level && sens->tap) {
+		if(data2 == &(sens->level))	sens->tap= 0;
+		else							sens->level= 0;
+		allqueue(REDRAWBUTSLOGIC, 0);
+	}
+}
+
+
 /**
  * Draws a toggle for pulse mode, a frequency field and a toggle to invert
  * the value of this sensor. Operates on the shared data block of sensors.
@@ -1036,30 +1049,39 @@
 								short y,
 								short w) 
 {
+	uiBut *but;
+	
 	/* Pulsing and frequency */
 	uiDefIconButBitS(block, TOG, SENS_PULSE_REPEAT, 1, ICON_DOTSUP,
-			 (short)(x + 10 + 0. * (w-20)), (short)(y - 21), (short)(0.15 * (w-20)), 19,
+			 (short)(x + 10 + 0. * (w-20)), (short)(y - 21), (short)(0.1 * (w-20)), 19,
 			 &sens->pulse, 0.0, 0.0, 0, 0,
 			 "Activate TRUE level triggering (pulse mode)");
 
 	uiDefIconButBitS(block, TOG, SENS_NEG_PULSE_MODE, 1, ICON_DOTSDOWN,
-			 (short)(x + 10 + 0.15 * (w-20)), (short)(y - 21), (short)(0.15 * (w-20)), 19,
+			 (short)(x + 10 + 0.1 * (w-20)), (short)(y - 21), (short)(0.1 * (w-20)), 19,
 			 &sens->pulse, 0.0, 0.0, 0, 0,
 			 "Activate FALSE level triggering (pulse mode)");
 	uiDefButS(block, NUM, 1, "f:",
-			 (short)(x + 10 + 0.3 * (w-20)), (short)(y - 21), (short)(0.275 * (w-20)), 19,
+			 (short)(x + 10 + 0.2 * (w-20)), (short)(y - 21), (short)(0.275 * (w-20)), 19,
 			 &sens->freq, 0.0, 10000.0, 0, 0,
 			 "Delay between repeated pulses (in logic tics, 0 = no delay)");
 	
 	/* value or shift? */
+	but= uiDefButS(block, TOG, 1, "Level",
+			 (short)(x + 10 + 0.5 * (w-20)), (short)(y - 21), (short)(0.20 * (w-20)), 19,
+			 &sens->level, 0.0, 0.0, 0, 0,
+			 "Level detector, trigger controllers of new states (only applicable upon logic state transition)");
+	uiButSetFunc(but, verify_logicbutton_func, sens, &(sens->level));
+	but= uiDefButS(block, TOG, 1, "Tap",
+			 (short)(x + 10 + 0.702 * (w-20)), (short)(y - 21), (short)(0.12 * (w-20)), 19,
+			 &sens->tap, 0.0, 0.0, 0, 0,
+			 "Trigger controllers only for an instant, even while the sensor remains true");
+	uiButSetFunc(but, verify_logicbutton_func, sens, &(sens->tap));
+	
 	uiDefButS(block, TOG, 1, "Inv",
 			 (short)(x + 10 + 0.85 * (w-20)), (short)(y - 21), (short)(0.15 * (w-20)), 19,
 			 &sens->invert, 0.0, 0.0, 0, 0,
 			 "Invert the level (output) of this sensor");
-	uiDefButS(block, TOG, 1, "Level",
-			 (short)(x + 10 + 0.65 * (w-20)), (short)(y - 21), (short)(0.20 * (w-20)), 19,
-			 &sens->level, 0.0, 0.0, 0, 0,
-			 "Level detector, trigger controllers of new states (only applicable upon logic state transition)");
 }
 
 static short draw_sensorbuttons(bSensor *sens, uiBlock *block, short xco, short yco, short width,char* objectname)

Modified: trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/gameengine/Converter/KX_ConvertSensors.cpp	2009-05-04 22:21:02 UTC (rev 20064)
@@ -255,6 +255,7 @@
 	int frequency = 0;
 	bool invert = false;
 	bool level = false;
+	bool tap = false;
 	
 	while(sens)
 	{
@@ -268,6 +269,7 @@
 		frequency = sens->freq;
 		invert    = !(sens->invert == 0);
 		level     = !(sens->level == 0);
+		tap       = !(sens->tap == 0);
 
 		switch (sens->type)
 		{
@@ -755,6 +757,7 @@
 									 frequency);
 			gamesensor->SetInvert(invert);
 			gamesensor->SetLevel(level);
+			gamesensor->SetTap(tap);
 			gamesensor->SetName(STR_String(sens->name));			
 			
 			gameobj->AddSensor(gamesensor);

Modified: trunk/blender/source/gameengine/GameLogic/SCA_ANDController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_ANDController.cpp	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/gameengine/GameLogic/SCA_ANDController.cpp	2009-05-04 22:21:02 UTC (rev 20064)
@@ -66,7 +66,7 @@
 	!(is==m_linkedsensors.end());is++)
 	{
 		SCA_ISensor* sensor = *is;
-		if (!sensor->IsPositiveTrigger())
+		if (!sensor->GetState())
 		{
 			sensorresult = false;
 			break;

Modified: trunk/blender/source/gameengine/GameLogic/SCA_ExpressionController.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_ExpressionController.cpp	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/gameengine/GameLogic/SCA_ExpressionController.cpp	2009-05-04 22:21:02 UTC (rev 20064)
@@ -161,7 +161,7 @@
 		SCA_ISensor* sensor = *is;
 		if (sensor->GetName() == identifiername)
 		{
-			identifierval = new CBoolValue(sensor->IsPositiveTrigger());
+			identifierval = new CBoolValue(sensor->GetState());
 			//identifierval = sensor->AddRef();
 		}
 

Modified: trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp	2009-05-04 21:49:25 UTC (rev 20063)
+++ trunk/blender/source/gameengine/GameLogic/SCA_ISensor.cpp	2009-05-04 22:21:02 UTC (rev 20064)
@@ -52,19 +52,21 @@
 SCA_ISensor::SCA_ISensor(SCA_IObject* gameobj,
 						 class SCA_EventManager* eventmgr,
 						 PyTypeObject* T ) :
-	SCA_ILogicBrick(gameobj,T),
-	m_triggered(false)
+	SCA_ILogicBrick(gameobj,T)
 {
 	m_links = 0;
 	m_suspended = false;
 	m_invert = false;
 	m_level = false;
+	m_tap = false;
 	m_reset = false;
 	m_pos_ticks = 0;
 	m_neg_ticks = 0;
 	m_pos_pulsemode = false;
 	m_neg_pulsemode = false;
 	m_pulse_frequency = 0;
+	m_state = false;
+	m_prev_state = false;
 	
 	m_eventmgr = eventmgr;
 }
@@ -104,9 +106,13 @@
 	m_level = lvl;
 }
 
+void SCA_ISensor::SetTap(bool tap) {
+	m_tap = tap;
+}
 
+
 double SCA_ISensor::GetNumber() {
-	return IsPositiveTrigger();
+	return GetState();
 }
 
 void SCA_ISensor::Suspend() {
@@ -143,6 +149,7 @@
 {
 	// sensor is just activated, initialize it
 	Init();
+	m_state = false;
 	m_newControllers.erase(m_newControllers.begin(), m_newControllers.end());
 	m_eventmgr->RegisterSensor(this);
 }
@@ -159,11 +166,20 @@
 	// don't evaluate a sensor that is not connected to any controller
 	if (m_links && !m_suspended) {
 		bool result = this->Evaluate(event);
+		// store the state for the rest of the logic system
+		m_prev_state = m_state;
+		m_state = this->IsPositiveTrigger();
 		if (result) {
-			logicmgr->AddActivatedSensor(this);	
-			// reset these counters so that pulse are synchronized with transition
-			m_pos_ticks = 0;
-			m_neg_ticks = 0;
+			// the sensor triggered this frame
+			if (m_state || !m_tap) {
+				logicmgr->AddActivatedSensor(this);	
+				// reset these counters so that pulse are synchronized with transition
+				m_pos_ticks = 0;
+				m_neg_ticks = 0;
+			} else
+			{
+				result = false;
+			}
 		} else
 		{
 			/* First, the pulsing behaviour, if pulse mode is
@@ -172,19 +188,20 @@
 			if (m_pos_pulsemode) {
 				m_pos_ticks++;
 				if (m_pos_ticks > m_pulse_frequency) {
-					if ( this->IsPositiveTrigger() )
+					if ( m_state )
 					{
 						logicmgr->AddActivatedSensor(this);
+						result = true;
 					}
 					m_pos_ticks = 0;
 				} 
 			}
-			
-			if (m_neg_pulsemode)
+			// negative pulse doesn't make sense in tap mode, skip
+			if (m_neg_pulsemode && !m_tap)
 			{
 				m_neg_ticks++;
 				if (m_neg_ticks > m_pulse_frequency) {
-					if (!this->IsPositiveTrigger() )
+					if (!m_state )
 					{
 						logicmgr->AddActivatedSensor(this);
 					}
@@ -192,6 +209,21 @@
 				}
 			}
 		}
+		if (m_tap)
+		{
+			// in tap mode: we send always a negative pulse immediately after a positive pulse
+			if (!result)
+			{
+				// the sensor did not trigger on this frame
+				if (m_prev_state)
+				{
+					// but it triggered on previous frame => send a negative pulse
+					logicmgr->AddActivatedSensor(this);	
+				}
+				// in any case, absence of trigger means sensor off
+				m_state = false;
+			}
+		}
 		if (!m_newControllers.empty())
 		{
 			if (!IsActive() && m_level)
@@ -221,7 +253,7 @@
 PyObject* SCA_ISensor::PyIsPositive()
 {
 	ShowDeprecationWarning("isPositive()", "the read-only positive property");
-	int retval = IsPositiveTrigger();
+	int retval = GetState();
 	return PyInt_FromLong(retval);
 }
 
@@ -385,6 +417,7 @@
 "\tThe sensor is put in its initial state as if it was just activated.\n")
 {
 	Init();
+	m_prev_state = false;
 	Py_RETURN_NONE;
 }
 
@@ -458,7 +491,8 @@
 	KX_PYATTRIBUTE_BOOL_RW("useNegPulseMode",SCA_ISensor,m_neg_pulsemode),
 	KX_PYATTRIBUTE_INT_RW("frequency",0,100000,true,SCA_ISensor,m_pulse_frequency),
 	KX_PYATTRIBUTE_BOOL_RW("invert",SCA_ISensor,m_invert),
-	KX_PYATTRIBUTE_BOOL_RW("level",SCA_ISensor,m_level),
+	KX_PYATTRIBUTE_BOOL_RW_CHECK("level",SCA_ISensor,m_level,pyattr_check_level),
+	KX_PYATTRIBUTE_BOOL_RW_CHECK("tap",SCA_ISensor,m_tap,pyattr_check_tap),
 	KX_PYATTRIBUTE_RO_FUNCTION("triggered", SCA_ISensor, pyattr_get_triggered),
 	KX_PYATTRIBUTE_RO_FUNCTION("positive", SCA_ISensor, pyattr_get_positive),
 	//KX_PYATTRIBUTE_TODO("links"),
@@ -493,7 +527,23 @@
 PyObject* SCA_ISensor::pyattr_get_positive(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
 	SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v);
-	return PyInt_FromLong(self->IsPositiveTrigger());
+	return PyInt_FromLong(self->GetState());
 }
 
+int SCA_ISensor::pyattr_check_level(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+	SCA_ISensor* self= static_cast<SCA_ISensor*>(self_v);
+	if (self->m_level)
+		self->m_tap = false;
+	return 0;
+}
+

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list