diff --git a/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp b/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp index 7951a749254..3d5d3568335 100644 --- a/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp +++ b/source/gameengine/GameLogic/SCA_PythonKeyboard.cpp @@ -33,11 +33,17 @@ SCA_PythonKeyboard::SCA_PythonKeyboard(SCA_IInputDevice* keyboard) : 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 @@ PyAttributeDef SCA_PythonKeyboard::Attributes[] = { PyObject* SCA_PythonKeyboard::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { SCA_PythonKeyboard* self = static_cast(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 diff --git a/source/gameengine/GameLogic/SCA_PythonKeyboard.h b/source/gameengine/GameLogic/SCA_PythonKeyboard.h index 0b353ac444c..260835155e6 100644 --- a/source/gameengine/GameLogic/SCA_PythonKeyboard.h +++ b/source/gameengine/GameLogic/SCA_PythonKeyboard.h @@ -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(); diff --git a/source/gameengine/GameLogic/SCA_PythonMouse.cpp b/source/gameengine/GameLogic/SCA_PythonMouse.cpp index 041a0169879..936ee2ff5c4 100644 --- a/source/gameengine/GameLogic/SCA_PythonMouse.cpp +++ b/source/gameengine/GameLogic/SCA_PythonMouse.cpp @@ -35,11 +35,17 @@ SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas) 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 @@ PyTypeObject SCA_PythonMouse::Type = { }; PyMethodDef SCA_PythonMouse::Methods[] = { -// KX_PYMETHODTABLE(SCA_PythonMouse, show), {NULL,NULL} //Sentinel }; @@ -86,26 +91,18 @@ PyAttributeDef SCA_PythonMouse::Attributes[] = { PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) { SCA_PythonMouse* self = static_cast(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(self_v); diff --git a/source/gameengine/GameLogic/SCA_PythonMouse.h b/source/gameengine/GameLogic/SCA_PythonMouse.h index c73e6683fc8..9e1085e6bec 100644 --- a/source/gameengine/GameLogic/SCA_PythonMouse.h +++ b/source/gameengine/GameLogic/SCA_PythonMouse.h @@ -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(); diff --git a/source/gameengine/PyDoc/bge.types.rst b/source/gameengine/PyDoc/bge.types.rst index 028b4fa72f3..96ba8d278f1 100644 --- a/source/gameengine/PyDoc/bge.types.rst +++ b/source/gameengine/PyDoc/bge.types.rst @@ -71,9 +71,9 @@ Game Engine bge.types Module .. 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`, :ref:`status`], ...] + :type: dictionary {:ref:`keycode`::ref:`status`, ...} .. class:: SCA_PythonMouse(PyObjectPlus) @@ -81,9 +81,9 @@ Game Engine bge.types Module .. 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`, :ref:`status`], ...] + :type: dictionary {:ref:`keycode`::ref:`status`, ...} .. attribute:: position