ShapeAsset.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 SHAPE_ASSET_H
  23. #define SHAPE_ASSET_H
  24. #ifndef _ASSET_BASE_H_
  25. #include "assets/assetBase.h"
  26. #endif
  27. #ifndef _ASSET_DEFINITION_H_
  28. #include "assets/assetDefinition.h"
  29. #endif
  30. #ifndef _STRINGUNIT_H_
  31. #include "string/stringUnit.h"
  32. #endif
  33. #ifndef _ASSET_FIELD_TYPES_H_
  34. #include "assets/assetFieldTypes.h"
  35. #endif
  36. #ifndef _TSSHAPE_H_
  37. #include "ts/tsShape.h"
  38. #endif
  39. #ifndef __RESOURCE_H__
  40. #include "core/resource.h"
  41. #endif
  42. #ifndef _ASSET_PTR_H_
  43. #include "assets/assetPtr.h"
  44. #endif
  45. #ifndef MATERIALASSET_H
  46. #include "MaterialAsset.h"
  47. #endif
  48. #ifndef SHAPE_ANIMATION_ASSET_H
  49. #include "ShapeAnimationAsset.h"
  50. #endif
  51. #ifdef TORQUE_TOOLS
  52. #include "gui/editor/guiInspectorTypes.h"
  53. #endif
  54. //-----------------------------------------------------------------------------
  55. class ShapeAsset : public AssetBase
  56. {
  57. typedef AssetBase Parent;
  58. protected:
  59. StringTableEntry mFileName;
  60. StringTableEntry mConstructorFileName;
  61. StringTableEntry mFilePath;
  62. StringTableEntry mConstructorFilePath;
  63. Resource<TSShape> mShape;
  64. //Material assets we're dependent on and use
  65. Vector<StringTableEntry> mMaterialAssetIds;
  66. Vector<AssetPtr<MaterialAsset>> mMaterialAssets;
  67. //Animation assets we're dependent on and use
  68. Vector<StringTableEntry> mAnimationAssetIds;
  69. Vector<AssetPtr<ShapeAnimationAsset>> mAnimationAssets;
  70. typedef Signal<void()> ShapeAssetChanged;
  71. ShapeAssetChanged mChangeSignal;
  72. public:
  73. enum ShapeAssetErrCode
  74. {
  75. TooManyVerts = AssetErrCode::Extended,
  76. TooManyBones,
  77. MissingAnimatons,
  78. Extended
  79. };
  80. static const String mErrCodeStrings[ShapeAssetErrCode::Extended - Parent::Extended + 1];
  81. static String getAssetErrstrn(U32 errCode)
  82. {
  83. if (errCode < Parent::Extended) return Parent::getAssetErrstrn(errCode);
  84. if (errCode > ShapeAssetErrCode::Extended) return "undefined error";
  85. return mErrCodeStrings[errCode];
  86. };
  87. ShapeAsset();
  88. virtual ~ShapeAsset();
  89. /// Engine.
  90. static void initPersistFields();
  91. virtual void copyTo(SimObject* object);
  92. virtual void setDataField(StringTableEntry slotName, const char *array, const char *value);
  93. virtual void initializeAsset();
  94. /// Declare Console Object.
  95. DECLARE_CONOBJECT(ShapeAsset);
  96. bool loadShape();
  97. U32 mLoadedState;
  98. TSShape* getShape() { return mShape; }
  99. Resource<TSShape> getShapeResource() { return mShape; }
  100. void SplitSequencePathAndName(String& srcPath, String& srcName);
  101. StringTableEntry getShapeFilename() { return mFilePath; }
  102. U32 getShapeFilenameHash() { return _StringTable::hashString(mFilePath); }
  103. Vector<AssetPtr<MaterialAsset>> getMaterialAssets() { return mMaterialAssets; }
  104. inline AssetPtr<MaterialAsset> getMaterialAsset(U32 matId)
  105. {
  106. if(matId >= mMaterialAssets.size())
  107. return nullptr;
  108. else
  109. return mMaterialAssets[matId];
  110. }
  111. void clearMaterialAssets() { mMaterialAssets.clear(); }
  112. void addMaterialAssets(AssetPtr<MaterialAsset> matPtr) { mMaterialAssets.push_back(matPtr); }
  113. S32 getMaterialCount() { return mMaterialAssets.size(); }
  114. S32 getAnimationCount() { return mAnimationAssets.size(); }
  115. ShapeAnimationAsset* getAnimation(S32 index);
  116. void _onResourceChanged(const Torque::Path &path);
  117. ShapeAssetChanged& getChangedSignal() { return mChangeSignal; }
  118. void setShapeFile(const char* pScriptFile);
  119. inline StringTableEntry getShapeFile(void) const { return mFileName; };
  120. void setShapeConstructorFile(const char* pScriptFile);
  121. inline StringTableEntry getShapeConstructorFile(void) const { return mConstructorFileName; };
  122. inline StringTableEntry getShapeFilePath(void) const { return mFilePath; };
  123. inline StringTableEntry getShapeConstructorFilePath(void) const { return mConstructorFilePath; };
  124. static bool getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset);
  125. static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
  126. static U32 getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset);
  127. static StringTableEntry getNoShapeAssetId() { return StringTable->insert("Core_Rendering:noshape"); }
  128. protected:
  129. virtual void onAssetRefresh(void);
  130. static bool setShapeFile(void *obj, const char *index, const char *data) { static_cast<ShapeAsset*>(obj)->setShapeFile(data); return false; }
  131. static const char* getShapeFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeFile(); }
  132. static bool setShapeConstructorFile(void* obj, const char* index, const char* data) { static_cast<ShapeAsset*>(obj)->setShapeConstructorFile(data); return false; }
  133. static const char* getShapeConstructorFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeConstructorFile(); }
  134. };
  135. #ifdef TORQUE_TOOLS
  136. DefineConsoleType(TypeShapeAssetPtr, S32)
  137. DefineConsoleType(TypeShapeAssetId, String)
  138. //-----------------------------------------------------------------------------
  139. // TypeAssetId GuiInspectorField Class
  140. //-----------------------------------------------------------------------------
  141. class GuiInspectorTypeShapeAssetPtr : public GuiInspectorTypeFileName
  142. {
  143. typedef GuiInspectorTypeFileName Parent;
  144. public:
  145. GuiBitmapButtonCtrl *mShapeEdButton;
  146. DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  147. static void consoleInit();
  148. virtual GuiControl* constructEditControl();
  149. virtual bool updateRects();
  150. };
  151. class GuiInspectorTypeShapeAssetId : public GuiInspectorTypeShapeAssetPtr
  152. {
  153. typedef GuiInspectorTypeShapeAssetPtr Parent;
  154. public:
  155. DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetId);
  156. static void consoleInit();
  157. };
  158. #endif
  159. #define assetText(x,suff) std::string(std::string(#x) + std::string(#suff)).c_str()
  160. #define initShapeAsset(name) m##name##Filename = StringTable->EmptyString(); m##name##AssetId = StringTable->EmptyString(); m##name##Asset = NULL;
  161. #define bindShapeAsset(name) if (m##name##AssetId != StringTable->EmptyString()) m##name##Asset = m##name##AssetId;
  162. #define scriptBindShapeAsset(name, consoleClass, docs) addProtectedField(assetText(name, File), TypeShapeFilename, Offset(m##name##Filename, consoleClass), consoleClass::_set##name##Filename, & defaultProtectedGetFn, assetText(name, docs)); \
  163. addProtectedField(assetText(name, Asset), TypeShapeAssetId, Offset(m##name##AssetId, consoleClass), consoleClass::_set##name##Asset, & defaultProtectedGetFn, assetText(name, asset reference.));
  164. #define DECLARE_SHAPEASSET(className,name) protected: \
  165. StringTableEntry m##name##Filename;\
  166. StringTableEntry m##name##AssetId;\
  167. AssetPtr<ShapeAsset> m##name##Asset;\
  168. public: \
  169. const StringTableEntry& get##name() const { return m##name##Filename; }\
  170. void set##name(FileName _in) { m##name##Filename = _in; }\
  171. const AssetPtr<ShapeAsset> & get##name##Asset() const { return m##name##Asset; }\
  172. void set##name##Asset(AssetPtr<ShapeAsset>_in) { m##name##Asset = _in; }\
  173. static bool _set##name##Filename(void* obj, const char* index, const char* data)\
  174. {\
  175. className* shape = static_cast<className*>(obj);\
  176. \
  177. StringTableEntry assetId = ShapeAsset::getAssetIdByFilename(StringTable->insert(data));\
  178. if (assetId != StringTable->EmptyString())\
  179. {\
  180. if (shape->_set##name##Asset(obj, index, assetId))\
  181. {\
  182. if (assetId == StringTable->insert("Core_Rendering:noShape"))\
  183. {\
  184. shape->m##name##Filename = data;\
  185. shape->m##name##AssetId = StringTable->EmptyString();\
  186. \
  187. return true;\
  188. }\
  189. else\
  190. {\
  191. shape->m##name##AssetId = assetId;\
  192. shape->m##name##Filename = StringTable->EmptyString();\
  193. \
  194. return false;\
  195. }\
  196. }\
  197. }\
  198. else\
  199. {\
  200. shape->m##name##Asset = StringTable->EmptyString();\
  201. }\
  202. \
  203. return true;\
  204. }\
  205. \
  206. static bool _set##name##Asset(void* obj, const char* index, const char* data)\
  207. {\
  208. className* shape = static_cast<className*>(obj);\
  209. shape->m##name##AssetId = StringTable->insert(data);\
  210. if (ShapeAsset::getAssetById(shape->m##name##AssetId, &shape->m##name##Asset))\
  211. {\
  212. if (shape->m##name##Asset.getAssetId() != StringTable->insert("Core_Rendering:noShape"))\
  213. shape->m##name##Filename = StringTable->EmptyString();\
  214. \
  215. shape->setMaskBits(-1);\
  216. return true;\
  217. }\
  218. return false;\
  219. }
  220. #endif