[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19623] trunk/blender/source/gameengine: Added GameKeys.EventToCharacter(event, is_shift) so you can get the character that would be types when pressing a key.

Campbell Barton ideasman42 at gmail.com
Thu Apr 9 18:00:45 CEST 2009


Revision: 19623
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19623
Author:   campbellbarton
Date:     2009-04-09 18:00:45 +0200 (Thu, 09 Apr 2009)

Log Message:
-----------
Added GameKeys.EventToCharacter(event, is_shift) so you can get the character that would be types when pressing a key.

Last commit was made in the pydocs folder only, so this includes changes mentioned in rev ?\239?\187?\19119620.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
    trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/PyDoc/GameKeys.py

Modified: trunk/blender/source/gameengine/Expressions/PyObjectPlus.h
===================================================================
--- trunk/blender/source/gameengine/Expressions/PyObjectPlus.h	2009-04-09 13:32:14 UTC (rev 19622)
+++ trunk/blender/source/gameengine/Expressions/PyObjectPlus.h	2009-04-09 16:00:45 UTC (rev 19623)
@@ -129,7 +129,14 @@
 	 \
 	if(descr) { \
 		if (PyCObject_Check(descr)) { \
-			return py_set_attrdef((void *)this, (const PyAttributeDef*)PyCObject_AsVoidPtr(descr), value); \
+			const PyAttributeDef* attrdef= reinterpret_cast<const PyAttributeDef *>(PyCObject_AsVoidPtr(descr)); \
+			if (attrdef->m_access == KX_PYATTRIBUTE_RO) { \
+				PyErr_Format(PyExc_AttributeError, "\"%s\" is read only", PyString_AsString(attr)); \
+				return -1; \
+			} \
+			else { \
+				return py_set_attrdef((void *)this, attrdef, value); \
+			} \
 		} else { \
 			PyErr_Format(PyExc_AttributeError, "\"%s\" cannot be set", PyString_AsString(attr)); \
 			return -1; \

Modified: trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp	2009-04-09 13:32:14 UTC (rev 19622)
+++ trunk/blender/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp	2009-04-09 16:00:45 UTC (rev 19623)
@@ -352,152 +352,8 @@
 	}
 	
 }
