diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2008-12-29 19:36:58 +0300 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2008-12-29 19:36:58 +0300 |
commit | 1c663bbc7e53cda1fe35579302574b0d98aa8db3 (patch) | |
tree | 4c0d3be6453932c01a9dc0fee0f820275cd6701d /source/gameengine/GameLogic | |
parent | d91daaa5f690645153adf647c371262c9c6cb009 (diff) |
First batch of GE API cleanup.
The principle is to replace most get/set methods of logic bricks by direct property access.
To make porting of game code easier, the properties have usually the same type and use than
the return values/parameters of the get/set methods.
More details on http://wiki.blender.org/index.php/GameEngineDev/Python_API_Clean_Up
Old methods are still available but will produce deprecation warnings on the console:
"<method> is deprecated, use the <property> property instead"
You can avoid these messages by turning on the "Ignore deprecation warnings" option in Game menu.
PyDoc is updated to include the new properties and display a deprecation warning
for the get/set methods that are being deprecated.
Diffstat (limited to 'source/gameengine/GameLogic')
20 files changed, 870 insertions, 216 deletions
diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp index 7c37b237d60..5ad28dbf329 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp @@ -149,21 +149,44 @@ PyParentObject SCA_ActuatorSensor::Parents[] = { }; PyMethodDef SCA_ActuatorSensor::Methods[] = { + //Deprecated functions ------> {"getActuator", (PyCFunction) SCA_ActuatorSensor::sPyGetActuator, METH_NOARGS, (PY_METHODCHAR)GetActuator_doc}, {"setActuator", (PyCFunction) SCA_ActuatorSensor::sPySetActuator, METH_VARARGS, (PY_METHODCHAR)SetActuator_doc}, + //<----- Deprecated {NULL,NULL} //Sentinel }; PyObject* SCA_ActuatorSensor::_getattr(const STR_String& attr) { + if (attr == "actuator") { + return PyString_FromString(m_checkactname); + } _getattr_up(SCA_ISensor); /* implicit return! */ } +int SCA_ActuatorSensor::_setattr(const STR_String& attr, PyObject *value) { + if (PyString_Check(value)) { + char* sval = PyString_AsString(value); + if (attr == "actuator") { + SCA_IActuator* act = GetParent()->FindActuator(STR_String(sval)); + if (act) { + m_checkactname = sval; + m_actuator = act; + return 0; + } + PyErr_SetString(PyExc_AttributeError, "string does not correspond to an actuator"); + return 1; + } + } + return SCA_ISensor::_setattr(attr, value); +} + /* 3. getActuator */ const char SCA_ActuatorSensor::GetActuator_doc[] = "getActuator()\n" "\tReturn the Actuator with which the sensor operates.\n"; PyObject* SCA_ActuatorSensor::PyGetActuator(PyObject* self) { + ShowDeprecationWarning("getActuator()", "the actuator property"); return PyString_FromString(m_checkactname); } @@ -175,6 +198,7 @@ const char SCA_ActuatorSensor::SetActuator_doc[] = "\tof this name, the call is ignored.\n"; PyObject* SCA_ActuatorSensor::PySetActuator(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setActuator()", "the actuator property"); /* We should query whether the name exists. Or should we create a prop */ /* on the fly? */ char *actNameArg = NULL; diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.h b/source/gameengine/GameLogic/SCA_ActuatorSensor.h index a71145f6852..4fe4700602c 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.h +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.h @@ -62,6 +62,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); /* 3. setProperty */ KX_PYMETHOD_DOC(SCA_ActuatorSensor,SetActuator); diff --git a/source/gameengine/GameLogic/SCA_DelaySensor.cpp b/source/gameengine/GameLogic/SCA_DelaySensor.cpp index f15d4c7249f..362483b58a2 100644 --- a/source/gameengine/GameLogic/SCA_DelaySensor.cpp +++ b/source/gameengine/GameLogic/SCA_DelaySensor.cpp @@ -158,6 +158,7 @@ PyParentObject SCA_DelaySensor::Parents[] = { }; PyMethodDef SCA_DelaySensor::Methods[] = { + //Deprecated functions ------> /* setProperty */ {"setDelay", (PyCFunction) SCA_DelaySensor::sPySetDelay, METH_VARARGS, (PY_METHODCHAR)SetDelay_doc}, {"setDuration", (PyCFunction) SCA_DelaySensor::sPySetDuration, METH_VARARGS, (PY_METHODCHAR)SetDuration_doc}, @@ -166,13 +167,51 @@ PyMethodDef SCA_DelaySensor::Methods[] = { {"getDelay", (PyCFunction) SCA_DelaySensor::sPyGetDelay, METH_NOARGS, (PY_METHODCHAR)GetDelay_doc}, {"getDuration", (PyCFunction) SCA_DelaySensor::sPyGetDuration, METH_NOARGS, (PY_METHODCHAR)GetDuration_doc}, {"getRepeat", (PyCFunction) SCA_DelaySensor::sPyGetRepeat, METH_NOARGS, (PY_METHODCHAR)GetRepeat_doc}, + //<----- Deprecated {NULL,NULL} //Sentinel }; PyObject* SCA_DelaySensor::_getattr(const STR_String& attr) { + if (attr == "delay") { + return PyInt_FromLong(m_delay); + } + if (attr == "duration") { + return PyInt_FromLong(m_duration); + } + if (attr == "repeat") { + return PyInt_FromLong(m_repeat); + } _getattr_up(SCA_ISensor); } +int SCA_DelaySensor::_setattr(const STR_String& attr, PyObject *value) { + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); + if (attr == "delay") { + if (ival < 0) { + PyErr_SetString(PyExc_ValueError, "Delay cannot be negative"); + return 1; + } + m_delay = ival; + return 0; + } + if (attr == "duration") { + if (ival < 0) { + PyErr_SetString(PyExc_ValueError, "Duration cannot be negative"); + return 1; + } + m_duration = ival; + return 0; + } + if (attr == "repeat") { + m_repeat = (ival != 0); + return 0; + } + } + return SCA_ISensor::_setattr(attr, value); +} + + const char SCA_DelaySensor::SetDelay_doc[] = "setDelay(delay)\n" "\t- delay: length of the initial OFF period as number of frame\n" @@ -180,6 +219,7 @@ const char SCA_DelaySensor::SetDelay_doc[] = "\tSet the initial delay before the positive trigger\n"; PyObject* SCA_DelaySensor::PySetDelay(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setDelay()", "the delay property"); int delay; if(!PyArg_ParseTuple(args, "i", &delay)) { @@ -201,6 +241,7 @@ const char SCA_DelaySensor::SetDuration_doc[] = "\tIf > 0, a negative trigger is fired at the end of the ON pulse.\n"; PyObject* SCA_DelaySensor::PySetDuration(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setDuration()", "the duration property"); int duration; if(!PyArg_ParseTuple(args, "i", &duration)) { @@ -221,6 +262,7 @@ const char SCA_DelaySensor::SetRepeat_doc[] = "\tSet the sensor repeat mode\n"; PyObject* SCA_DelaySensor::PySetRepeat(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setRepeat()", "the repeat property"); int repeat; if(!PyArg_ParseTuple(args, "i", &repeat)) { @@ -235,6 +277,7 @@ const char SCA_DelaySensor::GetDelay_doc[] = "\tReturn the delay parameter value\n"; PyObject* SCA_DelaySensor::PyGetDelay(PyObject* self) { + ShowDeprecationWarning("getDelay()", "the delay property"); return PyInt_FromLong(m_delay); } @@ -243,6 +286,7 @@ const char SCA_DelaySensor::GetDuration_doc[] = "\tReturn the duration parameter value\n"; PyObject* SCA_DelaySensor::PyGetDuration(PyObject* self) { + ShowDeprecationWarning("getDuration()", "the duration property"); return PyInt_FromLong(m_duration); } @@ -251,6 +295,7 @@ const char SCA_DelaySensor::GetRepeat_doc[] = "\tReturn the repeat parameter value\n"; PyObject* SCA_DelaySensor::PyGetRepeat(PyObject* self) { + ShowDeprecationWarning("getRepeat()", "the repeat property"); return BoolToPyArg(m_repeat); } diff --git a/source/gameengine/GameLogic/SCA_DelaySensor.h b/source/gameengine/GameLogic/SCA_DelaySensor.h index a997fabe3cd..ff3afe16542 100644 --- a/source/gameengine/GameLogic/SCA_DelaySensor.h +++ b/source/gameengine/GameLogic/SCA_DelaySensor.h @@ -61,6 +61,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); /* setProperty */ KX_PYMETHOD_DOC(SCA_DelaySensor,SetDelay); diff --git a/source/gameengine/GameLogic/SCA_ISensor.cpp b/source/gameengine/GameLogic/SCA_ISensor.cpp index b10ac676464..6e46c8504bb 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.cpp +++ b/source/gameengine/GameLogic/SCA_ISensor.cpp @@ -139,71 +139,6 @@ void SCA_ISensor::DecLink() { } } -/* python integration */ - -PyTypeObject SCA_ISensor::Type = { - PyObject_HEAD_INIT(&PyType_Type) - 0, - "SCA_ISensor", - sizeof(SCA_ISensor), - 0, - PyDestructor, - 0, - __getattr, - __setattr, - 0, //&MyPyCompare, - __repr, - 0, //&cvalue_as_number, - 0, - 0, - 0, - 0 -}; - -PyParentObject SCA_ISensor::Parents[] = { - &SCA_ISensor::Type, - &SCA_ILogicBrick::Type, - &CValue::Type, - NULL -}; -PyMethodDef SCA_ISensor::Methods[] = { - {"isPositive", (PyCFunction) SCA_ISensor::sPyIsPositive, - METH_NOARGS, (PY_METHODCHAR)IsPositive_doc}, - {"isTriggered", (PyCFunction) SCA_ISensor::sPyIsTriggered, - METH_VARARGS, (PY_METHODCHAR)IsTriggered_doc}, - {"getUsePosPulseMode", (PyCFunction) SCA_ISensor::sPyGetUsePosPulseMode, - METH_NOARGS, (PY_METHODCHAR)GetUsePosPulseMode_doc}, - {"setUsePosPulseMode", (PyCFunction) SCA_ISensor::sPySetUsePosPulseMode, - METH_VARARGS, (PY_METHODCHAR)SetUsePosPulseMode_doc}, - {"getFrequency", (PyCFunction) SCA_ISensor::sPyGetFrequency, - METH_NOARGS, (PY_METHODCHAR)GetFrequency_doc}, - {"setFrequency", (PyCFunction) SCA_ISensor::sPySetFrequency, - METH_VARARGS, (PY_METHODCHAR)SetFrequency_doc}, - {"getUseNegPulseMode", (PyCFunction) SCA_ISensor::sPyGetUseNegPulseMode, - METH_NOARGS, (PY_METHODCHAR)GetUseNegPulseMode_doc}, - {"setUseNegPulseMode", (PyCFunction) SCA_ISensor::sPySetUseNegPulseMode, - METH_VARARGS, (PY_METHODCHAR)SetUseNegPulseMode_doc}, - {"getInvert", (PyCFunction) SCA_ISensor::sPyGetInvert, - METH_NOARGS, (PY_METHODCHAR)GetInvert_doc}, - {"setInvert", (PyCFunction) SCA_ISensor::sPySetInvert, - METH_VARARGS, (PY_METHODCHAR)SetInvert_doc}, - {"getLevel", (PyCFunction) SCA_ISensor::sPyGetLevel, - METH_NOARGS, (PY_METHODCHAR)GetLevel_doc}, - {"setLevel", (PyCFunction) SCA_ISensor::sPySetLevel, - METH_VARARGS, (PY_METHODCHAR)SetLevel_doc}, - {"reset", (PyCFunction) SCA_ISensor::sPyReset, - METH_NOARGS, (PY_METHODCHAR)Reset_doc}, - {NULL,NULL} //Sentinel -}; - - -PyObject* -SCA_ISensor::_getattr(const STR_String& attr) -{ - _getattr_up(SCA_ILogicBrick); -} - - void SCA_ISensor::RegisterToManager() { // sensor is just activated, initialize it @@ -275,13 +210,17 @@ void SCA_ISensor::Activate(class SCA_LogicManager* logicmgr, CValue* event) } } } +/* ----------------------------------------------- */ +/* Python Functions */ +/* ----------------------------------------------- */ -/* Python functions: */ +//Deprecated Functions ------> const char SCA_ISensor::IsPositive_doc[] = "isPositive()\n" "\tReturns whether the sensor is in an active state.\n"; PyObject* SCA_ISensor::PyIsPositive(PyObject* self) { + ShowDeprecationWarning("isPositive()", "the read-only positive property"); int retval = IsPositiveTrigger(); return PyInt_FromLong(retval); } @@ -291,6 +230,7 @@ const char SCA_ISensor::IsTriggered_doc[] = "\tReturns whether the sensor has triggered the current controller.\n"; PyObject* SCA_ISensor::PyIsTriggered(PyObject* self) { + ShowDeprecationWarning("isTriggered()", "the read-only triggered property"); // check with the current controller int retval = 0; if (SCA_PythonController::m_sCurrentController) @@ -306,6 +246,7 @@ const char SCA_ISensor::GetUsePosPulseMode_doc[] = "\tReturns whether positive pulse mode is active.\n"; PyObject* SCA_ISensor::PyGetUsePosPulseMode(PyObject* self) { + ShowDeprecationWarning("getUsePosPulseMode()", "the usePosPulseMode property"); return BoolToPyArg(m_pos_pulsemode); } @@ -319,6 +260,7 @@ const char SCA_ISensor::SetUsePosPulseMode_doc[] = "\tSet whether to do pulsing when positive pulses occur.\n"; PyObject* SCA_ISensor::PySetUsePosPulseMode(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setUsePosPulseMode()", "the usePosPulseMode property"); int pyarg = 0; if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } m_pos_pulsemode = PyArgToBool(pyarg); @@ -333,6 +275,7 @@ const char SCA_ISensor::GetFrequency_doc[] = "\tReturns the frequency of the updates in pulse mode.\n" ; PyObject* SCA_ISensor::PyGetFrequency(PyObject* self) { + ShowDeprecationWarning("getFrequency()", "the frequency property"); return PyInt_FromLong(m_pulse_frequency); } @@ -346,6 +289,7 @@ const char SCA_ISensor::SetFrequency_doc[] = "\tIf the frequency is negative, it is set to 0.\n" ; PyObject* SCA_ISensor::PySetFrequency(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setFrequency()", "the frequency property"); int pulse_frequencyArg = 0; if(!PyArg_ParseTuple(args, "i", &pulse_frequencyArg)) { @@ -368,6 +312,7 @@ const char SCA_ISensor::GetInvert_doc[] = "\tReturns whether or not pulses from this sensor are inverted.\n" ; PyObject* SCA_ISensor::PyGetInvert(PyObject* self) { + ShowDeprecationWarning("getInvert()", "the invert property"); return BoolToPyArg(m_invert); } @@ -377,6 +322,7 @@ const char SCA_ISensor::SetInvert_doc[] = "\tSet whether to invert pulses.\n"; PyObject* SCA_ISensor::PySetInvert(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setInvert()", "the invert property"); int pyarg = 0; if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } m_invert = PyArgToBool(pyarg); @@ -392,6 +338,7 @@ const char SCA_ISensor::GetLevel_doc[] = "\tA edge detector will wait for a state change before generating a pulse.\n"; PyObject* SCA_ISensor::PyGetLevel(PyObject* self) { + ShowDeprecationWarning("getLevel()", "the level property"); return BoolToPyArg(m_level); } @@ -401,6 +348,7 @@ const char SCA_ISensor::SetLevel_doc[] = "\tSet whether to detect level or edge transition when entering a state.\n"; PyObject* SCA_ISensor::PySetLevel(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setLevel()", "the level property"); int pyarg = 0; if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } m_level = PyArgToBool(pyarg); @@ -412,6 +360,7 @@ const char SCA_ISensor::GetUseNegPulseMode_doc[] = "\tReturns whether negative pulse mode is active.\n"; PyObject* SCA_ISensor::PyGetUseNegPulseMode(PyObject* self) { + ShowDeprecationWarning("getUseNegPulseMode()", "the useNegPulseMode property"); return BoolToPyArg(m_neg_pulsemode); } @@ -422,21 +371,156 @@ const char SCA_ISensor::SetUseNegPulseMode_doc[] = "\tSet whether to do pulsing when negative pulses occur.\n"; PyObject* SCA_ISensor::PySetUseNegPulseMode(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setUseNegPulseMode()", "the useNegPulseMode property"); int pyarg = 0; if(!PyArg_ParseTuple(args, "i", &pyarg)) { return NULL; } m_neg_pulsemode = PyArgToBool(pyarg); Py_Return; } +//<------Deprecated -const char SCA_ISensor::Reset_doc[] = +KX_PYMETHODDEF_DOC_NOARGS(SCA_ISensor, reset, "reset()\n" "\tReset sensor internal state, effect depends on the type of sensor and settings.\n" -"\tThe sensor is put in its initial state as if it was just activated.\n"; -PyObject* SCA_ISensor::PyReset(PyObject* self) +"\tThe sensor is put in its initial state as if it was just activated.\n") { Init(); Py_Return; } +/* ----------------------------------------------- */ +/* Python Integration Hooks */ +/* ----------------------------------------------- */ + +PyTypeObject SCA_ISensor::Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, + "SCA_ISensor", + sizeof(SCA_ISensor), + 0, + PyDestructor, + 0, + __getattr, + __setattr, + 0, //&MyPyCompare, + __repr, + 0, //&cvalue_as_number, + 0, + 0, + 0, + 0 +}; + +PyParentObject SCA_ISensor::Parents[] = { + &SCA_ISensor::Type, + &SCA_ILogicBrick::Type, + &CValue::Type, + NULL +}; +PyMethodDef SCA_ISensor::Methods[] = { + //Deprecated functions -----> + {"isPositive", (PyCFunction) SCA_ISensor::sPyIsPositive, + METH_NOARGS, (PY_METHODCHAR)IsPositive_doc}, + {"isTriggered", (PyCFunction) SCA_ISensor::sPyIsTriggered, + METH_VARARGS, (PY_METHODCHAR)IsTriggered_doc}, + {"getUsePosPulseMode", (PyCFunction) SCA_ISensor::sPyGetUsePosPulseMode, + METH_NOARGS, (PY_METHODCHAR)GetUsePosPulseMode_doc}, + {"setUsePosPulseMode", (PyCFunction) SCA_ISensor::sPySetUsePosPulseMode, + METH_VARARGS, (PY_METHODCHAR)SetUsePosPulseMode_doc}, + {"getFrequency", (PyCFunction) SCA_ISensor::sPyGetFrequency, + METH_NOARGS, (PY_METHODCHAR)GetFrequency_doc}, + {"setFrequency", (PyCFunction) SCA_ISensor::sPySetFrequency, + METH_VARARGS, (PY_METHODCHAR)SetFrequency_doc}, + {"getUseNegPulseMode", (PyCFunction) SCA_ISensor::sPyGetUseNegPulseMode, + METH_NOARGS, (PY_METHODCHAR)GetUseNegPulseMode_doc}, + {"setUseNegPulseMode", (PyCFunction) SCA_ISensor::sPySetUseNegPulseMode, + METH_VARARGS, (PY_METHODCHAR)SetUseNegPulseMode_doc}, + {"getInvert", (PyCFunction) SCA_ISensor::sPyGetInvert, + METH_NOARGS, (PY_METHODCHAR)GetInvert_doc}, + {"setInvert", (PyCFunction) SCA_ISensor::sPySetInvert, + METH_VARARGS, (PY_METHODCHAR)SetInvert_doc}, + {"getLevel", (PyCFunction) SCA_ISensor::sPyGetLevel, + METH_NOARGS, (PY_METHODCHAR)GetLevel_doc}, + {"setLevel", (PyCFunction) SCA_ISensor::sPySetLevel, + METH_VARARGS, (PY_METHODCHAR)SetLevel_doc}, + //<----- Deprecated + KX_PYMETHODTABLE_NOARGS(SCA_ISensor, reset), + {NULL,NULL} //Sentinel +}; + +PyObject* +SCA_ISensor::_getattr(const STR_String& attr) +{ + if (attr == "usePosPulseMode") + return PyInt_FromLong(m_pos_pulsemode); + if (attr == "useNegPulseMode") + return PyInt_FromLong(m_neg_pulsemode); + if (attr == "frequency") + return PyInt_FromLong(m_pulse_frequency); + if (attr == "invert") + return PyInt_FromLong(m_invert); + if (attr == "level") + return PyInt_FromLong(m_level); + if (attr == "triggered") + { + int retval = 0; + if (SCA_PythonController::m_sCurrentController) + retval = SCA_PythonController::m_sCurrentController->IsTriggered(this); + return PyInt_FromLong(retval); + } + if (attr == "positive") + { + int retval = IsPositiveTrigger(); + return PyInt_FromLong(retval); + } + + _getattr_up(SCA_ILogicBrick); +} + +int SCA_ISensor::_setattr(const STR_String& attr, PyObject *value) +{ + if (attr == "triggered") + PyErr_SetString(PyExc_AttributeError, "attribute \"triggered\" is read only"); + if (attr == "positive") + PyErr_SetString(PyExc_AttributeError, "attribute \"positive\" is read only"); + + if (PyInt_Check(value)) + { + int val = PyInt_AsLong(value); + + if (attr == "usePosPulseMode") + { + m_pos_pulsemode = (val != 0); + return 0; + } + + if (attr == "useNegPulseMode") + { + m_neg_pulsemode = (val != 0); + return 0; + } + + if (attr == "invert") + { + m_invert = (val != 0); + return 0; + } + if (attr == "level") + { + m_level = (val != 0); + return 0; + } + + if (attr == "frequency") + { + if (val < 0) + val = 0; + m_pulse_frequency = val; + return 0; + } + } + + return SCA_ILogicBrick::_setattr(attr, value); +} /* eof */ diff --git a/source/gameengine/GameLogic/SCA_ISensor.h b/source/gameengine/GameLogic/SCA_ISensor.h index d1872009291..1b57c4621e4 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.h +++ b/source/gameengine/GameLogic/SCA_ISensor.h @@ -93,8 +93,7 @@ public: virtual bool Evaluate(CValue* event) = 0; virtual bool IsPositiveTrigger(); virtual void Init(); - - virtual PyObject* _getattr(const STR_String& attr); + virtual CValue* GetReplica()=0; /** Set parameters for the pulsing behaviour. @@ -141,6 +140,11 @@ public: { return !m_links; } /* Python functions: */ + + virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); + + //Deprecated functions -----> KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,IsPositive); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,IsTriggered); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetUsePosPulseMode); @@ -153,8 +157,8 @@ public: KX_PYMETHOD_DOC(SCA_ISensor,SetInvert); KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,GetLevel); KX_PYMETHOD_DOC(SCA_ISensor,SetLevel); - KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,Reset); - + //<------ + KX_PYMETHOD_DOC_NOARGS(SCA_ISensor,reset); }; #endif //__SCA_ISENSOR diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp index 8b96840b149..ec5921bad85 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp @@ -304,6 +304,7 @@ PyParentObject SCA_JoystickSensor::Parents[] = { PyMethodDef SCA_JoystickSensor::Methods[] = { + //Deprecated functions ------> {"getIndex", (PyCFunction) SCA_JoystickSensor::sPyGetIndex, METH_NOARGS, (PY_METHODCHAR)GetIndex_doc}, {"setIndex", (PyCFunction) SCA_JoystickSensor::sPySetIndex, METH_O, (PY_METHODCHAR)SetIndex_doc}, {"getAxis", (PyCFunction) SCA_JoystickSensor::sPyGetAxis, METH_NOARGS, (PY_METHODCHAR)GetAxis_doc}, @@ -313,27 +314,144 @@ PyMethodDef SCA_JoystickSensor::Methods[] = { {"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS, (PY_METHODCHAR)SetThreshold_doc}, {"getButton", (PyCFunction) SCA_JoystickSensor::sPyGetButton, METH_NOARGS, (PY_METHODCHAR)GetButton_doc}, {"setButton", (PyCFunction) SCA_JoystickSensor::sPySetButton, METH_O, (PY_METHODCHAR)SetButton_doc}, - {"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue, METH_NOARGS,(PY_METHODCHAR)GetButtonValue_doc}, {"getHat", (PyCFunction) SCA_JoystickSensor::sPyGetHat, METH_NOARGS, (PY_METHODCHAR)GetHat_doc}, {"setHat", (PyCFunction) SCA_JoystickSensor::sPySetHat, METH_VARARGS, (PY_METHODCHAR)SetHat_doc}, {"getNumAxes", (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes, METH_NOARGS, (PY_METHODCHAR)NumberOfAxes_doc}, {"getNumButtons",(PyCFunction) SCA_JoystickSensor::sPyNumberOfButtons,METH_NOARGS, (PY_METHODCHAR)NumberOfButtons_doc}, {"getNumHats", (PyCFunction) SCA_JoystickSensor::sPyNumberOfHats, METH_NOARGS, (PY_METHODCHAR)NumberOfHats_doc}, {"isConnected", (PyCFunction) SCA_JoystickSensor::sPyConnected, METH_NOARGS, (PY_METHODCHAR)Connected_doc}, + {"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue, METH_NOARGS,(PY_METHODCHAR)GetButtonValue_doc}, + //<----- Deprecated + {"getButtonActiveList",(PyCFunction) SCA_JoystickSensor::sPyGetButtonActiveList, METH_NOARGS,(PY_METHODCHAR)GetButtonActiveList_doc}, + {"getButtonStatus",(PyCFunction) SCA_JoystickSensor::sPyGetButtonStatus, METH_VARARGS,(PY_METHODCHAR)GetButtonStatus_doc}, {NULL,NULL} //Sentinel }; PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) { + SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); + if (attr == "axisPosition") { + if(joy) + return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21()); + else + return Py_BuildValue("[iiii]", 0, 0, 0, 0); + } + if (attr == "numAxis") { + return PyInt_FromLong( joy ? joy->GetNumberOfAxes() : 0 ); + } + if (attr == "numButtons") { + return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 ); + } + if (attr == "numHats") { + return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 ); + } + if (attr == "connected") { + return PyBool_FromLong( joy ? joy->Connected() : 0 ); + } + if (attr == "index") { + return PyInt_FromLong(m_joyindex); + } + if (attr == "threshold") { + return PyInt_FromLong(m_precision); + } + if (attr == "button") { + return PyInt_FromLong(m_button); + } + if (attr == "axis") { + return Py_BuildValue("[ii]",m_axis, m_axisf); + } + if (attr == "hat") { + return Py_BuildValue("[ii]",m_hat, m_hatf); + } _getattr_up(SCA_ISensor); } +int SCA_JoystickSensor::_setattr(const STR_String& attr, PyObject *value) { + if (attr == "axisPosition") { + PyErr_SetString(PyExc_AttributeError, "axisPosition is read only"); + return 1; + } + if (attr == "numAxis") { + PyErr_SetString(PyExc_AttributeError, "numaxis is read only"); + return 1; + } + if (attr == "numButtons") { + PyErr_SetString(PyExc_AttributeError, "numbuttons is read only"); + return 1; + } + if (attr == "numHats") { + PyErr_SetString(PyExc_AttributeError, "numhats is read only"); + return 1; + } + if (attr == "connected") { + PyErr_SetString(PyExc_AttributeError, "connected is read only"); + return 1; + } + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); + if (attr == "index") { + if (ival < 0 || ival >= JOYINDEX_MAX) { + PyErr_SetString(PyExc_ValueError, "joystick index out of range"); + return 1; + } + m_joyindex = ival; + } else if (attr == "threshold") { + m_precision = ival; + } else if (attr == "button") { + if (ival < 0) { + PyErr_SetString(PyExc_ValueError, "button out of range"); + return 1; + } + m_button = ival; + } + return 0; + } + if (PySequence_Check(value)) { + if (attr == "axis" || attr == "hat") { + if (PySequence_Size(value) != 2) { + PyErr_SetString(PyExc_ValueError, "value must be sequence of 2 integers"); + return 1; + } + int ival = -1; + int ivalf = -1; + PyObject *item = PySequence_GetItem(value, 0); /* new ref */ + ival = PyInt_AsLong(item); + Py_DECREF(item); + item = PySequence_GetItem(value, 1); /* new ref */ + ivalf = PyInt_AsLong(item); + Py_DECREF(item); + if (PyErr_Occurred()) { + PyErr_SetString(PyExc_ValueError, "one or more of the items in the sequence was not an integer"); + return 1; + } + if (attr == "axis") { + if (ival < 1 || ival > 2 || ivalf < 0 || ivalf > 3) { + PyErr_SetString(PyExc_ValueError, "items in the sequence are out of range"); + return 1; + } + m_axis = ival; + m_axisf = ivalf; + } else { + if (ival < 1 || ival > 2) { + PyErr_SetString(PyExc_ValueError, "items in the sequence are out of range"); + return 1; + } + m_hat = ival; + m_hatf = ivalf; + } + } + return 0; + } + return SCA_ISensor::_setattr(attr, value); +} + /* get index ---------------------------------------------------------- */ const char SCA_JoystickSensor::GetIndex_doc[] = "getIndex\n" "\tReturns the joystick index to use.\n"; PyObject* SCA_JoystickSensor::PyGetIndex( PyObject* self ) { + ShowDeprecationWarning("getIndex()", "the index property"); return PyInt_FromLong(m_joyindex); } @@ -343,6 +461,7 @@ const char SCA_JoystickSensor::SetIndex_doc[] = "setIndex\n" "\tSets the joystick index to use.\n"; PyObject* SCA_JoystickSensor::PySetIndex( PyObject* self, PyObject* value ) { + ShowDeprecationWarning("setIndex()", "the index property"); int index = PyInt_AsLong( value ); /* -1 on error, will raise an error in this case */ if (index < 0 || index >= JOYINDEX_MAX) { PyErr_SetString(PyExc_ValueError, "joystick index out of range or not an int"); @@ -358,6 +477,7 @@ const char SCA_JoystickSensor::GetAxis_doc[] = "getAxis\n" "\tReturns the current axis this sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) { + ShowDeprecationWarning("getAxis()", "the axis property"); return Py_BuildValue("[ii]",m_axis, m_axisf); } @@ -367,6 +487,7 @@ const char SCA_JoystickSensor::SetAxis_doc[] = "setAxis\n" "\tSets the current axis this sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) { + ShowDeprecationWarning("setAxis()", "the axis property"); int axis,axisflag; if(!PyArg_ParseTuple(args, "ii", &axis, &axisflag)){ @@ -383,6 +504,7 @@ const char SCA_JoystickSensor::GetAxisValue_doc[] = "getAxisValue\n" "\tReturns a list of the values for the current state of each axis.\n"; PyObject* SCA_JoystickSensor::PyGetAxisValue( PyObject* self) { + ShowDeprecationWarning("getAxisValue()", "the axisPosition property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); if(joy) return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21()); @@ -396,6 +518,7 @@ const char SCA_JoystickSensor::GetThreshold_doc[] = "getThreshold\n" "\tReturns the threshold of the axis.\n"; PyObject* SCA_JoystickSensor::PyGetThreshold( PyObject* self) { + ShowDeprecationWarning("getThreshold()", "the threshold property"); return PyInt_FromLong(m_precision); } @@ -405,6 +528,7 @@ const char SCA_JoystickSensor::SetThreshold_doc[] = "setThreshold\n" "\tSets the threshold of the axis.\n"; PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, PyObject* args ) { + ShowDeprecationWarning("setThreshold()", "the threshold property"); int thresh; if(!PyArg_ParseTuple(args, "i", &thresh)){ return NULL; @@ -418,6 +542,7 @@ const char SCA_JoystickSensor::GetButton_doc[] = "getButton\n" "\tReturns the current button this sensor is checking.\n"; PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self) { + ShowDeprecationWarning("getButton()", "the button property"); return PyInt_FromLong(m_button); } @@ -426,6 +551,7 @@ const char SCA_JoystickSensor::SetButton_doc[] = "setButton\n" "\tSets the button the sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* value ) { + ShowDeprecationWarning("setButton()", "the button property"); int button = PyInt_AsLong(value); if(button==-1 && PyErr_Occurred()) { PyErr_SetString(PyExc_ValueError, "expected an int"); @@ -440,6 +566,15 @@ const char SCA_JoystickSensor::GetButtonValue_doc[] = "getButtonValue\n" "\tReturns a list containing the indicies of the current pressed state of each button.\n"; PyObject* SCA_JoystickSensor::PyGetButtonValue( PyObject* self) { + ShowDeprecationWarning("getButtonValue()", "getButtonActiveList"); + return PyGetButtonActiveList(self); +} + +/* get button active list -------------------------------------------------- */ +const char SCA_JoystickSensor::GetButtonActiveList_doc[] = +"getButtonActiveList\n" +"\tReturns a list containing the indicies of the button currently pressed.\n"; +PyObject* SCA_JoystickSensor::PyGetButtonActiveList( PyObject* self) { SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); PyObject *ls = PyList_New(0); PyObject *value; @@ -457,11 +592,30 @@ PyObject* SCA_JoystickSensor::PyGetButtonValue( PyObject* self) { return ls; } +/* get button status -------------------------------------------------- */ +const char SCA_JoystickSensor::GetButtonStatus_doc[] = +"getButtonStatus(buttonIndex)\n" +"\tReturns a bool of the current pressed state of the specified button.\n"; +PyObject* SCA_JoystickSensor::PyGetButtonStatus( PyObject* self, PyObject* args ) { + SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); + PyObject *value; + int index; + + if(!PyArg_ParseTuple(args, "i", &index)){ + return NULL; + } + if(joy && index >= 0 && index < joy->GetNumberOfButtons()) { + return PyBool_FromLong(joy->aButtonPressIsPositive(index) ? 1 : 0); + } + return PyBool_FromLong(0); +} + /* get hat ----------------------------------------------------------- */ const char SCA_JoystickSensor::GetHat_doc[] = "getHat\n" "\tReturns the current direction of the hat.\n"; PyObject* SCA_JoystickSensor::PyGetHat( PyObject* self ) { + ShowDeprecationWarning("getHat()", "the hat property"); return Py_BuildValue("[ii]",m_hat, m_hatf); } @@ -471,6 +625,7 @@ const char SCA_JoystickSensor::SetHat_doc[] = "setHat\n" "\tSets the hat the sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PySetHat( PyObject* self, PyObject* args ) { + ShowDeprecationWarning("setHat()", "the hat property"); int hat,hatflag; if(!PyArg_ParseTuple(args, "ii", &hat, &hatflag)){ return NULL; @@ -486,6 +641,7 @@ const char SCA_JoystickSensor::NumberOfAxes_doc[] = "getNumAxes\n" "\tReturns the number of axes .\n"; PyObject* SCA_JoystickSensor::PyNumberOfAxes( PyObject* self ) { + ShowDeprecationWarning("getNumAxes()", "the numAxis property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); // when the joystick is null their is 0 exis still. dumb but scripters should use isConnected() return PyInt_FromLong( joy ? joy->GetNumberOfAxes() : 0 ); @@ -496,6 +652,7 @@ const char SCA_JoystickSensor::NumberOfButtons_doc[] = "getNumButtons\n" "\tReturns the number of buttons .\n"; PyObject* SCA_JoystickSensor::PyNumberOfButtons( PyObject* self ) { + ShowDeprecationWarning("getNumButtons()", "the numButtons property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyInt_FromLong( joy ? joy->GetNumberOfButtons() : 0 ); } @@ -505,6 +662,7 @@ const char SCA_JoystickSensor::NumberOfHats_doc[] = "getNumHats\n" "\tReturns the number of hats .\n"; PyObject* SCA_JoystickSensor::PyNumberOfHats( PyObject* self ) { + ShowDeprecationWarning("getNumHats()", "the numHats property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyInt_FromLong( joy ? joy->GetNumberOfHats() : 0 ); } @@ -513,6 +671,7 @@ const char SCA_JoystickSensor::Connected_doc[] = "getConnected\n" "\tReturns True if a joystick is connected at this joysticks index.\n"; PyObject* SCA_JoystickSensor::PyConnected( PyObject* self ) { + ShowDeprecationWarning("getConnected()", "the connected property"); SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex); return PyBool_FromLong( joy ? joy->Connected() : 0 ); } diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.h b/source/gameengine/GameLogic/SCA_JoystickSensor.h index d316ad1119c..03653070db7 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.h +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.h @@ -122,6 +122,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); /* Joystick Index */ KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetIndex); @@ -136,6 +137,8 @@ public: KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButton); KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetButton); KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonValue); + KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonActiveList); + KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,GetButtonStatus); /* Hats */ KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetHat); KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetHat); diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp index fa39a13679f..f880cd7768e 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp +++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp @@ -545,42 +545,17 @@ void SCA_KeyboardSensor::LogKeystrokes(void) /* ------------------------------------------------------------------------- */ -/* Python functions : specific */ +/* Python Functions */ /* ------------------------------------------------------------------------- */ - -PyObject* SCA_KeyboardSensor::PySetAllMode(PyObject* self, - PyObject* args, - PyObject* kwds) -{ - bool allkeys; - - if (!PyArg_ParseTuple(args, "i", &allkeys)) - { - return NULL; - } - - m_bAllKeys = allkeys; - Py_Return -} - - - -PyObject* SCA_KeyboardSensor::sPySetAllMode(PyObject* self, - PyObject* args, - PyObject* kwds) -{ -// printf("sPyIsPositive\n"); - return ((SCA_KeyboardSensor*) self)->PyIsPositive(self); -} - - +//Deprecated -----> /** 1. GetKey : check which key this sensor looks at */ const char SCA_KeyboardSensor::GetKey_doc[] = "getKey()\n" "\tReturn the code of the key this sensor is listening to.\n" ; PyObject* SCA_KeyboardSensor::PyGetKey(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getKey()", "the key property"); return PyInt_FromLong(m_hotkey); } @@ -591,6 +566,7 @@ const char SCA_KeyboardSensor::SetKey_doc[] = "\tSet the key this sensor should listen to.\n" ; PyObject* SCA_KeyboardSensor::PySetKey(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setKey()", "the key property"); int keyCode; if(!PyArg_ParseTuple(args, "i", &keyCode)) { @@ -611,6 +587,7 @@ const char SCA_KeyboardSensor::GetHold1_doc[] = "\tsensor is listening to.\n" ; PyObject* SCA_KeyboardSensor::PyGetHold1(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getHold1()", "the hold1 property"); return PyInt_FromLong(m_qual); } @@ -621,6 +598,7 @@ const char SCA_KeyboardSensor::SetHold1_doc[] = "\tSet the first modifier to the key this sensor should listen to.\n" ; PyObject* SCA_KeyboardSensor::PySetHold1(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setHold1()", "the hold1 property"); int keyCode; if(!PyArg_ParseTuple(args, "i", &keyCode)) { @@ -641,6 +619,7 @@ const char SCA_KeyboardSensor::GetHold2_doc[] = "\tsensor is listening to.\n" ; PyObject* SCA_KeyboardSensor::PyGetHold2(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getHold2()", "the hold2 property"); return PyInt_FromLong(m_qual2); } @@ -651,6 +630,7 @@ const char SCA_KeyboardSensor::SetHold2_doc[] = "\tSet the first modifier to the key this sensor should listen to.\n" ; PyObject* SCA_KeyboardSensor::PySetHold2(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setHold2()", "the hold2 property"); int keyCode; if(!PyArg_ParseTuple(args, "i", &keyCode)) { @@ -671,6 +651,8 @@ const char SCA_KeyboardSensor::GetPressedKeys_doc[] = PyObject* SCA_KeyboardSensor::PyGetPressedKeys(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getPressedKeys()", "getEventList()"); + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); int num = inputdev->GetNumJustEvents(); @@ -711,6 +693,8 @@ const char SCA_KeyboardSensor::GetCurrentlyPressedKeys_doc[] = PyObject* SCA_KeyboardSensor::PyGetCurrentlyPressedKeys(PyObject* self, PyObject* args, PyObject* kwds) { +ShowDeprecationWarning("getCurrentlyPressedKeys()", "getEventList()"); + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); int num = inputdev->GetNumActiveEvents(); @@ -743,9 +727,54 @@ SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); Py_Return; } +//<---- Deprecated + +KX_PYMETHODDEF_DOC_NOARGS(SCA_KeyboardSensor, getEventList, +"getEventList()\n" +"\tGet the list of the keyboard events in this frame.\n") +{ + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); + + PyObject* resultlist = PyList_New(0); + + for (int i=SCA_IInputDevice::KX_BEGINKEY ; i< SCA_IInputDevice::KX_ENDKEY;i++) + { + const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) i); + if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS) + { + PyObject* keypair = PyList_New(2); + PyList_SetItem(keypair,0,PyInt_FromLong(i)); + PyList_SetItem(keypair,1,PyInt_FromLong(inevent.m_status)); + PyList_Append(resultlist,keypair); + } + } + return resultlist; +} + +KX_PYMETHODDEF_DOC_O(SCA_KeyboardSensor, getKeyStatus, +"getKeyStatus(keycode)\n" +"\tGet the given key's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n") +{ + if (PyInt_Check(value)) + { + int keycode = PyInt_AsLong(value); + + if ((keycode < SCA_IInputDevice::KX_BEGINKEY) + || (keycode > SCA_IInputDevice::KX_ENDKEY)){ + PyErr_SetString(PyExc_AttributeError, "invalid keycode specified!"); + return NULL; + } + + SCA_IInputDevice* inputdev = m_pKeyboardMgr->GetInputDevice(); + const SCA_InputEvent & inevent = inputdev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) keycode); + return PyInt_FromLong(inevent.m_status); + } + + Py_Return; +} /* ------------------------------------------------------------------------- */ -/* Python functions : integration hooks */ +/* Python Integration Hooks */ /* ------------------------------------------------------------------------- */ PyTypeObject SCA_KeyboardSensor::Type = { @@ -776,23 +805,91 @@ PyParentObject SCA_KeyboardSensor::Parents[] = { }; PyMethodDef SCA_KeyboardSensor::Methods[] = { - {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_VARARGS, (PY_METHODCHAR)GetKey_doc}, - {"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, (PY_METHODCHAR)SetKey_doc}, - {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_VARARGS, (PY_METHODCHAR)GetHold1_doc}, - {"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, (PY_METHODCHAR)SetHold1_doc}, - {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_VARARGS, (PY_METHODCHAR)GetHold2_doc}, - {"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, (PY_METHODCHAR)SetHold2_doc}, -// {"getUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetUseAllKeys, METH_VARARGS, (PY_METHODCHAR)GetUseAllKeys_doc}, -// {"setUseAllKeys", (PyCFunction) SCA_KeyboardSensor::sPySetUseAllKeys, METH_VARARGS, (PY_METHODCHAR)SetUseAllKeys_doc}, - {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetPressedKeys_doc}, - {"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetCurrentlyPressedKeys_doc}, -// {"getKeyEvents", (PyCFunction) SCA_KeyboardSensor::sPyGetKeyEvents, METH_VARARGS, (PY_METHODCHAR)GetKeyEvents_doc}, - {NULL,NULL} //Sentinel + //Deprecated functions ------> + {"getKey", (PyCFunction) SCA_KeyboardSensor::sPyGetKey, METH_VARARGS, (PY_METHODCHAR)GetKey_doc}, + {"setKey", (PyCFunction) SCA_KeyboardSensor::sPySetKey, METH_VARARGS, (PY_METHODCHAR)SetKey_doc}, + {"getHold1", (PyCFunction) SCA_KeyboardSensor::sPyGetHold1, METH_VARARGS, (PY_METHODCHAR)GetHold1_doc}, + {"setHold1", (PyCFunction) SCA_KeyboardSensor::sPySetHold1, METH_VARARGS, (PY_METHODCHAR)SetHold1_doc}, + {"getHold2", (PyCFunction) SCA_KeyboardSensor::sPyGetHold2, METH_VARARGS, (PY_METHODCHAR)GetHold2_doc}, + {"setHold2", (PyCFunction) SCA_KeyboardSensor::sPySetHold2, METH_VARARGS, (PY_METHODCHAR)SetHold2_doc}, + {"getPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetPressedKeys_doc}, + {"getCurrentlyPressedKeys", (PyCFunction) SCA_KeyboardSensor::sPyGetCurrentlyPressedKeys, METH_VARARGS, (PY_METHODCHAR)GetCurrentlyPressedKeys_doc}, + //<----- Deprecated + KX_PYMETHODTABLE_NOARGS(SCA_KeyboardSensor, getEventList), + KX_PYMETHODTABLE_O(SCA_KeyboardSensor, getKeyStatus), + {NULL,NULL} //Sentinel }; PyObject* SCA_KeyboardSensor::_getattr(const STR_String& attr) { + if (attr == "key") + return PyInt_FromLong(m_hotkey); + + if (attr == "hold1") + return PyInt_FromLong(m_qual); + + if (attr == "hold2") + return PyInt_FromLong(m_qual2); + + if (attr == "toggleProperty") + return PyString_FromString(m_toggleprop); + + if (attr == "targetProperty") + return PyString_FromString(m_targetprop); + + if (attr == "useAllKeys") + return PyInt_FromLong(m_bAllKeys); + _getattr_up(SCA_ISensor); } +int SCA_KeyboardSensor::_setattr(const STR_String& attr, PyObject *value) +{ + if (PyInt_Check(value)) + { + int val = PyInt_AsLong(value); + + if (attr == "key") + { + m_hotkey = val; + return 0; + } + + if (attr == "hold1") + { + m_qual = val; + return 0; + } + + if (attr == "hold2") + { + m_qual2 = val; + return 0; + } + + if (attr == "useAllKeys") + { + m_bAllKeys = (val != 0); + return 0; + } + } + + if (PyString_Check(value)) + { + STR_String val = PyString_AsString(value); + if (attr == "logToggleProperty") + { + m_toggleprop = val; + return 0; + } + + if (attr == "logTargetProperty") + { + m_targetprop = val; + return 0; + } + } + + return SCA_ISensor::_setattr(attr, value); +} diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.h b/source/gameengine/GameLogic/SCA_KeyboardSensor.h index b86f6931d27..4efbe9366cc 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardSensor.h +++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.h @@ -127,15 +127,9 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); - - PyObject* PySetAllMode(PyObject* self, - PyObject* args, - PyObject* kwds); - static PyObject* sPySetAllMode(PyObject* self, - PyObject* args, - PyObject* kwds); - + //Deprecated functions -----> /** 1. GetKey : check which key this sensor looks at */ KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetKey); /** 2. SetKey: change the key to look at */ @@ -152,6 +146,12 @@ public: KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetPressedKeys); /** 9. GetCurrrentlyPressedKeys: */ KX_PYMETHOD_DOC(SCA_KeyboardSensor,GetCurrentlyPressedKeys); + // <------ + + // KeyEvents: + KX_PYMETHOD_DOC_NOARGS(SCA_KeyboardSensor,getEventList); + // KeyStatus: + KX_PYMETHOD_DOC_O(SCA_KeyboardSensor,getKeyStatus); }; #endif //__KX_KEYBOARDSENSOR diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.cpp b/source/gameengine/GameLogic/SCA_MouseSensor.cpp index d4952ce4777..9562003aec0 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.cpp +++ b/source/gameengine/GameLogic/SCA_MouseSensor.cpp @@ -25,7 +25,7 @@ * * The Original Code is: all of this file. * - * Contributor(s): none yet. + * Contributor(s): José I. Romero (cleanup and fixes) * * ***** END GPL LICENSE BLOCK ***** */ @@ -59,6 +59,29 @@ SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, m_mousemode = mousemode; m_triggermode = true; + UpdateHotkey(); + Init(); +} + +void SCA_MouseSensor::Init() +{ + m_val = (m_invert)?1:0; /* stores the latest attribute */ + m_reset = true; +} + +SCA_MouseSensor::~SCA_MouseSensor() +{ + /* Nothing to be done here. */ +} + +void SCA_MouseSensor::UpdateHotkey() +{ + // gosh, this function is so damn stupid + // its here because of a design mistake in the mouse sensor, it should only + // have 3 trigger modes (button, wheel, move), and let the user set the + // hotkey separately, like the other sensors. but instead it has a mode for + // each friggin key and i have to update the hotkey based on it... genius! + switch (m_mousemode) { case KX_MOUSESENSORMODE_LEFTBUTTON: m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE; @@ -78,22 +101,8 @@ SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, default: ; /* ignore, no hotkey */ } - Init(); -} - -void SCA_MouseSensor::Init() -{ - m_val = (m_invert)?1:0; /* stores the latest attribute */ - m_reset = true; } -SCA_MouseSensor::~SCA_MouseSensor() -{ - /* Nothing to be done here. */ -} - - - CValue* SCA_MouseSensor::GetReplica() { SCA_MouseSensor* replica = new SCA_MouseSensor(*this); @@ -137,15 +146,6 @@ bool SCA_MouseSensor::Evaluate(CValue* event) bool reset = m_reset && m_level; SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice(); - - -// SCA_ILogicBrick::RegisterEvent(event); -// if (m_mousemode == KX_MOUSESENSORMODE_MOVEMENT) cout << "\nChecking for movement..."; -//CValue* val = event->GetProperty("val"); - - /* both MOUSEX and MOUSEY. Treat all of these as key-presses. */ - /* So, treat KX_MOUSESENSORMODE_POSITION as */ - /* KX_MOUSESENSORMODE_POSITIONX || KX_MOUSESENSORMODE_POSITIONY */ m_reset = false; switch (m_mousemode) { case KX_MOUSESENSORMODE_LEFTBUTTON: @@ -155,42 +155,34 @@ bool SCA_MouseSensor::Evaluate(CValue* event) case KX_MOUSESENSORMODE_WHEELDOWN: { const SCA_InputEvent& event = mousedev->GetEventValue(m_hotkey); - if (event.m_status == SCA_InputEvent::KX_JUSTACTIVATED) - { + switch (event.m_status){ + case SCA_InputEvent::KX_JUSTACTIVATED: m_val = 1; result = true; - } else - { - if (event.m_status == SCA_InputEvent::KX_JUSTRELEASED) + break; + case SCA_InputEvent::KX_JUSTRELEASED: + m_val = 0; + result = true; + break; + case SCA_InputEvent::KX_ACTIVE: + if (m_val == 0) + { + m_val = 1; + if (m_level) + result = true; + } + break; + default: + if (m_val == 1) { m_val = 0; result = true; - } else - { - if (event.m_status == SCA_InputEvent::KX_ACTIVE) - { - if (m_val == 0) - { - m_val = 1; - if (m_level) - { - result = true; - } - } - } else - { - if (m_val == 1) - { - m_val = 0; - result = true; - } - } } + break; } break; } case KX_MOUSESENSORMODE_MOVEMENT: - { const SCA_InputEvent& eventX = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEX); const SCA_InputEvent& eventY = mousedev->GetEventValue(SCA_IInputDevice::KX_MOUSEY); @@ -198,27 +190,26 @@ bool SCA_MouseSensor::Evaluate(CValue* event) if (eventX.m_status == SCA_InputEvent::KX_JUSTACTIVATED || eventY.m_status == SCA_InputEvent::KX_JUSTACTIVATED || eventX.m_status == SCA_InputEvent::KX_ACTIVE || - eventY.m_status == SCA_InputEvent::KX_ACTIVE) - + eventY.m_status == SCA_InputEvent::KX_ACTIVE) { m_val = 1; result = true; - } else - { - if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED || + } + else if (eventX.m_status == SCA_InputEvent::KX_JUSTRELEASED || eventY.m_status == SCA_InputEvent::KX_JUSTRELEASED ) + { + m_val = 0; + result = true; + } + else //KX_NO_IMPUTSTATUS + { + if (m_val == 1) { m_val = 0; result = true; - } else - { - if (m_val == 1) - { - m_val = 0; - result = true; - } } } + break; } default: @@ -243,18 +234,67 @@ void SCA_MouseSensor::setY(short y) bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m) { - bool res = false; + return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX)); +} + +/* ------------------------------------------------------------------------- */ +/* Python functions */ +/* ------------------------------------------------------------------------- */ - res = ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX)); +//Deprecated functions ------> +/* get x position ---------------------------------------------------------- */ +const char SCA_MouseSensor::GetXPosition_doc[] = +"getXPosition\n" +"\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n" +"\tThe lower-left corner is the origin. The coordinate is given in\n" +"\tpixels\n"; +PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, + PyObject* args, + PyObject* kwds) { + ShowDeprecationWarning("getXPosition()", "the position property"); + return PyInt_FromLong(m_x); +} + +/* get y position ---------------------------------------------------------- */ +const char SCA_MouseSensor::GetYPosition_doc[] = +"getYPosition\n" +"\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n" +"\tThe lower-left corner is the origin. The coordinate is given in\n" +"\tpixels\n"; +PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, + PyObject* args, + PyObject* kwds) { + ShowDeprecationWarning("getYPosition()", "the position property"); + return PyInt_FromLong(m_y); +} +//<----- Deprecated + +KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus, +"getButtonStatus(button)\n" +"\tGet the given button's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n") +{ + if (PyInt_Check(value)) + { + int button = PyInt_AsLong(value); + + if ((button < SCA_IInputDevice::KX_LEFTMOUSE) + || (button > SCA_IInputDevice::KX_MIDDLEMOUSE)){ + PyErr_SetString(PyExc_ValueError, "invalid button specified!"); + return NULL; + } + + SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice(); + const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button); + return PyInt_FromLong(event.m_status); + } - return res; + Py_Return; } /* ------------------------------------------------------------------------- */ -/* Python functions */ +/* Python Integration Hooks */ /* ------------------------------------------------------------------------- */ -/* Integration hooks ------------------------------------------------------- */ PyTypeObject SCA_MouseSensor::Type = { PyObject_HEAD_INIT(&PyType_Type) 0, @@ -283,37 +323,54 @@ PyParentObject SCA_MouseSensor::Parents[] = { }; PyMethodDef SCA_MouseSensor::Methods[] = { + //Deprecated functions ------> {"getXPosition", (PyCFunction) SCA_MouseSensor::sPyGetXPosition, METH_VARARGS, (PY_METHODCHAR)GetXPosition_doc}, {"getYPosition", (PyCFunction) SCA_MouseSensor::sPyGetYPosition, METH_VARARGS, (PY_METHODCHAR)GetYPosition_doc}, + //<----- Deprecated + KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus), {NULL,NULL} //Sentinel }; PyObject* SCA_MouseSensor::_getattr(const STR_String& attr) { + if (attr == "position") + return Py_BuildValue("[ii]", m_x, m_y); + + if (attr == "mode") + return PyInt_FromLong(m_mousemode); + _getattr_up(SCA_ISensor); } -/* get x position ---------------------------------------------------------- */ -const char SCA_MouseSensor::GetXPosition_doc[] = -"getXPosition\n" -"\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n" -"\tThe lower-left corner is the origin. The coordinate is given in\n" -"\tpixels\n"; -PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, - PyObject* args, - PyObject* kwds) { - return PyInt_FromLong(m_x); -} +int SCA_MouseSensor::_setattr(const STR_String& attr, PyObject *value) +{ + if (PyInt_Check(value)) + { + int val = PyInt_AsLong(value); -/* get y position ---------------------------------------------------------- */ -const char SCA_MouseSensor::GetYPosition_doc[] = -"getYPosition\n" -"\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n" -"\tThe lower-left corner is the origin. The coordinate is given in\n" -"\tpixels\n"; -PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, - PyObject* args, - PyObject* kwds) { - return PyInt_FromLong(m_y); + if (attr == "mode") + { + if ((val < KX_MOUSESENSORMODE_NODEF) + || (val > KX_MOUSESENSORMODE_MAX)){ + + PyErr_SetString(PyExc_ValueError, "invalid mode specified!"); + return NULL; + } + + m_mousemode = val; + UpdateHotkey(); + return 0; + } + } + else + { + if (attr == "position") + { + PyErr_SetString(PyExc_AttributeError, "read-only property!"); + return NULL; + } + } + + return SCA_ISensor::_setattr(attr, value); } /* eof */ diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.h b/source/gameengine/GameLogic/SCA_MouseSensor.h index 26a1c5e3fd2..6d6e641c798 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.h +++ b/source/gameengine/GameLogic/SCA_MouseSensor.h @@ -24,7 +24,7 @@ * * The Original Code is: all of this file. * - * Contributor(s): none yet. + * Contributor(s): José I. Romero (cleanup and fixes) * * ***** END GPL LICENSE BLOCK ***** */ @@ -87,6 +87,8 @@ class SCA_MouseSensor : public SCA_ISensor bool isValid(KX_MOUSESENSORMODE); + void UpdateHotkey(); + SCA_MouseSensor(class SCA_MouseManager* keybdmgr, int startx,int starty, short int mousemode, @@ -108,11 +110,17 @@ class SCA_MouseSensor : public SCA_ISensor /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); + //Deprecated functions -----> /* read x-coordinate */ KX_PYMETHOD_DOC(SCA_MouseSensor,GetXPosition); /* read y-coordinate */ KX_PYMETHOD_DOC(SCA_MouseSensor,GetYPosition); + //<----- deprecated + + // get button status + KX_PYMETHOD_DOC_O(SCA_MouseSensor,getButtonStatus); }; diff --git a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp index 3b541e87f02..f8509fe380e 100644 --- a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp +++ b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp @@ -245,17 +245,48 @@ PyParentObject SCA_PropertyActuator::Parents[] = { }; PyMethodDef SCA_PropertyActuator::Methods[] = { + //Deprecated functions ------> {"setProperty", (PyCFunction) SCA_PropertyActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, {"getProperty", (PyCFunction) SCA_PropertyActuator::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, {"setValue", (PyCFunction) SCA_PropertyActuator::sPySetValue, METH_VARARGS, (PY_METHODCHAR)SetValue_doc}, {"getValue", (PyCFunction) SCA_PropertyActuator::sPyGetValue, METH_VARARGS, (PY_METHODCHAR)GetValue_doc}, + //<----- Deprecated {NULL,NULL} //Sentinel }; PyObject* SCA_PropertyActuator::_getattr(const STR_String& attr) { + if (attr == "property") { + return PyString_FromString(m_propname); + } + if (attr == "value") { + return PyString_FromString(m_exprtxt); + } _getattr_up(SCA_IActuator); } +int SCA_PropertyActuator::_setattr(const STR_String& attr, PyObject *value) { + if (PyString_Check(value)) { + char* sval = PyString_AsString(value); + if (attr == "property") { + CValue* prop = GetParent()->FindIdentifier(sval); + bool error = prop->IsError(); + prop->Release(); + if (!prop->IsError()) { + m_propname = sval; + return 0; + } else { + PyErr_SetString(PyExc_ValueError, "string does not correspond to a property"); + return 1; + } + } + if (attr == "value") { + m_exprtxt = sval; + return 0; + } + } + return SCA_IActuator::_setattr(attr, value); +} + /* 1. setProperty */ const char SCA_PropertyActuator::SetProperty_doc[] = "setProperty(name)\n" @@ -264,6 +295,7 @@ const char SCA_PropertyActuator::SetProperty_doc[] = "\tof this name, the call is ignored.\n"; PyObject* SCA_PropertyActuator::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setProperty()", "the 'property' property"); /* Check whether the name exists first ! */ char *nameArg; if (!PyArg_ParseTuple(args, "s", &nameArg)) { @@ -288,6 +320,7 @@ const char SCA_PropertyActuator::GetProperty_doc[] = "\tReturn the property on which the actuator operates.\n"; PyObject* SCA_PropertyActuator::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_propname); } @@ -300,6 +333,7 @@ const char SCA_PropertyActuator::SetValue_doc[] = "\t action is ignored.\n"; PyObject* SCA_PropertyActuator::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setValue()", "the value property"); char *valArg; if(!PyArg_ParseTuple(args, "s", &valArg)) { return NULL; @@ -316,6 +350,7 @@ const char SCA_PropertyActuator::GetValue_doc[] = "\tReturns the value with which the actuator operates.\n"; PyObject* SCA_PropertyActuator::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getValue()", "the value property"); return PyString_FromString(m_exprtxt); } diff --git a/source/gameengine/GameLogic/SCA_PropertyActuator.h b/source/gameengine/GameLogic/SCA_PropertyActuator.h index 1e435684572..cdfeee4c67d 100644 --- a/source/gameengine/GameLogic/SCA_PropertyActuator.h +++ b/source/gameengine/GameLogic/SCA_PropertyActuator.h @@ -86,6 +86,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); // python wrapped methods KX_PYMETHOD_DOC(SCA_PropertyActuator,SetProperty); diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.cpp b/source/gameengine/GameLogic/SCA_PropertySensor.cpp index 64e3d49c6cb..19ee85207bb 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.cpp +++ b/source/gameengine/GameLogic/SCA_PropertySensor.cpp @@ -333,25 +333,72 @@ PyParentObject SCA_PropertySensor::Parents[] = { }; PyMethodDef SCA_PropertySensor::Methods[] = { + //Deprecated functions ------> {"getType", (PyCFunction) SCA_PropertySensor::sPyGetType, METH_VARARGS, (PY_METHODCHAR)GetType_doc}, {"setType", (PyCFunction) SCA_PropertySensor::sPySetType, METH_VARARGS, (PY_METHODCHAR)SetType_doc}, {"getProperty", (PyCFunction) SCA_PropertySensor::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, {"setProperty", (PyCFunction) SCA_PropertySensor::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, {"getValue", (PyCFunction) SCA_PropertySensor::sPyGetValue, METH_VARARGS, (PY_METHODCHAR)GetValue_doc}, {"setValue", (PyCFunction) SCA_PropertySensor::sPySetValue, METH_VARARGS, (PY_METHODCHAR)SetValue_doc}, + //<----- Deprecated {NULL,NULL} //Sentinel }; PyObject* SCA_PropertySensor::_getattr(const STR_String& attr) { + if (attr == "type") { + return PyInt_FromLong(m_checktype); + } + if (attr == "property") { + return PyString_FromString(m_checkpropname); + } + if (attr == "value") { + return PyString_FromString(m_checkpropval); + } _getattr_up(SCA_ISensor); /* implicit return! */ } +int SCA_PropertySensor::_setattr(const STR_String& attr, PyObject *value) { + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); + if (attr == "type") { + if ((ival <= KX_PROPSENSOR_NODEF) || (ival >= KX_PROPSENSOR_MAX)) { + PyErr_SetString(PyExc_ValueError, "type out of range"); + return 1; + } + m_checktype = ival; + } + return 0; + } + if (PyString_Check(value)) { + char* sval = PyString_AsString(value); + if (attr == "property") { + CValue *prop = FindIdentifier(STR_String(sval)); + bool error = prop->IsError(); + prop->Release(); + if (error) { + PyErr_SetString(PyExc_ValueError, "string does not correspond to a property"); + return 1; + } + m_checkpropname = sval; + } else if (attr == "value") { + if (!validValueForProperty(sval, m_checkpropname)) { + PyErr_SetString(PyExc_ValueError, "string does not represent a suitable value for the property"); + return 1; + } + m_checkpropval = sval; + } + return 0; + } + return SCA_ISensor::_setattr(attr, value); +} + /* 1. getType */ const char SCA_PropertySensor::GetType_doc[] = "getType()\n" "\tReturns the type of check this sensor performs.\n"; PyObject* SCA_PropertySensor::PyGetType(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getType()", "the type property"); return PyInt_FromLong(m_checktype); } @@ -364,6 +411,7 @@ const char SCA_PropertySensor::SetType_doc[] = "\tSet the type of check to perform.\n"; PyObject* SCA_PropertySensor::PySetType(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setType()", "the type property"); int typeArg; if (!PyArg_ParseTuple(args, "i", &typeArg)) { @@ -384,6 +432,7 @@ const char SCA_PropertySensor::GetProperty_doc[] = "\tReturn the property with which the sensor operates.\n"; PyObject* SCA_PropertySensor::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_checkpropname); } @@ -395,6 +444,7 @@ const char SCA_PropertySensor::SetProperty_doc[] = "\tof this name, the call is ignored.\n"; PyObject* SCA_PropertySensor::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setProperty()", "the 'property' property"); /* We should query whether the name exists. Or should we create a prop */ /* on the fly? */ char *propNameArg = NULL; @@ -419,6 +469,7 @@ const char SCA_PropertySensor::GetValue_doc[] = "\tReturns the value with which the sensor operates.\n"; PyObject* SCA_PropertySensor::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getValue()", "the value property"); return PyString_FromString(m_checkpropval); } @@ -431,6 +482,7 @@ const char SCA_PropertySensor::SetValue_doc[] = "\t action is ignored.\n"; PyObject* SCA_PropertySensor::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setValue()", "the value property"); /* Here, we need to check whether the value is 'valid' for this property.*/ /* We know that the property exists, or is NULL. */ char *propValArg = NULL; diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.h b/source/gameengine/GameLogic/SCA_PropertySensor.h index 6871cb3afdc..81203c78f9d 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.h +++ b/source/gameengine/GameLogic/SCA_PropertySensor.h @@ -90,6 +90,7 @@ public: /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); /* 1. getType */ KX_PYMETHOD_DOC(SCA_PropertySensor,GetType); diff --git a/source/gameengine/GameLogic/SCA_PythonController.cpp b/source/gameengine/GameLogic/SCA_PythonController.cpp index c354ab39747..e879481a444 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.cpp +++ b/source/gameengine/GameLogic/SCA_PythonController.cpp @@ -233,9 +233,11 @@ PyMethodDef SCA_PythonController::Methods[] = { {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, (PY_METHODCHAR)SCA_PythonController::GetActuator_doc}, {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetSensors_doc}, {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, (PY_METHODCHAR)SCA_PythonController::GetSensor_doc}, - {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS}, {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O}, + //Deprecated functions ------> + {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS}, {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS}, + //<----- Deprecated {NULL,NULL} //Sentinel }; @@ -329,9 +331,27 @@ void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr) PyObject* SCA_PythonController::_getattr(const STR_String& attr) { + if (attr == "script") { + return PyString_FromString(m_scriptText); + } + if (attr == "state") { + return PyInt_FromLong(m_statemask); + } _getattr_up(SCA_IController); } +int SCA_PythonController::_setattr(const STR_String& attr, PyObject *value) +{ + if (attr == "script") { + PyErr_SetString(PyExc_AttributeError, "script is read only, use setScript() to update the script"); + return 1; + } + if (attr == "state") { + PyErr_SetString(PyExc_AttributeError, "state is read only"); + return 1; + } + return SCA_IController::_setattr(attr, value); +} PyObject* SCA_PythonController::PyGetActuators(PyObject* self) @@ -420,6 +440,7 @@ SCA_PythonController::PyGetSensors(PyObject* self) /* 1. getScript */ PyObject* SCA_PythonController::PyGetScript(PyObject* self) { + ShowDeprecationWarning("getScript()", "the script property"); return PyString_FromString(m_scriptText); } @@ -443,6 +464,7 @@ PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value) /* 1. getScript */ PyObject* SCA_PythonController::PyGetState(PyObject* self) { + ShowDeprecationWarning("getState()", "the state property"); return PyInt_FromLong(m_statemask); } diff --git a/source/gameengine/GameLogic/SCA_PythonController.h b/source/gameengine/GameLogic/SCA_PythonController.h index d9b2e242bea..1918cc488d8 100644 --- a/source/gameengine/GameLogic/SCA_PythonController.h +++ b/source/gameengine/GameLogic/SCA_PythonController.h @@ -77,6 +77,7 @@ class SCA_PythonController : public SCA_IController static PyObject* sPyAddActiveActuator(PyObject* self, PyObject* args); virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetSensors); KX_PYMETHOD_DOC_NOARGS(SCA_PythonController,GetActuators); diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.cpp b/source/gameengine/GameLogic/SCA_RandomActuator.cpp index 7b50a483cd8..8dd405c3d82 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.cpp +++ b/source/gameengine/GameLogic/SCA_RandomActuator.cpp @@ -339,6 +339,7 @@ PyParentObject SCA_RandomActuator::Parents[] = { }; PyMethodDef SCA_RandomActuator::Methods[] = { + //Deprecated functions ------> {"setSeed", (PyCFunction) SCA_RandomActuator::sPySetSeed, METH_VARARGS, (PY_METHODCHAR)SetSeed_doc}, {"getSeed", (PyCFunction) SCA_RandomActuator::sPyGetSeed, METH_VARARGS, (PY_METHODCHAR)GetSeed_doc}, {"getPara1", (PyCFunction) SCA_RandomActuator::sPyGetPara1, METH_VARARGS, (PY_METHODCHAR)GetPara1_doc}, @@ -346,6 +347,7 @@ PyMethodDef SCA_RandomActuator::Methods[] = { {"getDistribution", (PyCFunction) SCA_RandomActuator::sPyGetDistribution, METH_VARARGS, (PY_METHODCHAR)GetDistribution_doc}, {"setProperty", (PyCFunction) SCA_RandomActuator::sPySetProperty, METH_VARARGS, (PY_METHODCHAR)SetProperty_doc}, {"getProperty", (PyCFunction) SCA_RandomActuator::sPyGetProperty, METH_VARARGS, (PY_METHODCHAR)GetProperty_doc}, + //<----- Deprecated {"setBoolConst", (PyCFunction) SCA_RandomActuator::sPySetBoolConst, METH_VARARGS, (PY_METHODCHAR)SetBoolConst_doc}, {"setBoolUniform", (PyCFunction) SCA_RandomActuator::sPySetBoolUniform, METH_VARARGS, (PY_METHODCHAR)SetBoolUniform_doc}, {"setBoolBernouilli",(PyCFunction) SCA_RandomActuator::sPySetBoolBernouilli, METH_VARARGS, (PY_METHODCHAR)SetBoolBernouilli_doc}, @@ -360,9 +362,60 @@ PyMethodDef SCA_RandomActuator::Methods[] = { }; PyObject* SCA_RandomActuator::_getattr(const STR_String& attr) { + if (attr == "seed") { + return PyInt_FromLong(m_base->GetSeed()); + } + if (attr == "para1") { + return PyFloat_FromDouble(m_parameter1); + } + if (attr == "para2") { + return PyFloat_FromDouble(m_parameter2); + } + if (attr == "distribution") { + return PyInt_FromLong(m_distribution); + } + if (attr == "property") { + return PyString_FromString(m_propname); + } _getattr_up(SCA_IActuator); } +int SCA_RandomActuator::_setattr(const STR_String& attr, PyObject *value) +{ + if (attr == "para1") { + PyErr_SetString(PyExc_AttributeError, "para1 is read only"); + } + if (attr == "para2") { + PyErr_SetString(PyExc_AttributeError, "para2 is read only"); + } + if (attr == "distribution") { + PyErr_SetString(PyExc_AttributeError, "distribution is read only"); + } + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); + if (attr == "seed") { + m_base->SetSeed(ival); + } + return 0; + } + if (PyString_Check(value)) { + char* sval = PyString_AsString(value); + if (attr == "property") { + CValue* prop = GetParent()->FindIdentifier(sval); + bool error = prop->IsError(); + prop->Release(); + if (!prop->IsError()) { + m_propname = sval; + return 0; + } else { + PyErr_SetString(PyExc_ValueError, "string does not correspond to a property"); + return 1; + } + } + } + return SCA_IActuator::_setattr(attr, value); +} + /* 1. setSeed */ const char SCA_RandomActuator::SetSeed_doc[] = "setSeed(seed)\n" @@ -371,6 +424,7 @@ const char SCA_RandomActuator::SetSeed_doc[] = "\tequal series. If the seed is 0, the generator will produce\n" "\tthe same value on every call.\n"; PyObject* SCA_RandomActuator::PySetSeed(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setSeed()", "the seed property"); long seedArg; if(!PyArg_ParseTuple(args, "i", &seedArg)) { return NULL; @@ -386,6 +440,7 @@ const char SCA_RandomActuator::GetSeed_doc[] = "\tReturns the initial seed of the generator. Equal seeds produce\n" "\tequal series.\n"; PyObject* SCA_RandomActuator::PyGetSeed(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getSeed()", "the seed property"); return PyInt_FromLong(m_base->GetSeed()); } @@ -396,6 +451,7 @@ const char SCA_RandomActuator::GetPara1_doc[] = "\tto the documentation of the generator types for the meaning\n" "\tof this value."; PyObject* SCA_RandomActuator::PyGetPara1(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getPara1()", "the para1 property"); return PyFloat_FromDouble(m_parameter1); } @@ -406,6 +462,7 @@ const char SCA_RandomActuator::GetPara2_doc[] = "\tto the documentation of the generator types for the meaning\n" "\tof this value."; PyObject* SCA_RandomActuator::PyGetPara2(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getPara2()", "the para2 property"); return PyFloat_FromDouble(m_parameter2); } @@ -414,6 +471,7 @@ const char SCA_RandomActuator::GetDistribution_doc[] = "getDistribution()\n" "\tReturns the type of the active distribution.\n"; PyObject* SCA_RandomActuator::PyGetDistribution(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getDistribution()", "the distribution property"); return PyInt_FromLong(m_distribution); } @@ -424,6 +482,7 @@ const char SCA_RandomActuator::SetProperty_doc[] = "\tSet the property to which the random value is assigned. If the \n" "\tgenerator and property types do not match, the assignment is ignored.\n"; PyObject* SCA_RandomActuator::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("setProperty()", "the 'property' property"); char *nameArg; if (!PyArg_ParseTuple(args, "s", &nameArg)) { return NULL; @@ -446,6 +505,7 @@ const char SCA_RandomActuator::GetProperty_doc[] = "\tReturn the property to which the random value is assigned. If the \n" "\tgenerator and property types do not match, the assignment is ignored.\n"; PyObject* SCA_RandomActuator::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) { + ShowDeprecationWarning("getProperty()", "the 'property' property"); return PyString_FromString(m_propname); } @@ -463,9 +523,7 @@ PyObject* SCA_RandomActuator::PySetBoolConst(PyObject* self, } m_distribution = KX_RANDOMACT_BOOL_CONST; - if (paraArg) { - m_parameter1 = 1; - } + m_parameter1 = (paraArg) ? 1.0 : 0.0; Py_Return; } diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.h b/source/gameengine/GameLogic/SCA_RandomActuator.h index f6da0595d1a..2fc14f9a0b7 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.h +++ b/source/gameengine/GameLogic/SCA_RandomActuator.h @@ -97,6 +97,7 @@ class SCA_RandomActuator : public SCA_IActuator /* --------------------------------------------------------------------- */ virtual PyObject* _getattr(const STR_String& attr); + virtual int _setattr(const STR_String& attr, PyObject *value); /* 1. setSeed */ KX_PYMETHOD_DOC(SCA_RandomActuator,SetSeed); |