ShapeAsset.cpp 18 KB

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