- Attributes for the collision sensor: propertyName, materialCheck, pulseCollisions...
authorCampbell Barton <ideasman42@gmail.com>
Thu, 26 Feb 2009 04:17:23 +0000 (04:17 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 26 Feb 2009 04:17:23 +0000 (04:17 +0000)
Removed a check in Python API touch.setProperty() for the property name on the sensor owner before allowing the name to be set - it makes no sense and isnt checked when creating the sensor.
- SCA_DelaySensor.py indent error making epydoc fail.

source/gameengine/Ketsji/KX_NearSensor.cpp
source/gameengine/Ketsji/KX_TouchSensor.cpp
source/gameengine/Ketsji/KX_TouchSensor.h
source/gameengine/PyDoc/KX_TouchSensor.py
source/gameengine/PyDoc/SCA_DelaySensor.py

index a4aebdd8b6b155c6c655b08b2bab11ee37b40904..d859f670b07122cf86bbe5cea3f66047b7e38f22 100644 (file)
@@ -311,10 +311,10 @@ PyParentObject KX_NearSensor::Parents[] = {
 
 
 PyMethodDef KX_NearSensor::Methods[] = {
-       {"setProperty", (PyCFunction) KX_NearSensor::sPySetProperty,      METH_VARARGS, (PY_METHODCHAR)SetProperty_doc},
-       {"getProperty", (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_VARARGS, (PY_METHODCHAR)GetProperty_doc},
-       {"getHitObject",(PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_VARARGS, (PY_METHODCHAR)GetHitObject_doc},
-       {"getHitObjectList", (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_VARARGS, (PY_METHODCHAR)GetHitObjectList_doc},
+       {"setProperty", (PyCFunction) KX_NearSensor::sPySetProperty,      METH_O, (PY_METHODCHAR)SetProperty_doc},
+       {"getProperty", (PyCFunction) KX_NearSensor::sPyGetProperty,      METH_NOARGS, (PY_METHODCHAR)GetProperty_doc},
+       {"getHitObject",(PyCFunction) KX_NearSensor::sPyGetHitObject,     METH_NOARGS, (PY_METHODCHAR)GetHitObject_doc},
+       {"getHitObjectList", (PyCFunction) KX_NearSensor::sPyGetHitObjectList, METH_NOARGS, (PY_METHODCHAR)GetHitObjectList_doc},
        {NULL,NULL} //Sentinel
 };
 
index a30dc34e2fa8d92e073a05767bf362aee8b810ab..de4b5c401fcd2c29de6a4fd7e5c3388b8f851727 100644 (file)
@@ -270,20 +270,57 @@ PyParentObject KX_TouchSensor::Parents[] = {
 
 PyMethodDef KX_TouchSensor::Methods[] = {
        {"setProperty", 
-        (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_VARARGS, (PY_METHODCHAR)SetProperty_doc},
+        (PyCFunction) KX_TouchSensor::sPySetProperty,      METH_O, (PY_METHODCHAR)SetProperty_doc},
        {"getProperty", 
-        (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_VARARGS, (PY_METHODCHAR)GetProperty_doc},
+        (PyCFunction) KX_TouchSensor::sPyGetProperty,      METH_NOARGS, (PY_METHODCHAR)GetProperty_doc},
        {"getHitObject", 
-        (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_VARARGS, (PY_METHODCHAR)GetHitObject_doc},
+        (PyCFunction) KX_TouchSensor::sPyGetHitObject,     METH_NOARGS, (PY_METHODCHAR)GetHitObject_doc},
        {"getHitObjectList", 
-        (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_VARARGS, (PY_METHODCHAR)GetHitObjectList_doc},
+        (PyCFunction) KX_TouchSensor::sPyGetHitObjectList, METH_NOARGS, (PY_METHODCHAR)GetHitObjectList_doc},
        {NULL,NULL} //Sentinel
 };
 
-PyObject* KX_TouchSensor::_getattr(const char *attr) {
+PyAttributeDef KX_TouchSensor::Attributes[] = {
+       KX_PYATTRIBUTE_STRING_RW("propertyName",0,100,false,KX_TouchSensor,m_touchedpropname),
+       KX_PYATTRIBUTE_BOOL_RW("materialCheck",KX_TouchSensor,m_bFindMaterial),
+       KX_PYATTRIBUTE_BOOL_RW("pulseCollisions",KX_TouchSensor,m_bTouchPulse),
+       { NULL }        //Sentinel
+};
+
+PyObject* KX_TouchSensor::_getattr(const char *attr)
+{      
+       if (!strcmp(attr, "objectHit")) {
+               if (m_hitObject)        return m_hitObject->AddRef();
+               else                            Py_RETURN_NONE;
+       }
+       if (!strcmp(attr, "objectHitList")) {
+               return m_colliders->AddRef();
+       }
+
+       PyObject* object= _getattr_self(Attributes, this, attr);
+       if (object != NULL)
+               return object;
        _getattr_up(SCA_ISensor);
 }
 
+int KX_TouchSensor::_setattr(const char *attr, PyObject *value)
+{
+       int ret = _setattr_self(Attributes, this, attr, value);
+       if (ret >= 0)
+               return ret;
+       
+       if (!strcmp(attr, "objectHit")) {
+               PyErr_SetString(PyExc_AttributeError, "attribute \"objectHit\" is read only");
+               return 1;
+       }
+       if (!strcmp(attr, "objectHitList")) {
+               PyErr_SetString(PyExc_AttributeError, "attribute \"objectHit\" is read only");
+               return 1;
+       }
+       
+       return SCA_ISensor::_setattr(attr, value);
+}
+
 /* Python API */
 
 /* 1. setProperty */
@@ -293,23 +330,16 @@ const char KX_TouchSensor::SetProperty_doc[] =
 "\tSet the property or material to collide with. Use\n"
 "\tsetTouchMaterial() to switch between properties and\n"
 "\tmaterials.";
-PyObject* KX_TouchSensor::PySetProperty(PyObject* self, 
-                                                                               PyObject* args, 
-                                                                               PyObject* kwds) {
-       char *nameArg;
-       if (!PyArg_ParseTuple(args, "s", &nameArg)) {
+PyObject* KX_TouchSensor::PySetProperty(PyObject* self, PyObject* value)
+{
+       ShowDeprecationWarning("setProperty()", "the propertyName property");
+       char *nameArg= PyString_AsString(value);
+       if (nameArg==NULL) {
+               PyErr_SetString(PyExc_ValueError, "expected a ");
                return NULL;
        }
-
-       CValue* prop = GetParent()->FindIdentifier(nameArg);
-
-       if (!prop->IsError()) {
-               m_touchedpropname = nameArg;
-       } else {
-               ; /* not found ... */
-       }
-       prop->Release();
        
+       m_touchedpropname = nameArg;
        Py_RETURN_NONE;
 }
 /* 2. getProperty */
@@ -318,19 +348,16 @@ const char KX_TouchSensor::GetProperty_doc[] =
 "\tReturns the property or material to collide with. Use\n"
 "\tgetTouchMaterial() to find out whether this sensor\n"
 "\tlooks for properties or materials.";
-PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self, 
-                                                                                PyObject* args, 
-                                                                                PyObject* kwds) {
+PyObject*  KX_TouchSensor::PyGetProperty(PyObject* self) {
        return PyString_FromString(m_touchedpropname);
 }
 
 const char KX_TouchSensor::GetHitObject_doc[] = 
 "getHitObject()\n"
 ;
-PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self, 
-                                                                                PyObject* args, 
-                                                                                PyObject* kwds)
+PyObject* KX_TouchSensor::PyGetHitObject(PyObject* self)
 {
+       ShowDeprecationWarning("getHitObject()", "the objectHit property");
        /* to do: do Py_IncRef if the object is already known in Python */
        /* otherwise, this leaks memory */
        if (m_hitObject)
@@ -344,13 +371,11 @@ const char KX_TouchSensor::GetHitObjectList_doc[] =
 "getHitObjectList()\n"
 "\tReturn a list of the objects this object collided with,\n"
 "\tbut only those matching the property/material condition.\n";
-PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self, 
-                                                                                PyObject* args, 
-                                                                                PyObject* kwds)
+PyObject* KX_TouchSensor::PyGetHitObjectList(PyObject* self)
 {
-
+       ShowDeprecationWarning("getHitObjectList()", "the objectHitList property");
        /* to do: do Py_IncRef if the object is already known in Python */
-       /* otherwise, this leaks memory */
+       /* otherwise, this leaks memory */ /* Edit, this seems ok and not to leak memory - Campbell */
        return m_colliders->AddRef();
 }
 
@@ -359,24 +384,25 @@ const char KX_TouchSensor::GetTouchMaterial_doc[] =
 "getTouchMaterial()\n"
 "\tReturns KX_TRUE if this sensor looks for a specific material,\n"
 "\tKX_FALSE if it looks for a specific property.\n" ;
-PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self, 
-                                                                                        PyObject* args, 
-                                                                                        PyObject* kwds)
+PyObject* KX_TouchSensor::PyGetTouchMaterial(PyObject* self)
 {
+       ShowDeprecationWarning("getTouchMaterial()", "the materialCheck property");
        return PyInt_FromLong(m_bFindMaterial);
 }
 
 /* 6. setTouchMaterial */
+#if 0
 const char KX_TouchSensor::SetTouchMaterial_doc[] = 
 "setTouchMaterial(flag)\n"
 "\t- flag: KX_TRUE or KX_FALSE.\n"
 "\tSet flag to KX_TRUE to switch on positive pulse mode,\n"
 "\tKX_FALSE to switch off positive pulse mode.\n" ;
-PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyObject* kwds)
+PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject *value)
 {
-       int pulseArg = 0;
+       int pulseArg = PyInt_AsLong(value);
 
-       if(!PyArg_ParseTuple(args, "i", &pulseArg)) {
+       if(pulseArg ==-1 && PyErr_Occurred()) {
+               PyErr_SetString(PyExc_ValueError, "expected a bool");
                return NULL;
        }
        
@@ -384,6 +410,6 @@ PyObject* KX_TouchSensor::PySetTouchMaterial(PyObject* self, PyObject* args, PyO
 
        Py_RETURN_NONE;
 }
-
+#endif
 
 /* eof */
index c463226a5be534aeb5466e8567d468e4cf6e1952..c523736967b799803a3fecca23850d5f647a847e 100644 (file)
@@ -121,19 +121,22 @@ public:
        /* --------------------------------------------------------------------- */
        
        virtual PyObject* _getattr(const char *attr);
+       virtual int _setattr(const char *attr, PyObject *value);
 
        /* 1. setProperty */
-       KX_PYMETHOD_DOC(KX_TouchSensor,SetProperty);
+       KX_PYMETHOD_DOC_O(KX_TouchSensor,SetProperty);
        /* 2. getProperty */
-       KX_PYMETHOD_DOC(KX_TouchSensor,GetProperty);
+       KX_PYMETHOD_DOC_NOARGS(KX_TouchSensor,GetProperty);
        /* 3. getHitObject */
-       KX_PYMETHOD_DOC(KX_TouchSensor,GetHitObject);
+       KX_PYMETHOD_DOC_NOARGS(KX_TouchSensor,GetHitObject);
        /* 4. getHitObject */
-       KX_PYMETHOD_DOC(KX_TouchSensor,GetHitObjectList);
+       KX_PYMETHOD_DOC_NOARGS(KX_TouchSensor,GetHitObjectList);
        /* 5. getTouchMaterial */
-       KX_PYMETHOD_DOC(KX_TouchSensor,GetTouchMaterial);
+       KX_PYMETHOD_DOC_NOARGS(KX_TouchSensor,GetTouchMaterial);
+#if 0
        /* 6. setTouchMaterial */
-       KX_PYMETHOD_DOC(KX_TouchSensor,SetTouchMaterial);
+       KX_PYMETHOD_DOC_O(KX_TouchSensor,SetTouchMaterial);
+#endif
        
 };
 
index f2cc101af102986cda96cc91f82961a3810141f4..4f020930c4c3cef161104705b9730761b11ef803 100644 (file)
@@ -1,13 +1,26 @@
 # $Id$
 # Documentation for KX_TouchSensor
 from SCA_ISensor import *
+from KX_GameObject import *
 
 class KX_TouchSensor(SCA_ISensor):
        """
        Touch sensor detects collisions between objects.
+       
+       @ivar propertyName: The name of the property or material this sensor detects (depending on the materialCheck property).
+       @type propertyName: string
+       @ivar materialCheck: when enabled this sensor checks for object materials rather then properties.
+       @type materialCheck: bool
+       @ivar pulseCollisions: The last collided object.
+       @type pulseCollisions: bool
+       @ivar objectHit: The last collided object. (Read Only)
+       @type objectHit: L{KX_GameObject} or None
+       @ivar objectHitList: A list of colliding objects. (Read Only)
+       @type objectHitList: list
        """
        def setProperty(name):
                """
+               DEPRECATED: use the propertyName property
                Set the property or material to collide with. Use
                setTouchMaterial() to switch between properties and
                materials.
@@ -15,22 +28,25 @@ class KX_TouchSensor(SCA_ISensor):
                """
        def getProperty():
                """
+               DEPRECATED: use the propertyName property
                Returns the property or material to collide with. Use
                getTouchMaterial() to find out whether this sensor
-               looks for properties or materials.
+               looks for properties or materials. (B{deprecated})
                
                @rtype: string
                """
 
        def getHitObject():
                """
-               Returns the last object hit by this touch sensor.
+               DEPRECATED: use the objectHit property
+               Returns the last object hit by this touch sensor. (B{deprecated})
                
                @rtype: L{KX_GameObject}
                """
        def getHitObjectList():
                """
-               Returns a list of all objects hit in the last frame.
+               DEPRECATED: use the objectHitList property
+               Returns a list of all objects hit in the last frame. (B{deprecated})
                
                Only objects that have the requisite material/property are listed.
                
@@ -38,13 +54,7 @@ class KX_TouchSensor(SCA_ISensor):
                """
        def getTouchMaterial():
                """
+               DEPRECATED: use the materialCheck property
                Returns KX_TRUE if this sensor looks for a specific material,
-               KX_FALSE if it looks for a specific property.
-               """
-       def setTouchMaterial(flag):
-               """
-               Set flag to KX_TRUE to switch on positive pulse mode,
-               KX_FALSE to switch off positive pulse mode.
-               
-               @type flag: KX_TRUE or KX_FALSE.
+               KX_FALSE if it looks for a specific property. (B{deprecated})
                """
index b99ed08bed5378e2e423888474a06850a318751b..6560df6573e0394c6bb00083cc774893b778860b 100644 (file)
@@ -20,10 +20,9 @@ class SCA_DelaySensor(SCA_ISensor):
        @type delay: integer.
        @ivar duration: length of the ON period in number of frame after the initial OFF period.
                        If duration is greater than 0, a negative trigger is sent at the end of the ON pulse.
-    @type duration: integer
-    @ivar repeat: 1 if the OFF-ON cycle should be repeated indefinately, 0 if it should run once.
-    @type repeat: integer
-    
+       @type duration: integer
+       @ivar repeat: 1 if the OFF-ON cycle should be repeated indefinately, 0 if it should run once.
+       @type repeat: integer
        """
        def setDelay(delay):
                """