behaviorTemplate.h 7.4 KB

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