component.h 6.6 KB

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