merge with 2.5/trunk at r23271
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "KX_ConvertProperties.h"
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
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 /* This little block needed for linking to Blender... */
52 #ifdef WIN32
53 #include "BLI_winstuff.h"
54 #endif
55
56 void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
57 {
58         
59         bProperty* prop = (bProperty*)object->prop.first;
60         CValue* propval;        
61         bool show_debug_info;
62         while(prop)
63         {
64         
65                 propval = NULL;
66                 show_debug_info = bool (prop->flag & PROP_DEBUG);
67
68                 switch(prop->type) {
69                         case GPROP_BOOL:
70                         {
71                                 propval = new CBoolValue((bool)(prop->data != 0));
72                                 gameobj->SetProperty(prop->name,propval);
73                                 //promp->poin= &prop->data;
74                                 break;
75                         }
76                         case GPROP_INT:
77                         {
78                                 propval = new CIntValue((int)prop->data);
79                                 gameobj->SetProperty(prop->name,propval);
80                                 break;
81                         }
82                         case GPROP_FLOAT:
83                         {
84                                 //prop->poin= &prop->data;
85                                 float floatprop = *((float*)&prop->data);
86                                 propval = new CFloatValue(floatprop);
87                                 gameobj->SetProperty(prop->name,propval);
88                         }
89                         break;
90                         case GPROP_STRING:
91                         {
92                                 //prop->poin= callocN(MAX_PROPSTRING, "property string");
93                                 propval = new CStringValue((char*)prop->poin,"");
94                                 gameobj->SetProperty(prop->name,propval);
95                                 break;
96                         }
97                         case GPROP_TIME:
98                         {
99                                 float floatprop = *((float*)&prop->data);
100
101                                 CValue* timeval = new CFloatValue(floatprop);
102                                 // set a subproperty called 'timer' so that 
103                                 // we can register the replica of this property 
104                                 // at the time a game object is replicated (AddObjectActuator triggers this)
105                                 CValue *bval = new CBoolValue(true);
106                                 timeval->SetProperty("timer",bval);
107                                 bval->Release();
108                                 if (isInActiveLayer)
109                                 {
110                                         timemgr->AddTimeProperty(timeval);
111                                 }
112                                 
113                                 propval = timeval;
114                                 gameobj->SetProperty(prop->name,timeval);
115
116                         }
117                         default:
118                         {
119                                 // todo make an assert etc.
120                         }
121                 }
122                 
123                 if (propval)
124                 {
125                         if (show_debug_info)
126                         {
127                                 scene->AddDebugProperty(gameobj,STR_String(prop->name));
128                         }
129                         // done with propval, release it
130                         propval->Release();
131                 }
132                 
133                 
134                 /* Warn if we double up on attributes, this isnt quite right since it wont find inherited attributes however there arnt many */
135                 for(PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
136                         if(strcmp(prop->name, attrdef->m_name)==0) {
137                                 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);
138                                 break;
139                         }
140                 }
141                 for(PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
142                         if(strcmp(prop->name, methdef->ml_name)==0) {
143                                 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);
144                                 break;
145                         }
146                 }
147                 /* end warning check */
148
149                 prop = prop->next;
150         }
151         // check if state needs to be debugged
152         if (object->scaflag & OB_DEBUGSTATE)
153         {
154                 //  reserve name for object state
155                 scene->AddDebugProperty(gameobj,STR_String("__state__"));
156         }
157 }