component.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/simBase.h"
  24. #include "console/consoleTypes.h"
  25. #include "T3D/components/component.h"
  26. #include "core/util/safeDelete.h"
  27. #include "core/resourceManager.h"
  28. #include "core/stream/fileStream.h"
  29. #include "core/stream/bitStream.h"
  30. #include "console/engineAPI.h"
  31. #include "sim/netConnection.h"
  32. #include "console/consoleInternal.h"
  33. #include "T3D/assets/MaterialAsset.h"
  34. #define DECLARE_NATIVE_COMPONENT( ComponentType ) \
  35. Component* staticComponentTemplate = new ComponentType; \
  36. Sim::gNativeComponentSet->addObject(staticComponentTemplate);
  37. //////////////////////////////////////////////////////////////////////////
  38. // Constructor/Destructor
  39. //////////////////////////////////////////////////////////////////////////
  40. Component::Component()
  41. {
  42. mFriendlyName = StringTable->EmptyString();
  43. mFromResource = StringTable->EmptyString();
  44. mComponentType = StringTable->EmptyString();
  45. mComponentGroup = StringTable->EmptyString();
  46. mNetworkType = StringTable->EmptyString();
  47. mTemplateName = StringTable->EmptyString();
  48. //mDependency = StringTable->EmptyString();
  49. mNetworked = false;
  50. // [tom, 1/12/2007] We manage the memory for the description since it
  51. // could be loaded from a file and thus massive. This is accomplished with
  52. // protected fields, but since they still call Con::getData() the field
  53. // needs to always be valid. This is pretty lame.
  54. mDescription = new char[1];
  55. ((char *)mDescription)[0] = 0;
  56. mOwner = NULL;
  57. mCanSaveFieldDictionary = false;
  58. mOriginatingAssetId = StringTable->EmptyString();
  59. mIsServerObject = true;
  60. componentIdx = 0;
  61. mHidden = false;
  62. mEnabled = true;
  63. mDirtyMaskBits = 0;
  64. }
  65. Component::~Component()
  66. {
  67. for (S32 i = 0; i < mFields.size(); ++i)
  68. {
  69. ComponentField &field = mFields[i];
  70. SAFE_DELETE_ARRAY(field.mFieldDescription);
  71. }
  72. SAFE_DELETE_ARRAY(mDescription);
  73. }
  74. IMPLEMENT_CO_NETOBJECT_V1(Component);
  75. //////////////////////////////////////////////////////////////////////////
  76. void Component::initPersistFields()
  77. {
  78. addGroup("Component");
  79. addField("componentType", TypeCaseString, Offset(mComponentType, Component), "The type of behavior.", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  80. addField("networkType", TypeCaseString, Offset(mNetworkType, Component), "The type of behavior.", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  81. addField("friendlyName", TypeCaseString, Offset(mFriendlyName, Component), "Human friendly name of this behavior", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  82. addProtectedField("description", TypeCaseString, Offset(mDescription, Component), &setDescription, &getDescription,
  83. "The description of this behavior which can be set to a \"string\" or a fileName\n", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  84. addField("networked", TypeBool, Offset(mNetworked, Component), "Is this behavior ghosted to clients?", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  85. addProtectedField("Owner", TypeSimObjectPtr, Offset(mOwner, Component), &setOwner, &defaultProtectedGetFn, "", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  86. //addField("hidden", TypeBool, Offset(mHidden, Component), "Flags if this behavior is shown in the editor or not", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  87. addProtectedField("enabled", TypeBool, Offset(mEnabled, Component), &_setEnabled, &defaultProtectedGetFn, "");
  88. addField("originatingAsset", TypeComponentAssetPtr, Offset(mOriginatingAsset, Component),
  89. "Asset that spawned this component, used for tracking/housekeeping", AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  90. endGroup("Component");
  91. Parent::initPersistFields();
  92. //clear out irrelevent fields
  93. removeField("name");
  94. //removeField("internalName");
  95. removeField("parentGroup");
  96. //removeField("class");
  97. removeField("superClass");
  98. removeField("hidden");
  99. removeField("canSave");
  100. removeField("canSaveDynamicFields");
  101. removeField("persistentId");
  102. }
  103. bool Component::_setEnabled(void *object, const char *index, const char *data)
  104. {
  105. Component *c = static_cast<Component*>(object);
  106. c->mEnabled = dAtob(data);
  107. c->setMaskBits(EnableMask);
  108. return true;
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. bool Component::setDescription(void *object, const char *index, const char *data)
  112. {
  113. Component *bT = static_cast<Component *>(object);
  114. SAFE_DELETE_ARRAY(bT->mDescription);
  115. bT->mDescription = bT->getDescriptionText(data);
  116. // We return false since we don't want the console to mess with the data
  117. return false;
  118. }
  119. const char * Component::getDescription(void* obj, const char* data)
  120. {
  121. Component *object = static_cast<Component *>(obj);
  122. return object->mDescription ? object->mDescription : "";
  123. }
  124. //////////////////////////////////////////////////////////////////////////
  125. bool Component::onAdd()
  126. {
  127. if (!Parent::onAdd())
  128. return false;
  129. setMaskBits(UpdateMask);
  130. setMaskBits(NamespaceMask);
  131. return true;
  132. }
  133. void Component::onRemove()
  134. {
  135. onDataSet.removeAll();
  136. if (mOwner)
  137. {
  138. //notify our removal to the owner, so we have no loose ends
  139. mOwner->removeComponent(this, false);
  140. }
  141. Parent::onRemove();
  142. }
  143. void Component::onComponentAdd()
  144. {
  145. if (isServerObject())
  146. {
  147. if (isMethod("onAdd"))
  148. Con::executef(this, "onAdd");
  149. }
  150. mEnabled = true;
  151. }
  152. void Component::onComponentRemove()
  153. {
  154. mEnabled = false;
  155. if (isServerObject())
  156. {
  157. if (isMethod("onRemove"))
  158. Con::executef(this, "onRemove");
  159. }
  160. if (mOwner)
  161. {
  162. mOwner->onComponentAdded.remove(this, &Component::componentAddedToOwner);
  163. mOwner->onComponentRemoved.remove(this, &Component::componentRemovedFromOwner);
  164. }
  165. mOwner = NULL;
  166. setDataField("owner", NULL, "");
  167. }
  168. void Component::setOwner(Entity* owner)
  169. {
  170. //first, catch if we have an existing owner, and we're changing from it
  171. if (mOwner && mOwner != owner)
  172. {
  173. mOwner->onComponentAdded.remove(this, &Component::componentAddedToOwner);
  174. mOwner->onComponentRemoved.remove(this, &Component::componentRemovedFromOwner);
  175. mOwner->removeComponent(this, false);
  176. }
  177. mOwner = owner;
  178. if (mOwner != NULL)
  179. {
  180. mOwner->onComponentAdded.notify(this, &Component::componentAddedToOwner);
  181. mOwner->onComponentRemoved.notify(this, &Component::componentRemovedFromOwner);
  182. }
  183. if (isServerObject())
  184. {
  185. setMaskBits(OwnerMask);
  186. //if we have any outstanding maskbits, push them along to have the network update happen on the entity
  187. if (mDirtyMaskBits != 0 && mOwner)
  188. {
  189. mOwner->setMaskBits(Entity::ComponentsUpdateMask);
  190. }
  191. }
  192. }
  193. void Component::componentAddedToOwner(Component *comp)
  194. {
  195. return;
  196. }
  197. void Component::componentRemovedFromOwner(Component *comp)
  198. {
  199. return;
  200. }
  201. void Component::setMaskBits(U32 orMask)
  202. {
  203. AssertFatal(orMask != 0, "Invalid net mask bits set.");
  204. if (mOwner)
  205. mOwner->setComponentNetMask(this, orMask);
  206. }
  207. U32 Component::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  208. {
  209. U32 retMask = 0;
  210. /*if (mask & OwnerMask)
  211. {
  212. if (mOwner != NULL)
  213. {
  214. S32 ghostIndex = con->getGhostIndex(mOwner);
  215. if (ghostIndex == -1)
  216. {
  217. stream->writeFlag(false);
  218. retMask |= OwnerMask;
  219. }
  220. else
  221. {
  222. stream->writeFlag(true);
  223. stream->writeFlag(true);
  224. stream->writeInt(ghostIndex, NetConnection::GhostIdBitSize);
  225. }
  226. }
  227. else
  228. {
  229. stream->writeFlag(true);
  230. stream->writeFlag(false);
  231. }
  232. }
  233. else
  234. stream->writeFlag(false);*/
  235. if (stream->writeFlag(mask & EnableMask))
  236. {
  237. stream->writeFlag(mEnabled);
  238. }
  239. /*if (stream->writeFlag(mask & NamespaceMask))
  240. {
  241. const char* name = getName();
  242. if (stream->writeFlag(name && name[0]))
  243. stream->writeString(String(name));
  244. if (stream->writeFlag(mSuperClassName && mSuperClassName[0]))
  245. stream->writeString(String(mSuperClassName));
  246. if (stream->writeFlag(mClassName && mClassName[0]))
  247. stream->writeString(String(mClassName));
  248. }*/
  249. return retMask;
  250. }
  251. void Component::unpackUpdate(NetConnection *con, BitStream *stream)
  252. {
  253. /*if (stream->readFlag())
  254. {
  255. if (stream->readFlag())
  256. {
  257. //we have an owner object, so fetch it
  258. S32 gIndex = stream->readInt(NetConnection::GhostIdBitSize);
  259. Entity *e = dynamic_cast<Entity*>(con->resolveGhost(gIndex));
  260. if (e)
  261. e->addComponent(this);
  262. }
  263. else
  264. {
  265. //it's being nulled out
  266. setOwner(NULL);
  267. }
  268. }*/
  269. if (stream->readFlag())
  270. {
  271. mEnabled = stream->readFlag();
  272. }
  273. /*if (stream->readFlag())
  274. {
  275. if (stream->readFlag())
  276. {
  277. char name[256];
  278. stream->readString(name);
  279. assignName(name);
  280. }
  281. if (stream->readFlag())
  282. {
  283. char superClassname[256];
  284. stream->readString(superClassname);
  285. mSuperClassName = superClassname;
  286. }
  287. if (stream->readFlag())
  288. {
  289. char classname[256];
  290. stream->readString(classname);
  291. mClassName = classname;
  292. }
  293. linkNamespaces();
  294. }*/
  295. }
  296. void Component::packToStream(Stream &stream, U32 tabStop, S32 behaviorID, U32 flags /* = 0 */)
  297. {
  298. char buffer[1024];
  299. writeFields(stream, tabStop);
  300. // Write out the fields which the behavior template knows about
  301. for (int i = 0; i < getComponentFieldCount(); i++)
  302. {
  303. ComponentField *field = getComponentField(i);
  304. const char *objFieldValue = getDataField(field->mFieldName, NULL);
  305. // If the field holds the same value as the template's default value than it
  306. // will get initialized by the template, and so it won't be included just
  307. // to try to keep the object files looking as non-horrible as possible.
  308. if (dStrcmp(field->mDefaultValue, objFieldValue) != 0)
  309. {
  310. dSprintf(buffer, sizeof(buffer), "%s = \"%s\";\n", field->mFieldName, (dStrlen(objFieldValue) > 0 ? objFieldValue : "0"));
  311. stream.writeTabs(tabStop);
  312. stream.write(dStrlen(buffer), buffer);
  313. }
  314. }
  315. }
  316. void Component::processTick()
  317. {
  318. if (isServerObject() && mEnabled)
  319. {
  320. if (mOwner != NULL && isMethod("Update"))
  321. Con::executef(this, "Update");
  322. }
  323. }
  324. void Component::setDataField(StringTableEntry slotName, const char *array, const char *value)
  325. {
  326. Parent::setDataField(slotName, array, value);
  327. onDataSet.trigger(this, slotName, value);
  328. }
  329. StringTableEntry Component::getComponentName()
  330. {
  331. return getNamespace()->getName();
  332. }
  333. //catch any behavior field updates
  334. void Component::onStaticModified(const char* slotName, const char* newValue)
  335. {
  336. Parent::onStaticModified(slotName, newValue);
  337. //If we don't have an owner yet, then this is probably the initial setup, so we don't need the console callbacks yet.
  338. if (!mOwner)
  339. return;
  340. onDataSet.trigger(this, slotName, newValue);
  341. checkComponentFieldModified(slotName, newValue);
  342. }
  343. void Component::onDynamicModified(const char* slotName, const char* newValue)
  344. {
  345. Parent::onDynamicModified(slotName, newValue);
  346. //If we don't have an owner yet, then this is probably the initial setup, so we don't need the console callbacks yet.
  347. if (!mOwner)
  348. return;
  349. checkComponentFieldModified(slotName, newValue);
  350. }
  351. void Component::checkComponentFieldModified(const char* slotName, const char* newValue)
  352. {
  353. StringTableEntry slotNameEntry = StringTable->insert(slotName);
  354. //find if it's a behavior field
  355. for (int i = 0; i < mFields.size(); i++)
  356. {
  357. ComponentField *field = getComponentField(i);
  358. if (field->mFieldName == slotNameEntry)
  359. {
  360. //we have a match, do the script callback that we updated a field
  361. if (isMethod("onInspectorUpdate"))
  362. Con::executef(this, "onInspectorUpdate", slotName);
  363. return;
  364. }
  365. }
  366. }
  367. //////////////////////////////////////////////////////////////////////////
  368. //////////////////////////////////////////////////////////////////////////
  369. void Component::addComponentField(const char *fieldName, const char *desc, const char *type, const char *defaultValue /* = NULL */, const char *userData /* = NULL */, /*const char* dependency /* = NULL *//*,*/ bool hidden /* = false */)
  370. {
  371. StringTableEntry stFieldName = StringTable->insert(fieldName);
  372. for (S32 i = 0; i < mFields.size(); ++i)
  373. {
  374. if (mFields[i].mFieldName == stFieldName)
  375. return;
  376. }
  377. ComponentField field;
  378. field.mFieldName = stFieldName;
  379. //find the field type
  380. S32 fieldTypeMask = -1;
  381. StringTableEntry fieldType = StringTable->insert(type);
  382. if (fieldType == StringTable->insert("int"))
  383. fieldTypeMask = TypeS32;
  384. else if (fieldType == StringTable->insert("float"))
  385. fieldTypeMask = TypeF32;
  386. else if (fieldType == StringTable->insert("vector"))
  387. fieldTypeMask = TypePoint3F;
  388. else if (fieldType == StringTable->insert("material"))
  389. fieldTypeMask = TypeMaterialAssetPtr;
  390. else if (fieldType == StringTable->insert("image"))
  391. fieldTypeMask = TypeImageFilename;
  392. else if (fieldType == StringTable->insert("shape"))
  393. fieldTypeMask = TypeShapeFilename;
  394. else if (fieldType == StringTable->insert("bool"))
  395. fieldTypeMask = TypeBool;
  396. else if (fieldType == StringTable->insert("object"))
  397. fieldTypeMask = TypeSimObjectPtr;
  398. else if (fieldType == StringTable->insert("string"))
  399. fieldTypeMask = TypeString;
  400. else if (fieldType == StringTable->insert("colorI"))
  401. fieldTypeMask = TypeColorI;
  402. else if (fieldType == StringTable->insert("colorF"))
  403. fieldTypeMask = TypeColorF;
  404. else if (fieldType == StringTable->insert("ease"))
  405. fieldTypeMask = TypeEaseF;
  406. else if (fieldType == StringTable->insert("gameObject"))
  407. fieldTypeMask = TypeGameObjectAssetPtr;
  408. else
  409. fieldTypeMask = TypeString;
  410. field.mFieldTypeName = fieldType;
  411. field.mFieldType = fieldTypeMask;
  412. field.mUserData = StringTable->insert(userData ? userData : "");
  413. field.mDefaultValue = StringTable->insert(defaultValue ? defaultValue : "");
  414. field.mFieldDescription = getDescriptionText(desc);
  415. field.mGroup = mComponentGroup;
  416. field.mHidden = hidden;
  417. mFields.push_back(field);
  418. //Before we set this, we need to do a test to see if this field was already set, like from the mission file or a taml file
  419. const char* curFieldData = getDataField(field.mFieldName, NULL);
  420. if (dStrIsEmpty(curFieldData))
  421. setDataField(field.mFieldName, NULL, field.mDefaultValue);
  422. }
  423. ComponentField* Component::getComponentField(const char *fieldName)
  424. {
  425. StringTableEntry stFieldName = StringTable->insert(fieldName);
  426. for (S32 i = 0; i < mFields.size(); ++i)
  427. {
  428. if (mFields[i].mFieldName == stFieldName)
  429. return &mFields[i];
  430. }
  431. return NULL;
  432. }
  433. //////////////////////////////////////////////////////////////////////////
  434. const char * Component::getDescriptionText(const char *desc)
  435. {
  436. if (desc == NULL)
  437. return NULL;
  438. char *newDesc = "";
  439. // [tom, 1/12/2007] If it isn't a file, just do it the easy way
  440. if (!Platform::isFile(desc))
  441. {
  442. newDesc = new char[dStrlen(desc) + 1];
  443. dStrcpy(newDesc, desc);
  444. return newDesc;
  445. }
  446. FileStream str;
  447. str.open(desc, Torque::FS::File::Read);
  448. Stream *stream = &str;
  449. if (stream == NULL){
  450. str.close();
  451. return NULL;
  452. }
  453. U32 size = stream->getStreamSize();
  454. if (size > 0)
  455. {
  456. newDesc = new char[size + 1];
  457. if (stream->read(size, (void *)newDesc))
  458. newDesc[size] = 0;
  459. else
  460. {
  461. SAFE_DELETE_ARRAY(newDesc);
  462. }
  463. }
  464. str.close();
  465. //delete stream;
  466. return newDesc;
  467. }
  468. //////////////////////////////////////////////////////////////////////////
  469. void Component::beginFieldGroup(const char* groupName)
  470. {
  471. if (dStrcmp(mComponentGroup, ""))
  472. {
  473. Con::errorf("Component: attempting to begin new field group with a group already begun!");
  474. return;
  475. }
  476. mComponentGroup = StringTable->insert(groupName);
  477. }
  478. void Component::endFieldGroup()
  479. {
  480. mComponentGroup = StringTable->insert("");
  481. }
  482. void Component::addDependency(StringTableEntry name)
  483. {
  484. mDependencies.push_back_unique(name);
  485. }
  486. //////////////////////////////////////////////////////////////////////////
  487. // Console Methods
  488. //////////////////////////////////////////////////////////////////////////
  489. ConsoleMethod(Component, beginGroup, void, 3, 3, "(groupName)\n"
  490. "Starts the grouping for following fields being added to be grouped into\n"
  491. "@param groupName The name of this group\n"
  492. "@param desc The Description of this field\n"
  493. "@param type The DataType for this field (default, int, float, Point2F, bool, enum, Object, keybind, color)\n"
  494. "@param defaultValue The Default value for this field\n"
  495. "@param userData An extra data field that can be used for custom data on a per-field basis<br>Usage for default types<br>"
  496. "-enum: a TAB separated list of possible values<br>"
  497. "-object: the T2D object type that are valid choices for the field. The object types observe inheritance, so if you have a t2dSceneObject field you will be able to choose t2dStaticSrpites, t2dAnimatedSprites, etc.\n"
  498. "@return Nothing\n")
  499. {
  500. object->beginFieldGroup(argv[2]);
  501. }
  502. ConsoleMethod(Component, endGroup, void, 2, 2, "()\n"
  503. "Ends the grouping for prior fields being added to be grouped into\n"
  504. "@param groupName The name of this group\n"
  505. "@param desc The Description of this field\n"
  506. "@param type The DataType for this field (default, int, float, Point2F, bool, enum, Object, keybind, color)\n"
  507. "@param defaultValue The Default value for this field\n"
  508. "@param userData An extra data field that can be used for custom data on a per-field basis<br>Usage for default types<br>"
  509. "-enum: a TAB separated list of possible values<br>"
  510. "-object: the T2D object type that are valid choices for the field. The object types observe inheritance, so if you have a t2dSceneObject field you will be able to choose t2dStaticSrpites, t2dAnimatedSprites, etc.\n"
  511. "@return Nothing\n")
  512. {
  513. object->endFieldGroup();
  514. }
  515. DefineConsoleMethod(Component, addComponentField, void, (String fieldName, String fieldDesc, String fieldType, String defValue, String userData, bool hidden),
  516. ("", "", "", "", "", false),
  517. "Get the number of static fields on the object.\n"
  518. "@return The number of static fields defined on the object.")
  519. {
  520. object->addComponentField(fieldName, fieldDesc, fieldType, defValue, userData, hidden);
  521. }
  522. ConsoleMethod(Component, getComponentFieldCount, S32, 2, 2, "() - Get the number of ComponentField's on this object\n"
  523. "@return Returns the number of BehaviorFields as a nonnegative integer\n")
  524. {
  525. return object->getComponentFieldCount();
  526. }
  527. // [tom, 1/12/2007] Field accessors split into multiple methods to allow space
  528. // for long descriptions and type data.
  529. ConsoleMethod(Component, getComponentField, const char *, 3, 3, "(int index) - Gets a Tab-Delimited list of information about a ComponentField specified by Index\n"
  530. "@param index The index of the behavior\n"
  531. "@return FieldName, FieldType and FieldDefaultValue, each separated by a TAB character.\n")
  532. {
  533. ComponentField *field = object->getComponentField(dAtoi(argv[2]));
  534. if (field == NULL)
  535. return "";
  536. char *buf = Con::getReturnBuffer(1024);
  537. dSprintf(buf, 1024, "%s\t%s\t%s\t%s", field->mFieldName, field->mFieldType, field->mDefaultValue, field->mGroup);
  538. return buf;
  539. }
  540. ConsoleMethod(Component, setComponentield, const char *, 3, 3, "(int index) - Gets a Tab-Delimited list of information about a ComponentField specified by Index\n"
  541. "@param index The index of the behavior\n"
  542. "@return FieldName, FieldType and FieldDefaultValue, each separated by a TAB character.\n")
  543. {
  544. ComponentField *field = object->getComponentField(dAtoi(argv[2]));
  545. if (field == NULL)
  546. return "";
  547. char *buf = Con::getReturnBuffer(1024);
  548. dSprintf(buf, 1024, "%s\t%s\t%s", field->mFieldName, field->mFieldType, field->mDefaultValue);
  549. return buf;
  550. }
  551. DefineConsoleMethod(Component, getComponentFieldType, const char *, (String fieldName), ,
  552. "Get the number of static fields on the object.\n"
  553. "@return The number of static fields defined on the object.")
  554. {
  555. ComponentField *field = object->getComponentField(fieldName);
  556. if (field == NULL)
  557. return "";
  558. return field->mFieldTypeName;;
  559. }
  560. ConsoleMethod(Component, getBehaviorFieldUserData, const char *, 3, 3, "(int index) - Gets the UserData associated with a field by index in the field list\n"
  561. "@param index The index of the behavior\n"
  562. "@return Returns a string representing the user data of this field\n")
  563. {
  564. ComponentField *field = object->getComponentField(dAtoi(argv[2]));
  565. if (field == NULL)
  566. return "";
  567. return field->mUserData;
  568. }
  569. ConsoleMethod(Component, getComponentFieldDescription, const char *, 3, 3, "(int index) - Gets a field description by index\n"
  570. "@param index The index of the behavior\n"
  571. "@return Returns a string representing the description of this field\n")
  572. {
  573. ComponentField *field = object->getComponentField(dAtoi(argv[2]));
  574. if (field == NULL)
  575. return "";
  576. return field->mFieldDescription ? field->mFieldDescription : "";
  577. }
  578. ConsoleMethod(Component, addDependency, void, 3, 3, "(string behaviorName) - Gets a field description by index\n"
  579. "@param index The index of the behavior\n"
  580. "@return Returns a string representing the description of this field\n")
  581. {
  582. object->addDependency(argv[2]);
  583. }
  584. ConsoleMethod(Component, setDirty, void, 2, 2, "() - Gets a field description by index\n"
  585. "@param index The index of the behavior\n"
  586. "@return Returns a string representing the description of this field\n")
  587. {
  588. object->setMaskBits(Component::OwnerMask);
  589. }