Skip to content

Commit

Permalink
UPBGE: Use dictionary for SCA_KeyboardSensor.inputs.
Browse files Browse the repository at this point in the history
In same time the dictionary in SCA_PythonKeyboard returned by
events, active_events, inputs and activeInputs are not shared
to avoid messing when the user call inputs first and then later
activeInputs.
  • Loading branch information
panzergame committed Aug 25, 2017
1 parent 2a72166 commit ff92967
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ base class --- :class:`SCA_ISensor`

A list of pressed input keys that have either been pressed, or just released, or are active this frame. (read-only).

:type: list [[:ref:`keycode<keyboard-keys>`, :class:`SCA_InputEvent`], ...]
:type: dictionary {:ref:`keycode<keyboard-keys>`::class:`SCA_InputEvent`, ...}

.. attribute:: events

Expand Down
14 changes: 7 additions & 7 deletions source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,21 @@ PyObject *SCA_KeyboardSensor::pyattr_get_inputs(PyObjectPlus *self_v, const KX_P

SCA_IInputDevice* inputdev = ((SCA_KeyboardManager *)self->m_eventmgr)->GetInputDevice();

PyObject *resultlist = PyList_New(0);
PyObject *dict = PyDict_New();

for (int i=SCA_IInputDevice::BEGINKEY ; i<= SCA_IInputDevice::ENDKEY;i++)
{
SCA_InputEvent& input = inputdev->GetInput((SCA_IInputDevice::SCA_EnumInputs) i);
if (input.Find(SCA_InputEvent::ACTIVE))
{
PyObject *keypair = PyList_New(2);
PyList_SET_ITEM(keypair,0,PyLong_FromLong(i));
PyList_SET_ITEM(keypair,1,input.GetProxy());
PyList_Append(resultlist,keypair);
Py_DECREF(keypair);
PyObject *key = PyLong_FromLong(i);

PyDict_SetItem(dict, key, input.GetProxy());

Py_DECREF(key);
}
}
return resultlist;
return dict;
}

PyObject *SCA_KeyboardSensor::pyattr_get_events(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
Expand Down
36 changes: 14 additions & 22 deletions source/gameengine/GameLogic/SCA_PythonKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,10 @@ SCA_PythonKeyboard::SCA_PythonKeyboard(SCA_IInputDevice* keyboard)
: PyObjectPlus(),
m_keyboard(keyboard)
{
#ifdef WITH_PYTHON
m_event_dict = PyDict_New();
#endif
}

SCA_PythonKeyboard::~SCA_PythonKeyboard()
{
#ifdef WITH_PYTHON
PyDict_Clear(m_event_dict);
Py_DECREF(m_event_dict);
#endif
}

#ifdef WITH_PYTHON
Expand Down Expand Up @@ -118,6 +111,8 @@ PyObject *SCA_PythonKeyboard::pyattr_get_events(PyObjectPlus *self_v, const KX_P

ShowDeprecationWarning("keyboard.events", "keyboard.inputs");

PyObject *dict = PyDict_New();

for (int i=SCA_IInputDevice::BEGINKEY; i<=SCA_IInputDevice::ENDKEY; i++)
{
SCA_InputEvent& input = self->m_keyboard->GetInput((SCA_IInputDevice::SCA_EnumInputs)i);
Expand All @@ -132,39 +127,38 @@ PyObject *SCA_PythonKeyboard::pyattr_get_events(PyObjectPlus *self_v, const KX_P
PyObject *key = PyLong_FromLong(i);
PyObject *value = PyLong_FromLong(event);

PyDict_SetItem(self->m_event_dict, key, value);
PyDict_SetItem(dict, key, value);

Py_DECREF(key);
Py_DECREF(value);
}
Py_INCREF(self->m_event_dict);
return self->m_event_dict;
return dict;
}

PyObject *SCA_PythonKeyboard::pyattr_get_inputs(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);

PyObject *dict = PyDict_New();

for (int i=SCA_IInputDevice::BEGINKEY; i<=SCA_IInputDevice::ENDKEY; i++)
{
SCA_InputEvent& input = self->m_keyboard->GetInput((SCA_IInputDevice::SCA_EnumInputs)i);

PyObject *key = PyLong_FromLong(i);

PyDict_SetItem(self->m_event_dict, key, input.GetProxy());
PyDict_SetItem(dict, key, input.GetProxy());

Py_DECREF(key);
}
Py_INCREF(self->m_event_dict);
return self->m_event_dict;
return dict;
}

PyObject *SCA_PythonKeyboard::pyattr_get_active_inputs(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_PythonKeyboard* self = static_cast<SCA_PythonKeyboard*>(self_v);


PyDict_Clear(self->m_event_dict);
PyObject *dict = PyDict_New();

for (int i=SCA_IInputDevice::BEGINKEY; i<=SCA_IInputDevice::ENDKEY; i++)
{
Expand All @@ -173,13 +167,12 @@ PyObject *SCA_PythonKeyboard::pyattr_get_active_inputs(PyObjectPlus *self_v, con
if (input.Find(SCA_InputEvent::ACTIVE)) {
PyObject *key = PyLong_FromLong(i);

PyDict_SetItem(self->m_event_dict, key, input.GetProxy());
PyDict_SetItem(dict, key, input.GetProxy());

Py_DECREF(key);
}
}
Py_INCREF(self->m_event_dict);
return self->m_event_dict;
return dict;
}

PyObject *SCA_PythonKeyboard::pyattr_get_active_events(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
Expand All @@ -188,7 +181,7 @@ PyObject *SCA_PythonKeyboard::pyattr_get_active_events(PyObjectPlus *self_v, con

ShowDeprecationWarning("keyboard.active_events", "keyboard.activeInputs");

PyDict_Clear(self->m_event_dict);
PyObject *dict = PyDict_New();

for (int i=SCA_IInputDevice::BEGINKEY; i<=SCA_IInputDevice::ENDKEY; i++)
{
Expand All @@ -206,14 +199,13 @@ PyObject *SCA_PythonKeyboard::pyattr_get_active_events(PyObjectPlus *self_v, con
PyObject *key = PyLong_FromLong(i);
PyObject *value = PyLong_FromLong(event);

PyDict_SetItem(self->m_event_dict, key, value);
PyDict_SetItem(dict, key, value);

Py_DECREF(key);
Py_DECREF(value);
}
}
Py_INCREF(self->m_event_dict);
return self->m_event_dict;
return dict;
}

PyObject *SCA_PythonKeyboard::pyattr_get_text(PyObjectPlus *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
Expand Down
4 changes: 1 addition & 3 deletions source/gameengine/GameLogic/SCA_PythonKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class SCA_PythonKeyboard : public PyObjectPlus
Py_Header
private:
class SCA_IInputDevice *m_keyboard;
#ifdef WITH_PYTHON
PyObject *m_event_dict;
#endif

public:
SCA_PythonKeyboard(class SCA_IInputDevice* keyboard);
virtual ~SCA_PythonKeyboard();
Expand Down

0 comments on commit ff92967

Please sign in to comment.