-
-/**
- * Determine whether this character can be printed. We cannot use
- * the library functions here, because we need to test our own
- * keycodes. */
-bool SCA_KeyboardSensor::IsPrintable(int keyIndex)
-{
-	/* only print 
-	 * - numerals: KX_ZEROKEY to KX_NINEKEY
-	 * - alphas:   KX_AKEY to KX_ZKEY. 
-	 * - specials: KX_RETKEY, KX_PADASTERKEY, KX_PADCOMMAKEY to KX_PERIODKEY,
-	 *             KX_TABKEY , KX_SEMICOLONKEY to KX_RIGHTBRACKETKEY, 
-	 *             KX_PAD2 to KX_PADPLUSKEY
-	 * - delete and backspace: also printable in the sense that they modify 
-	 *                         the string
-	 * - retkey: should this be printable?
-	 * - virgule: prints a space... don't know which key that's supposed
-	 *   to be...
-	 */
-	if ( ((keyIndex >= SCA_IInputDevice::KX_ZEROKEY) 
-		  && (keyIndex <= SCA_IInputDevice::KX_NINEKEY))
-		 || ((keyIndex >= SCA_IInputDevice::KX_AKEY) 
-			 && (keyIndex <= SCA_IInputDevice::KX_ZKEY)) 
-		 || (keyIndex == SCA_IInputDevice::KX_SPACEKEY) 
-		 || (keyIndex == SCA_IInputDevice::KX_RETKEY)
-		 || (keyIndex == SCA_IInputDevice::KX_PADENTER)
-		 || (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) 
-		 || (keyIndex == SCA_IInputDevice::KX_TABKEY) 
-		 || ((keyIndex >= SCA_IInputDevice::KX_COMMAKEY) 
-			 && (keyIndex <= SCA_IInputDevice::KX_PERIODKEY)) 
-		 || ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) 
-			 && (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) 
-		 || ((keyIndex >= SCA_IInputDevice::KX_PAD2) 
-			 && (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) 
-		 || (keyIndex == SCA_IInputDevice::KX_DELKEY)
-		 || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY)
-		)
-	{
-		return true;
-	} else {
-		return false;
-	}
-}
-
-// this code looks ugly, please use an ordinary hashtable
-
-char SCA_KeyboardSensor::ToCharacter(int keyIndex, bool shifted)
-{
-	/* numerals */
-	if ( (keyIndex >= SCA_IInputDevice::KX_ZEROKEY) 
-		 && (keyIndex <= SCA_IInputDevice::KX_NINEKEY) ) {
-		if (shifted) {
-			char numshift[] = ")!@#$%^&*(";
-			return numshift[keyIndex - '0']; 
-		} else {
-			return keyIndex - SCA_IInputDevice::KX_ZEROKEY + '0'; 
-		}
-	}
 	
-	/* letters... always lowercase... is that desirable? */
-	if ( (keyIndex >= SCA_IInputDevice::KX_AKEY) 
-		 && (keyIndex <= SCA_IInputDevice::KX_ZKEY) ) {
-		if (shifted) {
-			return keyIndex - SCA_IInputDevice::KX_AKEY + 'A'; 
-		} else {
-			return keyIndex - SCA_IInputDevice::KX_AKEY + 'a'; 
-		}
-	}
-	
-	if (keyIndex == SCA_IInputDevice::KX_SPACEKEY) {
-		return ' ';
-	}
-	if (keyIndex == SCA_IInputDevice::KX_RETKEY || keyIndex == SCA_IInputDevice::KX_PADENTER) {
-		return '\n';
-	}
-	
-	
-	if (keyIndex == SCA_IInputDevice::KX_PADASTERKEY) {
-		return '*';
-	}
-	
-	if (keyIndex == SCA_IInputDevice::KX_TABKEY) {
-		return '\t';
-	}
-	
-	/* comma to period */
-	char commatoperiod[] = ",-.";
-	char commatoperiodshifted[] = "<_>";
-	if (keyIndex == SCA_IInputDevice::KX_COMMAKEY) {
-		if (shifted) {
-			return commatoperiodshifted[0];
-		} else {
-			return commatoperiod[0];
-		}
-	}
-	if (keyIndex == SCA_IInputDevice::KX_MINUSKEY) {
-		if (shifted) {
-			return commatoperiodshifted[1];
-		} else {
-			return commatoperiod[1];
-		}
-	}
-	if (keyIndex == SCA_IInputDevice::KX_PERIODKEY) {
-		if (shifted) {
-			return commatoperiodshifted[2];
-		} else {
-			return commatoperiod[2];
-		}
-	}
-	
-	/* semicolon to rightbracket */
-	char semicolontorightbracket[] = ";\'`/\\=[]";
-	char semicolontorightbracketshifted[] = ":\"~\?|+{}";
-	if ((keyIndex >= SCA_IInputDevice::KX_SEMICOLONKEY) 
-		&& (keyIndex <= SCA_IInputDevice::KX_RIGHTBRACKETKEY)) {
-		if (shifted) {
-			return semicolontorightbracketshifted[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
-		} else {
-			return semicolontorightbracket[keyIndex - SCA_IInputDevice::KX_SEMICOLONKEY];
-		}
-	}
-	
-	/* keypad2 to padplus */
-	char pad2topadplus[] = "246813579. 0- +";
-	if ((keyIndex >= SCA_IInputDevice::KX_PAD2) 
-		&& (keyIndex <= SCA_IInputDevice::KX_PADPLUSKEY)) { 
-		return pad2topadplus[keyIndex - SCA_IInputDevice::KX_PAD2];
-	}
-
-	return '!';
-}
-	
 /**
- * Tests whether this is a delete key.
- */	
-bool SCA_KeyboardSensor::IsDelete(int keyIndex)
-{
-	if ( (keyIndex == SCA_IInputDevice::KX_DELKEY)
-		 || (keyIndex == SCA_IInputDevice::KX_BACKSPACEKEY) ) {
-		return true;
-	} else {
-		return false;
-	}
-}
-
-/**
  * Tests whether shift is pressed
  */	
 bool SCA_KeyboardSensor::IsShifted(void)
@@ -654,7 +510,7 @@
 
 PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, PyObject* kwds)
 {
-	ShowDeprecationWarning("getPressedKeys()", "getEventList()");
+	ShowDeprecationWarning("getPressedKeys()", "events");
 
 	SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
 
@@ -672,20 +528,19 @@
 			if ((inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED)
 				|| (inevent.m_status == SCA_InputEvent::KX_JUSTRELEASED))
 			{
-				if (index < num)
-				{
-					PyObject* keypair = PyList_New(2);
-					PyList_SetItem(keypair,0,PyInt_FromLong(i));
-					PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
-					PyList_SetItem(resultlist,index,keypair);
-					index++;
-				}
+				PyObject* keypair = PyList_New(2);
+				PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
+				PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status));
+				PyList_SET_ITEM(resultlist,index,keypair);
+				index++;
+				
+				if (index >= num) /* should not happen */
+					break; 
 			}
