TerrainAsset.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 TERRAINASSET_H
  23. #include "TerrainAsset.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 "T3D/assets/TerrainMaterialAsset.h"
  38. //-----------------------------------------------------------------------------
  39. IMPLEMENT_CONOBJECT(TerrainAsset);
  40. ConsoleType(TerrainAssetPtr, TypeTerrainAssetPtr, TerrainAsset, ASSET_ID_FIELD_PREFIX)
  41. //-----------------------------------------------------------------------------
  42. ConsoleGetType(TypeTerrainAssetPtr)
  43. {
  44. // Fetch asset Id.
  45. return (*((AssetPtr<TerrainAsset>*)dptr)).getAssetId();
  46. }
  47. //-----------------------------------------------------------------------------
  48. ConsoleSetType(TypeTerrainAssetPtr)
  49. {
  50. // Was a single argument specified?
  51. if (argc == 1)
  52. {
  53. // Yes, so fetch field value.
  54. const char* pFieldValue = argv[0];
  55. // Fetch asset pointer.
  56. AssetPtr<TerrainAsset>* pAssetPtr = dynamic_cast<AssetPtr<TerrainAsset>*>((AssetPtrBase*)(dptr));
  57. // Is the asset pointer the correct type?
  58. if (pAssetPtr == NULL)
  59. {
  60. // No, so fail.
  61. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  62. return;
  63. }
  64. // Set asset.
  65. pAssetPtr->setAssetId(pFieldValue);
  66. return;
  67. }
  68. // Warn.
  69. Con::warnf("(TypeTerrainAssetPtr) - Cannot set multiple args to a single asset.");
  70. }
  71. //-----------------------------------------------------------------------------
  72. TerrainAsset::TerrainAsset()
  73. {
  74. mTerrainFilePath = StringTable->EmptyString();
  75. }
  76. //-----------------------------------------------------------------------------
  77. TerrainAsset::~TerrainAsset()
  78. {
  79. }
  80. //-----------------------------------------------------------------------------
  81. void TerrainAsset::initPersistFields()
  82. {
  83. // Call parent.
  84. Parent::initPersistFields();
  85. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, TerrainAsset), "");
  86. addProtectedField("terrainFile", TypeAssetLooseFilePath, Offset(mTerrainFilePath, TerrainAsset),
  87. &setTerrainFilePath, &getTerrainFilePath, "Path to the file containing the terrain data.");
  88. }
  89. void TerrainAsset::setDataField(StringTableEntry slotName, const char* array, const char* value)
  90. {
  91. Parent::setDataField(slotName, array, value);
  92. //Now, if it's a material slot of some fashion, set it up
  93. StringTableEntry matSlotName = StringTable->insert("terrainMaterialAsset");
  94. if (String(slotName).startsWith(matSlotName))
  95. {
  96. StringTableEntry matId = StringTable->insert(value);
  97. mTerrMaterialAssetIds.push_back(matId);
  98. }
  99. }
  100. void TerrainAsset::initializeAsset()
  101. {
  102. // Call parent.
  103. Parent::initializeAsset();
  104. mTerrainFilePath = expandAssetFilePath(mTerrainFilePath);
  105. loadTerrain();
  106. }
  107. void TerrainAsset::onAssetRefresh()
  108. {
  109. mTerrainFilePath = expandAssetFilePath(mTerrainFilePath);
  110. loadTerrain();
  111. }
  112. void TerrainAsset::setTerrainFilePath(const char* pScriptFile)
  113. {
  114. // Sanity!
  115. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  116. // Fetch image file.
  117. pScriptFile = StringTable->insert(pScriptFile);
  118. // Update.
  119. mTerrainFilePath = pScriptFile;
  120. // Refresh the asset.
  121. refreshAsset();
  122. }
  123. bool TerrainAsset::loadTerrain()
  124. {
  125. if (!Platform::isFile(mTerrainFilePath))
  126. return false;
  127. mTerrMaterialAssets.clear();
  128. mTerrMaterialAssetIds.clear();
  129. //First, load any material, animation, etc assets we may be referencing in our asset
  130. // Find any asset dependencies.
  131. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  132. // Does the asset have any dependencies?
  133. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  134. {
  135. // Iterate all dependencies.
  136. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  137. {
  138. StringTableEntry assetType = mpOwningAssetManager->getAssetType(assetDependenciesItr->value);
  139. if (assetType == StringTable->insert("TerrainMaterialAsset"))
  140. {
  141. mTerrMaterialAssetIds.push_front(assetDependenciesItr->value);
  142. //Force the asset to become initialized if it hasn't been already
  143. AssetPtr<TerrainMaterialAsset> matAsset = assetDependenciesItr->value;
  144. mTerrMaterialAssets.push_front(matAsset);
  145. }
  146. // Next dependency.
  147. assetDependenciesItr++;
  148. }
  149. }
  150. mTerrainFile = ResourceManager::get().load(mTerrainFilePath);
  151. if (mTerrainFile)
  152. return true;
  153. return false;
  154. }
  155. //------------------------------------------------------------------------------
  156. void TerrainAsset::copyTo(SimObject* object)
  157. {
  158. // Call to parent.
  159. Parent::copyTo(object);
  160. }
  161. //-----------------------------------------------------------------------------
  162. // GuiInspectorTypeAssetId
  163. //-----------------------------------------------------------------------------
  164. IMPLEMENT_CONOBJECT(GuiInspectorTypeTerrainAssetPtr);
  165. ConsoleDocClass(GuiInspectorTypeTerrainAssetPtr,
  166. "@brief Inspector field type for Material Asset Objects\n\n"
  167. "Editor use only.\n\n"
  168. "@internal"
  169. );
  170. void GuiInspectorTypeTerrainAssetPtr::consoleInit()
  171. {
  172. Parent::consoleInit();
  173. ConsoleBaseType::getType(TypeTerrainAssetPtr)->setInspectorFieldType("GuiInspectorTypeTerrainAssetPtr");
  174. }
  175. GuiControl* GuiInspectorTypeTerrainAssetPtr::constructEditControl()
  176. {
  177. // Create base filename edit controls
  178. mUseHeightOverride = true;
  179. mHeightOverride = 100;
  180. mMatEdContainer = new GuiControl();
  181. mMatEdContainer->registerObject();
  182. addObject(mMatEdContainer);
  183. // Create "Open in ShapeEditor" button
  184. mMatPreviewButton = new GuiBitmapButtonCtrl();
  185. const char* matAssetId = getData();
  186. TerrainAsset* matAsset = AssetDatabase.acquireAsset< TerrainAsset>(matAssetId);
  187. //TerrainMaterial* materialDef = nullptr;
  188. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  189. /*if (!Sim::findObject(matAsset->getMaterialDefinitionName(), materialDef))
  190. {
  191. Con::errorf("GuiInspectorTypeTerrainAssetPtr::constructEditControl() - unable to find material in asset");
  192. }
  193. else
  194. {
  195. mMatPreviewButton->setBitmap(materialDef->mDiffuseMapFilename[0]);
  196. }*/
  197. mMatPreviewButton->setPosition(0, 0);
  198. mMatPreviewButton->setExtent(100,100);
  199. // Change filespec
  200. char szBuffer[512];
  201. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"TerrainAsset\", \"AssetBrowser.changeAsset\", %d, %s);",
  202. mInspector->getComponentGroupTargetId(), mCaption);
  203. mMatPreviewButton->setField("Command", szBuffer);
  204. mMatPreviewButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  205. mMatPreviewButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  206. mMatPreviewButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  207. StringBuilder strbld;
  208. /*strbld.append(matAsset->getMaterialDefinitionName());
  209. strbld.append("\n");
  210. strbld.append("Open this file in the Material Editor");*/
  211. mMatPreviewButton->setDataField(StringTable->insert("tooltip"), NULL, strbld.data());
  212. _registerEditControl(mMatPreviewButton);
  213. //mMatPreviewButton->registerObject();
  214. mMatEdContainer->addObject(mMatPreviewButton);
  215. mMatAssetIdTxt = new GuiTextEditCtrl();
  216. mMatAssetIdTxt->registerObject();
  217. mMatAssetIdTxt->setActive(false);
  218. mMatAssetIdTxt->setText(matAssetId);
  219. mMatAssetIdTxt->setBounds(100, 0, 150, 18);
  220. mMatEdContainer->addObject(mMatAssetIdTxt);
  221. return mMatEdContainer;
  222. }
  223. bool GuiInspectorTypeTerrainAssetPtr::updateRects()
  224. {
  225. S32 dividerPos, dividerMargin;
  226. mInspector->getDivider(dividerPos, dividerMargin);
  227. Point2I fieldExtent = getExtent();
  228. Point2I fieldPos = getPosition();
  229. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  230. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  231. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  232. if (mMatEdContainer != nullptr)
  233. {
  234. mMatPreviewButton->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  235. }
  236. if (mMatPreviewButton != nullptr)
  237. {
  238. mMatPreviewButton->resize(Point2I::Zero, Point2I(100, 100));
  239. }
  240. if (mMatAssetIdTxt != nullptr)
  241. {
  242. mMatAssetIdTxt->resize(Point2I(100, 0), Point2I(mEditCtrlRect.extent.x - 100, 18));
  243. }
  244. return resized;
  245. }