KX_ConvertProperties.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /** \file gameengine/Converter/KX_ConvertProperties.cpp
  28. * \ingroup bgeconv
  29. */
  30. #include "KX_ConvertProperties.h"
  31. #include "DNA_object_types.h"
  32. #include "DNA_property_types.h"
  33. /* end of blender include block */
  34. #include "EXP_Value.h"
  35. #include "EXP_VectorValue.h"
  36. #include "EXP_BoolValue.h"
  37. #include "EXP_StringValue.h"
  38. #include "EXP_FloatValue.h"
  39. #include "KX_GameObject.h"
  40. #include "EXP_IntValue.h"
  41. #include "SCA_TimeEventManager.h"
  42. #include "SCA_IScene.h"
  43. #include "KX_FontObject.h"
  44. #include "DNA_curve_types.h"
  45. /* This little block needed for linking to Blender... */
  46. #ifdef WIN32
  47. #include "BLI_winstuff.h"
  48. #endif
  49. extern "C" {
  50. #include "BKE_property.h"
  51. }
  52. /* prototype */
  53. void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer);
  54. void BL_ConvertProperties(Object* object,KX_GameObject* gameobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
  55. {
  56. bProperty* prop = (bProperty*)object->prop.first;
  57. CValue* propval;
  58. bool show_debug_info;
  59. while (prop) {
  60. propval = NULL;
  61. show_debug_info = bool (prop->flag & PROP_DEBUG);
  62. switch (prop->type) {
  63. case GPROP_BOOL:
  64. {
  65. propval = new CBoolValue((bool)(prop->data != 0));
  66. gameobj->SetProperty(prop->name,propval);
  67. //promp->poin= &prop->data;
  68. break;
  69. }
  70. case GPROP_INT:
  71. {
  72. propval = new CIntValue((int)prop->data);
  73. gameobj->SetProperty(prop->name,propval);
  74. break;
  75. }
  76. case GPROP_FLOAT:
  77. {
  78. //prop->poin= &prop->data;
  79. float floatprop = *((float*)&prop->data);
  80. propval = new CFloatValue(floatprop);
  81. gameobj->SetProperty(prop->name,propval);
  82. }
  83. break;
  84. case GPROP_STRING:
  85. {
  86. //prop->poin= callocN(MAX_PROPSTRING, "property string");
  87. propval = new CStringValue((char*)prop->poin,"");
  88. gameobj->SetProperty(prop->name,propval);
  89. break;
  90. }
  91. case GPROP_TIME:
  92. {
  93. float floatprop = *((float*)&prop->data);
  94. CValue* timeval = new CFloatValue(floatprop);
  95. // set a subproperty called 'timer' so that
  96. // we can register the replica of this property
  97. // at the time a game object is replicated (AddObjectActuator triggers this)
  98. CValue *bval = new CBoolValue(true);
  99. timeval->SetProperty("timer",bval);
  100. bval->Release();
  101. if (isInActiveLayer)
  102. {
  103. timemgr->AddTimeProperty(timeval);
  104. }
  105. propval = timeval;
  106. gameobj->SetProperty(prop->name,timeval);
  107. }
  108. default:
  109. {
  110. // todo make an assert etc.
  111. }
  112. }
  113. if (propval)
  114. {
  115. if (show_debug_info && isInActiveLayer)
  116. {
  117. scene->AddDebugProperty(gameobj,STR_String(prop->name));
  118. }
  119. // done with propval, release it
  120. propval->Release();
  121. }
  122. #ifdef WITH_PYTHON
  123. /* Warn if we double up on attributes, this isn't quite right since it wont find inherited attributes however there arnt many */
  124. for (PyAttributeDef *attrdef = KX_GameObject::Attributes; attrdef->m_name; attrdef++) {
  125. if (strcmp(prop->name, attrdef->m_name)==0) {
  126. 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);
  127. break;
  128. }
  129. }
  130. for (PyMethodDef *methdef = KX_GameObject::Methods; methdef->ml_name; methdef++) {
  131. if (strcmp(prop->name, methdef->ml_name)==0) {
  132. 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);
  133. break;
  134. }
  135. }
  136. /* end warning check */
  137. #endif // WITH_PYTHON
  138. prop = prop->next;
  139. }
  140. // check if state needs to be debugged
  141. if (object->scaflag & OB_DEBUGSTATE && isInActiveLayer)
  142. {
  143. // reserve name for object state
  144. scene->AddDebugProperty(gameobj,STR_String("__state__"));
  145. }
  146. /* Font Objects need to 'copy' the Font Object data body to ["Text"] */
  147. if (object->type == OB_FONT)
  148. {
  149. BL_ConvertTextProperty(object, (KX_FontObject *)gameobj, timemgr, scene, isInActiveLayer);
  150. }
  151. }
  152. void BL_ConvertTextProperty(Object* object, KX_FontObject* fontobj,SCA_TimeEventManager* timemgr,SCA_IScene* scene, bool isInActiveLayer)
  153. {
  154. CValue* tprop = fontobj->GetProperty("Text");
  155. if (!tprop) return;
  156. bProperty* prop = BKE_bproperty_object_get(object, "Text");
  157. if (!prop) return;
  158. Curve *curve = static_cast<Curve *>(object->data);
  159. STR_String str = curve->str;
  160. CValue* propval = NULL;
  161. switch (prop->type) {
  162. case GPROP_BOOL:
  163. {
  164. int value = atoi(str);
  165. propval = new CBoolValue((bool)(value != 0));
  166. tprop->SetValue(propval);
  167. break;
  168. }
  169. case GPROP_INT:
  170. {
  171. int value = atoi(str);
  172. propval = new CIntValue(value);
  173. tprop->SetValue(propval);
  174. break;
  175. }
  176. case GPROP_FLOAT:
  177. {
  178. float floatprop = (float)atof(str);
  179. propval = new CFloatValue(floatprop);
  180. tprop->SetValue(propval);
  181. break;
  182. }
  183. case GPROP_STRING:
  184. {
  185. propval = new CStringValue(str, "");
  186. tprop->SetValue(propval);
  187. break;
  188. }
  189. case GPROP_TIME:
  190. {
  191. float floatprop = (float)atof(str);
  192. CValue* timeval = new CFloatValue(floatprop);
  193. // set a subproperty called 'timer' so that
  194. // we can register the replica of this property
  195. // at the time a game object is replicated (AddObjectActuator triggers this)
  196. CValue *bval = new CBoolValue(true);
  197. timeval->SetProperty("timer",bval);
  198. bval->Release();
  199. if (isInActiveLayer)
  200. {
  201. timemgr->AddTimeProperty(timeval);
  202. }
  203. propval = timeval;
  204. tprop->SetValue(timeval);
  205. }
  206. default:
  207. {
  208. // todo make an assert etc.
  209. }
  210. }
  211. if (propval) {
  212. propval->Release();
  213. }
  214. }