ShapeAsset.cpp 12 KB

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