component.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef COMPONENT_H
  23. #define COMPONENT_H
  24. #ifndef _NETOBJECT_H_
  25. #include "sim/netObject.h"
  26. #endif
  27. #ifndef ENTITY_H
  28. #include "T3D/entity.h"
  29. #endif
  30. #ifndef CORE_INTERFACES_H
  31. #include "T3D/components/coreInterfaces.h"
  32. #endif
  33. #ifndef _ASSET_PTR_H_
  34. #include "assets/assetPtr.h"
  35. #endif
  36. #ifndef COMPONENT_ASSET_H
  37. #include "T3D/assets/ComponentAsset.h"
  38. #endif
  39. class Entity;
  40. class Namespace;
  41. struct ComponentField
  42. {
  43. StringTableEntry mFieldName;
  44. StringTableEntry mFieldDescription;
  45. StringTableEntry mFieldTypeName;
  46. S32 mFieldType;
  47. StringTableEntry mUserData;
  48. StringTableEntry mDefaultValue;
  49. StringTableEntry mGroup;
  50. StringTableEntry mDependency;
  51. bool mHidden;
  52. };
  53. //////////////////////////////////////////////////////////////////////////
  54. ///
  55. ///
  56. //////////////////////////////////////////////////////////////////////////
  57. class Component : public NetObject, public UpdateInterface
  58. {
  59. typedef NetObject Parent;
  60. protected:
  61. StringTableEntry mFriendlyName;
  62. StringTableEntry mDescription;
  63. StringTableEntry mFromResource;
  64. StringTableEntry mComponentGroup;
  65. StringTableEntry mComponentType;
  66. StringTableEntry mNetworkType;
  67. StringTableEntry mTemplateName;
  68. Vector<StringTableEntry> mDependencies;
  69. Vector<ComponentField> mFields;
  70. bool mNetworked;
  71. U32 componentIdx;
  72. Entity* mOwner;
  73. bool mHidden;
  74. bool mEnabled;
  75. StringTableEntry mOriginatingAssetId;
  76. AssetPtr<ComponentAsset> mOriginatingAsset;
  77. public:
  78. Component();
  79. virtual ~Component();
  80. DECLARE_CONOBJECT(Component);
  81. virtual bool onAdd();
  82. virtual void onRemove();
  83. static void initPersistFields();
  84. virtual void packToStream(Stream &stream, U32 tabStop, S32 behaviorID, U32 flags = 0);
  85. //This is called when we are added to an entity
  86. virtual void onComponentAdd();
  87. //This is called when we are removed from an entity
  88. virtual void onComponentRemove();
  89. //This is called when a different component is added to our owner entity
  90. virtual void componentAddedToOwner(Component *comp);
  91. //This is called when a different component is removed from our owner entity
  92. virtual void componentRemovedFromOwner(Component *comp);
  93. virtual void ownerTransformSet(MatrixF *mat);
  94. void setOwner(Entity* pOwner);
  95. inline Entity *getOwner() { return mOwner ? mOwner : NULL; }
  96. static bool setOwner(void *object, const char *index, const char *data) { return true; }
  97. bool isEnabled() { return mEnabled; }
  98. void setEnabled(bool toggle) { mEnabled = toggle; setMaskBits(EnableMask); }
  99. bool isActive() { return mEnabled && mOwner != NULL; }
  100. static bool _setEnabled(void *object, const char *index, const char *data);
  101. virtual void processTick();
  102. virtual void interpolateTick(F32 dt){}
  103. virtual void advanceTime(F32 dt){}
  104. /// @name Adding Named Fields
  105. /// @{
  106. /// Adds a named field to a Component that can specify a description, data type, default value and userData
  107. ///
  108. /// @param fieldName The name of the Field
  109. /// @param desc The Description of the Field
  110. /// @param type The Type of field that this is, example 'Text' or 'Bool'
  111. /// @param defaultValue The Default value of this field
  112. /// @param userData An extra optional field that can be used for user data
  113. void addComponentField(const char *fieldName, const char *desc, const char *type, const char *defaultValue = NULL, const char *userData = NULL, bool hidden = false);
  114. /// Returns the number of ComponentField's on this template
  115. inline S32 getComponentFieldCount() { return mFields.size(); };
  116. /// Gets a ComponentField by its index in the mFields vector
  117. /// @param idx The index of the field in the mField vector
  118. inline ComponentField *getComponentField(S32 idx)
  119. {
  120. if (idx < 0 || idx >= mFields.size())
  121. return NULL;
  122. return &mFields[idx];
  123. }
  124. ComponentField *getComponentField(const char* fieldName);
  125. const char* getComponentType() { return mComponentType; }
  126. const char *getDescriptionText(const char *desc);
  127. const char *getName() { return mTemplateName; }
  128. const char *getFriendlyName() { return mFriendlyName; }
  129. bool isNetworked() { return mNetworked; }
  130. void beginFieldGroup(const char* groupName);
  131. void endFieldGroup();
  132. void addDependency(StringTableEntry name);
  133. /// @}
  134. /// @name Description
  135. /// @{
  136. static bool setDescription(void *object, const char *index, const char *data);
  137. static const char* getDescription(void* obj, const char* data);
  138. /// @Primary usage functions
  139. /// @These are used by the various engine-based behaviors to integrate with the component classes
  140. enum NetMaskBits
  141. {
  142. InitialUpdateMask = BIT(0),
  143. OwnerMask = BIT(1),
  144. UpdateMask = BIT(2),
  145. EnableMask = BIT(3),
  146. NamespaceMask = BIT(4),
  147. NextFreeMask = BIT(5)
  148. };
  149. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  150. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  151. /// @}
  152. Signal< void(SimObject*, String, String) > onDataSet;
  153. virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
  154. virtual void onStaticModified(const char* slotName, const char* newValue); ///< Called when a static field is modified.
  155. virtual void onDynamicModified(const char* slotName, const char*newValue = NULL); ///< Called when a dynamic field is modified.
  156. /// This is what we actually use to check if the modified field is one of our behavior fields. If it is, we update and make the correct callbacks
  157. void checkComponentFieldModified(const char* slotName, const char* newValue);
  158. virtual void checkDependencies(){}
  159. StringTableEntry getComponentName();
  160. };
  161. #endif // COMPONENT_H