Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

SCA_PropertySensor.cpp

Go to the documentation of this file.
00001 
00006 
00007 #include <stdio.h>
00008 #include "SCA_PropertySensor.h"
00009 #include "Operator2Expr.h"
00010 #include "ConstExpr.h"
00011 #include "InputParser.h"
00012 #include "StringValue.h"
00013 #include "SCA_EventManager.h"
00014 #include "SCA_LogicManager.h"
00015 
00016 SCA_PropertySensor::SCA_PropertySensor(SCA_EventManager* eventmgr,
00017                                                                          SCA_IObject* gameobj,
00018                                                                          const CCString& propname,
00019                                                                          const CCString & propval,
00020                                                                          const CCString& propmaxval,
00021                                                                          KX_PROPSENSOR_TYPE checktype,
00022                                                                          PyTypeObject* T )
00023         : SCA_ISensor(gameobj,eventmgr,T),
00024           m_checkpropname(propname),
00025           m_checkpropval(propval),
00026           m_checkpropmaxval(propmaxval),
00027           m_checktype(checktype),
00028           m_range_expr(NULL),
00029           m_lastresult(false)

00030 {
00031         m_recentresult=false;
00032         //CParser pars;
00033         //pars.SetContext(this->AddRef());
00034         //CValue* resultval = m_rightexpr->Calculate();
00035 
00036         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
00037         if (orgprop)
00038         {
00039                 m_previoustext = orgprop->GetText();
00040         }
00041 
00042         if (m_checktype==KX_PROPSENSOR_INTERVAL)
00043         {
00044                 CParser pars;
00045                 pars.SetContext(this->AddRef());
00046                 CCString checkstr = "(" + m_checkpropval + " <= " 
00047                                                         + m_checkpropname + ") && ( " 
00048                                                         + m_checkpropname + " <= " 
00049                                                         + m_checkpropmaxval;
00050 
00051                 m_range_expr = pars.ProcessText(checkstr);
00052         }
00053 
00054 }
00055 
00056 SCA_PropertySensor::~SCA_PropertySensor()

00057 {
00058         //if (m_rightexpr)
00059         //      m_rightexpr->Release();
00060 
00061         if (m_range_expr)
00062                 m_range_expr->Release();
00063 }
00064 
00065 bool SCA_PropertySensor::Evaluate(CValue* event)

00066 {
00067         bool result = CheckPropertyCondition();
00068         
00069         if (m_lastresult!=result)
00070         {
00071                 m_lastresult = result;
00072                 return true;
00073         }
00074 
00075         return false;
00076 }
00077 
00078 
00079 bool    SCA_PropertySensor::CheckPropertyCondition()

00080 {
00081 
00082         m_recentresult=false;
00083         bool result=false;
00084         bool reverse = false;
00085         switch (m_checktype)
00086         {
00087         case KX_PROPSENSOR_NOTEQUAL:
00088                 reverse = true;
00089         case KX_PROPSENSOR_EQUAL:
00090                 {
00091                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
00092                         if (orgprop)
00093                         {
00094                                 result = (orgprop->GetText() == m_checkpropval);//->GetText());
00095 
00096                         }
00097 
00098                         if (reverse)
00099                                 result = !result;
00100                         break;
00101 
00102                 }
00103 
00104         case KX_PROPSENSOR_EXPRESSION:
00105                 {
00106                         /*
00107                         if (m_rightexpr)

00108                         {

00109                                 CValue* resultval = m_rightexpr->Calculate();

00110                                 if (resultval->IsError())

00111                                 {

00112                                         int i=0;

00113                                         CCString errortest = resultval->GetText();

00114                                         printf(errortest);

00115 
00116                                 } else

00117                                 {

00118                                         result = resultval->GetNumber() != 0;

00119                                 }

00120                         }

00121                         */
00122                         break;
00123                 }
00124         case KX_PROPSENSOR_INTERVAL:
00125                 {
00126                         //CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
00127                         //if (orgprop)
00128                         //{
00129                                 if (m_range_expr)
00130                                 {
00131                                         CValue* vallie = m_range_expr->Calculate();
00132                                         if (vallie)
00133                                         {
00134                                                 CCString errtext = vallie->GetText();
00135                                                 if (errtext == "TRUE")
00136                                                 {
00137                                                         result = true;
00138                                                 } else
00139                                                 {
00140                                                         if (vallie->IsError())
00141                                                         {
00142                                                                 printf (errtext.ReadPtr());
00143                                                         } 
00144                                                 }
00145                                                 
00146                                                 vallie->Release();
00147                                         }
00148                                 }
00149 
00150                                 
00151                         //}
00152                         
00153                 //cout << " \nSens:Prop:interval!"; /* need implementation here!!! */
00154 
00155                 break;
00156                 }
00157         case KX_PROPSENSOR_CHANGED:
00158                 {
00159                         CValue* orgprop = GetParent()->FindIdentifier(m_checkpropname);
00160                                 
00161                         if (orgprop)
00162                         {
00163                                 if (m_previoustext != orgprop->GetText())
00164                                 {
00165                                         m_previoustext = orgprop->GetText();
00166                                         result = true;
00167                                 }
00168                         }
00169 
00170                         //cout << " \nSens:Prop:changed!"; /* need implementation here!!! */
00171                         break;
00172                 }
00173         default:
00174                 ; /* error */
00175         }
00176         m_recentresult=result;
00177         return result;
00178 }
00179 
00180 CValue* SCA_PropertySensor::FindIdentifier(const CCString& identifiername)

