MaterialAsset.cpp 22 KB

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