ShapeAsset.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. #ifndef _BITSTREAM_H_
  55. #include "core/stream/bitStream.h"
  56. #endif
  57. #include "assetMacroHelpers.h"
  58. //-----------------------------------------------------------------------------
  59. class ShapeAsset : public AssetBase
  60. {
  61. typedef AssetBase Parent;
  62. typedef AssetPtr<ShapeAsset> ConcreteAssetPtr;
  63. public:
  64. enum ShapeAssetErrCode
  65. {
  66. TooManyVerts = AssetErrCode::Extended,
  67. TooManyBones,
  68. MissingAnimatons,
  69. Extended
  70. };
  71. static StringTableEntry smNoShapeAssetFallback;
  72. static const String mErrCodeStrings[U32(ShapeAssetErrCode::Extended) - U32(Parent::Extended) + 1];
  73. static U32 getAssetErrCode(ConcreteAssetPtr checkAsset) { if (checkAsset) return checkAsset->mLoadedState; else return 0; }
  74. static String getAssetErrstrn(U32 errCode)
  75. {
  76. if (errCode < Parent::Extended) return Parent::getAssetErrstrn(errCode);
  77. if (errCode > ShapeAssetErrCode::Extended) return "undefined error";
  78. return mErrCodeStrings[errCode - Parent::Extended];
  79. };
  80. private:
  81. StringTableEntry mShapeFile;
  82. StringTableEntry mConstructorFileName;
  83. StringTableEntry mDiffuseImposterFileName;
  84. StringTableEntry mNormalImposterFileName;
  85. //Material assets we're dependent on and use
  86. Vector<StringTableEntry> mMaterialAssetIds;
  87. Vector<AssetPtr<MaterialAsset>> mMaterialAssets;
  88. //Animation assets we're dependent on and use
  89. Vector<StringTableEntry> mAnimationAssetIds;
  90. Vector<AssetPtr<ShapeAnimationAsset>> mAnimationAssets;
  91. Resource<TSShape> mShape;
  92. public:
  93. ShapeAsset();
  94. virtual ~ShapeAsset();
  95. /// Set up some global script interface stuff.
  96. static void consoleInit();
  97. /// Engine.
  98. static void initPersistFields();
  99. void copyTo(SimObject* object) override;
  100. virtual void setDataField(StringTableEntry slotName, StringTableEntry array, StringTableEntry value);
  101. /// Declare Console Object.
  102. DECLARE_CONOBJECT(ShapeAsset);
  103. U32 load() override;
  104. TSShape* getShape() { load(); return mShape; }
  105. Resource<TSShape> getShapeResource() { load(); return mShape; }
  106. void SplitSequencePathAndName(String& srcPath, String& srcName);
  107. U32 getShapeFilenameHash() { return _StringTable::hashString(mShapeFile); }
  108. Vector<AssetPtr<MaterialAsset>> getMaterialAssets() { return mMaterialAssets; }
  109. inline AssetPtr<MaterialAsset> getMaterialAsset(U32 matId)
  110. {
  111. if (matId >= mMaterialAssets.size())
  112. return nullptr;
  113. else
  114. return mMaterialAssets[matId];
  115. }
  116. void clearMaterialAssets() { mMaterialAssets.clear(); }
  117. void addMaterialAssets(AssetPtr<MaterialAsset> matPtr) { mMaterialAssets.push_back(matPtr); }
  118. S32 getMaterialCount() { return mMaterialAssets.size(); }
  119. S32 getAnimationCount() { return mAnimationAssets.size(); }
  120. ShapeAnimationAsset* getAnimation(S32 index);
  121. void _onResourceChanged(const Torque::Path& path);
  122. void setShapeFile(const char* pScriptFile);
  123. inline StringTableEntry getShapeFile(void) const { return mShapeFile; };
  124. void setShapeConstructorFile(const char* pScriptFile);
  125. inline StringTableEntry getShapeConstructorFile(void) const { return mConstructorFileName; };
  126. //Imposter images
  127. void setDiffuseImposterFile(const char* pImageFile);
  128. inline StringTableEntry getDiffuseImposterFile(void) const { return mDiffuseImposterFileName; };
  129. void setNormalImposterFile(const char* pImageFile);
  130. inline StringTableEntry getNormalImposterFile(void) const { return mNormalImposterFileName; };
  131. static U32 getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset);
  132. static StringTableEntry getAssetIdByFilename(StringTableEntry fileName);
  133. static U32 getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset);
  134. #ifdef TORQUE_TOOLS
  135. const char* generateCachedPreviewImage(S32 resolution, String overrideMaterial = "");
  136. #endif
  137. protected:
  138. // Asset Base callback
  139. void initializeAsset(void) override;
  140. void onAssetRefresh(void) override;
  141. /// Taml callbacks.
  142. void onTamlPreWrite(void) override;
  143. void onTamlPostWrite(void) override;
  144. protected:
  145. static bool setShapeFile(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ShapeAsset*>(obj)->setShapeFile(data); return false; }
  146. static const char* getShapeFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeFile(); }
  147. static bool writeShapeFile(void* obj, StringTableEntry pFieldName) { return static_cast<ShapeAsset*>(obj)->getShapeFile() != StringTable->EmptyString(); }
  148. static bool setShapeConstructorFile(void* obj, const char* index, const char* data) { static_cast<ShapeAsset*>(obj)->setShapeConstructorFile(data); return false; }
  149. static const char* getShapeConstructorFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getShapeConstructorFile(); }
  150. static bool setDiffuseImposterFile(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ShapeAsset*>(obj)->setDiffuseImposterFile(data); return false; }
  151. static const char* getDiffuseImposterFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getDiffuseImposterFile(); }
  152. static bool setNormalImposterFile(void* obj, StringTableEntry index, StringTableEntry data) { static_cast<ShapeAsset*>(obj)->setNormalImposterFile(data); return false; }
  153. static const char* getNormalImposterFile(void* obj, const char* data) { return static_cast<ShapeAsset*>(obj)->getNormalImposterFile(); }
  154. };
  155. DefineConsoleType(TypeShapeAssetId, String)
  156. DECLARE_STRUCT(AssetPtr<ShapeAsset>)
  157. DefineConsoleType(TypeShapeAssetPtr, AssetPtr<ShapeAsset>)
  158. #ifdef TORQUE_TOOLS
  159. //-----------------------------------------------------------------------------
  160. // TypeAssetId GuiInspectorField Class
  161. //-----------------------------------------------------------------------------
  162. class GuiInspectorTypeShapeAssetPtr : public GuiInspectorTypeFileName
  163. {
  164. typedef GuiInspectorTypeFileName Parent;
  165. public:
  166. GuiTextCtrl* mLabel;
  167. GuiBitmapButtonCtrl* mPreviewBorderButton;
  168. GuiBitmapCtrl* mPreviewImage;
  169. GuiButtonCtrl* mEditButton;
  170. DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  171. static void consoleInit();
  172. GuiControl* constructEditControl() override;
  173. bool updateRects() override;
  174. void updateValue() override;
  175. void updatePreviewImage();
  176. void setPreviewImage(StringTableEntry assetId);
  177. };
  178. class GuiInspectorTypeShapeAssetId : public GuiInspectorTypeShapeAssetPtr
  179. {
  180. typedef GuiInspectorTypeShapeAssetPtr Parent;
  181. public:
  182. DECLARE_CONOBJECT(GuiInspectorTypeShapeAssetId);
  183. static void consoleInit();
  184. };
  185. #endif
  186. //-----------------------------------------------------------------------------
  187. // REFACTOR
  188. //-----------------------------------------------------------------------------
  189. #pragma region Refactor Asset Macros
  190. #define DECLARE_SHAPEASSET_REFACTOR(className, name) \
  191. private: \
  192. AssetPtr<ShapeAsset> m##name##Asset; \
  193. StringTableEntry m##name##File = StringTable->EmptyString(); \
  194. public: \
  195. void _set##name(StringTableEntry _in) { \
  196. if (m##name##Asset.getAssetId() == _in) \
  197. return; \
  198. if(get##name##File() == _in) \
  199. return; \
  200. if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
  201. { \
  202. m##name##Asset = NULL; \
  203. m##name##File = ""; \
  204. return; \
  205. } \
  206. if (!AssetDatabase.isDeclaredAsset(_in)) \
  207. { \
  208. StringTableEntry shapeAssetId = StringTable->EmptyString(); \
  209. AssetQuery query; \
  210. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
  211. if (foundAssetcount != 0) \
  212. { \
  213. shapeAssetId = query.mAssetList[0]; \
  214. } \
  215. else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
  216. { \
  217. shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
  218. if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
  219. { \
  220. ShapeAsset* privateShape = new ShapeAsset(); \
  221. privateShape->setShapeFile(_in); \
  222. shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
  223. } \
  224. } \
  225. else \
  226. { \
  227. Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
  228. shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
  229. } \
  230. m##name##Asset = shapeAssetId; \
  231. m##name##File = _in; \
  232. } \
  233. else \
  234. { \
  235. m##name##Asset = _in; \
  236. m##name##File = get##name##File(); \
  237. } \
  238. }; \
  239. \
  240. inline StringTableEntry _get##name##AssetId(void) const { return m##name##Asset.getAssetId(); } \
  241. TSShape* get##name() { if (m##name##Asset.notNull()) return m##name##Asset->getShape(); else return NULL; } \
  242. AssetPtr<ShapeAsset> get##name##Asset(void) { return m##name##Asset; } \
  243. static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false; } \
  244. StringTableEntry get##name##File() { return m##name##Asset.notNull() ? m##name##Asset->getShapeFile() : ""; }
  245. #define DECLARE_SHAPEASSET_NET_REFACTOR(className, name, mask) \
  246. private: \
  247. AssetPtr<ShapeAsset> m##name##Asset; \
  248. StringTableEntry m##name##File = StringTable->EmptyString(); \
  249. public: \
  250. void _set##name(StringTableEntry _in) { \
  251. if (m##name##Asset.getAssetId() == _in) \
  252. return; \
  253. if(get##name##File() == _in) \
  254. return; \
  255. if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
  256. { \
  257. m##name##Asset = NULL; \
  258. m##name##File = ""; \
  259. setMaskBits(mask); \
  260. return; \
  261. } \
  262. if (!AssetDatabase.isDeclaredAsset(_in)) \
  263. { \
  264. StringTableEntry shapeAssetId = StringTable->EmptyString(); \
  265. AssetQuery query; \
  266. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
  267. if (foundAssetcount != 0) \
  268. { \
  269. shapeAssetId = query.mAssetList[0]; \
  270. } \
  271. else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
  272. { \
  273. shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
  274. if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
  275. { \
  276. ShapeAsset* privateShape = new ShapeAsset(); \
  277. privateShape->setShapeFile(_in); \
  278. shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
  279. } \
  280. } \
  281. else \
  282. { \
  283. Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
  284. shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
  285. } \
  286. m##name##Asset = shapeAssetId; \
  287. m##name##File = _in; \
  288. } \
  289. else \
  290. { \
  291. m##name##Asset = _in; \
  292. m##name##File = get##name##File(); \
  293. } \
  294. setMaskBits(mask); \
  295. }; \
  296. \
  297. inline StringTableEntry _get##name##AssetId(void) const { return m##name##Asset.getAssetId(); } \
  298. TSShape* get##name() { if (m##name##Asset.notNull()) return m##name##Asset->getShape(); else return NULL; } \
  299. AssetPtr<ShapeAsset> get##name##Asset(void) { return m##name##Asset; } \
  300. static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data)); return false; } \
  301. StringTableEntry get##name##File() { return m##name##Asset.notNull() ? m##name##Asset->getShapeFile() : ""; }
  302. #define INITPERSISTFIELD_SHAPEASSET_REFACTOR(name, consoleClass, docs) \
  303. addProtectedField(assetText(name, Asset), TypeShapeAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, asset docs.)); \
  304. addProtectedField(assetText(name, File), TypeFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, assetDoc(name, file docs.), AbstractClassRep::FIELD_HideInInspectors);
  305. #define DECLARE_SHAPEASSET_ARRAY_REFACTOR(className, name, max) \
  306. private: \
  307. AssetPtr<ShapeAsset> m##name##Asset[max]; \
  308. StringTableEntry m##name##File[max] = {StringTable->EmptyString() }; \
  309. public: \
  310. void _set##name(StringTableEntry _in, const U32& index){ \
  311. if (m##name##Asset[index].getAssetId() == _in) \
  312. return; \
  313. if(get##name##File(index) == _in) \
  314. return; \
  315. if(_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
  316. { \
  317. m##name##Asset[index] = NULL; \
  318. m##name##File[index] = ""; \
  319. return; \
  320. } \
  321. if (!AssetDatabase.isDeclaredAsset(_in)) \
  322. { \
  323. StringTableEntry shapeAssetId = StringTable->EmptyString(); \
  324. AssetQuery query; \
  325. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
  326. if (foundAssetcount != 0) \
  327. { \
  328. shapeAssetId = query.mAssetList[0]; \
  329. } \
  330. else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
  331. { \
  332. shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
  333. if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
  334. { \
  335. ShapeAsset* privateShape = new ShapeAsset(); \
  336. privateShape->setShapeFile(_in); \
  337. shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
  338. } \
  339. } \
  340. else \
  341. { \
  342. Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
  343. shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
  344. } \
  345. m##name##Asset[index] = shapeAssetId; \
  346. m##name##File[index] = _in; \
  347. } \
  348. else \
  349. { \
  350. m##name##Asset[index] = _in; \
  351. m##name##File[index] = get##name##File(index); \
  352. } \
  353. }; \
  354. \
  355. inline StringTableEntry _get##name##AssetId(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
  356. TSShape* get##name(const U32& index) { if (m##name##Asset[index].notNull()) return m##name##Asset[index]->getShape(); else return NULL; } \
  357. AssetPtr<ShapeAsset> get##name##Asset(const U32& index) { return m##name##Asset[index]; } \
  358. static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data), dAtoi(index)); return false;}\
  359. StringTableEntry get##name##File(const U32& idx) { return m##name##Asset[idx].notNull() ? m##name##Asset[idx]->getShapeFile() : ""; }
  360. #define DECLARE_SHAPEASSET_ARRAY_NET_REFACTOR(className, name, max, mask) \
  361. private: \
  362. AssetPtr<ShapeAsset> m##name##Asset[max]; \
  363. StringTableEntry m##name##File[max] = {StringTable->EmptyString() }; \
  364. public: \
  365. void _set##name(StringTableEntry _in, const U32& index){ \
  366. if (m##name##Asset[index].getAssetId() == _in) \
  367. return; \
  368. if(get##name##File(index) == _in) \
  369. return; \
  370. if (_in == NULL || !String::compare(_in,StringTable->EmptyString())) \
  371. { \
  372. m##name##Asset[index] = NULL; \
  373. m##name##File[index] = ""; \
  374. setMaskBits(mask); \
  375. return; \
  376. } \
  377. if (!AssetDatabase.isDeclaredAsset(_in)) \
  378. { \
  379. StringTableEntry shapeAssetId = StringTable->EmptyString(); \
  380. AssetQuery query; \
  381. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, _in); \
  382. if (foundAssetcount != 0) \
  383. { \
  384. shapeAssetId = query.mAssetList[0]; \
  385. } \
  386. else if (Torque::FS::IsFile(_in) || (_in[0] == '$' || _in[0] == '#')) \
  387. { \
  388. shapeAssetId = ShapeAsset::getAssetIdByFilename(_in); \
  389. if (shapeAssetId == ShapeAsset::smNoShapeAssetFallback) \
  390. { \
  391. ShapeAsset* privateShape = new ShapeAsset(); \
  392. privateShape->setShapeFile(_in); \
  393. shapeAssetId = AssetDatabase.addPrivateAsset(privateShape); \
  394. } \
  395. } \
  396. else \
  397. { \
  398. Con::warnf("%s::%s: Could not find asset for: %s using fallback", #className, #name, _in); \
  399. shapeAssetId = ShapeAsset::smNoShapeAssetFallback; \
  400. } \
  401. m##name##Asset[index] = shapeAssetId; \
  402. m##name##File[index] = _in; \
  403. } \
  404. else \
  405. { \
  406. m##name##Asset[index] = _in; \
  407. m##name##File[index] = get##name##File(index); \
  408. } \
  409. setMaskBits(mask); \
  410. }; \
  411. \
  412. inline StringTableEntry _get##name##AssetId(const U32& index) const { return m##name##Asset[index].getAssetId(); } \
  413. TSShape* get##name(const U32& index) { if (m##name##Asset[index].notNull()) return m##name##Asset[index]->getShape(); else return NULL; } \
  414. AssetPtr<ShapeAsset> get##name##Asset(const U32& index) { return m##name##Asset[index]; } \
  415. static bool _set##name##Data(void* obj, const char* index, const char* data) { static_cast<className*>(obj)->_set##name(_getStringTable()->insert(data), dAtoi(index)); return false;}\
  416. StringTableEntry get##name##File(const U32& idx) { return m##name##Asset[idx].notNull() ? m##name##Asset[idx]->getShapeFile() : ""; }
  417. #define INITPERSISTFIELD_SHAPEASSET_ARRAY_REFACTOR(name, arraySize, consoleClass, docs) \
  418. addProtectedField(assetText(name, Asset), TypeShapeAssetPtr, Offset(m##name##Asset, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));\
  419. addProtectedField(assetText(name, File), TypeFilename, Offset(m##name##File, consoleClass), _set##name##Data, &defaultProtectedGetFn, arraySize, assetDoc(name, asset docs.));
  420. #pragma endregion
  421. //-----------------------------------------------------------------------------
  422. // REFACTOR END
  423. //-----------------------------------------------------------------------------
  424. #endif