[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [53223] trunk/blender: BGE: Adding a Python interface for handling joysticks without needing logic bricks .

Mitchell Stokes mogurijin at gmail.com
Fri Dec 21 03:29:05 CET 2012


Revision: 53223
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=53223
Author:   moguri
Date:     2012-12-21 02:28:59 +0000 (Fri, 21 Dec 2012)
Log Message:
-----------
BGE: Adding a Python interface for handling joysticks without needing logic bricks. These new SCA_PythonJoystick objects can be accessed using bge.logic.joysticks, which is a list of joysticks. The length of the list is the number of maximum supported joysticks, and indexes that do not have a joystick available are set to None. This means joysticks can be checked for using something like:

if bge.logic.joysticks[0]:
    activate_player_one()

if bge.logic.joysticks[1]:
    activate_player_two()

etc..

The interface exposed by SCA_PythonJoystick is very similar to the joystick logic brick except for one key difference: axis values are normalized to a -1.0 to 1.0 range instead of -32767 to 32767, which is what the logic brick exposed.

Modified Paths:
--------------
    trunk/blender/doc/python_api/rst/bge.logic.rst
    trunk/blender/doc/python_api/rst/bge.types.rst
    trunk/blender/source/gameengine/GameLogic/CMakeLists.txt
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
    trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
    trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/Ketsji/KX_PythonInitTypes.cpp

Added Paths:
-----------
    trunk/blender/source/gameengine/GameLogic/SCA_PythonJoystick.cpp
    trunk/blender/source/gameengine/GameLogic/SCA_PythonJoystick.h

Modified: trunk/blender/doc/python_api/rst/bge.logic.rst
===================================================================
--- trunk/blender/doc/python_api/rst/bge.logic.rst	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/doc/python_api/rst/bge.logic.rst	2012-12-21 02:28:59 UTC (rev 53223)
@@ -125,6 +125,10 @@
 
    The current mouse wrapped in an :class:`~bge.types.SCA_PythonMouse` object.
 
+.. data:: joysticks
+
+   A list of attached joysticks. The list size it he maximum number of supported joysticks. If no joystick is available for a given slot, the slot is set to None.
+
 *****************
 General functions
 *****************

Modified: trunk/blender/doc/python_api/rst/bge.types.rst
===================================================================
--- trunk/blender/doc/python_api/rst/bge.types.rst	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/doc/python_api/rst/bge.types.rst	2012-12-21 02:28:59 UTC (rev 53223)
@@ -141,6 +141,74 @@
       
       :type: boolean
 
+.. class:: SCA_PythonJoystick(PyObjectPlus)
+
+   A Python interface to a joystick.
+
+   .. attribute:: name
+
+      The name assigned to the joystick by the operating system. (read-only)
+	  
+      :type: string
+
+   .. attribute:: activeButtons
+
+      A list of active button values. (read-only)
+	  
+      :type: list
+
+   .. attribute:: axisValues
+
+      The state of the joysticks axis as a list of values :data:`numAxis` long. (read-only).
+
+      :type: list of ints.
+
+      Each specifying the value of an axis between -1.0 and 1.0 depending on how far the axis is pushed, 0 for nothing.
+      The first 2 values are used by most joysticks and gamepads for directional control. 3rd and 4th values are only on some joysticks and can be used for arbitary controls.
+
+      * left:[-1.0, 0.0, ...]
+      * right:[1.0, 0.0, ...]
+      * up:[0.0, -1.0, ...]
+      * down:[0.0, 1.0, ...]
+
+   .. attribute:: hatValues
+
+      The state of the joysticks hats as a list of values :data:`numHats` long. (read-only).
+
+      :type: list of ints
+
+      Each specifying the direction of the hat from 1 to 12, 0 when inactive.
+
+      Hat directions are as follows...
+
+      * 0:None
+      * 1:Up
+      * 2:Right
+      * 4:Down
+      * 8:Left
+      * 3:Up - Right
+      * 6:Down - Right
+      * 12:Down - Left
+      * 9:Up - Left
+
+   .. attribute:: numAxis
+
+      The number of axes for the joystick at this index. (read-only).
+
+      :type: integer
+
+   .. attribute:: numButtons
+
+      The number of buttons for the joystick at this index. (read-only).
+
+      :type: integer
+
+   .. attribute:: numHats
+
+      The number of hats for the joystick at this index. (read-only).
+
+      :type: integer
+
 .. class:: SCA_IObject(CValue)
 
    This class has no python functions
@@ -3977,7 +4045,7 @@
 
       :type: list of ints.
 
-      Each spesifying the value of an axis between -32767 and 32767 depending on how far the axis is pushed, 0 for nothing.
+      Each specifying the value of an axis between -32767 and 32767 depending on how far the axis is pushed, 0 for nothing.
       The first 2 values are used by most joysticks and gamepads for directional control. 3rd and 4th values are only on some joysticks and can be used for arbitary controls.
 
       * left:[-32767, 0, ...]
@@ -4001,7 +4069,7 @@
 
       :type: list of ints
 
-      Each spesifying the direction of the hat from 1 to 12, 0 when inactive.
+      Each specifying the direction of the hat from 1 to 12, 0 when inactive.
 
       Hat directions are as follows...
 

Modified: trunk/blender/source/gameengine/GameLogic/CMakeLists.txt
===================================================================
--- trunk/blender/source/gameengine/GameLogic/CMakeLists.txt	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/source/gameengine/GameLogic/CMakeLists.txt	2012-12-21 02:28:59 UTC (rev 53223)
@@ -71,6 +71,7 @@
 	SCA_PropertyEventManager.cpp
 	SCA_PropertySensor.cpp
 	SCA_PythonController.cpp
+	SCA_PythonJoystick.cpp
 	SCA_PythonKeyboard.cpp
 	SCA_PythonMouse.cpp
 	SCA_RandomActuator.cpp
@@ -114,6 +115,7 @@
 	SCA_PropertyEventManager.h
 	SCA_PropertySensor.h
 	SCA_PythonController.h
+	SCA_PythonJoystick.h
 	SCA_PythonKeyboard.h
 	SCA_PythonMouse.h
 	SCA_RandomActuator.h

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.cpp	2012-12-21 02:28:59 UTC (rev 53223)
@@ -321,3 +321,12 @@
 	return 0;
 #endif /* WITH_SDL */
 }
