MaterialAsset.cpp 22 KB

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