diff options
author | Benoit Bolsee <benoit.bolsee@online.be> | 2009-01-02 20:43:56 +0300 |
---|---|---|
committer | Benoit Bolsee <benoit.bolsee@online.be> | 2009-01-02 20:43:56 +0300 |
commit | cc569504d0468ec19a1018ea804aa418c134cb0f (patch) | |
tree | 732f5d6fe3d742f63dae5f6e899aea566ae52b0f /source/gameengine/GameLogic | |
parent | abd4934d1aeceafaa2fc242f00db0ba53a7e24db (diff) |
BGE API Cleanup: update the python attribute definition framework.
* Value clamping to min/max is now supported as an option for integer, float
and string attribute (for string clamping=trim to max length)
* Post check function now take PyAttributeDef parameter so that more
generic function can be written.
* Definition of SCA_ILogicBrick::CheckProperty() function to check that
a string attribute contains a valid property name of the parent game object.
* Definition of enum attribute vi KX_PYATTRIBUTE_ENUM... macros.
Enum are handled just like integer but to be totally paranoid, the sizeof()
of the enum member is check at run time to match integer size.
* More bricks updated to use the framework.
Diffstat (limited to 'source/gameengine/GameLogic')
-rw-r--r-- | source/gameengine/GameLogic/SCA_ActuatorSensor.cpp | 39 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_ActuatorSensor.h | 1 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_DelaySensor.cpp | 45 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_ILogicBrick.cpp | 18 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_ILogicBrick.h | 3 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_ISensor.cpp | 73 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_JoystickSensor.cpp | 10 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_JoystickSensor.h | 22 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_KeyboardSensor.cpp | 10 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_MouseSensor.cpp | 6 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_MouseSensor.h | 2 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_PropertyActuator.cpp | 37 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_PropertySensor.cpp | 65 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_PropertySensor.h | 9 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_RandomActuator.cpp | 61 | ||||
-rw-r--r-- | source/gameengine/GameLogic/SCA_RandomActuator.h | 1 |
16 files changed, 157 insertions, 245 deletions
diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp index 5ad28dbf329..fae8d2ba5a7 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.cpp @@ -156,27 +156,34 @@ PyMethodDef SCA_ActuatorSensor::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_ActuatorSensor::Attributes[] = { + KX_PYATTRIBUTE_STRING_RW_CHECK("actuator",0,100,false,SCA_ActuatorSensor,m_checkactname,CheckActuator), + { NULL } //Sentinel +}; + PyObject* SCA_ActuatorSensor::_getattr(const STR_String& attr) { - if (attr == "actuator") { - return PyString_FromString(m_checkactname); - } + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; _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; - } +int SCA_ActuatorSensor::CheckActuator(void *self, const PyAttributeDef*) +{ + SCA_ActuatorSensor* sensor = reinterpret_cast<SCA_ActuatorSensor*>(self); + SCA_IActuator* act = sensor->GetParent()->FindActuator(sensor->m_checkactname); + if (act) { + sensor->m_actuator = act; + return 0; } + PyErr_SetString(PyExc_AttributeError, "string does not correspond to an actuator"); + return 1; +} + +int SCA_ActuatorSensor::_setattr(const STR_String& attr, PyObject *value) { + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; return SCA_ISensor::_setattr(attr, value); } diff --git a/source/gameengine/GameLogic/SCA_ActuatorSensor.h b/source/gameengine/GameLogic/SCA_ActuatorSensor.h index 4fe4700602c..3d64247461c 100644 --- a/source/gameengine/GameLogic/SCA_ActuatorSensor.h +++ b/source/gameengine/GameLogic/SCA_ActuatorSensor.h @@ -69,6 +69,7 @@ public: /* 4. getProperty */ KX_PYMETHOD_DOC_NOARGS(SCA_ActuatorSensor,GetActuator); + static int CheckActuator(void *self, const PyAttributeDef*); }; #endif diff --git a/source/gameengine/GameLogic/SCA_DelaySensor.cpp b/source/gameengine/GameLogic/SCA_DelaySensor.cpp index 362483b58a2..4c97ca98d72 100644 --- a/source/gameengine/GameLogic/SCA_DelaySensor.cpp +++ b/source/gameengine/GameLogic/SCA_DelaySensor.cpp @@ -171,43 +171,24 @@ PyMethodDef SCA_DelaySensor::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_DelaySensor::Attributes[] = { + KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay), + KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration), + KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat), + { 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); - } + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; _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; - } - } + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; return SCA_ISensor::_setattr(attr, value); } diff --git a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp index 515b485061d..37658480c05 100644 --- a/source/gameengine/GameLogic/SCA_ILogicBrick.cpp +++ b/source/gameengine/GameLogic/SCA_ILogicBrick.cpp @@ -252,7 +252,23 @@ PyMethodDef SCA_ILogicBrick::Methods[] = { {NULL,NULL} //Sentinel }; - +int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef) +{ + if (attrdef->m_type != KX_PYATTRIBUTE_TYPE_STRING || attrdef->m_length != 1) { + PyErr_SetString(PyExc_AttributeError, "inconsistent check function for attribute type, report to blender.org"); + return 1; + } + SCA_ILogicBrick* brick = reinterpret_cast<SCA_ILogicBrick*>(self); + STR_String* var = reinterpret_cast<STR_String*>((char*)self+attrdef->m_offset); + CValue* prop = brick->GetParent()->FindIdentifier(*var); + bool error = prop->IsError(); + prop->Release(); + if (error) { + PyErr_SetString(PyExc_ValueError, "string does not correspond to a property"); + return 1; + } + return 0; +} PyObject* SCA_ILogicBrick::_getattr(const STR_String& attr) diff --git a/source/gameengine/GameLogic/SCA_ILogicBrick.h b/source/gameengine/GameLogic/SCA_ILogicBrick.h index c28711ac0f6..38ec80d4096 100644 --- a/source/gameengine/GameLogic/SCA_ILogicBrick.h +++ b/source/gameengine/GameLogic/SCA_ILogicBrick.h @@ -89,6 +89,9 @@ public: KX_PYMETHOD(SCA_ILogicBrick,SetExecutePriority); KX_PYMETHOD_NOARGS(SCA_ILogicBrick,GetExecutePriority); + // check that attribute is a property + static int CheckProperty(void *self, const PyAttributeDef *attrdef); + enum KX_BOOL_TYPE { KX_BOOL_NODEF = 0, KX_TRUE, diff --git a/source/gameengine/GameLogic/SCA_ISensor.cpp b/source/gameengine/GameLogic/SCA_ISensor.cpp index 6e46c8504bb..68a3a93eab0 100644 --- a/source/gameengine/GameLogic/SCA_ISensor.cpp +++ b/source/gameengine/GameLogic/SCA_ISensor.cpp @@ -448,19 +448,24 @@ PyMethodDef SCA_ISensor::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_ISensor::Attributes[] = { + KX_PYATTRIBUTE_BOOL_RW("usePosPulseMode",SCA_ISensor,m_pos_pulsemode), + KX_PYATTRIBUTE_BOOL_RW("useNegPulseMode",SCA_ISensor,m_neg_pulsemode), + KX_PYATTRIBUTE_INT_RW("frequency",0,100000,true,SCA_ISensor,m_pulse_frequency), + KX_PYATTRIBUTE_BOOL_RW("invert",SCA_ISensor,m_invert), + KX_PYATTRIBUTE_BOOL_RW("level",SCA_ISensor,m_level), + // make these properties read-only in _setaddr, must still implement them in _getattr + KX_PYATTRIBUTE_DUMMY("triggered"), + KX_PYATTRIBUTE_DUMMY("positive"), + { 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); + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; if (attr == "triggered") { int retval = 0; @@ -473,54 +478,14 @@ SCA_ISensor::_getattr(const STR_String& attr) int retval = IsPositiveTrigger(); return PyInt_FromLong(retval); } - - _getattr_up(SCA_ILogicBrick); + _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; - } - } - + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; return SCA_ILogicBrick::_setattr(attr, value); } /* eof */ diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp index ce9058448ac..694bca9bac5 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp @@ -328,11 +328,11 @@ PyMethodDef SCA_JoystickSensor::Methods[] = { }; PyAttributeDef SCA_JoystickSensor::Attributes[] = { - KX_PYATTRIBUTE_SHORT_RW("index",0,JOYINDEX_MAX-1,SCA_JoystickSensor,m_joyindex), - KX_PYATTRIBUTE_INT_RW("threshold",0,32768,SCA_JoystickSensor,m_precision), - KX_PYATTRIBUTE_INT_RW("button",0,100,SCA_JoystickSensor,m_button), - KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("axis",0,3,SCA_JoystickSensor,m_axis,2,CheckAxis), - KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("hat",0,12,SCA_JoystickSensor,m_hat,2,CheckHat), + KX_PYATTRIBUTE_SHORT_RW("index",0,JOYINDEX_MAX-1,true,SCA_JoystickSensor,m_joyindex), + KX_PYATTRIBUTE_INT_RW("threshold",0,32768,true,SCA_JoystickSensor,m_precision), + KX_PYATTRIBUTE_INT_RW("button",0,100,false,SCA_JoystickSensor,m_button), + KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("axis",0,3,true,SCA_JoystickSensor,m_axis,2,CheckAxis), + KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("hat",0,12,true,SCA_JoystickSensor,m_hat,2,CheckHat), // dummy attributes will just be read-only in _setattr // you still need to defined them in _getattr KX_PYATTRIBUTE_DUMMY("axisPosition"), diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.h b/source/gameengine/GameLogic/SCA_JoystickSensor.h index 25103b3c6bc..fa11f1cc3d0 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.h +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.h @@ -149,24 +149,22 @@ public: KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,Connected); /* attribute check */ - static int CheckAxis(void *self) + static int CheckAxis(void *self, const PyAttributeDef*) { SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self); - if (sensor->m_axis < 1 || sensor->m_axis > 2) - { - PyErr_SetString(PyExc_ValueError, "axis number must be 1 or 2"); - return 1; - } + if (sensor->m_axis < 1) + sensor->m_axis = 1; + else if (sensor->m_axis > 2) + sensor->m_axis = 2; return 0; } - static int CheckHat(void *self) + static int CheckHat(void *self, const PyAttributeDef*) { SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self); - if (sensor->m_hat < 1 || sensor->m_hat > 2) - { - PyErr_SetString(PyExc_ValueError, "hat number must be 1 or 2"); - return 1; - } + if (sensor->m_hat < 1) + sensor->m_hat = 1; + else if (sensor->m_hat > 2) + sensor->m_hat = 2; return 0; } diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp index a05f9ae9879..981d165287b 100644 --- a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp +++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp @@ -822,11 +822,11 @@ PyMethodDef SCA_KeyboardSensor::Methods[] = { PyAttributeDef SCA_KeyboardSensor::Attributes[] = { KX_PYATTRIBUTE_BOOL_RW("useAllKeys",SCA_KeyboardSensor,m_bAllKeys), - KX_PYATTRIBUTE_INT_RW("key",0,1000,SCA_KeyboardSensor,m_hotkey), - KX_PYATTRIBUTE_SHORT_RW("hold1",0,1000,SCA_KeyboardSensor,m_qual), - KX_PYATTRIBUTE_SHORT_RW("hold2",0,1000,SCA_KeyboardSensor,m_qual2), - KX_PYATTRIBUTE_STRING_RW("toggleProperty",0,100,SCA_KeyboardSensor,m_toggleprop), - KX_PYATTRIBUTE_STRING_RW("targetProperty",0,100,SCA_KeyboardSensor,m_targetprop), + KX_PYATTRIBUTE_INT_RW("key",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_hotkey), + KX_PYATTRIBUTE_SHORT_RW("hold1",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_qual), + KX_PYATTRIBUTE_SHORT_RW("hold2",0,SCA_IInputDevice::KX_ENDKEY,true,SCA_KeyboardSensor,m_qual2), + KX_PYATTRIBUTE_STRING_RW("toggleProperty",0,100,false,SCA_KeyboardSensor,m_toggleprop), + KX_PYATTRIBUTE_STRING_RW("targetProperty",0,100,false,SCA_KeyboardSensor,m_targetprop), { NULL } //Sentinel }; diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.cpp b/source/gameengine/GameLogic/SCA_MouseSensor.cpp index 14d9c898980..09b46e6443e 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.cpp +++ b/source/gameengine/GameLogic/SCA_MouseSensor.cpp @@ -59,7 +59,7 @@ SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, m_mousemode = mousemode; m_triggermode = true; - UpdateHotkey(this); + UpdateHotkey(this, NULL); Init(); } @@ -74,7 +74,7 @@ SCA_MouseSensor::~SCA_MouseSensor() /* Nothing to be done here. */ } -int SCA_MouseSensor::UpdateHotkey(void *self) +int SCA_MouseSensor::UpdateHotkey(void *self, const PyAttributeDef*) { // gosh, this function is so damn stupid // its here because of a design mistake in the mouse sensor, it should only @@ -336,7 +336,7 @@ PyMethodDef SCA_MouseSensor::Methods[] = { }; PyAttributeDef SCA_MouseSensor::Attributes[] = { - KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,SCA_MouseSensor,m_mousemode,UpdateHotkey), + KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkey), KX_PYATTRIBUTE_SHORT_ARRAY_RO("position",SCA_MouseSensor,m_x,2), { NULL } //Sentinel }; diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.h b/source/gameengine/GameLogic/SCA_MouseSensor.h index 58ee96c8856..82af2ce9c04 100644 --- a/source/gameengine/GameLogic/SCA_MouseSensor.h +++ b/source/gameengine/GameLogic/SCA_MouseSensor.h @@ -87,7 +87,7 @@ class SCA_MouseSensor : public SCA_ISensor bool isValid(KX_MOUSESENSORMODE); - static int UpdateHotkey(void *self); + static int UpdateHotkey(void *self, const PyAttributeDef*); SCA_MouseSensor(class SCA_MouseManager* keybdmgr, int startx,int starty, diff --git a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp index f8509fe380e..566d3b63487 100644 --- a/source/gameengine/GameLogic/SCA_PropertyActuator.cpp +++ b/source/gameengine/GameLogic/SCA_PropertyActuator.cpp @@ -254,36 +254,23 @@ PyMethodDef SCA_PropertyActuator::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_PropertyActuator::Attributes[] = { + KX_PYATTRIBUTE_STRING_RW_CHECK("property",0,100,false,SCA_PropertyActuator,m_propname,CheckProperty), + KX_PYATTRIBUTE_STRING_RW("value",0,100,false,SCA_PropertyActuator,m_exprtxt), + { 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); - } + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; _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; - } - } + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; return SCA_IActuator::_setattr(attr, value); } diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.cpp b/source/gameengine/GameLogic/SCA_PropertySensor.cpp index 19ee85207bb..a6f7a9cd82b 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.cpp +++ b/source/gameengine/GameLogic/SCA_PropertySensor.cpp @@ -293,7 +293,7 @@ CValue* SCA_PropertySensor::FindIdentifier(const STR_String& identifiername) return GetParent()->FindIdentifier(identifiername); } -bool SCA_PropertySensor::validValueForProperty(char *val, STR_String &prop) +int SCA_PropertySensor::validValueForProperty(void *self, const PyAttributeDef*) { bool result = true; /* There is no type checking at this moment, unfortunately... */ @@ -344,51 +344,25 @@ PyMethodDef SCA_PropertySensor::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_PropertySensor::Attributes[] = { + KX_PYATTRIBUTE_INT_RW("type",KX_PROPSENSOR_NODEF,KX_PROPSENSOR_MAX-1,false,SCA_PropertySensor,m_checktype), + KX_PYATTRIBUTE_STRING_RW_CHECK("property",0,100,false,SCA_PropertySensor,m_checkpropname,CheckProperty), + KX_PYATTRIBUTE_STRING_RW_CHECK("value",0,100,false,SCA_PropertySensor,m_checkpropval,validValueForProperty), + { 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); - } + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; _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; - } + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; return SCA_ISensor::_setattr(attr, value); } @@ -490,11 +464,12 @@ PyObject* SCA_PropertySensor::PySetValue(PyObject* self, PyObject* args, PyObjec if(!PyArg_ParseTuple(args, "s", &propValArg)) { return NULL; } - - if (validValueForProperty(propValArg, m_checkpropname)) { - m_checkpropval = propValArg; + STR_String oldval = m_checkpropval; + m_checkpropval = propValArg; + if (validValueForProperty(self, NULL)) { + m_checkpropval = oldval; + return NULL; } - Py_Return; } diff --git a/source/gameengine/GameLogic/SCA_PropertySensor.h b/source/gameengine/GameLogic/SCA_PropertySensor.h index 81203c78f9d..e625e84a36f 100644 --- a/source/gameengine/GameLogic/SCA_PropertySensor.h +++ b/source/gameengine/GameLogic/SCA_PropertySensor.h @@ -47,10 +47,6 @@ class SCA_PropertySensor : public SCA_ISensor bool m_recentresult; CExpression* m_range_expr; - /** - * Test whether this is a sensible value (type check) - */ - bool validValueForProperty(char *val, STR_String &prop); protected: public: @@ -104,7 +100,10 @@ public: KX_PYMETHOD_DOC(SCA_PropertySensor,GetValue); /* 6. setValue */ KX_PYMETHOD_DOC(SCA_PropertySensor,SetValue); - + /** + * Test whether this is a sensible value (type check) + */ + static int validValueForProperty(void* self, const PyAttributeDef*); }; #endif diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.cpp b/source/gameengine/GameLogic/SCA_RandomActuator.cpp index 8dd405c3d82..840b95d559a 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.cpp +++ b/source/gameengine/GameLogic/SCA_RandomActuator.cpp @@ -361,57 +361,38 @@ PyMethodDef SCA_RandomActuator::Methods[] = { {NULL,NULL} //Sentinel }; +PyAttributeDef SCA_RandomActuator::Attributes[] = { + KX_PYATTRIBUTE_FLOAT_RO("para1",SCA_RandomActuator,m_parameter1), + KX_PYATTRIBUTE_FLOAT_RO("para2",SCA_RandomActuator,m_parameter2), + KX_PYATTRIBUTE_ENUM_RO("distribution",SCA_RandomActuator,m_distribution), + KX_PYATTRIBUTE_STRING_RW_CHECK("property",0,100,false,SCA_RandomActuator,m_propname,CheckProperty), + { NULL } //Sentinel +}; + PyObject* SCA_RandomActuator::_getattr(const STR_String& attr) { + PyObject* object = _getattr_self(Attributes, this, attr); + if (object != NULL) + return object; 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") { + int ret = _setattr_self(Attributes, this, attr, value); + if (ret >= 0) + return ret; + if (attr == "seed") { + if (PyInt_Check(value)) { + int ival = PyInt_AsLong(value); m_base->SetSeed(ival); + return 0; + } else { + PyErr_SetString(PyExc_TypeError, "expected an integer"); + return 1; } - 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); } diff --git a/source/gameengine/GameLogic/SCA_RandomActuator.h b/source/gameengine/GameLogic/SCA_RandomActuator.h index 2fc14f9a0b7..de8faaf9c72 100644 --- a/source/gameengine/GameLogic/SCA_RandomActuator.h +++ b/source/gameengine/GameLogic/SCA_RandomActuator.h @@ -136,7 +136,6 @@ class SCA_RandomActuator : public SCA_IActuator KX_PYMETHOD_DOC(SCA_RandomActuator,SetFloatNormal); /* 20. setFloatNegativeExponential, */ KX_PYMETHOD_DOC(SCA_RandomActuator,SetFloatNegativeExponential); - }; /* end of class KX_EditObjectActuator : public SCA_PropertyActuator */ #endif |