ShapeAsset.cpp 13 KB

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