Blender V2.61 - r43446

KX_ConvertProperties.cpp

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  */
00027 
00033 #include "KX_ConvertProperties.h"
00034 
00035 
00036 #include "DNA_object_types.h"
00037 #include "DNA_property_types.h"
00038 /* end of blender include block */
00039 
00040 
00041 #include "Value.h"
00042 #include "VectorValue.h"
00043 #include "BoolValue.h"
00044 #include "StringValue.h"
00045 #include "FloatValue.h"
00046 #include "KX_GameObject.h"
00047 #include "IntValue.h"
00048 #include "SCA_TimeEventManager.h"
00049 #include "SCA_IScene.h"
00050 
00051 #include "KX_FontObject.h"
00052 #include "DNA_curve_types.h"
00053 
00054 /* This little block needed for linking to Blender... */
00055 #ifdef WIN32
00056 #include "BLI_winstuff.h"
00057 #endif
00058 
00059 extern "C" {
00060     #include "BKE_property.h"
00061 }
00062 
00063 /* prototype */
00064 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer);
00065 
00066 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
00067 {
00068     
00069     bProperty* prop = (bProperty*)object->prop.first;
00070     CValue* propval;    
00071     bool show_debug_info;
00072     while(prop)
00073     {
00074     
00075         propval = NULL;
00076         show_debug_info = bool (prop->flag & PROP_DEBUG);
00077 
00078         switch(prop->type) {
00079             case GPROP_BOOL:
00080             {
00081                 propval = new CBoolValue((bool)(prop->data != 0));
00082                 gameobj->SetProperty(prop->name,propval);
00083                 //promp->poin= &prop->data;
00084                 break;
00085             }
00086             case GPROP_INT:
00087             {
00088                 propval = new CIntValue((int)prop->data);
00089                 gameobj->SetProperty(prop->name,propval);
00090                 break;
00091             }
00092             case GPROP_FLOAT:
00093             {
00094                 //prop->poin= &prop->data;
00095                 float floatprop = *((float*)&prop->data);
00096                 propval = new CFloatValue(floatprop);
00097                 gameobj->SetProperty(prop->name,propval);
00098             }
00099             break;
00100             case GPROP_STRING:
00101             {
00102                 //prop->poin= callocN(MAX_PROPSTRING, "property string");
00103                 propval = new CStringValue((char*)prop->poin,"");
00104                 gameobj->SetProperty(prop->name,propval);
00105                 break;
00106             }
00107             case GPROP_TIME:
00108             {
00109                 float floatprop = *((float*)&prop->data);
00110 
00111                 CValue* timeval = new CFloatValue(floatprop);
00112                 // set a subproperty called 'timer' so that 
00113                 // we can register the replica of this property 
00114                 // at the time a game object is replicated (AddObjectActuator triggers this)
00115                 CValue *bval = new CBoolValue(true);
00116                 timeval->SetProperty("timer",bval);
00117                 bval->Release();
00118                 if (isInActiveLayer)
00119                 {
00120                     timemgr->AddTimeProperty(timeval);
00121                 }
00122                 
00123                 propval = timeval;
00124                 gameobj->SetProperty(prop->name,timeval);
00125 
00126             }
00127             default:
00128             {
00129                 // todo make an assert etc.
00130             }
00131         }
00132         
00133         if (propval)
00134         {
00135             if (show_debug_info)
00136             {
00137                 scene->AddDebugProperty(gameobj,STR_String(prop->name));
00138             }
00139             // done with propval, release it
00140             propval->Release();
00141         }
00142         
00143 #ifdef WITH_PYTHON
00144         /* Warn if we double up on attributes, this isnt quite right since it wont find inherited attributes however there arnt many */
00145         for(PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
00146             if(strcmp(prop->name, attrdef->m_name)==0) {
00147                 printf("Warning! user defined property name \"%s\" is also a python attribute for object \"%s\"\n\tUse ob[\"%s\"] syntax to avoid conflict\n", prop->name, object->id.name+2, prop->name);
00148                 break;
00149             }
00150         }
00151         for(PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
00152             if(strcmp(prop->name, methdef->ml_name)==0) {
00153                 printf("Warning! user defined property name \"%s\" is also a python method for object \"%s\"\n\tUse ob[\"%s\"] syntax to avoid conflict\n", prop->name, object->id.name+2, prop->name);
00154                 break;
00155             }
00156         }
00157         /* end warning check */
00158 #endif // WITH_PYTHON
00159 
00160         prop = prop->next;
00161     }
00162     // check if state needs to be debugged
00163     if (object->scaflag & OB_DEBUGSTATE)
00164     {
00165         //  reserve name for object state
00166         scene->AddDebugProperty(gameobj,STR_String("__state__"));
00167     }
00168 
00169     /* Font Objects need to 'copy' the Font Object data body to ["Text"] */
00170     if (object->type == OB_FONT)
00171     {
00172         BL_ConvertTextProperty(object, (KX_FontObject *)gameobj, timemgr, scene, isInActiveLayer);
00173     }
00174 }
00175 
00176 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
00177 {
00178     CValue* tprop = fontobj->GetProperty("Text");
00179     if(!tprop) return;
00180     bProperty* prop = get_ob_property(object, "Text");
00181     if(!prop) return;
00182 
00183     Curve *curve = static_cast<Curve *>(object->data);
00184     STR_String str = curve->str;
00185     CValue* propval = NULL;
00186 
00187     switch(prop->type) {
00188         case GPROP_BOOL:
00189         {
00190             int value = atoi(str);
00191             propval = new CBoolValue((bool)(value != 0));
00192             tprop->SetValue(propval);
00193             break;
00194         }
00195         case GPROP_INT:
00196         {
00197             int value = atoi(str);
00198             propval = new CIntValue(value);
00199             tprop->SetValue(propval);
00200             break;
00201         }
00202         case GPROP_FLOAT:
00203         {
00204             float floatprop = atof(str);
00205             propval = new CFloatValue(floatprop);
00206             tprop->SetValue(propval);
00207             break;
00208         }
00209         case GPROP_STRING:
00210         {
00211             propval = new CStringValue(str, "");
00212             tprop->SetValue(propval);
00213             break;
00214         }
00215         case GPROP_TIME:
00216         {
00217             float floatprop = atof(str);
00218 
00219             CValue* timeval = new CFloatValue(floatprop);
00220             // set a subproperty called 'timer' so that
00221             // we can register the replica of this property
00222             // at the time a game object is replicated (AddObjectActuator triggers this)
00223             CValue *bval = new CBoolValue(true);
00224             timeval->SetProperty("timer",bval);
00225             bval->Release();
00226             if (isInActiveLayer)
00227             {
00228                 timemgr->AddTimeProperty(timeval);
00229             }
00230 
00231             propval = timeval;
00232             tprop->SetValue(timeval);
00233         }
00234         default:
00235         {
00236             // todo make an assert etc.
00237         }
00238     }
00239 
00240     if (propval) {
00241         propval->Release();
00242     }
00243 }
00244