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; i++)
  297. {
  298. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  299. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  300. {
  301. materialAssetId = matAsset->getAssetId();
  302. AssetDatabase.releaseAsset(query.mAssetList[i]);
  303. break;
  304. }
  305. AssetDatabase.releaseAsset(query.mAssetList[i]);
  306. }
  307. }
  308. return materialAssetId;
  309. }
  310. U32 MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  311. {
  312. (*materialAsset) = assetId;
  313. if (materialAsset->notNull())
  314. {
  315. return (*materialAsset)->mLoadedState;
  316. }
  317. else
  318. {
  319. //Didn't work, so have us fall back to a placeholder asset
  320. materialAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  321. if (materialAsset->isNull())
  322. {
  323. //Well that's bad, loading the fallback failed.
  324. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  325. return AssetErrCode::Failed;
  326. }
  327. //handle noshape not being loaded itself
  328. if ((*materialAsset)->mLoadedState == BadFileReference)
  329. {
  330. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  331. return AssetErrCode::BadFileReference;
  332. }
  333. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  334. (*materialAsset)->mLoadedState = AssetErrCode::UsingFallback;
  335. return AssetErrCode::UsingFallback;
  336. }
  337. }
  338. SimObjectPtr<Material> MaterialAsset::findMaterialDefinitionByAssetId(StringTableEntry assetId)
  339. {
  340. SimSet* matSet = MATMGR->getMaterialSet();
  341. if (matSet)
  342. {
  343. SimObjectPtr<Material> matDef = dynamic_cast<Material*>(matSet->findObjectByInternalName(assetId));
  344. return matDef;
  345. }
  346. return nullptr;
  347. }
  348. #ifdef TORQUE_TOOLS
  349. DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  350. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  351. "@return The AssetId of the associated asset, if any.")
  352. {
  353. return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  354. }
  355. //MaterialAsset::findMaterialDefinitionByAssetId("Prototyping:Detail")
  356. DefineEngineStaticMethod(MaterialAsset, findMaterialDefinitionByAssetId, S32, (const char* assetId), (""),
  357. "Queries the MaterialSet to see if any MaterialDefinition exists that is associated to the provided assetId.\n"
  358. "@return The MaterialDefinition Id associated to the assetId, if any")
  359. {
  360. SimObjectPtr<Material> matDef = MaterialAsset::findMaterialDefinitionByAssetId(StringTable->insert(assetId));
  361. if (matDef.isNull())
  362. return SimObjectId(0);
  363. else
  364. return matDef->getId();
  365. }
  366. DefineEngineMethod(MaterialAsset, getScriptPath, const char*, (), ,
  367. "Gets the script file path for the asset.")
  368. {
  369. return object->getScriptPath();
  370. }
  371. #endif
  372. #ifdef TORQUE_TOOLS
  373. //-----------------------------------------------------------------------------
  374. // GuiInspectorTypeAssetId
  375. //-----------------------------------------------------------------------------
  376. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  377. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  378. "@brief Inspector field type for Shapes\n\n"
  379. "Editor use only.\n\n"
  380. "@internal"
  381. );
  382. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  383. {
  384. Parent::consoleInit();
  385. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  386. }
  387. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  388. {
  389. // Create base filename edit controls
  390. GuiControl* retCtrl = Parent::constructEditControl();
  391. if (retCtrl == NULL)
  392. return retCtrl;
  393. // Change filespec
  394. char szBuffer[512];
  395. const char* previewImage;
  396. if (mInspector->getInspectObject() != nullptr)
  397. {
  398. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  399. mInspector->getIdString(), mCaption);
  400. mBrowseButton->setField("Command", szBuffer);
  401. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  402. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  403. }
  404. else
  405. {
  406. //if we don't have a target object, we'll be manipulating the desination value directly
  407. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
  408. mInspector->getIdString(), mVariableName);
  409. mBrowseButton->setField("Command", szBuffer);
  410. previewImage = Con::getVariable(mVariableName);
  411. }
  412. mLabel = new GuiTextCtrl();
  413. mLabel->registerObject();
  414. mLabel->setControlProfile(mProfile);
  415. mLabel->setText(mCaption);
  416. addObject(mLabel);
  417. //
  418. GuiTextEditCtrl* editTextCtrl = static_cast<GuiTextEditCtrl*>(retCtrl);
  419. GuiControlProfile* toolEditProfile;
  420. if (Sim::findObject("ToolsGuiTextEditProfile", toolEditProfile))
  421. editTextCtrl->setControlProfile(toolEditProfile);
  422. GuiControlProfile* toolDefaultProfile = nullptr;
  423. Sim::findObject("ToolsGuiDefaultProfile", toolDefaultProfile);
  424. //
  425. mPreviewImage = new GuiBitmapCtrl();
  426. mPreviewImage->registerObject();
  427. if(toolDefaultProfile)
  428. mPreviewImage->setControlProfile(toolDefaultProfile);
  429. updatePreviewImage();
  430. addObject(mPreviewImage);
  431. //
  432. mPreviewBorderButton = new GuiBitmapButtonCtrl();
  433. mPreviewBorderButton->registerObject();
  434. if(toolDefaultProfile)
  435. mPreviewBorderButton->setControlProfile(toolDefaultProfile);
  436. mPreviewBorderButton->_setBitmap(StringTable->insert("ToolsModule:cubemapBtnBorder_n_image"));
  437. mPreviewBorderButton->setField("Command", szBuffer); //clicking the preview does the same thing as the edit button, for simplicity
  438. addObject(mPreviewBorderButton);
  439. //
  440. // Create "Open in Editor" button
  441. mEditButton = new GuiBitmapButtonCtrl();
  442. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  443. mEditButton->setField("Command", szBuffer);
  444. mEditButton->setText("Edit");
  445. mEditButton->setSizing(horizResizeLeft, vertResizeAspectTop);
  446. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile");
  447. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  448. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  449. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Material Editor");
  450. mEditButton->registerObject();
  451. addObject(mEditButton);
  452. //
  453. mUseHeightOverride = true;
  454. mHeightOverride = 72;
  455. return retCtrl;
  456. }
  457. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  458. {
  459. S32 rowSize = 18;
  460. S32 dividerPos, dividerMargin;
  461. mInspector->getDivider(dividerPos, dividerMargin);
  462. Point2I fieldExtent = getExtent();
  463. Point2I fieldPos = getPosition();
  464. mEditCtrlRect.set(0, 0, fieldExtent.x, fieldExtent.y);
  465. mLabel->resize(Point2I(mProfile->mTextOffset.x, 0), Point2I(fieldExtent.x, rowSize));
  466. RectI previewRect = RectI(Point2I(mProfile->mTextOffset.x, rowSize), Point2I(50, 50));
  467. mPreviewBorderButton->resize(previewRect.point, previewRect.extent);
  468. mPreviewImage->resize(previewRect.point, previewRect.extent);
  469. S32 editPos = previewRect.point.x + previewRect.extent.x + 10;
  470. mEdit->resize(Point2I(editPos, rowSize * 1.5), Point2I(fieldExtent.x - editPos - 5, rowSize));
  471. mEditButton->resize(Point2I(fieldExtent.x - 105, previewRect.point.y + previewRect.extent.y - rowSize), Point2I(100, rowSize));
  472. mBrowseButton->setHidden(true);
  473. return true;
  474. }
  475. void GuiInspectorTypeMaterialAssetPtr::updateValue()
  476. {
  477. Parent::updateValue();
  478. updatePreviewImage();
  479. }
  480. void GuiInspectorTypeMaterialAssetPtr::updatePreviewImage()
  481. {
  482. const char* previewImage;
  483. if (mInspector->getInspectObject() != nullptr)
  484. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  485. else
  486. previewImage = Con::getVariable(mVariableName);
  487. //if what we're working with isn't even a valid asset, don't present like we found a good one
  488. if (!AssetDatabase.isDeclaredAsset(previewImage))
  489. {
  490. mPreviewImage->_setBitmap(StringTable->EmptyString());
  491. return;
  492. }
  493. String matPreviewAssetId = String(previewImage) + "_PreviewImage";
  494. matPreviewAssetId.replace(":", "_");
  495. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  496. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  497. {
  498. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  499. }
  500. else
  501. {
  502. if (AssetDatabase.isDeclaredAsset(previewImage))
  503. {
  504. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(previewImage);
  505. if (matAsset && matAsset->getMaterialDefinition())
  506. {
  507. mPreviewImage->_setBitmap(matAsset->getMaterialDefinition()->mDiffuseMapAssetId[0]);
  508. }
  509. }
  510. }
  511. if (mPreviewImage->getBitmapAsset().isNull())
  512. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  513. }
  514. void GuiInspectorTypeMaterialAssetPtr::setPreviewImage(StringTableEntry assetId)
  515. {
  516. //if what we're working with isn't even a valid asset, don't present like we found a good one
  517. if (!AssetDatabase.isDeclaredAsset(assetId))
  518. {
  519. mPreviewImage->_setBitmap(StringTable->EmptyString());
  520. return;
  521. }
  522. String matPreviewAssetId = String(assetId) + "_PreviewImage";
  523. matPreviewAssetId.replace(":", "_");
  524. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  525. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  526. {
  527. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  528. }
  529. else
  530. {
  531. if (AssetDatabase.isDeclaredAsset(assetId))
  532. {
  533. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(assetId);
  534. if (matAsset && matAsset->getMaterialDefinition())
  535. {
  536. mPreviewImage->_setBitmap(matAsset->getMaterialDefinition()->mDiffuseMapAssetId[0]);
  537. }
  538. }
  539. }
  540. if (mPreviewImage->getBitmapAsset().isNull())
  541. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  542. }
  543. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  544. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  545. "@brief Inspector field type for Material Assets\n\n"
  546. "Editor use only.\n\n"
  547. "@internal"
  548. );
  549. void GuiInspectorTypeMaterialAssetId::consoleInit()
  550. {
  551. Parent::consoleInit();
  552. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  553. }
  554. #endif