MaterialAsset.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 MATERIALASSET_H
  23. #include "MaterialAsset.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/assetImporter.h"
  38. StringTableEntry MaterialAsset::smNoMaterialAssetFallback = NULL;
  39. //-----------------------------------------------------------------------------
  40. IMPLEMENT_CONOBJECT(MaterialAsset);
  41. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  42. //-----------------------------------------------------------------------------
  43. ConsoleGetType(TypeMaterialAssetPtr)
  44. {
  45. // Fetch asset Id.
  46. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  47. }
  48. //-----------------------------------------------------------------------------
  49. ConsoleSetType(TypeMaterialAssetPtr)
  50. {
  51. // Was a single argument specified?
  52. if (argc == 1)
  53. {
  54. // Yes, so fetch field value.
  55. const char* pFieldValue = argv[0];
  56. // Fetch asset pointer.
  57. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  58. // Is the asset pointer the correct type?
  59. if (pAssetPtr == NULL)
  60. {
  61. // No, so fail.
  62. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  63. return;
  64. }
  65. // Set asset.
  66. pAssetPtr->setAssetId(pFieldValue);
  67. return;
  68. }
  69. // Warn.
  70. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  71. }
  72. ConsoleType(assetIdString, TypeMaterialAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  73. ConsoleGetType(TypeMaterialAssetId)
  74. {
  75. // Fetch asset Id.
  76. return *((const char**)(dptr));
  77. }
  78. ConsoleSetType(TypeMaterialAssetId)
  79. {
  80. // Was a single argument specified?
  81. if (argc == 1)
  82. {
  83. // Yes, so fetch field value.
  84. const char* pFieldValue = argv[0];
  85. // Fetch asset Id.
  86. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  87. // Update asset value.
  88. *assetId = StringTable->insert(pFieldValue);
  89. return;
  90. }
  91. // Warn.
  92. Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset.");
  93. }
  94. //-----------------------------------------------------------------------------
  95. MaterialAsset::MaterialAsset()
  96. {
  97. mShaderGraphFile = "";
  98. mScriptFile = StringTable->EmptyString();
  99. mScriptPath = StringTable->EmptyString();
  100. mMatDefinitionName = StringTable->EmptyString();
  101. mMaterialDefinition = nullptr;
  102. }
  103. //-----------------------------------------------------------------------------
  104. MaterialAsset::~MaterialAsset()
  105. {
  106. //SAFE_DELETE(mMaterialDefinition);
  107. }
  108. //-----------------------------------------------------------------------------
  109. void MaterialAsset::consoleInit()
  110. {
  111. Parent::consoleInit();
  112. Con::addVariable("$Core::NoMaterialAssetFallback", TypeString, &smNoMaterialAssetFallback,
  113. "The assetId of the material to display when the requested material asset is missing.\n"
  114. "@ingroup GFX\n");
  115. smNoMaterialAssetFallback = StringTable->insert(Con::getVariable("$Core::NoMaterialAssetFallback"));
  116. }
  117. void MaterialAsset::initPersistFields()
  118. {
  119. docsURL;
  120. // Call parent.
  121. Parent::initPersistFields();
  122. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  123. //addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  124. // &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  125. addField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset), "");
  126. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  127. }
  128. void MaterialAsset::initializeAsset()
  129. {
  130. // Call parent.
  131. Parent::initializeAsset();
  132. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  133. if (mMatDefinitionName == StringTable->EmptyString())
  134. {
  135. mLoadedState = Failed;
  136. return;
  137. }
  138. if (size() != 0 && mScriptPath == StringTable->EmptyString())
  139. {
  140. mLoadedState = EmbeddedDefinition;
  141. }
  142. else if (Torque::FS::IsScriptFile(mScriptPath))
  143. {
  144. if (!Sim::findObject(mMatDefinitionName))
  145. {
  146. if (Con::executeFile(mScriptPath, false, false))
  147. {
  148. mLoadedState = ScriptLoaded;
  149. }
  150. else
  151. {
  152. mLoadedState = Failed;
  153. }
  154. }
  155. else
  156. {
  157. mLoadedState = DefinitionAlreadyExists;
  158. }
  159. }
  160. loadMaterial();
  161. }
  162. void MaterialAsset::onAssetRefresh()
  163. {
  164. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  165. if (mMatDefinitionName == StringTable->EmptyString())
  166. {
  167. mLoadedState = Failed;
  168. return;
  169. }
  170. if (Torque::FS::IsScriptFile(mScriptPath))
  171. {
  172. //Since we're refreshing, we can assume that the file we're executing WILL have an existing definition.
  173. //But that definition, whatever it is, is the 'correct' one, so we enable the Replace Existing behavior
  174. //when the engine encounters a named object conflict.
  175. String redefineBehaviorPrev = Con::getVariable("$Con::redefineBehavior");
  176. Con::setVariable("$Con::redefineBehavior", "replaceExisting");
  177. if (Con::executeFile(mScriptPath, false, false))
  178. mLoadedState = ScriptLoaded;
  179. else
  180. mLoadedState = Failed;
  181. //And now that we've executed, switch back to the prior behavior
  182. Con::setVariable("$Con::redefineBehavior", redefineBehaviorPrev.c_str());
  183. }
  184. loadMaterial();
  185. }
  186. void MaterialAsset::setScriptFile(const char* pScriptFile)
  187. {
  188. // Sanity!
  189. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  190. pScriptFile = StringTable->insert(pScriptFile, true);
  191. // Update.
  192. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  193. // Refresh the asset.
  194. refreshAsset();
  195. }
  196. //------------------------------------------------------------------------------
  197. void MaterialAsset::loadMaterial()
  198. {
  199. if (mMaterialDefinition)
  200. {
  201. mMaterialDefinition->safeDeleteObject();
  202. }
  203. if (mLoadedState == EmbeddedDefinition)
  204. {
  205. if (size() != 0)
  206. {
  207. for (U32 i = 0; i < size(); i++)
  208. {
  209. mMaterialDefinition = dynamic_cast<Material*>(getObject(i));
  210. if (mMaterialDefinition)
  211. {
  212. mLoadedState = Ok;
  213. mMaterialDefinition->setInternalName(getAssetId());
  214. mMaterialDefinition->reload();
  215. return;
  216. }
  217. }
  218. }
  219. }
  220. else if ((mLoadedState == ScriptLoaded || mLoadedState == DefinitionAlreadyExists) && mMatDefinitionName != StringTable->EmptyString())
  221. {
  222. Material* matDef;
  223. if (!Sim::findObject(mMatDefinitionName, matDef))
  224. {
  225. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  226. mLoadedState = BadFileReference;
  227. return;
  228. }
  229. mMaterialDefinition = matDef;
  230. mLoadedState = Ok;
  231. mMaterialDefinition->setInternalName(getAssetId());
  232. mMaterialDefinition->reload();
  233. return;
  234. }
  235. mLoadedState = Failed;
  236. }
  237. //------------------------------------------------------------------------------
  238. void MaterialAsset::copyTo(SimObject* object)
  239. {
  240. // Call to parent.
  241. Parent::copyTo(object);
  242. }
  243. //------------------------------------------------------------------------------
  244. U32 MaterialAsset::getAssetByMaterialName(StringTableEntry matName, AssetPtr<MaterialAsset>* matAsset)
  245. {
  246. AssetQuery query;
  247. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  248. if (foundAssetcount == 0)
  249. {
  250. //Didn't work, so have us fall back to a placeholder asset
  251. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  252. if (matAsset->isNull())
  253. {
  254. //Well that's bad, loading the fallback failed.
  255. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with material name %s failed with no fallback asset", matName);
  256. return AssetErrCode::Failed;
  257. }
  258. //handle noshape not being loaded itself
  259. if ((*matAsset)->mLoadedState == BadFileReference)
  260. {
  261. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, and fallback asset reported error of Bad File Reference.", matName);
  262. return AssetErrCode::BadFileReference;
  263. }
  264. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, utilizing fallback asset", matName);
  265. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  266. return AssetErrCode::UsingFallback;
  267. }
  268. else
  269. {
  270. for (U32 i = 0; i < foundAssetcount; i++)
  271. {
  272. MaterialAsset* tMatAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  273. if (tMatAsset && tMatAsset->getMaterialDefinitionName() == matName)
  274. {
  275. matAsset->setAssetId(query.mAssetList[i]);
  276. AssetDatabase.releaseAsset(query.mAssetList[i]);
  277. return (*matAsset)->mLoadedState;
  278. }
  279. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  280. }
  281. }
  282. //Somehow we failed to bind an asset, so just use the fallback and mark the failure
  283. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  284. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  285. return AssetErrCode::UsingFallback;
  286. }
  287. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  288. {
  289. if (matName == StringTable->EmptyString())
  290. return StringTable->EmptyString();
  291. StringTableEntry materialAssetId = MaterialAsset::smNoMaterialAssetFallback;
  292. AssetQuery query;
  293. U32 foundCount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  294. if (foundCount != 0)
  295. {
  296. for (U32 i = 0; i < foundCount && materialAssetId == MaterialAsset::smNoMaterialAssetFallback; i++)
  297. {
  298. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  299. if (matAsset)
  300. {
  301. if (matAsset->getMaterialDefinitionName() == matName)
  302. materialAssetId = matAsset->getAssetId();
  303. AssetDatabase.releaseAsset(query.mAssetList[i]);
  304. }
  305. }
  306. }
  307. return materialAssetId;
  308. }
  309. U32 MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  310. {
  311. (*materialAsset) = assetId;
  312. if (materialAsset->notNull())
  313. {
  314. return (*materialAsset)->mLoadedState;
  315. }
  316. else
  317. {
  318. //Didn't work, so have us fall back to a placeholder asset
  319. materialAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  320. if (materialAsset->isNull())
  321. {
  322. //Well that's bad, loading the fallback failed.
  323. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  324. return AssetErrCode::Failed;
  325. }
  326. //handle noshape not being loaded itself
  327. if ((*materialAsset)->mLoadedState == BadFileReference)
  328. {
  329. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  330. return AssetErrCode::BadFileReference;
  331. }
  332. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  333. (*materialAsset)->mLoadedState = AssetErrCode::UsingFallback;
  334. return AssetErrCode::UsingFallback;
  335. }
  336. }
  337. SimObjectPtr<Material> MaterialAsset::findMaterialDefinitionByAssetId(StringTableEntry assetId)
  338. {
  339. SimSet* matSet = MATMGR->getMaterialSet();
  340. if (matSet)
  341. {
  342. SimObjectPtr<Material> matDef = dynamic_cast<Material*>(matSet->findObjectByInternalName(assetId));
  343. return matDef;
  344. }
  345. return nullptr;
  346. }
  347. #ifdef TORQUE_TOOLS
  348. DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  349. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  350. "@return The AssetId of the associated asset, if any.")
  351. {
  352. return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  353. }
  354. //MaterialAsset::findMaterialDefinitionByAssetId("Prototyping:Detail")
  355. DefineEngineStaticMethod(MaterialAsset, findMaterialDefinitionByAssetId, S32, (const char* assetId), (""),
  356. "Queries the MaterialSet to see if any MaterialDefinition exists that is associated to the provided assetId.\n"
  357. "@return The MaterialDefinition Id associated to the assetId, if any")
  358. {
  359. SimObjectPtr<Material> matDef = MaterialAsset::findMaterialDefinitionByAssetId(StringTable->insert(assetId));
  360. if (matDef.isNull())
  361. return SimObjectId(0);
  362. else
  363. return matDef->getId();
  364. }
  365. DefineEngineMethod(MaterialAsset, getScriptPath, const char*, (), ,
  366. "Gets the script file path for the asset.")
  367. {
  368. return object->getScriptPath();
  369. }
  370. #endif
  371. #ifdef TORQUE_TOOLS
  372. //-----------------------------------------------------------------------------
  373. // GuiInspectorTypeAssetId
  374. //-----------------------------------------------------------------------------
  375. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  376. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  377. "@brief Inspector field type for Shapes\n\n"
  378. "Editor use only.\n\n"
  379. "@internal"
  380. );
  381. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  382. {
  383. Parent::consoleInit();
  384. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  385. }
  386. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  387. {
  388. // Create base filename edit controls
  389. GuiControl* retCtrl = Parent::constructEditControl();
  390. if (retCtrl == NULL)
  391. return retCtrl;
  392. // Change filespec
  393. char szBuffer[512];
  394. const char* previewImage;
  395. if (mInspector->getInspectObject() != nullptr)
  396. {
  397. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  398. mInspector->getIdString(), mCaption);
  399. mBrowseButton->setField("Command", szBuffer);
  400. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  401. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  402. }
  403. else
  404. {
  405. //if we don't have a target object, we'll be manipulating the desination value directly
  406. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
  407. mInspector->getIdString(), mVariableName);
  408. mBrowseButton->setField("Command", szBuffer);
  409. previewImage = Con::getVariable(mVariableName);
  410. }
  411. mLabel = new GuiTextCtrl();
  412. mLabel->registerObject();
  413. mLabel->setControlProfile(mProfile);
  414. mLabel->setText(mCaption);
  415. addObject(mLabel);
  416. //
  417. GuiTextEditCtrl* editTextCtrl = static_cast<GuiTextEditCtrl*>(retCtrl);
  418. GuiControlProfile* toolEditProfile;
  419. if (Sim::findObject("ToolsGuiTextEditProfile", toolEditProfile))
  420. editTextCtrl->setControlProfile(toolEditProfile);
  421. GuiControlProfile* toolDefaultProfile = nullptr;
  422. Sim::findObject("ToolsGuiDefaultProfile", toolDefaultProfile);
  423. //
  424. mPreviewImage = new GuiBitmapCtrl();
  425. mPreviewImage->registerObject();
  426. if(toolDefaultProfile)
  427. mPreviewImage->setControlProfile(toolDefaultProfile);
  428. updatePreviewImage();
  429. addObject(mPreviewImage);
  430. //
  431. mPreviewBorderButton = new GuiBitmapButtonCtrl();
  432. mPreviewBorderButton->registerObject();
  433. if(toolDefaultProfile)
  434. mPreviewBorderButton->setControlProfile(toolDefaultProfile);
  435. mPreviewBorderButton->_setBitmap(StringTable->insert("ToolsModule:cubemapBtnBorder_n_image"));
  436. mPreviewBorderButton->setField("Command", szBuffer); //clicking the preview does the same thing as the edit button, for simplicity
  437. addObject(mPreviewBorderButton);
  438. //
  439. // Create "Open in Editor" button
  440. mEditButton = new GuiBitmapButtonCtrl();
  441. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  442. mEditButton->setField("Command", szBuffer);
  443. mEditButton->setText("Edit");
  444. mEditButton->setSizing(horizResizeLeft, vertResizeAspectTop);
  445. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile");
  446. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  447. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  448. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Material Editor");
  449. mEditButton->registerObject();
  450. addObject(mEditButton);
  451. //
  452. mUseHeightOverride = true;
  453. mHeightOverride = 72;
  454. return retCtrl;
  455. }
  456. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  457. {
  458. S32 rowSize = 18;
  459. S32 dividerPos, dividerMargin;
  460. mInspector->getDivider(dividerPos, dividerMargin);
  461. Point2I fieldExtent = getExtent();
  462. Point2I fieldPos = getPosition();
  463. mEditCtrlRect.set(0, 0, fieldExtent.x, fieldExtent.y);
  464. mLabel->resize(Point2I(mProfile->mTextOffset.x, 0), Point2I(fieldExtent.x, rowSize));
  465. RectI previewRect = RectI(Point2I(mProfile->mTextOffset.x, rowSize), Point2I(50, 50));
  466. mPreviewBorderButton->resize(previewRect.point, previewRect.extent);
  467. mPreviewImage->resize(previewRect.point, previewRect.extent);
  468. S32 editPos = previewRect.point.x + previewRect.extent.x + 10;
  469. mEdit->resize(Point2I(editPos, rowSize * 1.5), Point2I(fieldExtent.x - editPos - 5, rowSize));
  470. mEditButton->resize(Point2I(fieldExtent.x - 105, previewRect.point.y + previewRect.extent.y - rowSize), Point2I(100, rowSize));
  471. mBrowseButton->setHidden(true);
  472. return true;
  473. }
  474. void GuiInspectorTypeMaterialAssetPtr::updateValue()
  475. {
  476. Parent::updateValue();
  477. updatePreviewImage();
  478. }
  479. void GuiInspectorTypeMaterialAssetPtr::updatePreviewImage()
  480. {
  481. const char* previewImage;
  482. if (mInspector->getInspectObject() != nullptr)
  483. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  484. else
  485. previewImage = Con::getVariable(mVariableName);
  486. //if what we're working with isn't even a valid asset, don't present like we found a good one
  487. if (!AssetDatabase.isDeclaredAsset(previewImage))
  488. {
  489. mPreviewImage->_setBitmap(StringTable->EmptyString());
  490. return;
  491. }
  492. String matPreviewAssetId = String(previewImage) + "_PreviewImage";
  493. matPreviewAssetId.replace(":", "_");
  494. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  495. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  496. {
  497. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  498. }
  499. else
  500. {
  501. if (AssetDatabase.isDeclaredAsset(previewImage))
  502. {
  503. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(previewImage);
  504. if (matAsset && matAsset->getMaterialDefinition())
  505. {
  506. mPreviewImage->_setBitmap(matAsset->getMaterialDefinition()->mDiffuseMapAssetId[0]);
  507. }
  508. }
  509. }
  510. if (mPreviewImage->getBitmapAsset().isNull())
  511. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  512. }
  513. void GuiInspectorTypeMaterialAssetPtr::setPreviewImage(StringTableEntry assetId)
  514. {
  515. //if what we're working with isn't even a valid asset, don't present like we found a good one
  516. if (!AssetDatabase.isDeclaredAsset(assetId))
  517. {
  518. mPreviewImage->_setBitmap(StringTable->EmptyString());
  519. return;
  520. }
  521. String matPreviewAssetId = String(assetId) + "_PreviewImage";
  522. matPreviewAssetId.replace(":", "_");
  523. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  524. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  525. {
  526. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  527. }
  528. else
  529. {
  530. if (AssetDatabase.isDeclaredAsset(assetId))
  531. {
  532. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(assetId);
  533. if (matAsset && matAsset->getMaterialDefinition())
  534. {
  535. mPreviewImage->_setBitmap(matAsset->getMaterialDefinition()->mDiffuseMapAssetId[0]);
  536. }
  537. }
  538. }
  539. if (mPreviewImage->getBitmapAsset().isNull())
  540. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  541. }
  542. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  543. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  544. "@brief Inspector field type for Material Assets\n\n"
  545. "Editor use only.\n\n"
  546. "@internal"
  547. );
  548. void GuiInspectorTypeMaterialAssetId::consoleInit()
  549. {
  550. Parent::consoleInit();
  551. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  552. }
  553. #endif