component.h 7.4 KB

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