component.h

Engine/source/T3D/components/component.h

More...

Classes:

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef COMPONENT_H
 25#define COMPONENT_H
 26
 27#ifndef _NETOBJECT_H_
 28#include "sim/netObject.h"
 29#endif
 30#ifndef ENTITY_H
 31#include "T3D/entity.h"
 32#endif
 33#ifndef CORE_INTERFACES_H
 34#include "T3D/components/coreInterfaces.h"
 35#endif
 36
 37class Entity;
 38
 39struct ComponentField
 40{
 41   StringTableEntry mFieldName;
 42   StringTableEntry mFieldDescription;
 43
 44   S32 mFieldType;
 45   StringTableEntry mUserData;
 46
 47   StringTableEntry mDefaultValue;
 48
 49   StringTableEntry mGroup;
 50
 51   StringTableEntry mDependency;
 52
 53   bool mHidden;
 54};
 55
 56//////////////////////////////////////////////////////////////////////////
 57/// 
 58/// 
 59//////////////////////////////////////////////////////////////////////////
 60class Component : public NetObject, public UpdateInterface
 61{
 62   typedef NetObject Parent;
 63
 64protected:
 65   StringTableEntry mFriendlyName;
 66   StringTableEntry mDescription;
 67
 68   StringTableEntry mFromResource;
 69   StringTableEntry mComponentGroup;
 70   StringTableEntry mComponentType;
 71   StringTableEntry mNetworkType;
 72   StringTableEntry mTemplateName;
 73
 74   Vector<StringTableEntry> mDependencies;
 75   Vector<ComponentField> mFields;
 76
 77   bool mNetworked;
 78
 79   U32 componentIdx;
 80
 81   Entity*              mOwner;
 82   bool                 mHidden;
 83   bool                 mEnabled;
 84
 85public:
 86   Component();
 87   virtual ~Component();
 88   DECLARE_CONOBJECT(Component);
 89
 90   virtual bool onAdd();
 91   virtual void onRemove();
 92   static void initPersistFields();
 93
 94   virtual void packToStream(Stream &stream, U32 tabStop, S32 behaviorID, U32 flags = 0);
 95
 96   //This is called when we are added to an entity
 97   virtual void onComponentAdd();            
 98   //This is called when we are removed from an entity
 99   virtual void onComponentRemove();         
100
101   //This is called when a different component is added to our owner entity
102   virtual void componentAddedToOwner(Component *comp);  
103   //This is called when a different component is removed from our owner entity
104   virtual void componentRemovedFromOwner(Component *comp);  
105
106   virtual void ownerTransformSet(MatrixF *mat);
107
108   void setOwner(Entity* pOwner);
109   inline Entity *getOwner() { return mOwner ? mOwner : NULL; }
110   static bool setOwner(void *object, const char *index, const char *data) { return true; }
111
112   bool  isEnabled() { return mEnabled; }
113   void  setEnabled(bool toggle) { mEnabled = toggle; setMaskBits(EnableMask); }
114
115   bool isActive() { return mEnabled && mOwner != NULL; }
116
117   static bool _setEnabled(void *object, const char *index, const char *data);
118
119   virtual void processTick();
120   virtual void interpolateTick(F32 dt){}
121   virtual void advanceTime(F32 dt){}
122
123   /// @name Adding Named Fields
124   /// @{
125
126   /// Adds a named field to a Component that can specify a description, data type, default value and userData
127   ///
128   /// @param   fieldName    The name of the Field
129   /// @param   desc         The Description of the Field
130   /// @param   type         The Type of field that this is, example 'Text' or 'Bool'
131   /// @param   defaultValue The Default value of this field
132   /// @param   userData     An extra optional field that can be used for user data
133   void addComponentField(const char *fieldName, const char *desc, const char *type, const char *defaultValue = NULL, const char *userData = NULL, bool hidden = false);
134
135   /// Returns the number of ComponentField's on this template
136   inline S32 getComponentFieldCount() { return mFields.size(); };
137
138   /// Gets a ComponentField by its index in the mFields vector 
139   /// @param idx  The index of the field in the mField vector
140   inline ComponentField *getComponentField(S32 idx)
141   {
142      if (idx < 0 || idx >= mFields.size())
143         return NULL;
144
145      return &mFields[idx];
146   }
147
148   ComponentField *getComponentField(const char* fieldName);
149
150   const char* getComponentType() { return mComponentType; }
151
152   const char *getDescriptionText(const char *desc);
153
154   const char *getName() { return mTemplateName; }
155
156   const char *getFriendlyName() { return mFriendlyName; }
157
158   bool isNetworked() { return mNetworked; }
159
160   void beginFieldGroup(const char* groupName);
161   void endFieldGroup();
162
163   void addDependency(StringTableEntry name);
164   /// @}
165
166   /// @name Description
167   /// @{
168   static bool setDescription(void *object, const char *index, const char *data);
169   static const char* getDescription(void* obj, const char* data);
170
171   /// @Primary usage functions
172   /// @These are used by the various engine-based behaviors to integrate with the component classes
173   enum NetMaskBits
174   {
175      InitialUpdateMask = BIT(0),
176      OwnerMask = BIT(1),
177      UpdateMask = BIT(2),
178      EnableMask = BIT(3),
179      NextFreeMask = BIT(4)
180   };
181
182   virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
183   virtual void unpackUpdate(NetConnection *con, BitStream *stream);
184   /// @}
185
186   Signal< void(SimObject*, String, String) > onDataSet;
187   virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
188
189   virtual void onStaticModified(const char* slotName, const char* newValue); ///< Called when a static field is modified.
190   virtual void onDynamicModified(const char* slotName, const char*newValue = NULL); ///< Called when a dynamic field is modified.
191
192   /// 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
193   void checkComponentFieldModified(const char* slotName, const char* newValue);
194
195   virtual void checkDependencies(){}
196};
197
198#endif // COMPONENT_H
199