00181 {
00182         return  GetParent()->FindIdentifier(identifiername);
00183 }
00184 
00185 bool SCA_PropertySensor::validValueForProperty(char *val, CCString &prop)

00186 {
00187         bool result = true;
00188         /*  There is no type checking at this moment, unfortunately...           */
00189         return result;
00190 }
00191 
00192 /* ------------------------------------------------------------------------- */
00193 /* Python functions                                                          */
00194 /* ------------------------------------------------------------------------- */
00195 
00196 /* Integration hooks ------------------------------------------------------- */
00197 PyTypeObject SCA_PropertySensor::Type = {
00198         PyObject_HEAD_INIT(&PyType_Type)
00199         0,
00200         "SCA_PropertySensor",
00201         sizeof(SCA_PropertySensor),
00202         0,
00203         PyDestructor,
00204         0,
00205         __getattr,
00206         __setattr,
00207         0, //&MyPyCompare,
00208         __repr,
00209         0, //&cvalue_as_number,
00210         0,
00211         0,
00212         0,
00213         0
00214 };
00215 
00216 PyParentObject SCA_PropertySensor::Parents[] = {
00217         &SCA_PropertySensor::Type,
00218         &SCA_ISensor::Type,
00219         &SCA_ILogicBrick::Type,
00220         &CValue::Type,
00221         NULL
00222 };
00223 
00224 PyMethodDef SCA_PropertySensor::Methods[] = {
00225         {"getType", (PyCFunction) SCA_PropertySensor::sPyGetType, METH_VARARGS, GetType_doc},
00226         {"setType", (PyCFunction) SCA_PropertySensor::sPySetType, METH_VARARGS, SetType_doc},
00227         {"getProperty", (PyCFunction) SCA_PropertySensor::sPyGetProperty, METH_VARARGS, GetProperty_doc},
00228         {"setProperty", (PyCFunction) SCA_PropertySensor::sPySetProperty, METH_VARARGS, SetProperty_doc},
00229         {"getValue", (PyCFunction) SCA_PropertySensor::sPyGetValue, METH_VARARGS, GetValue_doc},
00230         {"setValue", (PyCFunction) SCA_PropertySensor::sPySetValue, METH_VARARGS, SetValue_doc},
00231         {NULL,NULL} //Sentinel
00232 };
00233 
00234 PyObject* SCA_PropertySensor::_getattr(char* attr) {
00235         _getattr_up(SCA_ISensor); /* implicit return! */
00236 }
00237 
00238 /* 1. getType */
00239 char SCA_PropertySensor::GetType_doc[] = 
00240 "getType()\n"
00241 "\tReturns the type of check this sensor performs.\n";
00242 PyObject* SCA_PropertySensor::PyGetType(PyObject* self, PyObject* args, PyObject* kwds)

