diff options
author | Mitchell Stokes <mogurijin@gmail.com> | 2012-12-21 06:28:59 +0400 |
---|---|---|
committer | Mitchell Stokes <mogurijin@gmail.com> | 2012-12-21 06:28:59 +0400 |
commit | f2f2b6153a2a818ca940a4df5b7dafc743ef2d2f (patch) | |
tree | a6562a6c389b7f3e99461dda1245b1037b78a020 /source/gameengine/Ketsji | |
parent | 26752e8b3a367a7c5161ad7418e910bb2ce6ee5d (diff) |
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.
Diffstat (limited to 'source/gameengine/Ketsji')
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInit.cpp | 33 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_PythonInitTypes.cpp | 2 |
2 files changed, 35 insertions, 0 deletions
diff --git a/source/gameengine/Ketsji/KX_PythonInit.cpp b/source/gameengine/Ketsji/KX_PythonInit.cpp index 996be97c474..df8f6eb44e8 100644 --- a/source/gameengine/Ketsji/KX_PythonInit.cpp +++ b/source/gameengine/Ketsji/KX_PythonInit.cpp @@ -92,6 +92,8 @@ extern "C" { #include "SCA_PropertySensor.h" #include "SCA_RandomActuator.h" #include "SCA_KeyboardSensor.h" /* IsPrintable, ToCharacter */ +#include "SCA_JoystickManager.h" /* JOYINDEX_MAX */ +#include "SCA_PythonJoystick.h" #include "SCA_PythonKeyboard.h" #include "SCA_PythonMouse.h" #include "KX_ConstraintActuator.h" @@ -151,6 +153,7 @@ static char gp_GamePythonPathOrig[FILE_MAX] = ""; // not super happy about this, static SCA_PythonKeyboard* gp_PythonKeyboard = NULL; static SCA_PythonMouse* gp_PythonMouse = NULL; +static SCA_PythonJoystick* gp_PythonJoysticks[JOYINDEX_MAX] = {NULL}; #endif // WITH_PYTHON static KX_Scene* gp_KetsjiScene = NULL; @@ -1420,6 +1423,22 @@ PyObject *initGameLogic(KX_KetsjiEngine *engine, KX_Scene* scene) // quick hack gp_PythonMouse = new SCA_PythonMouse(gp_KetsjiEngine->GetMouseDevice(), gp_Canvas); PyDict_SetItemString(d, "mouse", gp_PythonMouse->NewProxy(true)); + PyObject* joylist = PyList_New(JOYINDEX_MAX); + SCA_JoystickManager* joyevent = (SCA_JoystickManager*)gp_KetsjiScene->GetLogicManager()->FindEventManager(SCA_EventManager::JOY_EVENTMGR); + for (int i=0; i<JOYINDEX_MAX; ++i) { + SCA_Joystick* joy = joyevent->GetJoystickDevice(i); + if (joy && joy->Connected()) { + gp_PythonJoysticks[i] = new SCA_PythonJoystick(joy); + PyObject* tmp = gp_PythonJoysticks[i]->NewProxy(true); + Py_INCREF(tmp); + PyList_SET_ITEM(joylist, i, tmp); + } else { + Py_INCREF(Py_None); + PyList_SET_ITEM(joylist, i, Py_None); + } + } + PyDict_SetItemString(d, "joysticks", joylist); + ErrorObject = PyUnicode_FromString("GameLogic.error"); PyDict_SetItemString(d, "error", ErrorObject); Py_DECREF(ErrorObject); @@ -1937,6 +1956,13 @@ void exitGamePlayerPythonScripting() delete gp_PythonMouse; gp_PythonMouse = NULL; + for (int i=0; i<JOYINDEX_MAX; ++i) { + if (gp_PythonJoysticks[i]) { + delete gp_PythonJoysticks[i]; + gp_PythonJoysticks[i] = NULL; + } + } + /* since python restarts we cant let the python backup of the sys.path hang around in a global pointer */ restorePySysObjects(); /* get back the original sys.path and clear the backup */ @@ -1985,6 +2011,13 @@ void exitGamePythonScripting() delete gp_PythonMouse; gp_PythonMouse = NULL; + for (int i=0; i<JOYINDEX_MAX; ++i) { + if (gp_PythonJoysticks[i]) { + delete gp_PythonJoysticks[i]; + gp_PythonJoysticks[i] = NULL; + } + } + restorePySysObjects(); /* get back the original sys.path and clear the backup */ bpy_import_main_set(NULL); PyObjectPlus::ClearDeprecationWarning(); diff --git a/source/gameengine/Ketsji/KX_PythonInitTypes.cpp b/source/gameengine/Ketsji/KX_PythonInitTypes.cpp index 971730672db..10c210cf16e 100644 --- a/source/gameengine/Ketsji/KX_PythonInitTypes.cpp +++ b/source/gameengine/Ketsji/KX_PythonInitTypes.cpp @@ -81,6 +81,7 @@ #include "SCA_RandomSensor.h" #include "SCA_XNORController.h" #include "SCA_XORController.h" +#include "SCA_PythonJoystick.h" #include "SCA_PythonKeyboard.h" #include "SCA_PythonMouse.h" #include "KX_IpoActuator.h" @@ -250,6 +251,7 @@ void initPyTypes(void) PyType_Ready_Attr(dict, SCA_XNORController, init_getset); PyType_Ready_Attr(dict, SCA_XORController, init_getset); PyType_Ready_Attr(dict, SCA_IController, init_getset); + PyType_Ready_Attr(dict, SCA_PythonJoystick, init_getset); PyType_Ready_Attr(dict, SCA_PythonKeyboard, init_getset); PyType_Ready_Attr(dict, SCA_PythonMouse, init_getset); } |