[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31649] trunk/blender/source/gameengine: Committing patch [#23489] add method getEventStatus() to PythonKeyboard and PythonMouse by Geoff Gollmer (gomer)

Mitchell Stokes mogurijin at gmail.com
Mon Aug 30 02:18:51 CEST 2010


Revision: 31649
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31649
Author:   moguri
Date:     2010-08-30 02:18:50 +0200 (Mon, 30 Aug 2010)

Log Message:
-----------
Committing patch [#23489] add method getEventStatus() to PythonKeyboard and PythonMouse by Geoff Gollmer (gomer)

This patch makes SCA_PythonKeyboard.events and SCA_PythonMouse.events return a dictionary of all inputs (including inactive) instead of a list of active inputs.

Example usage:
import bge

if bge.logic.keyboard.events[bge.events.SPACEBAR] = bge.logic.KX_INPUT_JUST_ACTIVATED:
  print("Spacebar pressed!")

A couple of changes to the patch:
  * Wrap python stuff in #ifndef DISABLE_PYTHON
  * Clear and decref m_event_dict in the destructors

A couple of things not related to the patch:
  * Made member variables private
  * Removed a commented out (and no longer used) method (SCA_PythonMouse.show())

Modified Paths:
--------------
    trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h
    trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h
    trunk/blender/source/gameengine/PyDoc/bge.types.rst

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp	2010-08-29 21:53:12 UTC (rev 31648)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp	2010-08-30 00:18:50 UTC (rev 31649)
@@ -33,11 +33,17 @@
 : PyObjectPlus(),
 m_keyboard(keyboard)
 {
+#ifndef DISABLE_PYTHON
+	m_event_dict = PyDict_New();
+#endif
 }
 
 SCA_PythonKeyboard::~SCA_PythonKeyboard()
 {
-	/* intentionally empty */
+#ifndef DISABLE_PYTHON
+	PyDict_Clear(m_event_dict);
+	Py_DECREF(m_event_dict);
+#endif
 }
 
 #ifndef DISABLE_PYTHON
@@ -81,24 +87,15 @@
 PyObject* SCA_PythonKeyboard::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
 	SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);
-
-	PyObject* resultlist = PyList_New(0);
 	
 	for (int i=SCA_IInputDevice::KX_BEGINKEY; i<=SCA_IInputDevice::KX_ENDKEY; i++)
 	{
 		const SCA_InputEvent & inevent = self->m_keyboard->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
 		
-		
-		if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
-		{
-			PyObject* keypair = PyTuple_New(2);
-			PyTuple_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
-			PyTuple_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
-			PyList_Append(resultlist, keypair);
-		}
+		PyDict_SetItem(self->m_event_dict, PyLong_FromSsize_t(i), PyLong_FromSsize_t(inevent.m_status));
 	}
-
-	return resultlist;
+	Py_INCREF(self->m_event_dict);
+	return self->m_event_dict;
 }
 
 #endif

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h	2010-08-29 21:53:12 UTC (rev 31648)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonKeyboard.h	2010-08-30 00:18:50 UTC (rev 31649)
@@ -30,7 +30,11 @@
 class SCA_PythonKeyboard : public PyObjectPlus
 {
 	Py_Header;
+private:
 	class SCA_IInputDevice *m_keyboard;
+#ifndef DISABLE_PYTHON
+	PyObject* m_event_dict;
+#endif
 public:
 	SCA_PythonKeyboard(class SCA_IInputDevice* keyboard);
 	virtual ~SCA_PythonKeyboard();

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.cpp	2010-08-29 21:53:12 UTC (rev 31648)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.cpp	2010-08-30 00:18:50 UTC (rev 31649)
@@ -35,11 +35,17 @@
 m_mouse(mouse),
 m_canvas(canvas)
 {
+#ifndef DISABLE_PYTHON
+	m_event_dict = PyDict_New();
+#endif
 }
 
 SCA_PythonMouse::~SCA_PythonMouse()
 {
-	/* intentionally empty */
+#ifndef DISABLE_PYTHON
+	PyDict_Clear(m_event_dict);
+	Py_DECREF(m_event_dict);
+#endif
 }
 
 #ifndef DISABLE_PYTHON
@@ -72,7 +78,6 @@
 };
 
 PyMethodDef SCA_PythonMouse::Methods[] = {
-//	KX_PYMETHODTABLE(SCA_PythonMouse, show),
 	{NULL,NULL} //Sentinel
 };
 
@@ -86,26 +91,18 @@
 PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
 	SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
-
-	PyObject* resultlist = PyList_New(0);
-
+	
 	for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
 	{
 		const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
 		
-		
-		if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
-		{
-			PyObject* keypair = PyTuple_New(2);
-			PyTuple_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
-			PyTuple_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
-			PyList_Append(resultlist, keypair);
-		}
+		PyDict_SetItem(self->m_event_dict, PyLong_FromSsize_t(i), PyLong_FromSsize_t(inevent.m_status));
 	}
-
-	return resultlist;
+	Py_INCREF(self->m_event_dict);
+	return self->m_event_dict;
 }
 
+
 PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
 	SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);

Modified: trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h	2010-08-29 21:53:12 UTC (rev 31648)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonMouse.h	2010-08-30 00:18:50 UTC (rev 31649)
@@ -30,8 +30,12 @@
 class SCA_PythonMouse : public PyObjectPlus
 {
 	Py_Header;
+private:
 	class SCA_IInputDevice *m_mouse;
 	class RAS_ICanvas *m_canvas;
+#ifndef DISABLE_PYTHON
+	PyObject* m_event_dict;
+#endif
 public:
 	SCA_PythonMouse(class SCA_IInputDevice* mouse, class RAS_ICanvas* canvas);
 	virtual ~SCA_PythonMouse();

Modified: trunk/blender/source/gameengine/PyDoc/bge.types.rst
===================================================================
--- trunk/blender/source/gameengine/PyDoc/bge.types.rst	2010-08-29 21:53:12 UTC (rev 31648)
+++ trunk/blender/source/gameengine/PyDoc/bge.types.rst	2010-08-30 00:18:50 UTC (rev 31649)
@@ -71,9 +71,9 @@
 
    .. attribute:: events
 
-      A list of pressed keys that have either been pressed, or just released, or are active this frame. (read-only).
+      A dictionary containing the status of each keyboard event or key. (read-only).
 
-      :type: list [[:ref:`keycode<keyboard-keys>`, :ref:`status<input-status>`], ...]
+      :type: dictionary {:ref:`keycode<keyboard-keys>`::ref:`status<input-status>`, ...}
 
 .. class:: SCA_PythonMouse(PyObjectPlus)
 
@@ -81,9 +81,9 @@
 
    .. attribute:: events
 
-      a list of pressed buttons that have either been pressed, or just released, or are active this frame. (read-only).
+      a dictionary containing the status of each mouse event. (read-only).
 
-      :type: list [[:ref:`keycode<mouse-keys>`, :ref:`status<input-status>`], ...]
+      :type: dictionary {:ref:`keycode<mouse-keys>`::ref:`status<input-status>`, ...}
       
    .. attribute:: position
 





More information about the Bf-blender-cvs mailing list