behaviorTemplate.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 _BEHAVIORTEMPLATE_H_
  23. #define _BEHAVIORTEMPLATE_H_
  24. #ifndef _SIMBASE_H_
  25. #include "sim/simBase.h"
  26. #endif
  27. #ifndef _BEHAVIORINSTANCE_H_
  28. #include "behaviorInstance.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. #define BEHAVIORTEMPLATE_ASSET_FIELDTYPE "Asset"
  32. //-----------------------------------------------------------------------------
  33. class BehaviorTemplate : public SimObject
  34. {
  35. typedef SimObject Parent;
  36. public:
  37. struct BehaviorField
  38. {
  39. BehaviorField( const char* name, const char* description, const char* type, const char* defaultValue, const char* userData )
  40. {
  41. mName = name ? StringTable->insert(name) : StringTable->EmptyString;;
  42. mDescription = description ? StringTable->insert(description) : StringTable->EmptyString;
  43. mType = type ? StringTable->insert(type) : StringTable->EmptyString;
  44. mDefaultValue = defaultValue ? StringTable->insert(defaultValue) : StringTable->EmptyString;
  45. mUserData = userData ? StringTable->insert(userData) : StringTable->EmptyString;
  46. }
  47. StringTableEntry mName;
  48. StringTableEntry mDescription;
  49. StringTableEntry mType;
  50. StringTableEntry mUserData;
  51. StringTableEntry mDefaultValue;
  52. };
  53. /// Behavior port common functionality.
  54. struct BehaviorPort
  55. {
  56. BehaviorPort( const char* name, const char* label, const char* description )
  57. {
  58. mName = name ? StringTable->insert(name) : StringTable->EmptyString;
  59. mLabel = label ? StringTable->insert(label) : StringTable->EmptyString;
  60. mDescription = description ? StringTable->insert(description) : StringTable->EmptyString;
  61. }
  62. StringTableEntry mName;
  63. StringTableEntry mLabel;
  64. StringTableEntry mDescription;
  65. };
  66. /// A behavior port that accepts input.
  67. struct BehaviorPortInput : public BehaviorPort
  68. {
  69. BehaviorPortInput( const char* name, const char* label, const char* description ) :
  70. BehaviorPort( name, label, description )
  71. {
  72. }
  73. };
  74. /// A behavior port that raises an output.
  75. struct BehaviorPortOutput : public BehaviorPort
  76. {
  77. BehaviorPortOutput( const char* name, const char* label, const char* description ) :
  78. BehaviorPort( name, label, description )
  79. {
  80. }
  81. };
  82. public:
  83. BehaviorTemplate();
  84. virtual ~BehaviorTemplate() {}
  85. virtual bool onAdd();
  86. virtual void onRemove();
  87. static void initPersistFields();
  88. /// Create a BehaviorInstance from this template
  89. BehaviorInstance* createInstance( void );
  90. /// Template.
  91. inline StringTableEntry getFriendlyName( void ) const { return mFriendlyName; }
  92. inline StringTableEntry getDescription( void ) const { return mDescription; }
  93. inline StringTableEntry getBehaviorType( void ) const { return mBehaviorType; }
  94. /// Fields.
  95. bool addBehaviorField( const char* fieldName, const char* description, const char* type, const char* defaultValue = NULL, const char* userData = NULL );
  96. inline U32 getBehaviorFieldCount( void ) const { return mFields.size(); };
  97. inline BehaviorField* getBehaviorField( const U32 index ) { return index < (U32)mFields.size() ? &mFields[index] : NULL; }
  98. inline BehaviorField* getBehaviorField( const char* fieldName )
  99. {
  100. StringTableEntry name = StringTable->insert( fieldName );
  101. for( Vector<BehaviorField>::iterator itr = mFields.begin(); itr != mFields.end(); ++itr )
  102. {
  103. // Check if found.
  104. if ( name == itr->mName )
  105. return itr;
  106. }
  107. return NULL;
  108. }
  109. inline bool hasBehaviorField( const char* fieldName )
  110. {
  111. StringTableEntry name = StringTable->insert( fieldName );
  112. for( Vector<BehaviorField>::iterator itr = mFields.begin(); itr != mFields.end(); ++itr )
  113. {
  114. // Check if found.
  115. if ( name == itr->mName )
  116. return true;
  117. }
  118. return false;
  119. }
  120. /// Outputs.
  121. bool addBehaviorOutput( const char* portName, const char* label, const char* description );
  122. inline U32 getBehaviorOutputCount( void ) const { return mPortOutputs.size(); }
  123. inline BehaviorPortOutput* getBehaviourOutput( const U32 index ) { return index < (U32)mPortOutputs.size() ? &mPortOutputs[index] : NULL; }
  124. inline bool hasBehaviorOutput( const char* portName )
  125. {
  126. StringTableEntry name = StringTable->insert( portName );
  127. for( Vector<BehaviorPortOutput>::iterator itr = mPortOutputs.begin(); itr != mPortOutputs.end(); ++itr )
  128. {
  129. // Check if found.
  130. if ( name == itr->mName )
  131. return true;
  132. }
  133. return false;
  134. }
  135. /// Inputs.
  136. bool addBehaviorInput( const char* portName, const char* label, const char* description );
  137. inline U32 getBehaviorInputCount( void ) const { return mPortInputs.size(); }
  138. inline BehaviorPortInput* getBehaviourInput( const U32 index ) { return index < (U32)mPortInputs.size() ? &mPortInputs[index] : NULL; }
  139. inline bool hasBehaviorInput( const char* portName )
  140. {
  141. StringTableEntry name = StringTable->insert( portName );
  142. for( Vector<BehaviorPortInput>::iterator itr = mPortInputs.begin(); itr != mPortInputs.end(); ++itr )
  143. {
  144. // Check if found.
  145. if ( name == itr->mName )
  146. return true;
  147. }
  148. return false;
  149. }
  150. DECLARE_CONOBJECT(BehaviorTemplate);
  151. protected:
  152. StringTableEntry mFriendlyName;
  153. StringTableEntry mDescription;
  154. StringTableEntry mBehaviorType;
  155. Vector<BehaviorField> mFields;
  156. Vector<BehaviorPortInput> mPortInputs;
  157. Vector<BehaviorPortOutput> mPortOutputs;
  158. static bool setDescription(void* obj, const char* data) { static_cast<BehaviorTemplate *>(obj)->mDescription = data ? StringTable->insert(data) : StringTable->EmptyString; return false; }
  159. static const char* getDescription(void* obj, const char* data) { return static_cast<BehaviorTemplate *>(obj)->mDescription; }
  160. };
  161. #endif // _BEHAVIORTEMPLATE_H_