00001
00006
00007 #include "SCA_PropertyActuator.h"
00008 #include "InputParser.h"
00009
00010 #include "Operator2Expr.h"
00011 #include "ConstExpr.h"
00012
00013
00014
00015
00016
00017 SCA_PropertyActuator::SCA_PropertyActuator(SCA_IObject* gameobj,CValue* sourceObj,const CCString& propname,const CCString& expr,int acttype,PyTypeObject* T )
00018 : SCA_IActuator(gameobj,T),
00019 m_propname(propname),
00020 m_exprtxt(expr),
00021 m_type(acttype),
00022 m_sourceObj(sourceObj)
00023 {
00024 }
00025
00026 SCA_PropertyActuator::~SCA_PropertyActuator()
00027 {
00028
00029 }
00030
00031 bool SCA_PropertyActuator::Update(double curtime,double deltatime)
00032 {
00033 bool result = false;
00034
00035 bool bNegativeEvent = IsNegativeEvent();
00036
00037 if (bNegativeEvent)
00038 return false;
00039
00040
00041 CValue* propowner = GetParent();
00042 CParser parser;
00043 parser.SetContext( propowner->AddRef());
00044
00045 CExpression* userexpr = parser.ProcessText(m_exprtxt);
00046 if (userexpr)
00047 {
00048
00049
00050 switch (m_type)
00051 {
00052
00053 case KX_ACT_PROP_ASSIGN:
00054 {
00055
00056 CValue* newval = userexpr->Calculate();
00057 CValue* oldprop = propowner->GetProperty(m_propname);
00058 if (oldprop)
00059 {
00060 oldprop->SetValue(newval);
00061 newval->Release();
00062 } else
00063 {
00064 propowner->SetProperty(m_propname,newval);
00065 }
00066
00067
00068 break;
00069 }
00070 case KX_ACT_PROP_ADD:
00071 {
00072 CValue* oldprop = propowner->GetProperty(m_propname);
00073 if (oldprop)
00074 {
00075 int waarde = (int)oldprop->GetNumber();
00076 CExpression* expr = new COperator2Expr(VALUE_ADD_OPERATOR,new CConstExpr(oldprop->AddRef()),
00077 userexpr->AddRef());
00078
00079 CValue* newprop = expr->Calculate();
00080 oldprop->SetValue(newprop);
00081 newprop->Release();
00082
00083
00084 expr->Release();
00085
00086 }
00087
00088 break;
00089 }
00090 case KX_ACT_PROP_COPY:
00091 {
00092 if (m_sourceObj)
00093 {
00094 CValue* copyprop = m_sourceObj->GetProperty(m_exprtxt);
00095 if (copyprop)
00096 {
00097
00098
00099 GetParent()->SetProperty(
00100 m_propname,
00101 copyprop->GetReplica());
00102
00103
00104 }
00105 }
00106 break;
00107 }
00108 default:
00109 {
00110
00111 }
00112 }
00113
00114 userexpr->Release();
00115 }
00116
00117 return result;
00118 }
00119
00120 bool SCA_PropertyActuator::isValid(SCA_PropertyActuator::KX_ACT_PROP_MODE mode)
00121 {
00122 bool res = false;
00123
00124 res = ((mode > KX_ACT_PROP_NODEF) && (mode < KX_ACT_PROP_MAX));
00125
00126 return res;
00127 }
00128
00129
00130
00131
00132
00133
00134 PyTypeObject SCA_PropertyActuator::Type = {
00135 PyObject_HEAD_INIT(&PyType_Type)
00136 0,
00137 "SCA_PropertyActuator",
00138 sizeof(SCA_PropertyActuator),
00139 0,
00140 PyDestructor,
00141 0,
00142 __getattr,
00143 __setattr,
00144 0,
00145 __repr,
00146 0,
00147 0,
00148 0,
00149 0,
00150 0
00151 };
00152
00153 PyParentObject SCA_PropertyActuator::Parents[] = {
00154 &SCA_PropertyActuator::Type,
00155 &SCA_IActuator::Type,
00156 &SCA_ILogicBrick::Type,
00157 &CValue::Type,
00158 NULL
00159 };
00160
00161 PyMethodDef SCA_PropertyActuator::Methods[] = {
00162 {"setProperty", (PyCFunction) SCA_PropertyActuator::sPySetProperty, METH_VARARGS, SetProperty_doc},
00163 {"getProperty", (PyCFunction) SCA_PropertyActuator::sPyGetProperty, METH_VARARGS, GetProperty_doc},
00164 {"setValue", (PyCFunction) SCA_PropertyActuator::sPySetValue, METH_VARARGS, SetValue_doc},
00165 {"getValue", (PyCFunction) SCA_PropertyActuator::sPyGetValue, METH_VARARGS, GetValue_doc},
00166 {NULL,NULL}
00167 };
00168
00169 PyObject* SCA_PropertyActuator::_getattr(char* attr) {
00170 _getattr_up(SCA_IActuator);
00171 }
00172
00173
00174 char SCA_PropertyActuator::SetProperty_doc[] =
00175 "setProperty(name)\n"
00176 "\t- name: string\n"
00177 "\tSet the property on which to operate. If there is no property\n"
00178 "\tof this name, the call is ignored.\n";
00179 PyObject* SCA_PropertyActuator::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds)
00180 {
00181
00182 char *nameArg;
00183 if (!PyArg_ParseTuple(args, "s", &nameArg)) {
00184 return NULL;
00185 }
00186
00187 CValue* prop = GetParent()->FindIdentifier(nameArg);
00188
00189 if (prop) {
00190 m_propname = nameArg;
00191 } else {
00192 ;
00193 }
00194
00195 Py_Return;
00196 }
00197
00198
00199 char SCA_PropertyActuator::GetProperty_doc[] =
00200 "getProperty(name)\n"
00201 "\tReturn the property on which the actuator operates.\n";
00202 PyObject* SCA_PropertyActuator::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds)
00203 {
00204 return PyString_FromString(m_propname);
00205 }
00206
00207
00208 char SCA_PropertyActuator::SetValue_doc[] =
00209 "setValue(value)\n"
00210 "\t- value: string\n"
00211 "\tSet the value with which the actuator operates. If the value\n"
00212 "\tis not compatible with the type of the property, the subsequent\n"
00213 "\t action is ignored.\n";
00214 PyObject* SCA_PropertyActuator::PySetValue(PyObject* self, PyObject* args, PyObject* kwds)
00215 {
00216 char *valArg;
00217 if(!PyArg_ParseTuple(args, "s", &valArg)) {
00218 return NULL;
00219 }
00220
00221 if (valArg) m_exprtxt = valArg;
00222
00223 Py_Return;
00224 }
00225
00226
00227 char SCA_PropertyActuator::GetValue_doc[] =
00228 "getValue()\n"
00229 "\tReturns the value with which the actuator operates.\n";
00230 PyObject* SCA_PropertyActuator::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds)
00231 {
00232 return PyString_FromString(m_exprtxt);
00233 }
00234
00235