ShapeAsset.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. #include "ShapeAsset.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assets/assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. #ifndef _ASSET_PTR_H_
  35. #include "assets/assetPtr.h"
  36. #endif
  37. #include "core/resourceManager.h"
  38. // Debug Profiling.
  39. #include "platform/profiler.h"
  40. #include "T3D/assets/assetImporter.h"
  41. //-----------------------------------------------------------------------------
  42. IMPLEMENT_CONOBJECT(ShapeAsset);
  43. ConsoleType(assetIdString, TypeShapeAssetPtr, String, ASSET_ID_FIELD_PREFIX)
  44. ConsoleGetType(TypeShapeAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. //return *((StringTableEntry*)dptr);
  48. return (*((AssetPtr<ShapeAsset>*)dptr)).getAssetId();
  49. }
  50. ConsoleSetType(TypeShapeAssetPtr)
  51. {
  52. // Was a single argument specified?
  53. if (argc == 1)
  54. {
  55. // Yes, so fetch field value.
  56. const char* pFieldValue = argv[0];
  57. // Fetch asset Id.
  58. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  59. // Update asset value.
  60. *assetId = StringTable->insert(pFieldValue);
  61. return;
  62. }
  63. // Warn.
  64. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  65. }
  66. //-----------------------------------------------------------------------------
  67. ConsoleType(assetIdString, TypeShapeAssetId, String, ASSET_ID_FIELD_PREFIX)
  68. ConsoleGetType(TypeShapeAssetId)
  69. {
  70. // Fetch asset Id.
  71. return *((const char**)(dptr));
  72. }
  73. ConsoleSetType(TypeShapeAssetId)
  74. {
  75. // Was a single argument specified?
  76. if (argc == 1)
  77. {
  78. // Yes, so fetch field value.
  79. const char* pFieldValue = argv[0];
  80. // Fetch asset Id.
  81. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  82. // Update asset value.
  83. *assetId = StringTable->insert(pFieldValue);
  84. return;
  85. }
  86. // Warn.
  87. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  88. }
  89. //-----------------------------------------------------------------------------
  90. ShapeAsset::ShapeAsset()
  91. {
  92. mFileName = StringTable->EmptyString();
  93. mConstructorFileName = StringTable->EmptyString();
  94. mFilePath = StringTable->EmptyString();
  95. mConstructorFilePath = StringTable->EmptyString();
  96. }
  97. //-----------------------------------------------------------------------------
  98. ShapeAsset::~ShapeAsset()
  99. {
  100. }
  101. //-----------------------------------------------------------------------------
  102. void ShapeAsset::initPersistFields()
  103. {
  104. // Call parent.
  105. Parent::initPersistFields();
  106. addProtectedField("fileName", TypeAssetLooseFilePath, Offset(mFileName, ShapeAsset),
  107. &setShapeFile, &getShapeFile, "Path to the shape file we want to render");
  108. addProtectedField("constuctorFileName", TypeAssetLooseFilePath, Offset(mConstructorFileName, ShapeAsset),
  109. &setShapeConstructorFile, &getShapeConstructorFile, "Path to the shape file we want to render");
  110. }
  111. void ShapeAsset::setDataField(StringTableEntry slotName, const char *array, const char *value)
  112. {
  113. Parent::setDataField(slotName, array, value);
  114. //Now, if it's a material slot of some fashion, set it up
  115. StringTableEntry matSlotName = StringTable->insert("materialAsset");
  116. if (String(slotName).startsWith(matSlotName))
  117. {
  118. StringTableEntry matId = StringTable->insert(value);
  119. mMaterialAssetIds.push_back(matId);
  120. }
  121. }
  122. void ShapeAsset::initializeAsset()
  123. {
  124. // Call parent.
  125. Parent::initializeAsset();
  126. if (mFileName == StringTable->EmptyString())
  127. return;
  128. ResourceManager::get().getChangedSignal().notify(this, &ShapeAsset::_onResourceChanged);
  129. //Ensure our path is expando'd if it isn't already
  130. if (!Platform::isFullPath(mFilePath))
  131. mFilePath = getOwned() ? expandAssetFilePath(mFileName) : mFilePath;
  132. mConstructorFilePath = expandAssetFilePath(mConstructorFilePath);
  133. loadShape();
  134. }
  135. void ShapeAsset::setShapeFile(const char* pShapeFile)
  136. {
  137. // Sanity!
  138. AssertFatal(pShapeFile != NULL, "Cannot use a NULL shape file.");
  139. // Fetch image file.
  140. pShapeFile = StringTable->insert(pShapeFile);
  141. // Ignore no change,
  142. if (pShapeFile == mFileName)
  143. return;
  144. mFileName = pShapeFile;
  145. // Refresh the asset.
  146. refreshAsset();
  147. }
  148. void ShapeAsset::setShapeConstructorFile(const char* pShapeConstructorFile)
  149. {
  150. // Sanity!
  151. AssertFatal(pShapeConstructorFile != NULL, "Cannot use a NULL shape constructor file.");
  152. // Fetch image file.
  153. pShapeConstructorFile = StringTable->insert(pShapeConstructorFile);
  154. // Ignore no change,
  155. if (pShapeConstructorFile == mConstructorFileName)
  156. return;
  157. mConstructorFileName = pShapeConstructorFile;
  158. // Refresh the asset.
  159. refreshAsset();
  160. }
  161. void ShapeAsset::_onResourceChanged(const Torque::Path &path)
  162. {
  163. if (path != Torque::Path(mFilePath) )
  164. return;
  165. refreshAsset();
  166. loadShape();
  167. }
  168. bool ShapeAsset::loadShape()
  169. {
  170. mMaterialAssets.clear();
  171. mMaterialAssetIds.clear();
  172. //First, load any material, animation, etc assets we may be referencing in our asset
  173. // Find any asset dependencies.
  174. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  175. // Does the asset have any dependencies?
  176. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  177. {
  178. // Iterate all dependencies.
  179. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  180. {
  181. StringTableEntry assetType = mpOwningAssetManager->getAssetType(assetDependenciesItr->value);
  182. if (assetType == StringTable->insert("MaterialAsset"))
  183. {
  184. mMaterialAssetIds.push_front(assetDependenciesItr->value);
  185. //Force the asset to become initialized if it hasn't been already
  186. AssetPtr<MaterialAsset> matAsset = assetDependenciesItr->value;
  187. mMaterialAssets.push_front(matAsset);
  188. }
  189. else if (assetType == StringTable->insert("ShapeAnimationAsset"))
  190. {
  191. mAnimationAssetIds.push_back(assetDependenciesItr->value);
  192. //Force the asset to become initialized if it hasn't been already
  193. AssetPtr<ShapeAnimationAsset> animAsset = assetDependenciesItr->value;
  194. mAnimationAssets.push_back(animAsset);
  195. }
  196. // Next dependency.
  197. assetDependenciesItr++;
  198. }
  199. }
  200. mShape = ResourceManager::get().load(mFilePath);
  201. if (!mShape)
  202. {
  203. Con::errorf("StaticMesh::updateShape : failed to load shape file!");
  204. return false; //if it failed to load, bail out
  205. }
  206. bool hasBlends = false;
  207. //Now that we've successfully loaded our shape and have any materials and animations loaded
  208. //we need to set up the animations we're using on our shape
  209. for (S32 i = mAnimationAssets.size()-1; i >= 0; --i)
  210. {
  211. String srcName = mAnimationAssets[i]->getAnimationName();
  212. String srcPath(mAnimationAssets[i]->getAnimationFilename());
  213. //SplitSequencePathAndName(srcPath, srcName);
  214. if (!mShape->addSequence(srcPath, srcName, srcName,
  215. mAnimationAssets[i]->getStartFrame(), mAnimationAssets[i]->getEndFrame(), mAnimationAssets[i]->getPadRotation(), mAnimationAssets[i]->getPadTransforms()))
  216. return false;
  217. if (mAnimationAssets[i]->isBlend())
  218. hasBlends = true;
  219. }
  220. //if any of our animations are blends, set those up now
  221. if (hasBlends)
  222. {
  223. for (U32 i=0; i < mAnimationAssets.size(); ++i)
  224. {
  225. if (mAnimationAssets[i]->isBlend() && mAnimationAssets[i]->getBlendAnimationName() != StringTable->EmptyString())
  226. {
  227. //gotta do a bit of logic here.
  228. //First, we need to make sure the anim asset we depend on for our blend is loaded
  229. AssetPtr<ShapeAnimationAsset> blendAnimAsset = mAnimationAssets[i]->getBlendAnimationName();
  230. if (blendAnimAsset.isNull())
  231. {
  232. Con::errorf("ShapeAsset::initializeAsset - Unable to acquire reference animation asset %s for asset %s to blend!", mAnimationAssets[i]->getBlendAnimationName(), mAnimationAssets[i]->getAssetName());
  233. return false;
  234. }
  235. String refAnimName = blendAnimAsset->getAnimationName();
  236. if (!mShape->setSequenceBlend(mAnimationAssets[i]->getAnimationName(), true, blendAnimAsset->getAnimationName(), mAnimationAssets[i]->getBlendFrame()))
  237. {
  238. Con::errorf("ShapeAnimationAsset::initializeAsset - Unable to set animation clip %s for asset %s to blend!", mAnimationAssets[i]->getAnimationName(), mAnimationAssets[i]->getAssetName());
  239. return false;
  240. }
  241. }
  242. }
  243. }
  244. mChangeSignal.trigger();
  245. return true;
  246. }
  247. //------------------------------------------------------------------------------
  248. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  249. bool ShapeAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset)
  250. {
  251. AssetQuery query;
  252. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  253. if (foundAssetcount == 0)
  254. {
  255. //Didn't find any assets
  256. //If possible, see if we can run an in-place import and the get the asset from that
  257. #if TORQUE_DEBUG
  258. Con::warnf("ShapeAsset::getAssetByFilename - Attempted to in-place import a shapefile(%s) that had no associated asset", fileName);
  259. #endif
  260. AssetImporter* autoAssetImporter;
  261. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  262. {
  263. autoAssetImporter = new AssetImporter();
  264. autoAssetImporter->registerObject("autoAssetImporter");
  265. }
  266. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  267. if (resultingAssetId != StringTable->EmptyString())
  268. {
  269. shapeAsset->setAssetId(resultingAssetId);
  270. if (!shapeAsset->isNull())
  271. return true;
  272. }
  273. //Didn't work, so have us fall back to a placeholder asset
  274. shapeAsset->setAssetId(StringTable->insert("Core_Rendering:noshape"));
  275. if (!shapeAsset->isNull())
  276. return true;
  277. //That didn't work, so fail out
  278. return false;
  279. }
  280. else
  281. {
  282. //acquire and bind the asset, and return it out
  283. shapeAsset->setAssetId(query.mAssetList[0]);
  284. return true;
  285. }
  286. }
  287. StringTableEntry ShapeAsset::getAssetIdByFilename(StringTableEntry fileName)
  288. {
  289. if (fileName == StringTable->EmptyString())
  290. return StringTable->EmptyString();
  291. StringTableEntry shapeAssetId = StringTable->EmptyString();
  292. AssetQuery query;
  293. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  294. if (foundAssetcount == 0)
  295. {
  296. //Didn't find any assets
  297. //If possible, see if we can run an in-place import and the get the asset from that
  298. #if TORQUE_DEBUG
  299. Con::warnf("ShapeAsset::getAssetByFilename - Attempted to in-place import a shapefile(%s) that had no associated asset", fileName);
  300. #endif
  301. AssetImporter* autoAssetImporter;
  302. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  303. {
  304. autoAssetImporter = new AssetImporter();
  305. autoAssetImporter->registerObject("autoAssetImporter");
  306. }
  307. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  308. if (resultingAssetId != StringTable->EmptyString())
  309. {
  310. shapeAssetId = resultingAssetId;
  311. return shapeAssetId;
  312. }
  313. //Didn't work, so have us fall back to a placeholder asset
  314. shapeAssetId = StringTable->insert("Core_Rendering:noshape");
  315. }
  316. else
  317. {
  318. //acquire and bind the asset, and return it out
  319. shapeAssetId = query.mAssetList[0];
  320. }
  321. return shapeAssetId;
  322. }
  323. bool ShapeAsset::getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset)
  324. {
  325. (*shapeAsset) = assetId;
  326. if (!shapeAsset->isNull())
  327. return true;
  328. //Didn't work, so have us fall back to a placeholder asset
  329. StringTableEntry noShapeId = StringTable->insert("Core_Rendering:noshape");
  330. shapeAsset->setAssetId(noShapeId);
  331. if (!shapeAsset->isNull())
  332. return true;
  333. return false;
  334. }
  335. //------------------------------------------------------------------------------
  336. void ShapeAsset::copyTo(SimObject* object)
  337. {
  338. // Call to parent.
  339. Parent::copyTo(object);
  340. }
  341. void ShapeAsset::onAssetRefresh(void)
  342. {
  343. if (mFileName == StringTable->EmptyString())
  344. return;
  345. // Update.
  346. if(!Platform::isFullPath(mFileName))
  347. mFilePath = getOwned() ? expandAssetFilePath(mFileName) : mFilePath;
  348. loadShape();
  349. }
  350. void ShapeAsset::SplitSequencePathAndName(String& srcPath, String& srcName)
  351. {
  352. srcName = "";
  353. // Determine if there is a sequence name at the end of the source string, and
  354. // if so, split the filename from the sequence name
  355. S32 split = srcPath.find(' ', 0, String::Right);
  356. S32 split2 = srcPath.find('\t', 0, String::Right);
  357. if ((split == String::NPos) || (split2 > split))
  358. split = split2;
  359. if (split != String::NPos)
  360. {
  361. split2 = split + 1;
  362. while ((srcPath[split2] != '\0') && dIsspace(srcPath[split2]))
  363. split2++;
  364. // now 'split' is at the end of the path, and 'split2' is at the start of the sequence name
  365. srcName = srcPath.substr(split2);
  366. srcPath = srcPath.erase(split, srcPath.length() - split);
  367. }
  368. }
  369. ShapeAnimationAsset* ShapeAsset::getAnimation(S32 index)
  370. {
  371. if (index < mAnimationAssets.size())
  372. {
  373. return mAnimationAssets[index];
  374. }
  375. return nullptr;
  376. }
  377. DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
  378. "Gets the number of materials for this shape asset.\n"
  379. "@return Material count.\n")
  380. {
  381. return object->getMaterialCount();
  382. }
  383. DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
  384. "Gets the number of animations for this shape asset.\n"
  385. "@return Animation count.\n")
  386. {
  387. return object->getAnimationCount();
  388. }
  389. DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index), (0),
  390. "Gets a particular shape animation asset for this shape.\n"
  391. "@param animation asset index.\n"
  392. "@return Shape Animation Asset.\n")
  393. {
  394. return object->getAnimation(index);
  395. }
  396. //-----------------------------------------------------------------------------
  397. // GuiInspectorTypeAssetId
  398. //-----------------------------------------------------------------------------
  399. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  400. ConsoleDocClass(GuiInspectorTypeShapeAssetPtr,
  401. "@brief Inspector field type for Shapes\n\n"
  402. "Editor use only.\n\n"
  403. "@internal"
  404. );
  405. void GuiInspectorTypeShapeAssetPtr::consoleInit()
  406. {
  407. Parent::consoleInit();
  408. ConsoleBaseType::getType(TypeShapeAssetPtr)->setInspectorFieldType("GuiInspectorTypeShapeAssetPtr");
  409. }
  410. GuiControl* GuiInspectorTypeShapeAssetPtr::constructEditControl()
  411. {
  412. // Create base filename edit controls
  413. GuiControl *retCtrl = Parent::constructEditControl();
  414. if (retCtrl == NULL)
  415. return retCtrl;
  416. // Change filespec
  417. char szBuffer[512];
  418. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  419. mInspector->getInspectObject()->getIdString(), mCaption);
  420. mBrowseButton->setField("Command", szBuffer);
  421. const char* id = mInspector->getInspectObject()->getIdString();
  422. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  423. // Create "Open in ShapeEditor" button
  424. mShapeEdButton = new GuiBitmapButtonCtrl();
  425. dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAssetId(%d.getText());", retCtrl->getId());
  426. mShapeEdButton->setField("Command", szBuffer);
  427. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  428. mShapeEdButton->setBitmap(bitmapName);
  429. mShapeEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  430. mShapeEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  431. mShapeEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  432. mShapeEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
  433. mShapeEdButton->registerObject();
  434. addObject(mShapeEdButton);
  435. return retCtrl;
  436. }
  437. bool GuiInspectorTypeShapeAssetPtr::updateRects()
  438. {
  439. S32 dividerPos, dividerMargin;
  440. mInspector->getDivider(dividerPos, dividerMargin);
  441. Point2I fieldExtent = getExtent();
  442. Point2I fieldPos = getPosition();
  443. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  444. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  445. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  446. if (mBrowseButton != NULL)
  447. {
  448. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  449. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  450. }
  451. if (mShapeEdButton != NULL)
  452. {
  453. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  454. resized |= mShapeEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
  455. }
  456. return resized;
  457. }
  458. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetId);
  459. ConsoleDocClass(GuiInspectorTypeShapeAssetId,
  460. "@brief Inspector field type for Shapes\n\n"
  461. "Editor use only.\n\n"
  462. "@internal"
  463. );
  464. void GuiInspectorTypeShapeAssetId::consoleInit()
  465. {
  466. Parent::consoleInit();
  467. ConsoleBaseType::getType(TypeShapeAssetId)->setInspectorFieldType("GuiInspectorTypeShapeAssetId");
  468. }
  469. DefineEngineMethod(ShapeAsset, getShapeFile, const char*, (), ,
  470. "Creates a new script asset using the targetFilePath.\n"
  471. "@return The bool result of calling exec")
  472. {
  473. return object->getShapeFilePath();
  474. }