ShapeAsset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. //Now that we've successfully loaded our shape and have any materials and animations loaded
  143. //we need to set up the animations we're using on our shape
  144. for (U32 i = 0; i < mAnimationAssets.size(); i++)
  145. {
  146. String srcName;
  147. String srcPath(mAnimationAssets[i]->getAnimationFilename());
  148. SplitSequencePathAndName(srcPath, srcName);
  149. if (!mShape->addSequence(srcPath, srcName, mAnimationAssets[i]->getAnimationName(),
  150. mAnimationAssets[i]->getStartFrame(), mAnimationAssets[i]->getEndFrame(), mAnimationAssets[i]->getPadRotation(), mAnimationAssets[i]->getPadTransforms()))
  151. return false;
  152. }
  153. return true;
  154. }
  155. //------------------------------------------------------------------------------
  156. void ShapeAsset::copyTo(SimObject* object)
  157. {
  158. // Call to parent.
  159. Parent::copyTo(object);
  160. }
  161. void ShapeAsset::onAssetRefresh(void)
  162. {
  163. if (dStrcmp(mFileName, "") == 0)
  164. return;
  165. loadShape();
  166. }
  167. void ShapeAsset::SplitSequencePathAndName(String& srcPath, String& srcName)
  168. {
  169. srcName = "";
  170. // Determine if there is a sequence name at the end of the source string, and
  171. // if so, split the filename from the sequence name
  172. S32 split = srcPath.find(' ', 0, String::Right);
  173. S32 split2 = srcPath.find('\t', 0, String::Right);
  174. if ((split == String::NPos) || (split2 > split))
  175. split = split2;
  176. if (split != String::NPos)
  177. {
  178. split2 = split + 1;
  179. while ((srcPath[split2] != '\0') && dIsspace(srcPath[split2]))
  180. split2++;
  181. // now 'split' is at the end of the path, and 'split2' is at the start of the sequence name
  182. srcName = srcPath.substr(split2);
  183. srcPath = srcPath.erase(split, srcPath.length() - split);
  184. }
  185. }
  186. ShapeAnimationAsset* ShapeAsset::getAnimation(S32 index)
  187. {
  188. if (index < mAnimationAssets.size())
  189. {
  190. return mAnimationAssets[index];
  191. }
  192. return nullptr;
  193. }
  194. DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
  195. "Gets the number of materials for this shape asset.\n"
  196. "@return Material count.\n")
  197. {
  198. return object->getMaterialCount();
  199. }
  200. DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
  201. "Gets the number of animations for this shape asset.\n"
  202. "@return Animation count.\n")
  203. {
  204. return object->getAnimationCount();
  205. }
  206. DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index), (0),
  207. "Gets a particular shape animation asset for this shape.\n"
  208. "@param animation asset index.\n"
  209. "@return Shape Animation Asset.\n")
  210. {
  211. return object->getAnimation(index);
  212. }
  213. //-----------------------------------------------------------------------------
  214. // GuiInspectorTypeAssetId
  215. //-----------------------------------------------------------------------------
  216. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  217. ConsoleDocClass(GuiInspectorTypeShapeAssetPtr,
  218. "@brief Inspector field type for Shapes\n\n"
  219. "Editor use only.\n\n"
  220. "@internal"
  221. );
  222. void GuiInspectorTypeShapeAssetPtr::consoleInit()
  223. {
  224. Parent::consoleInit();
  225. ConsoleBaseType::getType(TypeShapeAssetPtr)->setInspectorFieldType("GuiInspectorTypeShapeAssetPtr");
  226. }
  227. GuiControl* GuiInspectorTypeShapeAssetPtr::constructEditControl()
  228. {
  229. // Create base filename edit controls
  230. GuiControl *retCtrl = Parent::constructEditControl();
  231. if (retCtrl == NULL)
  232. return retCtrl;
  233. // Change filespec
  234. char szBuffer[512];
  235. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %d, %s);",
  236. mInspector->getComponentGroupTargetId(), mCaption);
  237. mBrowseButton->setField("Command", szBuffer);
  238. setDataField(StringTable->insert("ComponentOwner"), NULL, String::ToString(mInspector->getComponentGroupTargetId()).c_str());
  239. // Create "Open in ShapeEditor" button
  240. mShapeEdButton = new GuiBitmapButtonCtrl();
  241. dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAsset(%d.getText());", retCtrl->getId());
  242. mShapeEdButton->setField("Command", szBuffer);
  243. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  244. mShapeEdButton->setBitmap(bitmapName);
  245. mShapeEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  246. mShapeEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  247. mShapeEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  248. mShapeEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
  249. mShapeEdButton->registerObject();
  250. addObject(mShapeEdButton);
  251. return retCtrl;
  252. }
  253. bool GuiInspectorTypeShapeAssetPtr::updateRects()
  254. {
  255. S32 dividerPos, dividerMargin;
  256. mInspector->getDivider(dividerPos, dividerMargin);
  257. Point2I fieldExtent = getExtent();
  258. Point2I fieldPos = getPosition();
  259. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  260. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  261. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  262. if (mBrowseButton != NULL)
  263. {
  264. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  265. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  266. }
  267. if (mShapeEdButton != NULL)
  268. {
  269. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  270. resized |= mShapeEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
  271. }
  272. return resized;
  273. }