TerrainMaterialAsset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 TERRAINMATERIALASSET_H
  23. #include "TerrainMaterialAsset.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. //-----------------------------------------------------------------------------
  38. IMPLEMENT_CONOBJECT(TerrainMaterialAsset);
  39. ConsoleType(TerrainMaterialAssetPtr, TypeTerrainMaterialAssetPtr, TerrainMaterialAsset, ASSET_ID_FIELD_PREFIX)
  40. //-----------------------------------------------------------------------------
  41. ConsoleGetType(TypeTerrainMaterialAssetPtr)
  42. {
  43. // Fetch asset Id.
  44. return (*((AssetPtr<TerrainMaterialAsset>*)dptr)).getAssetId();
  45. }
  46. //-----------------------------------------------------------------------------
  47. ConsoleSetType(TypeTerrainMaterialAssetPtr)
  48. {
  49. // Was a single argument specified?
  50. if (argc == 1)
  51. {
  52. // Yes, so fetch field value.
  53. const char* pFieldValue = argv[0];
  54. // Fetch asset pointer.
  55. AssetPtr<TerrainMaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<TerrainMaterialAsset>*>((AssetPtrBase*)(dptr));
  56. // Is the asset pointer the correct type?
  57. if (pAssetPtr == NULL)
  58. {
  59. // No, so fail.
  60. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  61. return;
  62. }
  63. // Set asset.
  64. pAssetPtr->setAssetId(pFieldValue);
  65. return;
  66. }
  67. // Warn.
  68. Con::warnf("(TypeTerrainMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  69. }
  70. //-----------------------------------------------------------------------------
  71. TerrainMaterialAsset::TerrainMaterialAsset()
  72. {
  73. mScriptFile = StringTable->EmptyString();
  74. mScriptPath = StringTable->EmptyString();
  75. mMatDefinitionName = StringTable->EmptyString();
  76. }
  77. //-----------------------------------------------------------------------------
  78. TerrainMaterialAsset::~TerrainMaterialAsset()
  79. {
  80. }
  81. //-----------------------------------------------------------------------------
  82. void TerrainMaterialAsset::initPersistFields()
  83. {
  84. // Call parent.
  85. Parent::initPersistFields();
  86. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, TerrainMaterialAsset), "");
  87. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, TerrainMaterialAsset),
  88. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  89. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, TerrainMaterialAsset), "Name of the material definition this asset is for.");
  90. }
  91. void TerrainMaterialAsset::initializeAsset()
  92. {
  93. // Call parent.
  94. Parent::initializeAsset();
  95. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  96. if (Torque::FS::IsScriptFile(mScriptPath))
  97. Con::executeFile(mScriptPath, false, false);
  98. }
  99. void TerrainMaterialAsset::onAssetRefresh()
  100. {
  101. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  102. if (Torque::FS::IsScriptFile(mScriptPath))
  103. Con::executeFile(mScriptPath, false, false);
  104. if (mMatDefinitionName != StringTable->EmptyString())
  105. {
  106. TerrainMaterial* matDef;
  107. if (!Sim::findObject(mMatDefinitionName, matDef))
  108. {
  109. Con::errorf("TerrainMaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  110. return;
  111. }
  112. //matDef->reload();
  113. }
  114. }
  115. void TerrainMaterialAsset::setScriptFile(const char* pScriptFile)
  116. {
  117. // Sanity!
  118. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  119. pScriptFile = StringTable->insert(pScriptFile, true);
  120. // Ignore no change,
  121. if (pScriptFile == mScriptFile)
  122. return;
  123. // Update.
  124. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  125. // Refresh the asset.
  126. refreshAsset();
  127. }
  128. //------------------------------------------------------------------------------
  129. void TerrainMaterialAsset::copyTo(SimObject* object)
  130. {
  131. // Call to parent.
  132. Parent::copyTo(object);
  133. }
  134. StringTableEntry TerrainMaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  135. {
  136. StringTableEntry materialAssetId = StringTable->EmptyString();
  137. AssetQuery* query = new AssetQuery();
  138. U32 foundCount = AssetDatabase.findAssetType(query, "TerrainMaterialAsset");
  139. if (foundCount == 0)
  140. {
  141. //Didn't work, so have us fall back to a placeholder asset
  142. materialAssetId = StringTable->insert("Core_Rendering:noMaterial");
  143. }
  144. else
  145. {
  146. for (U32 i = 0; i < foundCount; i++)
  147. {
  148. TerrainMaterialAsset* matAsset = AssetDatabase.acquireAsset<TerrainMaterialAsset>(query->mAssetList[i]);
  149. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  150. {
  151. materialAssetId = matAsset->getAssetId();
  152. AssetDatabase.releaseAsset(query->mAssetList[i]);
  153. break;
  154. }
  155. AssetDatabase.releaseAsset(query->mAssetList[i]);
  156. }
  157. }
  158. return materialAssetId;
  159. }
  160. #ifdef TORQUE_TOOLS
  161. DefineEngineStaticMethod(TerrainMaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  162. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  163. "@return The AssetId of the associated asset, if any.")
  164. {
  165. return TerrainMaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  166. }
  167. #endif
  168. //-----------------------------------------------------------------------------
  169. // GuiInspectorTypeAssetId
  170. //-----------------------------------------------------------------------------
  171. IMPLEMENT_CONOBJECT(GuiInspectorTypeTerrainMaterialAssetPtr);
  172. ConsoleDocClass(GuiInspectorTypeTerrainMaterialAssetPtr,
  173. "@brief Inspector field type for Material Asset Objects\n\n"
  174. "Editor use only.\n\n"
  175. "@internal"
  176. );
  177. void GuiInspectorTypeTerrainMaterialAssetPtr::consoleInit()
  178. {
  179. Parent::consoleInit();
  180. ConsoleBaseType::getType(TypeTerrainMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeTerrainMaterialAssetPtr");
  181. }
  182. GuiControl* GuiInspectorTypeTerrainMaterialAssetPtr::constructEditControl()
  183. {
  184. // Create base filename edit controls
  185. mUseHeightOverride = true;
  186. mHeightOverride = 100;
  187. mMatEdContainer = new GuiControl();
  188. mMatEdContainer->registerObject();
  189. addObject(mMatEdContainer);
  190. // Create "Open in ShapeEditor" button
  191. mMatPreviewButton = new GuiBitmapButtonCtrl();
  192. const char* matAssetId = getData();
  193. TerrainMaterialAsset* matAsset = AssetDatabase.acquireAsset< TerrainMaterialAsset>(matAssetId);
  194. TerrainMaterial* materialDef = nullptr;
  195. char bitmapName[512] = "ToolsModule:material_editor_n_image";
  196. /*if (!Sim::findObject(matAsset->getMaterialDefinitionName(), materialDef))
  197. {
  198. Con::errorf("GuiInspectorTypeTerrainMaterialAssetPtr::constructEditControl() - unable to find material in asset");
  199. }
  200. else
  201. {
  202. mMatPreviewButton->setBitmap(materialDef->mDiffuseMapFilename[0]);
  203. }*/
  204. mMatPreviewButton->setPosition(0, 0);
  205. mMatPreviewButton->setExtent(100,100);
  206. // Change filespec
  207. char szBuffer[512];
  208. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"TerrainMaterialAsset\", \"AssetBrowser.changeAsset\", %d, %s);",
  209. mInspector->getComponentGroupTargetId(), mCaption);
  210. mMatPreviewButton->setField("Command", szBuffer);
  211. mMatPreviewButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  212. mMatPreviewButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  213. mMatPreviewButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  214. StringBuilder strbld;
  215. strbld.append(matAsset->getMaterialDefinitionName());
  216. strbld.append("\n");
  217. strbld.append("Open this asset in the Material Editor");
  218. mMatPreviewButton->setDataField(StringTable->insert("tooltip"), NULL, strbld.data());
  219. _registerEditControl(mMatPreviewButton);
  220. //mMatPreviewButton->registerObject();
  221. mMatEdContainer->addObject(mMatPreviewButton);
  222. mMatAssetIdTxt = new GuiTextEditCtrl();
  223. mMatAssetIdTxt->registerObject();
  224. mMatAssetIdTxt->setActive(false);
  225. mMatAssetIdTxt->setText(matAssetId);
  226. mMatAssetIdTxt->setBounds(100, 0, 150, 18);
  227. mMatEdContainer->addObject(mMatAssetIdTxt);
  228. return mMatEdContainer;
  229. }
  230. bool GuiInspectorTypeTerrainMaterialAssetPtr::updateRects()
  231. {
  232. S32 dividerPos, dividerMargin;
  233. mInspector->getDivider(dividerPos, dividerMargin);
  234. Point2I fieldExtent = getExtent();
  235. Point2I fieldPos = getPosition();
  236. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  237. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  238. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  239. if (mMatEdContainer != nullptr)
  240. {
  241. mMatPreviewButton->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  242. }
  243. if (mMatPreviewButton != nullptr)
  244. {
  245. mMatPreviewButton->resize(Point2I::Zero, Point2I(100, 100));
  246. }
  247. if (mMatAssetIdTxt != nullptr)
  248. {
  249. mMatAssetIdTxt->resize(Point2I(100, 0), Point2I(mEditCtrlRect.extent.x - 100, 18));
  250. }
  251. return resized;
  252. }
  253. void GuiInspectorTypeTerrainMaterialAssetPtr::setMaterialAsset(String assetId)
  254. {
  255. mTargetObject->setDataField(mCaption, "", assetId);
  256. //force a refresh
  257. SimObject* obj = mInspector->getInspectObject();
  258. mInspector->inspectObject(obj);
  259. }
  260. DefineEngineMethod(GuiInspectorTypeTerrainMaterialAssetPtr, setMaterialAsset, void, (String assetId), (""),
  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. if (assetId == String::EmptyString)
  266. return;
  267. return object->setMaterialAsset(assetId);
  268. }