ShapeAsset.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. //-----------------------------------------------------------------------------
  44. ConsoleGetType(TypeShapeAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. //return *((StringTableEntry*)dptr);
  48. return (*((AssetPtr<ShapeAsset>*)dptr)).getAssetId();
  49. }
  50. //-----------------------------------------------------------------------------
  51. ConsoleSetType(TypeShapeAssetPtr)
  52. {
  53. // Was a single argument specified?
  54. if (argc == 1)
  55. {
  56. // Yes, so fetch field value.
  57. const char* pFieldValue = argv[0];
  58. // Fetch asset Id.
  59. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  60. // Update asset value.
  61. *assetId = StringTable->insert(pFieldValue);
  62. return;
  63. }
  64. // Warn.
  65. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  66. }
  67. //-----------------------------------------------------------------------------
  68. ShapeAsset::ShapeAsset()
  69. {
  70. mFileName = StringTable->EmptyString();
  71. mConstructorFileName = StringTable->EmptyString();
  72. }
  73. //-----------------------------------------------------------------------------
  74. ShapeAsset::~ShapeAsset()
  75. {
  76. }
  77. //-----------------------------------------------------------------------------
  78. void ShapeAsset::initPersistFields()
  79. {
  80. // Call parent.
  81. Parent::initPersistFields();
  82. addProtectedField("fileName", TypeAssetLooseFilePath, Offset(mFileName, ShapeAsset),
  83. &setShapeFile, &getShapeFile, "Path to the shape file we want to render");
  84. addProtectedField("constuctorFileName", TypeAssetLooseFilePath, Offset(mConstructorFileName, ShapeAsset),
  85. &setShapeConstructorFile, &getShapeConstructorFile, "Path to the shape file we want to render");
  86. }
  87. void ShapeAsset::setDataField(StringTableEntry slotName, const char *array, const char *value)
  88. {
  89. Parent::setDataField(slotName, array, value);
  90. //Now, if it's a material slot of some fashion, set it up
  91. StringTableEntry matSlotName = StringTable->insert("materialAsset");
  92. if (String(slotName).startsWith(matSlotName))
  93. {
  94. StringTableEntry matId = StringTable->insert(value);
  95. mMaterialAssetIds.push_back(matId);
  96. }
  97. }
  98. void ShapeAsset::initializeAsset()
  99. {
  100. // Call parent.
  101. Parent::initializeAsset();
  102. if (mFileName == StringTable->EmptyString())
  103. return;
  104. ResourceManager::get().getChangedSignal().notify(this, &ShapeAsset::_onResourceChanged);
  105. //Ensure our path is expando'd if it isn't already
  106. if (!Platform::isFullPath(mFileName))
  107. mFileName = getOwned() ? expandAssetFilePath(mFileName) : mFileName;
  108. mConstructorFileName = expandAssetFilePath(mConstructorFileName);
  109. loadShape();
  110. }
  111. void ShapeAsset::setShapeFile(const char* pShapeFile)
  112. {
  113. // Sanity!
  114. AssertFatal(pShapeFile != NULL, "Cannot use a NULL shape file.");
  115. // Fetch image file.
  116. pShapeFile = StringTable->insert(pShapeFile);
  117. // Ignore no change,
  118. if (pShapeFile == mFileName)
  119. return;
  120. mFileName = pShapeFile;
  121. // Refresh the asset.
  122. refreshAsset();
  123. }
  124. void ShapeAsset::setShapeConstructorFile(const char* pShapeConstructorFile)
  125. {
  126. // Sanity!
  127. AssertFatal(pShapeConstructorFile != NULL, "Cannot use a NULL shape constructor file.");
  128. // Fetch image file.
  129. pShapeConstructorFile = StringTable->insert(pShapeConstructorFile);
  130. // Ignore no change,
  131. if (pShapeConstructorFile == mConstructorFileName)
  132. return;
  133. mConstructorFileName = pShapeConstructorFile;
  134. // Refresh the asset.
  135. refreshAsset();
  136. }
  137. void ShapeAsset::_onResourceChanged(const Torque::Path &path)
  138. {
  139. if (path != Torque::Path(mFileName) )
  140. return;
  141. refreshAsset();
  142. loadShape();
  143. }
  144. bool ShapeAsset::loadShape()
  145. {
  146. mMaterialAssets.clear();
  147. mMaterialAssetIds.clear();
  148. //First, load any material, animation, etc assets we may be referencing in our asset
  149. // Find any asset dependencies.
  150. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  151. // Does the asset have any dependencies?
  152. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  153. {
  154. // Iterate all dependencies.
  155. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  156. {
  157. StringTableEntry assetType = mpOwningAssetManager->getAssetType(assetDependenciesItr->value);
  158. if (assetType == StringTable->insert("MaterialAsset"))
  159. {
  160. mMaterialAssetIds.push_front(assetDependenciesItr->value);
  161. //Force the asset to become initialized if it hasn't been already
  162. AssetPtr<MaterialAsset> matAsset = assetDependenciesItr->value;
  163. mMaterialAssets.push_front(matAsset);
  164. }
  165. else if (assetType == StringTable->insert("ShapeAnimationAsset"))
  166. {
  167. mAnimationAssetIds.push_back(assetDependenciesItr->value);
  168. //Force the asset to become initialized if it hasn't been already
  169. AssetPtr<ShapeAnimationAsset> animAsset = assetDependenciesItr->value;
  170. mAnimationAssets.push_back(animAsset);
  171. }
  172. // Next dependency.
  173. assetDependenciesItr++;
  174. }
  175. }
  176. mShape = ResourceManager::get().load(mFileName);
  177. if (!mShape)
  178. {
  179. Con::errorf("StaticMesh::updateShape : failed to load shape file!");
  180. return false; //if it failed to load, bail out
  181. }
  182. bool hasBlends = false;
  183. //Now that we've successfully loaded our shape and have any materials and animations loaded
  184. //we need to set up the animations we're using on our shape
  185. for (S32 i = mAnimationAssets.size()-1; i >= 0; --i)
  186. {
  187. String srcName = mAnimationAssets[i]->getAnimationName();
  188. String srcPath(mAnimationAssets[i]->getAnimationFilename());
  189. //SplitSequencePathAndName(srcPath, srcName);
  190. if (!mShape->addSequence(srcPath, srcName, srcName,
  191. mAnimationAssets[i]->getStartFrame(), mAnimationAssets[i]->getEndFrame(), mAnimationAssets[i]->getPadRotation(), mAnimationAssets[i]->getPadTransforms()))
  192. return false;
  193. if (mAnimationAssets[i]->isBlend())
  194. hasBlends = true;
  195. }
  196. //if any of our animations are blends, set those up now
  197. if (hasBlends)
  198. {
  199. for (U32 i=0; i < mAnimationAssets.size(); ++i)
  200. {
  201. if (mAnimationAssets[i]->isBlend() && mAnimationAssets[i]->getBlendAnimationName() != StringTable->EmptyString())
  202. {
  203. //gotta do a bit of logic here.
  204. //First, we need to make sure the anim asset we depend on for our blend is loaded
  205. AssetPtr<ShapeAnimationAsset> blendAnimAsset = mAnimationAssets[i]->getBlendAnimationName();
  206. if (blendAnimAsset.isNull())
  207. {
  208. Con::errorf("ShapeAsset::initializeAsset - Unable to acquire reference animation asset %s for asset %s to blend!", mAnimationAssets[i]->getBlendAnimationName(), mAnimationAssets[i]->getAssetName());
  209. return false;
  210. }
  211. String refAnimName = blendAnimAsset->getAnimationName();
  212. if (!mShape->setSequenceBlend(mAnimationAssets[i]->getAnimationName(), true, blendAnimAsset->getAnimationName(), mAnimationAssets[i]->getBlendFrame()))
  213. {
  214. Con::errorf("ShapeAnimationAsset::initializeAsset - Unable to set animation clip %s for asset %s to blend!", mAnimationAssets[i]->getAnimationName(), mAnimationAssets[i]->getAssetName());
  215. return false;
  216. }
  217. }
  218. }
  219. }
  220. onShapeChanged.trigger(this);
  221. return true;
  222. }
  223. //------------------------------------------------------------------------------
  224. void ShapeAsset::copyTo(SimObject* object)
  225. {
  226. // Call to parent.
  227. Parent::copyTo(object);
  228. }
  229. void ShapeAsset::onAssetRefresh(void)
  230. {
  231. if (mFileName == StringTable->EmptyString())
  232. return;
  233. // Update.
  234. if(!Platform::isFullPath(mFileName))
  235. mFileName = getOwned() ? expandAssetFilePath(mFileName) : mFileName;
  236. loadShape();
  237. }
  238. void ShapeAsset::SplitSequencePathAndName(String& srcPath, String& srcName)
  239. {
  240. srcName = "";
  241. // Determine if there is a sequence name at the end of the source string, and
  242. // if so, split the filename from the sequence name
  243. S32 split = srcPath.find(' ', 0, String::Right);
  244. S32 split2 = srcPath.find('\t', 0, String::Right);
  245. if ((split == String::NPos) || (split2 > split))
  246. split = split2;
  247. if (split != String::NPos)
  248. {
  249. split2 = split + 1;
  250. while ((srcPath[split2] != '\0') && dIsspace(srcPath[split2]))
  251. split2++;
  252. // now 'split' is at the end of the path, and 'split2' is at the start of the sequence name
  253. srcName = srcPath.substr(split2);
  254. srcPath = srcPath.erase(split, srcPath.length() - split);
  255. }
  256. }
  257. ShapeAnimationAsset* ShapeAsset::getAnimation(S32 index)
  258. {
  259. if (index < mAnimationAssets.size())
  260. {
  261. return mAnimationAssets[index];
  262. }
  263. return nullptr;
  264. }
  265. DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
  266. "Gets the number of materials for this shape asset.\n"
  267. "@return Material count.\n")
  268. {
  269. return object->getMaterialCount();
  270. }
  271. DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
  272. "Gets the number of animations for this shape asset.\n"
  273. "@return Animation count.\n")
  274. {
  275. return object->getAnimationCount();
  276. }
  277. DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index), (0),
  278. "Gets a particular shape animation asset for this shape.\n"
  279. "@param animation asset index.\n"
  280. "@return Shape Animation Asset.\n")
  281. {
  282. return object->getAnimation(index);
  283. }
  284. //-----------------------------------------------------------------------------
  285. // GuiInspectorTypeAssetId
  286. //-----------------------------------------------------------------------------
  287. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  288. ConsoleDocClass(GuiInspectorTypeShapeAssetPtr,
  289. "@brief Inspector field type for Shapes\n\n"
  290. "Editor use only.\n\n"
  291. "@internal"
  292. );
  293. void GuiInspectorTypeShapeAssetPtr::consoleInit()
  294. {
  295. Parent::consoleInit();
  296. ConsoleBaseType::getType(TypeShapeAssetPtr)->setInspectorFieldType("GuiInspectorTypeShapeAssetPtr");
  297. }
  298. GuiControl* GuiInspectorTypeShapeAssetPtr::constructEditControl()
  299. {
  300. // Create base filename edit controls
  301. GuiControl *retCtrl = Parent::constructEditControl();
  302. if (retCtrl == NULL)
  303. return retCtrl;
  304. // Change filespec
  305. char szBuffer[512];
  306. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  307. mInspector->getInspectObject()->getIdString(), mCaption);
  308. mBrowseButton->setField("Command", szBuffer);
  309. const char* id = mInspector->getInspectObject()->getIdString();
  310. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  311. // Create "Open in ShapeEditor" button
  312. mShapeEdButton = new GuiBitmapButtonCtrl();
  313. dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAsset(%d.getText());", retCtrl->getId());
  314. mShapeEdButton->setField("Command", szBuffer);
  315. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  316. mShapeEdButton->setBitmap(bitmapName);
  317. mShapeEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  318. mShapeEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  319. mShapeEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  320. mShapeEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
  321. mShapeEdButton->registerObject();
  322. addObject(mShapeEdButton);
  323. return retCtrl;
  324. }
  325. bool GuiInspectorTypeShapeAssetPtr::updateRects()
  326. {
  327. S32 dividerPos, dividerMargin;
  328. mInspector->getDivider(dividerPos, dividerMargin);
  329. Point2I fieldExtent = getExtent();
  330. Point2I fieldPos = getPosition();
  331. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  332. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  333. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  334. if (mBrowseButton != NULL)
  335. {
  336. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  337. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  338. }
  339. if (mShapeEdButton != NULL)
  340. {
  341. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  342. resized |= mShapeEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
  343. }
  344. return resized;
  345. }