simObject.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _SIM_OBJECT_H_
  23. #define _SIM_OBJECT_H_
  24. #ifndef _CONSOLEOBJECT_H_
  25. #include "console/consoleObject.h"
  26. #endif
  27. #ifndef _SIM_FIELD_DICTIONARY_H_
  28. #include "sim/simFieldDictionary.h"
  29. #endif
  30. #ifndef _TAML_CALLBACKS_H_
  31. #include "persistence/taml/tamlCallbacks.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. using namespace std;
  35. #include <vector>
  36. #include <string>
  37. typedef U32 SimObjectId;
  38. class SimGroup;
  39. //---------------------------------------------------------------------------
  40. /// Base class for objects involved in the simulation.
  41. ///
  42. /// @section simobject_intro Introduction
  43. ///
  44. /// SimObject is a base class for most of the classes you'll encounter
  45. /// working in Torque. It provides fundamental services allowing "smart"
  46. /// object referencing, creation, destruction, organization, and location.
  47. /// Along with SimEvent, it gives you a flexible event-scheduling system,
  48. /// as well as laying the foundation for the in-game editors, GUI system,
  49. /// and other vital subsystems.
  50. ///
  51. /// @section simobject_subclassing Subclassing
  52. ///
  53. /// You will spend a lot of your time in Torque subclassing, or working
  54. /// with subclasses of, SimObject. SimObject is designed to be easy to
  55. /// subclass.
  56. ///
  57. /// You should not need to override anything in a subclass except:
  58. /// - The constructor/destructor.
  59. /// - processArguments()
  60. /// - onAdd()/onRemove()
  61. /// - onGroupAdd()/onGroupRemove()
  62. /// - onNameChange()
  63. /// - onStaticModified()
  64. /// - onDeleteNotify()
  65. /// - onEditorEnable()/onEditorDisable()
  66. /// - inspectPreApply()/inspectPostApply()
  67. /// - things from ConsoleObject (see ConsoleObject docs for specifics)
  68. ///
  69. /// Of course, if you know what you're doing, go nuts! But in most cases, you
  70. /// shouldn't need to touch things not on that list.
  71. ///
  72. /// When you subclass, you should define a typedef in the class, called Parent,
  73. /// that references the class you're inheriting from.
  74. ///
  75. /// @code
  76. /// class mySubClass : public SimObject {
  77. /// typedef SimObject Parent;
  78. /// ...
  79. /// @endcode
  80. ///
  81. /// Then, when you override a method, put in:
  82. ///
  83. /// @code
  84. /// bool mySubClass::onAdd()
  85. /// {
  86. /// if(!Parent::onAdd())
  87. /// return false;
  88. ///
  89. /// // ... do other things ...
  90. /// }
  91. /// @endcode
  92. ///
  93. /// Of course, you want to replace onAdd with the appropriate method call.
  94. ///
  95. /// @section simobject_lifecycle A SimObject's Life Cycle
  96. ///
  97. /// SimObjects do not live apart. One of the primary benefits of using a
  98. /// SimObject is that you can uniquely identify it and easily find it (using
  99. /// its ID). Torque does this by keeping a global hierarchy of SimGroups -
  100. /// a tree - containing every registered SimObject. You can then query
  101. /// for a given object using Sim::findObject() (or SimSet::findObject() if
  102. /// you want to search only a specific set).
  103. ///
  104. /// @code
  105. /// // Three examples of registering an object.
  106. ///
  107. /// // Method 1:
  108. /// AIClient *aiPlayer = new AIClient();
  109. /// aiPlayer->registerObject();
  110. ///
  111. /// // Method 2:
  112. /// ActionMap* globalMap = new ActionMap;
  113. /// globalMap->registerObject("GlobalActionMap");
  114. ///
  115. /// // Method 3:
  116. /// bool reg = mObj->registerObject(id);
  117. /// @endcode
  118. ///
  119. /// Registering a SimObject performs these tasks:
  120. /// - Marks the object as not cleared and not removed.
  121. /// - Assigns the object a unique SimObjectID if it does not have one already.
  122. /// - Adds the object to the global name and ID dictionaries so it can be found
  123. /// again.
  124. /// - Calls the object's onAdd() method. <b>Note:</b> SimObject::onAdd() performs
  125. /// some important initialization steps. See @ref simobject_subclassing "here
  126. /// for details" on how to properly subclass SimObject.
  127. /// - If onAdd() fails (returns false), it calls unregisterObject().
  128. /// - Checks to make sure that the SimObject was properly initialized (and asserts
  129. /// if not).
  130. ///
  131. /// Calling registerObject() and passing an ID or a name will cause the object to be
  132. /// assigned that name and/or ID before it is registered.
  133. ///
  134. /// Congratulations, you have now registered your object! What now?
  135. ///
  136. /// Well, hopefully, the SimObject will have a long, useful life. But eventually,
  137. /// it must die.
  138. ///
  139. /// There are a two ways a SimObject can die.
  140. /// - First, the game can be shut down. This causes the root SimGroup
  141. /// to be unregistered and deleted. When a SimGroup is unregistered,
  142. /// it unregisters all of its member SimObjects; this results in everything
  143. /// that has been registered with Sim being unregistered, as everything
  144. /// registered with Sim is in the root group.
  145. /// - Second, you can manually kill it off, either by calling unregisterObject()
  146. /// or by calling deleteObject().
  147. ///
  148. /// When you unregister a SimObject, the following tasks are performed:
  149. /// - The object is flagged as removed.
  150. /// - Notifications are cleaned up.
  151. /// - If the object is in a group, then it removes itself from the group.
  152. /// - Delete notifications are sent out.
  153. /// - Finally, the object removes itself from the Sim globals, and tells
  154. /// Sim to get rid of any pending events for it.
  155. ///
  156. /// If you call deleteObject(), all of the above tasks are performed, in addition
  157. /// to some sanity checking to make sure the object was previously added properly,
  158. /// and isn't in the process of being deleted. After the object is unregistered, it
  159. /// de-allocates itself.
  160. ///
  161. /// @section simobject_editor Torque Editors
  162. ///
  163. /// SimObjects are one of the building blocks for the in-game editors. They
  164. /// provide a basic interface for the editor to be able to list the fields
  165. /// of the object, update them safely and reliably, and inform the object
  166. /// things have changed.
  167. ///
  168. /// This interface is implemented in the following areas:
  169. /// - onNameChange() is called when the object is renamed.
  170. /// - onStaticModified() is called whenever a static field is modified.
  171. /// - inspectPreApply() is called before the object's fields are updated,
  172. /// when changes are being applied.
  173. /// - inspectPostApply() is called after the object's fields are updated.
  174. /// - onEditorEnable() is called whenever an editor is enabled (for instance,
  175. /// when you hit F11 to bring up the world editor).
  176. /// - onEditorDisable() is called whenever the editor is disabled (for instance,
  177. /// when you hit F11 again to close the world editor).
  178. ///
  179. /// (Note: you can check the variable gEditingMission to see if the mission editor
  180. /// is running; if so, you may want to render special indicators. For instance, the
  181. /// fxFoliageReplicator renders inner and outer radii when the mission editor is
  182. /// running.)
  183. ///
  184. /// @section simobject_console The Console
  185. ///
  186. /// SimObject extends ConsoleObject by allowing you to
  187. /// to set arbitrary dynamic fields on the object, as well as
  188. /// statically defined fields. This is done through two methods,
  189. /// setDataField and getDataField, which deal with the complexities of
  190. /// allowing access to two different types of object fields.
  191. ///
  192. /// Static fields take priority over dynamic fields. This is to be
  193. /// expected, as the role of dynamic fields is to allow data to be
  194. /// stored in addition to the predefined fields.
  195. ///
  196. /// The fields in a SimObject are like properties (or fields) in a class.
  197. ///
  198. /// Some fields may be arrays, which is what the array parameter is for; if it's non-null,
  199. /// then it is parsed with dAtoI and used as an index into the array. If you access something
  200. /// as an array which isn't, then you get an empty string.
  201. ///
  202. /// <b>You don't need to read any further than this.</b> Right now,
  203. /// set/getDataField are called a total of 6 times through the entire
  204. /// Torque codebase. Therefore, you probably don't need to be familiar
  205. /// with the details of accessing them. You may want to look at Con::setData
  206. /// instead. Most of the time you will probably be accessing fields directly,
  207. /// or using the scripting language, which in either case means you don't
  208. /// need to do anything special.
  209. ///
  210. /// The functions to get/set these fields are very straightforward:
  211. ///
  212. /// @code
  213. /// setDataField(StringTable->insert("locked", false), NULL, b ? "true" : "false" );
  214. /// curObject->setDataField(curField, curFieldArray, STR.getStringValue());
  215. /// setDataField(slotName, array, value);
  216. /// @endcode
  217. ///
  218. /// <i>For advanced users:</i> There are two flags which control the behavior
  219. /// of these functions. The first is ModStaticFields, which controls whether
  220. /// or not the DataField functions look through the static fields (defined
  221. /// with addField; see ConsoleObject for details) of the class. The second
  222. /// is ModDynamicFields, which controls dynamically defined fields. They are
  223. /// set automatically by the console constructor code.
  224. ///
  225. /// @nosubgrouping
  226. class SimObject: public ConsoleObject, public TamlCallbacks
  227. {
  228. typedef ConsoleObject Parent;
  229. friend class SimManager;
  230. friend class SimGroup;
  231. friend class SimNameDictionary;
  232. friend class SimManagerNameDictionary;
  233. friend class SimIdDictionary;
  234. //-------------------------------------- Structures and enumerations
  235. private:
  236. /// Flags for use in mFlags
  237. enum {
  238. Deleted = BIT(0), ///< This object is marked for deletion.
  239. Removed = BIT(1), ///< This object has been unregistered from the object system.
  240. Added = BIT(3), ///< This object has been registered with the object system.
  241. Selected = BIT(4), ///< This object has been marked as selected. (in editor)
  242. Expanded = BIT(5), ///< This object has been marked as expanded. (in editor)
  243. ModStaticFields = BIT(6), ///< The object allows you to read/modify static fields
  244. ModDynamicFields = BIT(7), ///< The object allows you to read/modify dynamic fields
  245. Hidden = BIT(8), ///< Object is hidden in editors.
  246. Locked = BIT(9) ///< Object is locked in editors.
  247. };
  248. static const char* _getHidden(void* object, const char* data)
  249. {
  250. if (static_cast<SimObject*>(object)->isHidden()) return "1"; return "0";
  251. }
  252. static const char* _getLocked(void* object, const char* data)
  253. {
  254. if (static_cast<SimObject*>(object)->isLocked()) return "1"; return "0";
  255. }
  256. static bool _setHidden(void* object, const char* data)
  257. {
  258. static_cast<SimObject*>(object)->setHidden(dAtob(data)); return false;
  259. }
  260. static bool _setLocked(void* object, const char* data)
  261. {
  262. static_cast<SimObject*>(object)->setLocked(dAtob(data)); return false;
  263. }
  264. static bool _writeHidden(void* object, const char* data)
  265. {
  266. return static_cast<SimObject*>(object)->isHidden();
  267. }
  268. static bool _writeLocked(void* object, const char* data)
  269. {
  270. return static_cast<SimObject*>(object)->isLocked();
  271. }
  272. public:
  273. /// @name Notification
  274. /// @{
  275. struct Notify {
  276. enum Type {
  277. ClearNotify, ///< Notified when the object is cleared.
  278. DeleteNotify, ///< Notified when the object is deleted.
  279. ObjectRef, ///< Cleverness to allow tracking of references.
  280. Invalid ///< Mark this notification as unused (used in freeNotify).
  281. } type;
  282. void *ptr; ///< Data (typically referencing or interested object).
  283. Notify *next; ///< Next notification in the linked list.
  284. };
  285. /// @}
  286. enum WriteFlags {
  287. SelectedOnly = BIT(0) ///< Passed to SimObject::write to indicate that only objects
  288. /// marked as selected should be outputted. Used in SimSet.
  289. };
  290. private:
  291. // dictionary information stored on the object
  292. StringTableEntry objectName;
  293. SimObject* nextNameObject;
  294. SimObject* nextManagerNameObject;
  295. SimObject* nextIdObject;
  296. SimGroup* mGroup; ///< SimGroup we're contained in, if any.
  297. BitSet32 mFlags;
  298. StringTableEntry mProgenitorFile;
  299. S32 mPeriodicTimerID;
  300. /// @name Notification
  301. /// @{
  302. Notify* mNotifyList;
  303. /// @}
  304. Vector<StringTableEntry> mFieldFilter;
  305. protected:
  306. SimObjectId mId; ///< Id number for this object.
  307. StringTableEntry mIdString;
  308. Namespace* mNameSpace;
  309. U32 mTypeMask;
  310. S32 mScriptCallbackGuard; ///< Whether the object is executing a script callback.
  311. protected:
  312. /// @name Notification
  313. /// Helper functions for notification code.
  314. /// @{
  315. static SimObject::Notify *mNotifyFreeList;
  316. static SimObject::Notify *allocNotify(); ///< Get a free Notify structure.
  317. static void freeNotify(SimObject::Notify*); ///< Mark a Notify structure as free.
  318. /// @}
  319. private:
  320. SimFieldDictionary *mFieldDictionary; ///< Storage for dynamic fields.
  321. protected:
  322. /// Taml callbacks.
  323. virtual void onTamlPreWrite( void ) {}
  324. virtual void onTamlPostWrite( void ) {}
  325. virtual void onTamlPreRead( void ) {}
  326. virtual void onTamlPostRead( const TamlCustomNodes& customNodes ) {}
  327. virtual void onTamlAddParent( SimObject* pParentObject ) {}
  328. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes ) {}
  329. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes ) {}
  330. protected:
  331. bool mCanSaveFieldDictionary; ///< true if dynamic fields (added at runtime) should be saved, defaults to true
  332. StringTableEntry mInternalName; ///< Stores object Internal Name
  333. // Namespace linking
  334. StringTableEntry mClassName; ///< Stores the class name to link script class namespaces
  335. StringTableEntry mSuperClassName; ///< Stores super class name to link script class namespaces
  336. static bool setClass(void* obj, const char* data) { static_cast<SimObject*>(obj)->setClassNamespace(data); return false; };
  337. static bool setSuperClass(void* obj, const char* data) { static_cast<SimObject*>(obj)->setSuperClassNamespace(data); return false; };
  338. static bool writeObjectName(void* obj, StringTableEntry pFieldName) { SimObject* simObject = static_cast<SimObject*>(obj); return simObject->objectName != NULL && simObject->objectName != StringTable->EmptyString; }
  339. static bool writeCanSaveDynamicFields( void* obj, StringTableEntry pFieldName ) { return static_cast<SimObject*>(obj)->mCanSaveFieldDictionary == false; }
  340. static bool writeInternalName( void* obj, StringTableEntry pFieldName ) { SimObject* simObject = static_cast<SimObject*>(obj); return simObject->mInternalName != NULL && simObject->mInternalName != StringTable->EmptyString; }
  341. static bool setParentGroup(void* obj, const char* data);
  342. static bool writeParentGroup( void* obj, StringTableEntry pFieldName ) { return static_cast<SimObject*>(obj)->mGroup != NULL; }
  343. static bool writeSuperclass( void* obj, StringTableEntry pFieldName ) { SimObject* simObject = static_cast<SimObject*>(obj); return simObject->mSuperClassName != NULL && simObject->mSuperClassName != StringTable->EmptyString; }
  344. static bool writeClass( void* obj, StringTableEntry pFieldName ) { SimObject* simObject = static_cast<SimObject*>(obj); return simObject->mClassName != NULL && simObject->mClassName != StringTable->EmptyString; }
  345. static bool setProtectedName(void * obj, const char * data);
  346. // Accessors
  347. public:
  348. StringTableEntry getClassNamespace() const { return mClassName; };
  349. StringTableEntry getSuperClassNamespace() const { return mSuperClassName; };
  350. void setClassNamespace( const char* classNamespace );
  351. void setSuperClassNamespace( const char* superClassNamespace );
  352. // Script callback deletion guard.
  353. inline void pushScriptCallbackGuard( void ) { mScriptCallbackGuard++; }
  354. inline void popScriptCallbackGuard( void ) { mScriptCallbackGuard--; AssertFatal( mScriptCallbackGuard >= 0, "Invalid script callback guard." ); }
  355. inline S32 getScriptCallbackGuard( void ) { return mScriptCallbackGuard; }
  356. protected:
  357. void linkNamespaces();
  358. void unlinkNamespaces();
  359. public:
  360. /// @name Accessors
  361. /// @{
  362. /// Get the value of a field on the object.
  363. ///
  364. /// See @ref simobject_console "here" for a detailed discussion of what this
  365. /// function does.
  366. ///
  367. /// @param slotName Field to access.
  368. /// @param array String containing index into array
  369. /// (if field is an array); if NULL, it is ignored.
  370. static bool disableNameChanging;
  371. const char *getDataField(StringTableEntry slotName, const char *array);
  372. /// Set the value of a field on the object.
  373. ///
  374. /// See @ref simobject_console "here" for a detailed discussion of what this
  375. /// function does.
  376. ///
  377. /// @param slotName Field to access.
  378. /// @param array String containing index into array; if NULL, it is ignored.
  379. /// @param value Value to store.
  380. virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
  381. const char *getPrefixedDataField(StringTableEntry fieldName, const char *array);
  382. void setPrefixedDataField(StringTableEntry fieldName, const char *array, const char *value);
  383. const char *getPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const S32 fieldType = -1);
  384. void setPrefixedDynamicDataField(StringTableEntry fieldName, const char *array, const char *value, const S32 fieldType = -1);
  385. StringTableEntry getDataFieldPrefix( StringTableEntry fieldName );
  386. /// Get the type of a field on the object.
  387. ///
  388. /// @param slotName Field to access.
  389. /// @param array String containing index into array
  390. /// (if field is an array); if NULL, it is ignored.
  391. U32 getDataFieldType(StringTableEntry slotName, const char *array);
  392. /// Get reference to the dictionary containing dynamic fields.
  393. ///
  394. /// See @ref simobject_console "here" for a detailed discussion of what this
  395. /// function does.
  396. ///
  397. /// This dictionary can be iterated over using a SimFieldDictionaryIterator.
  398. SimFieldDictionary * getFieldDictionary() {return(mFieldDictionary);}
  399. /// Clear all dynamic fields.
  400. inline void clearDynamicFields( void ) { if ( mFieldDictionary != NULL ) { delete mFieldDictionary; mFieldDictionary = new SimFieldDictionary; } }
  401. /// Set whether fields created at runtime should be saved. Default is true.
  402. void setCanSaveDynamicFields(bool bCanSave){ mCanSaveFieldDictionary = bCanSave;}
  403. /// Get whether fields created at runtime should be saved. Default is true.
  404. inline bool getCanSaveDynamicFields(void) const { return mCanSaveFieldDictionary;}
  405. /// These functions support internal naming that is not namespace
  406. /// bound for locating child controls in a generic way.
  407. ///
  408. /// Set the internal name of this control (Not linked to a namespace)
  409. void setInternalName(const char* newname);
  410. /// Get the internal of of this control
  411. StringTableEntry getInternalName();
  412. /// Save object as a TorqueScript File.
  413. virtual bool save(const char* pcFilePath, bool bOnlySelected=false);
  414. /// Check if a method exists in the objects current namespace.
  415. virtual bool isMethod( const char* methodName );
  416. /// @}
  417. /// @name Initialization
  418. /// @{
  419. ///
  420. SimObject();
  421. virtual ~SimObject();
  422. virtual bool processArguments(S32 argc, const char **argv); ///< Process constructor options. (ie, new SimObject(1,2,3))
  423. /// @}
  424. /// @name Events
  425. /// @{
  426. virtual bool onAdd(); ///< Called when the object is added to the sim.
  427. virtual void onRemove(); ///< Called when the object is removed from the sim.
  428. virtual void onGroupAdd(); ///< Called when the object is added to a SimGroup.
  429. virtual void onGroupRemove(); ///< Called when the object is removed from a SimGroup.
  430. virtual void onNameChange(const char *name); ///< Called when the object's name is changed.
  431. virtual void onStaticModified(const char* slotName, const char*newValue = NULL); ///< Called when a static field is modified.
  432. ///
  433. /// Specifically, this is called by setDataField
  434. /// when a static field is modified, see
  435. /// @ref simobject_console "the console details".
  436. /// Called before any property of the object is changed in the world editor.
  437. ///
  438. /// The calling order here is:
  439. /// - inspectPreApply()
  440. /// - ...
  441. /// - calls to setDataField()
  442. /// - ...
  443. /// - inspectPostApply()
  444. virtual void inspectPreApply();
  445. /// Called after any property of the object is changed in the world editor.
  446. ///
  447. /// @see inspectPreApply
  448. virtual void inspectPostApply();
  449. /// Called when a SimObject is deleted.
  450. ///
  451. /// When you are on the notification list for another object
  452. /// and it is deleted, this method is called.
  453. virtual void onDeleteNotify(SimObject *object);
  454. /// Called when the editor is activated.
  455. virtual void onEditorEnable(){};
  456. /// Called when the editor is deactivated.
  457. virtual void onEditorDisable(){};
  458. /// @}
  459. /// Find a named sub-object of this object.
  460. ///
  461. /// This is subclassed in the SimGroup and SimSet classes.
  462. ///
  463. /// For a single object, it just returns NULL, as normal objects cannot have children.
  464. virtual SimObject *findObject(const char *name);
  465. /// @name Notification
  466. /// @{
  467. Notify *removeNotify(void *ptr, Notify::Type); ///< Remove a notification from the list.
  468. void deleteNotify(SimObject* obj); ///< Notify an object when we are deleted.
  469. void clearNotify(SimObject* obj); ///< Notify an object when we are cleared.
  470. void clearAllNotifications(); ///< Remove all notifications for this object.
  471. void processDeleteNotifies(); ///< Send out deletion notifications.
  472. /// Register a reference to this object.
  473. ///
  474. /// You pass a pointer to your reference to this object.
  475. ///
  476. /// When the object is deleted, it will null your
  477. /// pointer, ensuring you don't access old memory.
  478. ///
  479. /// @param obj Pointer to your reference to the object.
  480. void registerReference(SimObject **obj);
  481. /// Unregister a reference to this object.
  482. ///
  483. /// Remove a reference from the list, so that it won't
  484. /// get nulled inappropriately.
  485. ///
  486. /// Call this when you're done with your reference to
  487. /// the object, especially if you're going to free the
  488. /// memory. Otherwise, you may erroneously get something
  489. /// overwritten.
  490. ///
  491. /// @see registerReference
  492. void unregisterReference(SimObject **obj);
  493. /// @}
  494. /// @name Registration
  495. ///
  496. /// SimObjects must be registered with the object system.
  497. /// @{
  498. /// Register an object with the object system.
  499. ///
  500. /// This must be called if you want to keep the object around.
  501. /// In the rare case that you will delete the object immediately, or
  502. /// don't want to be able to use Sim::findObject to locate it, then
  503. /// you don't need to register it.
  504. ///
  505. /// registerObject adds the object to the global ID and name dictionaries,
  506. /// after first assigning it a new ID number. It calls onAdd(). If onAdd fails,
  507. /// it unregisters the object and returns false.
  508. ///
  509. /// If a subclass's onAdd doesn't eventually call SimObject::onAdd(), it will
  510. /// cause an assertion.
  511. bool registerObject();
  512. /// Register the object, forcing the id.
  513. ///
  514. /// @see registerObject()
  515. /// @param id ID to assign to the object.
  516. bool registerObject(U32 id);
  517. /// Register the object, assigning the name.
  518. ///
  519. /// @see registerObject()
  520. /// @param name Name to assign to the object.
  521. bool registerObject(const char *name);
  522. /// Register the object, assigning a name and ID.
  523. ///
  524. /// @see registerObject()
  525. /// @param name Name to assign to the object.
  526. /// @param id ID to assign to the object.
  527. bool registerObject(const char *name, U32 id);
  528. /// Unregister the object from Sim.
  529. ///
  530. /// This performs several operations:
  531. /// - Sets the removed flag.
  532. /// - Call onRemove()
  533. /// - Clear out notifications.
  534. /// - Remove the object from...
  535. /// - its group, if any. (via getGroup)
  536. /// - Sim::gNameDictionary
  537. /// - Sim::gIDDictionary
  538. /// - Finally, cancel any pending events for this object (as it can't receive them now).
  539. void unregisterObject();
  540. void deleteObject(); ///< Unregister, mark as deleted, and free the object.
  541. ///
  542. /// This helper function can be used when you're done with the object
  543. /// and don't want to be bothered with the details of cleaning it up.
  544. /// @}
  545. /// @name Accessors
  546. /// @{
  547. inline SimObjectId getId( void ) const { return mId; }
  548. inline StringTableEntry getIdString( void ) const { return mIdString; }
  549. U32 getType() const { return mTypeMask; }
  550. const StringTableEntry getName( void ) const { return objectName; };
  551. void setId(SimObjectId id);
  552. void assignName(const char* name);
  553. SimGroup* getGroup() const { return mGroup; }
  554. bool isChildOfGroup(SimGroup* pGroup);
  555. bool isProperlyAdded() const { return mFlags.test(Added); }
  556. bool isDeleted() const { return mFlags.test(Deleted); }
  557. bool isRemoved() const { return mFlags.test(Deleted | Removed); }
  558. bool isLocked() const { return mFlags.test(Locked); }
  559. void setLocked( bool b );
  560. bool isHidden()const { return mFlags.test(Hidden); }
  561. void setHidden(bool b);
  562. inline void setProgenitorFile( const char* pFile ) { mProgenitorFile = StringTable->insert( pFile ); }
  563. inline StringTableEntry getProgenitorFile( void ) const { return mProgenitorFile; }
  564. inline void setPeriodicTimerID( const S32 timerID ) { mPeriodicTimerID = timerID; }
  565. inline S32 getPeriodicTimerID( void ) const { return mPeriodicTimerID; }
  566. inline bool isPeriodicTimerActive( void ) const { return mPeriodicTimerID != 0; }
  567. /// @}
  568. /// @name Sets
  569. ///
  570. /// The object must be properly registered before you can add/remove it to/from a set.
  571. ///
  572. /// All these functions accept either a name or ID to identify the set you wish
  573. /// to operate on. Then they call addObject or removeObject on the set, which
  574. /// sets up appropriate notifications.
  575. ///
  576. /// An object may be in multiple sets at a time.
  577. /// @{
  578. bool addToSet(SimObjectId);
  579. bool addToSet(const char *);
  580. bool removeFromSet(SimObjectId);
  581. bool removeFromSet(const char *);
  582. /// @}
  583. /// @name Serialization
  584. /// @{
  585. /// Determine whether or not a field should be written.
  586. ///
  587. /// @param fieldname The name of the field being written.
  588. /// @param value The value of the field.
  589. virtual bool writeField(StringTableEntry fieldname, const char* value);
  590. /// Output the TorqueScript to recreate this object.
  591. ///
  592. /// This calls writeFields internally.
  593. /// @param stream Stream to output to.
  594. /// @param tabStop Indentation level for this object.
  595. /// @param flags If SelectedOnly is passed here, then
  596. /// only objects marked as selected (using setSelected)
  597. /// will output themselves.
  598. virtual void write(Stream &stream, U32 tabStop, U32 flags = 0);
  599. /// Write the fields of this object in TorqueScript.
  600. ///
  601. /// @param stream Stream for output.
  602. /// @param tabStop Indentation level for the fields.
  603. virtual void writeFields(Stream &stream, U32 tabStop);
  604. virtual bool writeObject(Stream *stream);
  605. virtual bool readObject(Stream *stream);
  606. virtual void buildFilterList();
  607. void addFieldFilter(const char *fieldName);
  608. void removeFieldFilter(const char *fieldName);
  609. void clearFieldFilters();
  610. bool isFiltered(const char *fieldName);
  611. /// Copy fields from another object onto this one.
  612. ///
  613. /// Objects must be of same type. Everything from obj
  614. /// will overwrite what's in this object; extra fields
  615. /// in this object will remain. This includes dynamic
  616. /// fields.
  617. ///
  618. /// @param obj Object to copy from.
  619. void assignFieldsFrom(SimObject *obj);
  620. /// Copy dynamic fields from another object onto this one.
  621. ///
  622. /// Everything from obj will overwrite what's in this
  623. /// object.
  624. ///
  625. /// @param obj Object to copy from.
  626. void assignDynamicFieldsFrom(SimObject *obj);
  627. /// @}
  628. /// Return the object's namespace.
  629. Namespace* getNamespace() { return mNameSpace; }
  630. /// Get next matching item in namespace.
  631. ///
  632. /// This wraps a call to Namespace::tabComplete; it gets the
  633. /// next thing in the namespace, given a starting value
  634. /// and a base length of the string. See
  635. /// Namespace::tabComplete for details.
  636. const char *tabComplete(const char *prevText, S32 baseLen, bool);
  637. /// @name Accessors
  638. /// @{
  639. bool isSelected() const { return mFlags.test(Selected); }
  640. bool isExpanded() const { return mFlags.test(Expanded); }
  641. void setSelected(bool sel) { if(sel) mFlags.set(Selected); else mFlags.clear(Selected); }
  642. void setExpanded(bool exp) { if(exp) mFlags.set(Expanded); else mFlags.clear(Expanded); }
  643. void setModDynamicFields(bool dyn) { if(dyn) mFlags.set(ModDynamicFields); else mFlags.clear(ModDynamicFields); }
  644. void setModStaticFields(bool sta) { if(sta) mFlags.set(ModStaticFields); else mFlags.clear(ModStaticFields); }
  645. /// @}
  646. /// @Object to Object Events
  647. /// @{
  648. private:
  649. struct OtoListener {
  650. bool doomed;
  651. std::string objID;
  652. };
  653. std::vector<OtoListener> mListenerList;
  654. bool bIsEventRaised;
  655. public:
  656. void addListener(std::string objID);
  657. void removeListener(std::string objID);
  658. void removeAllListeners();
  659. void postEvent(std::string eventName, std::string data);
  660. /// @}
  661. public:
  662. virtual void dump();
  663. virtual void dumpClassHierarchy();
  664. static void initPersistFields();
  665. SimObject* clone( const bool copyDynamicFields );
  666. virtual void copyTo(SimObject* object);
  667. template<typename T> bool isType(void) { return dynamic_cast<T>(this) != NULL; }
  668. // Component Console Overrides
  669. virtual bool handlesConsoleMethod(const char * fname, S32 * routingId) { return false; }
  670. DECLARE_CONOBJECT(SimObject);
  671. };
  672. #endif // _SIM_OBJECT_H_