+
+const char *SCA_Joystick::GetName()
+{
+#ifdef WITH_SDL
+	return SDL_JoystickName(m_joyindex);
+#else /* WITH_SDL */
+	return "";
+#endif /* WITH_SDL */
+}

Modified: trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h
===================================================================
--- trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/source/gameengine/GameLogic/Joystick/SCA_Joystick.h	2012-12-21 02:28:59 UTC (rev 53223)
@@ -192,6 +192,11 @@
 	 * Test if the joystick is connected
 	 */
 	int Connected(void);
+
+	/**
+	 * Name of the joytsick
+	 */
+	const char *GetName();
 };
 
 #endif

Modified: trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp	2012-12-21 00:11:45 UTC (rev 53222)
+++ trunk/blender/source/gameengine/GameLogic/SCA_JoystickManager.cpp	2012-12-21 02:28:59 UTC (rev 53223)
@@ -60,14 +60,16 @@
 
 void SCA_JoystickManager::NextFrame(double curtime,double deltatime)
 {
+	// We should always handle events in case we want to grab them with Python
+#ifdef WITH_SDL
+	SCA_Joystick::HandleEvents(); /* Handle all SDL Joystick events */
+#endif
+
 	if (m_sensors.Empty()) {
 		return;
 	}
 	else {
 		;
-#ifdef WITH_SDL
-		SCA_Joystick::HandleEvents(); /* Handle all SDL Joystick events */
-#endif
 		SG_DList::iterator<SCA_JoystickSensor> it(m_sensors);
 		for (it.begin();!it.end();++it)
 		{

Added: trunk/blender/source/gameengine/GameLogic/SCA_PythonJoystick.cpp
===================================================================
--- trunk/blender/source/gameengine/GameLogic/SCA_PythonJoystick.cpp	                        (rev 0)
+++ trunk/blender/source/gameengine/GameLogic/SCA_PythonJoystick.cpp	2012-12-21 02:28:59 UTC (rev 53223)
@@ -0,0 +1,184 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Contributor(s): Mitchell Stokes.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file gameengine/GameLogic/SCA_PythonJoystick.cpp
+ *  \ingroup gamelogic
+ */
+
+
+#include "SCA_PythonJoystick.h"
+#include "./Joystick/SCA_Joystick.h"
+#include "SCA_IInputDevice.h"
+
+//#include "GHOST_C-api.h"
+
+/* ------------------------------------------------------------------------- */
+/* Native functions                                                          */
+/* ------------------------------------------------------------------------- */
+
+SCA_PythonJoystick::SCA_PythonJoystick(SCA_Joystick* joystick)
+: PyObjectPlus(),
+m_joystick(joystick)
+{
+#ifdef WITH_PYTHON
+	m_event_dict = PyDict_New();
+#endif
+}
+
+SCA_PythonJoystick::~SCA_PythonJoystick()
+{
+#ifdef WITH_PYTHON
+	PyDict_Clear(m_event_dict);
+	Py_DECREF(m_event_dict);
+#endif
+}
+
+#ifdef WITH_PYTHON
+
+/* ------------------------------------------------------------------------- */
+/* Python functions                                                          */
+/* ------------------------------------------------------------------------- */
+PyObject* SCA_PythonJoystick::py_repr(void)
+{
+	return PyUnicode_FromString(m_joystick->GetName());
+}
+
+
+/* Integration hooks ------------------------------------------------------- */
+PyTypeObject SCA_PythonJoystick::Type = {
+	PyVarObject_HEAD_INIT(NULL, 0)
+	"SCA_PythonJoystick",
+	sizeof(PyObjectPlus_Proxy),
+	0,
+	py_base_dealloc,
+	0,
+	0,
+	0,
+	0,
+	py_base_repr,
+	0,0,0,0,0,0,0,0,0,
+	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+	0,0,0,0,0,0,0,
+	Methods,
+	0,
+	0,
+	&PyObjectPlus::Type,
+	0,0,0,0,0,0,
+	py_base_new
+};
+
+PyMethodDef SCA_PythonJoystick::Methods[] = {
+	{NULL,NULL} //Sentinel
+};
+
+PyAttributeDef SCA_PythonJoystick::Attributes[] = {
+	KX_PYATTRIBUTE_RO_FUNCTION("numButtons", SCA_PythonJoystick, pyattr_get_num_x),
+	KX_PYATTRIBUTE_RO_FUNCTION("numHats", SCA_PythonJoystick, pyattr_get_num_x),
+	KX_PYATTRIBUTE_RO_FUNCTION("numAxis", SCA_PythonJoystick, pyattr_get_num_x),
+	KX_PYATTRIBUTE_RO_FUNCTION("activeButtons", SCA_PythonJoystick, pyattr_get_active_buttons),
+	KX_PYATTRIBUTE_RO_FUNCTION("hatValues", SCA_PythonJoystick, pyattr_get_hat_values),
+	KX_PYATTRIBUTE_RO_FUNCTION("axisValues", SCA_PythonJoystick, pyattr_get_axis_values),
+	KX_PYATTRIBUTE_RO_FUNCTION("name", SCA_PythonJoystick, pyattr_get_name),
+	{ NULL }	//Sentinel
+};
+
+// Use one function for numAxis, numButtons, and numHats
+PyObject* SCA_PythonJoystick::pyattr_get_num_x(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+	SCA_PythonJoystick* self = static_cast<SCA_PythonJoystick*>(self_v);
+
+	if (strcmp(attrdef->m_name, "numButtons") == 0)
+		return PyLong_FromLong(self->m_joystick->GetNumberOfButtons());
+	else if (strcmp(attrdef->m_name, "numAxis") == 0)
+		return PyLong_FromLong(self->m_joystick->GetNumberOfAxes());
+	else if (strcmp(attrdef->m_name, "numHats") == 0)
+		return PyLong_FromLong(self->m_joystick->GetNumberOfHats());
+
+	// If we got here, we have a problem...
+	PyErr_SetString(PyExc_AttributeError, "invalid attribute");
+	return NULL;
+}
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list