Warning Fixes - const correctness in unicode encoding, unused variables in blenlib...
[blender.git] / source / gameengine / Converter / KX_ConvertProperties.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/Converter/KX_ConvertProperties.cpp
29  *  \ingroup bgeconv
30  */
31
32
33 #include "KX_ConvertProperties.h"
34
35
36 #include "DNA_object_types.h"
37 #include "DNA_property_types.h"
38 /* end of blender include block */
39
40
41 #include "Value.h"
42 #include "VectorValue.h"
43 #include "BoolValue.h"
44 #include "StringValue.h"
45 #include "FloatValue.h"
46 #include "KX_GameObject.h"
47 #include "IntValue.h"
48 #include "SCA_TimeEventManager.h"
49 #include "SCA_IScene.h"
50
51 #include "KX_FontObject.h"
52 #include "DNA_curve_types.h"
53
54 /* This little block needed for linking to Blender... */
55 #ifdef WIN32
56 #include "BLI_winstuff.h"
57 #endif
58
59 extern "C" {
60         #include "BKE_property.h"
61 }
62
63 /* prototype */
64 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer);
65
66 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
67 {
68         
69         bProperty* prop = (bProperty*)object->prop.first;
70         CValue* propval;        
71         bool show_debug_info;
72         while(prop)
73         {
74         
75                 propval = NULL;
76                 show_debug_info = bool (prop->flag & PROP_DEBUG);
77
78                 switch(prop->type) {
79                         case GPROP_BOOL:
80                         {
81                                 propval = new CBoolValue((bool)(prop->data != 0));
82                                 gameobj->SetProperty(prop->name,propval);
83                                 //promp->poin= &prop->data;
84                                 break;
85                         }
86                         case GPROP_INT:
87                         {
88                                 propval = new CIntValue((int)prop->data);
89                                 gameobj->SetProperty(prop->name,propval);
90                                 break;
91                         }
92                         case GPROP_FLOAT:
93                         {
94                                 //prop->poin= &prop->data;
95                                 float floatprop = *((float*)&prop->data);
96                                 propval = new CFloatValue(floatprop);
97                                 gameobj->SetProperty(prop->name,propval);
98                         }
99                         break;
100                         case GPROP_STRING:
101                         {
102                                 //prop->poin= callocN(MAX_PROPSTRING, "property string");
103                                 propval = new CStringValue((char*)prop->poin,"");
104                                 gameobj->SetProperty(prop->name,propval);
105                                 break;
106                         }
107                         case GPROP_TIME:
108                         {
109                                 float floatprop = *((float*)&prop->data);
110
111                                 CValue* timeval = new CFloatValue(floatprop);
112                                 // set a subproperty called 'timer' so that 
113                                 // we can register the replica of this property 
114                                 // at the time a game object is replicated (AddObjectActuator triggers this)
115                                 CValue *bval = new CBoolValue(true);
116                                 timeval->SetProperty("timer",bval);
117                                 bval->Release();
118                                 if (isInActiveLayer)
119                                 {
120                                         timemgr->AddTimeProperty(timeval);
121                                 }
122                                 
123                                 propval = timeval;
124                                 gameobj->SetProperty(prop->name,timeval);
125
126                         }
127                         default:
128                         {
129                                 // todo make an assert etc.
130                         }
131                 }
132                 
133                 if (propval)
134                 {
135                         if (show_debug_info)
136                         {
137                                 scene->AddDebugProperty(gameobj,STR_String(prop->name));
138                         }
139                         // done with propval, release it
140                         propval->Release();
141                 }
142                 
143 #ifdef WITH_PYTHON
144                 /* Warn if we double up on attributes, this isn't quite right since it wont find inherited attributes however there arnt many */
145                 for (PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
146                         if (strcmp(prop->name, attrdef->m_name)==0) {
147                                 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);
148                                 break;
149                         }
150                 }
151                 for (PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
152                         if (strcmp(prop->name, methdef->ml_name)==0) {
153                                 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);
154                                 break;
155                         }
156                 }
157                 /* end warning check */
158 #endif // WITH_PYTHON
159
160                 prop = prop->next;
161         }
162         // check if state needs to be debugged
163         if (object->scaflag & OB_DEBUGSTATE)
164         {
165                 //  reserve name for object state
166                 scene->AddDebugProperty(gameobj,STR_String("__state__"));
167         }
168
169         /* Font Objects need to 'copy' the Font Object data body to ["Text"] */
170         if (object->type == OB_FONT)
171         {
172                 BL_ConvertTextProperty(object, (KX_FontObject *)gameobj, timemgr, scene, isInActiveLayer);
173         }
174 }
175
176 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
177 {
178         CValue* tprop = fontobj->GetProperty("Text");
179         if (!tprop) return;
180         bProperty* prop = get_ob_property(object, "Text");
181         if (!prop) return;
182
183         Curve *curve = static_cast<Curve *>(object->data);
184         STR_String str = curve->str;
185         CValue* propval = NULL;
186
187         switch(prop->type) {
188                 case GPROP_BOOL:
189                 {
190                         int value = atoi(str);
191                         propval = new CBoolValue((bool)(value != 0));
192                         tprop->SetValue(propval);
193                         break;
194                 }
195                 case GPROP_INT:
196                 {
197                         int value = atoi(str);
198                         propval = new CIntValue(value);
199                         tprop->SetValue(propval);
200                         break;
201                 }
202                 case GPROP_FLOAT:
203                 {
204                         float floatprop = (float)atof(str);
205                         propval = new CFloatValue(floatprop);
206                         tprop->SetValue(propval);
207                         break;
208                 }
209                 case GPROP_STRING:
210                 {
211                         propval = new CStringValue(str, "");
212                         tprop->SetValue(propval);
213                         break;
214                 }
215                 case GPROP_TIME:
216                 {
217                         float floatprop = (float)atof(str);
218
219                         CValue* timeval = new CFloatValue(floatprop);
220                         // set a subproperty called 'timer' so that
221                         // we can register the replica of this property
222                         // at the time a game object is replicated (AddObjectActuator triggers this)
223                         CValue *bval = new CBoolValue(true);
224                         timeval->SetProperty("timer",bval);
225                         bval->Release();
226                         if (isInActiveLayer)
227                         {
228                                 timemgr->AddTimeProperty(timeval);
229                         }
230
231                         propval = timeval;
232                         tprop->SetValue(timeval);
233                 }
234                 default:
235                 {
236                         // todo make an assert etc.
237                 }
238         }
239
240         if (propval) {
241                 propval->Release();
242         }
243 }
244