synched with trunk at revision 36569
[blender.git] / source / gameengine / Converter / KX_ConvertProperties.cpp
1 /*
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file gameengine/Converter/KX_ConvertProperties.cpp
30  *  \ingroup bgeconv
31  */
32
33
34 #include "KX_ConvertProperties.h"
35
36
37 #include "DNA_object_types.h"
38 #include "DNA_property_types.h"
39 /* end of blender include block */
40
41
42 #include "Value.h"
43 #include "VectorValue.h"
44 #include "BoolValue.h"
45 #include "StringValue.h"
46 #include "FloatValue.h"
47 #include "KX_GameObject.h"
48 #include "IntValue.h"
49 #include "SCA_TimeEventManager.h"
50 #include "SCA_IScene.h"
51
52 /* This little block needed for linking to Blender... */
53 #ifdef WIN32
54 #include "BLI_winstuff.h"
55 #endif
56
57 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
58 {
59         
60         bProperty* prop = (bProperty*)object->prop.first;
61         CValue* propval;        
62         bool show_debug_info;
63         while(prop)
64         {
65         
66                 propval = NULL;
67                 show_debug_info = bool (prop->flag & PROP_DEBUG);
68
69                 switch(prop->type) {
70                         case GPROP_BOOL:
71                         {
72                                 propval = new CBoolValue((bool)(prop->data != 0));
73                                 gameobj->SetProperty(prop->name,propval);
74                                 //promp->poin= &prop->data;
75                                 break;
76                         }
77                         case GPROP_INT:
78                         {
79                                 propval = new CIntValue((int)prop->data);
80                                 gameobj->SetProperty(prop->name,propval);
81                                 break;
82                         }
83                         case GPROP_FLOAT:
84                         {
85                                 //prop->poin= &prop->data;
86                                 float floatprop = *((float*)&prop->data);
87                                 propval = new CFloatValue(floatprop);
88                                 gameobj->SetProperty(prop->name,propval);
89                         }
90                         break;
91                         case GPROP_STRING:
92                         {
93                                 //prop->poin= callocN(MAX_PROPSTRING, "property string");
94                                 propval = new CStringValue((char*)prop->poin,"");
95                                 gameobj->SetProperty(prop->name,propval);
96                                 break;
97                         }
98                         case GPROP_TIME:
99                         {
100                                 float floatprop = *((float*)&prop->data);
101
102                                 CValue* timeval = new CFloatValue(floatprop);
103                                 // set a subproperty called 'timer' so that 
104                                 // we can register the replica of this property 
105                                 // at the time a game object is replicated (AddObjectActuator triggers this)
106                                 CValue *bval = new CBoolValue(true);
107                                 timeval->SetProperty("timer",bval);
108                                 bval->Release();
109                                 if (isInActiveLayer)
110                                 {
111                                         timemgr->AddTimeProperty(timeval);
112                                 }
113                                 
114                                 propval = timeval;
115                                 gameobj->SetProperty(prop->name,timeval);
116
117                         }
118                         default:
119                         {
120                                 // todo make an assert etc.
121                         }
122                 }
123                 
124                 if (propval)
125                 {
126                         if (show_debug_info)
127                         {
128                                 scene->AddDebugProperty(gameobj,STR_String(prop->name));
129                         }
130                         // done with propval, release it
131                         propval->Release();
132                 }
133                 
134 #ifdef WITH_PYTHON
135                 /* Warn if we double up on attributes, this isnt quite right since it wont find inherited attributes however there arnt many */
136                 for(PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
137                         if(strcmp(prop->name, attrdef->m_name)==0) {
138                                 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);
139                                 break;
140                         }
141                 }
142                 for(PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
143                         if(strcmp(prop->name, methdef->ml_name)==0) {
144                                 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);
145                                 break;
146                         }
147                 }
148                 /* end warning check */
149 #endif // WITH_PYTHON
150
151                 prop = prop->next;
152         }
153         // check if state needs to be debugged
154         if (object->scaflag & OB_DEBUGSTATE)
155         {
156                 //  reserve name for object state
157                 scene->AddDebugProperty(gameobj,STR_String("__state__"));
158         }
159 }