MaterialAsset.cpp 22 KB

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