This patch creates an interface for ["Text"] properties in Font objects.
[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
60 /* prototype */
61 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer);
62
63 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
64 {
65         
66         bProperty* prop = (bProperty*)object->prop.first;
67         CValue* propval;        
68         bool show_debug_info;
69         while(prop)
70         {
71         
72                 propval = NULL;
73                 show_debug_info = bool (prop->flag & PROP_DEBUG);
74
75                 switch(prop->type) {
76                         case GPROP_BOOL:
77                         {
78                                 propval = new CBoolValue((bool)(prop->data != 0));
79                                 gameobj->SetProperty(prop->name,propval);
80                                 //promp->poin= &prop->data;
81                                 break;
82                         }
83                         case GPROP_INT:
84                         {
85                                 propval = new CIntValue((int)prop->data);
86                                 gameobj->SetProperty(prop->name,propval);
87                                 break;
88                         }
89                         case GPROP_FLOAT:
90                         {
91                                 //prop->poin= &prop->data;
92                                 float floatprop = *((float*)&prop->data);
93                                 propval = new CFloatValue(floatprop);
94                                 gameobj->SetProperty(prop->name,propval);
95                         }
96                         break;
97                         case GPROP_STRING:
98                         {
99                                 //prop->poin= callocN(MAX_PROPSTRING, "property string");
100                                 propval = new CStringValue((char*)prop->poin,"");
101                                 gameobj->SetProperty(prop->name,propval);
102                                 break;
103                         }
104                         case GPROP_TIME:
105                         {
106                                 float floatprop = *((float*)&prop->data);
107
108                                 CValue* timeval = new CFloatValue(floatprop);
109                                 // set a subproperty called 'timer' so that 
110                                 // we can register the replica of this property 
111                                 // at the time a game object is replicated (AddObjectActuator triggers this)
112                                 CValue *bval = new CBoolValue(true);
113                                 timeval->SetProperty("timer",bval);
114                                 bval->Release();
115                                 if (isInActiveLayer)
116                                 {
117                                         timemgr->AddTimeProperty(timeval);
118                                 }
119                                 
120                                 propval = timeval;
121                                 gameobj->SetProperty(prop->name,timeval);
122
123                         }
124                         default:
125                         {
126                                 // todo make an assert etc.
127                         }
128                 }
129                 
130                 if (propval)
131                 {
132                         if (show_debug_info)
133                         {
134                                 scene->AddDebugProperty(gameobj,STR_String(prop->name));
135                         }
136                         // done with propval, release it
137                         propval->Release();
138                 }
139                 
140 #ifdef WITH_PYTHON
141                 /* Warn if we double up on attributes, this isnt quite right since it wont find inherited attributes however there arnt many */
142                 for(PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
143                         if(strcmp(prop->name, attrdef->m_name)==0) {
144                                 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);
145                                 break;
146                         }
147                 }
148                 for(PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
149                         if(strcmp(prop->name, methdef->ml_name)==0) {
150                                 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);
151                                 break;
152                         }
153                 }
154                 /* end warning check */
155 #endif // WITH_PYTHON
156
157                 prop = prop->next;
158         }
159         // check if state needs to be debugged
160         if (object->scaflag & OB_DEBUGSTATE)
161         {
162                 //  reserve name for object state
163                 scene->AddDebugProperty(gameobj,STR_String("__state__"));
164         }
165
166         /* Font Objects need to 'copy' the Font Object data body to ["Text"] */
167         if (object->type == OB_FONT)
168         {
169                 BL_ConvertTextProperty(object, (KX_FontObject *)gameobj, timemgr, scene, isInActiveLayer);
170         }
171 }
172
173 void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
174 {
175         CValue* tprop = fontobj->GetProperty("Text");
176         if(!tprop) return;
177
178         Curve *curve = static_cast<Curve *>(object->data);
179         STR_String str = curve->str;
180
181         bProperty* prop = (bProperty*)object->prop.first;
182         CValue* propval;        
183
184         while(prop)
185         {
186                 if (strcmp(prop->name, "Text")!=0) {
187                         prop = prop->next;
188                         continue;
189                 }
190         
191                 propval = NULL;
192
193                 switch(prop->type) {
194                         case GPROP_BOOL:
195                         {
196                                 int value = atoi(str);
197                                 propval = new CBoolValue((bool)(value != 0));
198                                 tprop->SetValue(propval);
199                                 break;
200                         }
201                         case GPROP_INT:
202                         {
203                                 int value = atoi(str);
204                                 propval = new CIntValue(value);
205                                 tprop->SetValue(propval);
206                                 break;
207                         }
208                         case GPROP_FLOAT:
209                         {
210                                 float floatprop = atof(str);
211                                 propval = new CFloatValue(floatprop);
212                                 tprop->SetValue(propval);
213                                 break;
214                         }
215                         case GPROP_STRING:
216                         {
217                                 propval = new CStringValue(str, "");
218                                 tprop->SetValue(propval);
219                                 break;
220                         }
221                         case GPROP_TIME:
222                         {
223                                 float floatprop = atof(str);
224
225                                 CValue* timeval = new CFloatValue(floatprop);
226                                 // set a subproperty called 'timer' so that 
227                                 // we can register the replica of this property 
228                                 // at the time a game object is replicated (AddObjectActuator triggers this)
229                                 CValue *bval = new CBoolValue(true);
230                                 timeval->SetProperty("timer",bval);
231                                 bval->Release();
232                                 if (isInActiveLayer)
233                                 {
234                                         timemgr->AddTimeProperty(timeval);
235                                 }
236                                 
237                                 propval = timeval;
238                                 tprop->SetValue(timeval);
239                         }
240                         default:
241                         {
242                                 // todo make an assert etc.
243                         }
244                 }
245                 
246                 if (propval)
247                         propval->Release();
248
249                 prop = prop->next;
250         }
251 }
252