00243 {
00244         return PyInt_FromLong(m_checktype);
00245 }
00246 
00247 /* 2. setType */
00248 char SCA_PropertySensor::SetType_doc[] = 
00249 "setType(type)\n"
00250 "\t- type: KX_PROPSENSOR_EQUAL, KX_PROPSENSOR_NOTEQUAL,\n"
00251 "\t        KX_PROPSENSOR_INTERVAL, KX_PROPSENSOR_CHANGED,\n"
00252 "\t        or KX_PROPSENSOR_EXPRESSION.\n"
00253 "\tSet the type of check to perform.\n";
00254 PyObject* SCA_PropertySensor::PySetType(PyObject* self, PyObject* args, PyObject* kwds) 

00255 {
00256         int typeArg;
00257         
00258         if (!PyArg_ParseTuple(args, "i", &typeArg)) {
00259                 return NULL;
00260         }
00261         
00262         if ( (typeArg > KX_PROPSENSOR_NODEF) 
00263                  && (typeArg < KX_PROPSENSOR_MAX) ) {
00264                 m_checktype =  typeArg;
00265         }
00266         
00267         Py_Return;
00268 }
00269 
00270 /* 3. getProperty */
00271 char SCA_PropertySensor::GetProperty_doc[] = 
00272 "getProperty(name)\n"
00273 "\tReturn the property with which the sensor operates.\n";
00274 PyObject* SCA_PropertySensor::PyGetProperty(PyObject* self, PyObject* args, PyObject* kwds) 

00275 {
00276         return PyString_FromString(m_checkpropname);
00277 }
00278 
00279 /* 4. setProperty */
00280 char SCA_PropertySensor::SetProperty_doc[] = 
00281 "setProperty(name)\n"
00282 "\t- name: string\n"
00283 "\tSets the property with which to operate. If there is no property\n"
00284 "\tof this name, the call is ignored.\n";
00285 PyObject* SCA_PropertySensor::PySetProperty(PyObject* self, PyObject* args, PyObject* kwds) 

00286 {
00287         /* We should query whether the name exists. Or should we create a prop   */
00288         /* on the fly?                                                           */
00289         char *propNameArg = NULL;
00290 
00291         if (!PyArg_ParseTuple(args, "s", &propNameArg)) {
00292                 return NULL;
00293         }
00294 
00295         if (FindIdentifier(CCString(propNameArg))) {
00296                 m_checkpropname = propNameArg;
00297         } else {
00298                 ; /* error: bad property name */
00299         }
00300 
00301         Py_Return;
00302 }
00303 
00304 /* 5. getValue */
00305 char SCA_PropertySensor::GetValue_doc[] = 
00306 "getValue()\n"
00307 "\tReturns the value with which the sensor operates.\n";
00308 PyObject* SCA_PropertySensor::PyGetValue(PyObject* self, PyObject* args, PyObject* kwds) 

00309 {
00310         return PyString_FromString(m_checkpropval);
00311 }
00312 
00313 /* 6. setValue */
00314 char SCA_PropertySensor::SetValue_doc[] = 
00315 "setValue(value)\n"
00316 "\t- value: string\n"
00317 "\tSet the value with which the sensor operates. If the value\n"
00318 "\tis not compatible with the type of the property, the subsequent\n"
00319 "\t action is ignored.\n";
00320 PyObject* SCA_PropertySensor::PySetValue(PyObject* self, PyObject* args, PyObject* kwds) 

00321 {
00322         /* Here, we need to check whether the value is 'valid' for this property.*/
00323         /* We know that the property exists, or is NULL.                         */
00324         char *propValArg = NULL;
00325 
00326         if(!PyArg_ParseTuple(args, "s", &propValArg)) {
00327                 return NULL;
00328         }
00329 
00330         if (validValueForProperty(propValArg, m_checkpropname)) {
00331                 m_checkpropval = propValArg;
00332         }       
00333 
00334         Py_Return;
00335 }
00336 
00337 /* eof */

Generated at Thu Feb 1 13:03:11 2001 for Ketsji Game Engine by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000