-		}	
-		if (index>0) return resultlist;
+		}
 	}
 	
-	Py_RETURN_NONE;
+	return resultlist;
 }
 
 
@@ -696,9 +551,9 @@
 
 PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys(PyObject* self, PyObject* args, PyObject* kwds)
 {
-ShowDeprecationWarning("getCurrentlyPressedKeys()", "getEventList()");
+	ShowDeprecationWarning("getCurrentlyPressedKeys()", "events");
 
-SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
+	SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
 
 	int num = inputdev->GetNumActiveEvents();
 	PyObject* resultlist = PyList_New(num);
@@ -713,29 +568,28 @@
 			if ( (inevent.m_status == SCA_InputEvent::KX_ACTIVE)
 				 || (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED))
 			{
-				if (index < num)
-				{
-					PyObject* keypair = PyList_New(2);
-					PyList_SetItem(keypair,0,PyInt_FromLong(i));
-					PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
-					PyList_SetItem(resultlist,index,keypair);
-					index++;
-				}
+				PyObject* keypair = PyList_New(2);
+				PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
+				PyList_SET_ITEM(keypair,1,PyInt_FromLong(inevent.m_status));
+				PyList_SET_ITEM(resultlist,index,keypair);
+				index++;
+				
+				if (index >= num) /* should never happen */
+					break;
 			}
 		}
-
-		/* why?*/
-		if (index > 0) return resultlist;
 	}
 
-	Py_RETURN_NONE;
+	return resultlist;
 }
-//<---- Deprecated
 
+
 KX_PYMETHODDEF_DOC_NOARGS(SCA_KeyboardSensor, getEventList,
 "getEventList()\n"
 "\tGet the list of the keyboard events in this frame.\n")
 {
+	ShowDeprecationWarning("getEventList()", "events");
+	
 	SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
 
 	PyObject* resultlist = PyList_New(0);
@@ -746,34 +600,35 @@
 		if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
 		{
 			PyObject* keypair = PyList_New(2);
-			PyList_SetItem(keypair,0,PyInt_FromLong(i));
+			PyList_SET_ITEM(keypair,0,PyInt_FromLong(i));
 			PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status));
 			PyList_Append(resultlist,keypair);
 		}
 	}	
 	return resultlist;
 }
+//<---- Deprecated
 
 KX_PYMETHODDEF_DOC_O(SCA_KeyboardSensor, getKeyStatus,
 "getKeyStatus(keycode)\n"
 "\tGet the given key's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n")
 {
-	if (PyInt_Check(value))
-	{
-		int keycode = PyInt_AsLong(value);
-		
-		if ((keycode < SCA_IInputDevice::KX_BEGINKEY)
-			|| (keycode > SCA_IInputDevice::KX_ENDKEY)){
-			PyErr_SetString(PyExc_AttributeError, "invalid keycode specified!");
-			return NULL;
-		}
-		
-		SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
-		const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode);
-		return PyInt_FromLong(inevent.m_status);
+	if (!PyInt_Check(value)) {
+		PyErr_SetString(PyExc_ValueError, "getKeyStatus expected an int");
+		return NULL;
 	}
 	
-	Py_RETURN_NONE;
+	int keycode = PyInt_AsLong(value);
+	
+	if ((keycode < SCA_IInputDevice::KX_BEGINKEY)
+		|| (keycode > SCA_IInputDevice::KX_ENDKEY)){
+		PyErr_SetString(PyExc_AttributeError, "invalid keycode specified!");
+		return NULL;
+	}
+	
+	SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice();
+	const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode);
+	return PyInt_FromLong(inevent.m_status);
 }
 
 /* ------------------------------------------------------------------------- */
@@ -824,6 +679,7 @@
 };
 
 PyAttributeDef SCA_KeyboardSensor::Attributes[] = {
+	KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_KeyboardSensor, pyattr_get_events),
 	KX_PYATTRIBUTE_BOOL_RW("useAllKeys",SCA_KeyboardSensor,m_bAllKeys),

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list