ImageAsset.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 IMAGE_ASSET_H
  23. #include "ImageAsset.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 "gfx/gfxStringEnumTranslate.h"
  38. #include "ImageAssetInspectors.h"
  39. // Debug Profiling.
  40. #include "platform/profiler.h"
  41. #include "T3D/assets/assetImporter.h"
  42. #include "gfx/gfxDrawUtil.h"
  43. //-----------------------------------------------------------------------------
  44. StringTableEntry ImageAsset::smNoImageAssetFallback = NULL;
  45. //-----------------------------------------------------------------------------
  46. IMPLEMENT_CONOBJECT(ImageAsset);
  47. ConsoleType(ImageAssetPtr, TypeImageAssetPtr, const char*, "")
  48. //-----------------------------------------------------------------------------
  49. ConsoleGetType(TypeImageAssetPtr)
  50. {
  51. // Fetch asset Id.
  52. return *((const char**)(dptr));
  53. }
  54. //-----------------------------------------------------------------------------
  55. ConsoleSetType(TypeImageAssetPtr)
  56. {
  57. // Was a single argument specified?
  58. if (argc == 1)
  59. {
  60. // Yes, so fetch field value.
  61. *((const char**)dptr) = StringTable->insert(argv[0]);
  62. return;
  63. }
  64. // Warn.
  65. Con::warnf("(TypeImageAssetPtr) - Cannot set multiple args to a single asset.");
  66. }
  67. ConsoleType(assetIdString, TypeImageAssetId, const char*, "")
  68. ConsoleGetType(TypeImageAssetId)
  69. {
  70. // Fetch asset Id.
  71. return *((const char**)(dptr));
  72. }
  73. ConsoleSetType(TypeImageAssetId)
  74. {
  75. // Was a single argument specified?
  76. if (argc == 1)
  77. {
  78. *((const char**)dptr) = StringTable->insert(argv[0]);
  79. return;
  80. }
  81. // Warn.
  82. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  83. }
  84. //-----------------------------------------------------------------------------
  85. ImplementEnumType(ImageAssetType,
  86. "Type of mesh data available in a shape.\n"
  87. "@ingroup gameObjects")
  88. { ImageAsset::Albedo, "Albedo", "" },
  89. { ImageAsset::Normal, "Normal", "" },
  90. { ImageAsset::ORMConfig, "ORMConfig", "" },
  91. { ImageAsset::GUI, "GUI", "" },
  92. { ImageAsset::Roughness, "Roughness", "" },
  93. { ImageAsset::AO, "AO", "" },
  94. { ImageAsset::Metalness, "Metalness", "" },
  95. { ImageAsset::Glow, "Glow", "" },
  96. { ImageAsset::Particle, "Particle", "" },
  97. { ImageAsset::Decal, "Decal", "" },
  98. { ImageAsset::Cubemap, "Cubemap", "" },
  99. EndImplementEnumType;
  100. //-----------------------------------------------------------------------------
  101. ImageAsset::ImageAsset() : AssetBase(), mUseMips(true), mIsHDRImage(false), mIsValidImage(false), mImageType(Albedo)
  102. {
  103. mImageFileName = StringTable->EmptyString();
  104. mImagePath = StringTable->EmptyString();
  105. mLoadedState = AssetErrCode::NotLoaded;
  106. }
  107. //-----------------------------------------------------------------------------
  108. ImageAsset::~ImageAsset()
  109. {
  110. }
  111. void ImageAsset::consoleInit()
  112. {
  113. Parent::consoleInit();
  114. Con::addVariable("$Core::NoImageAssetFallback", TypeString, &smNoImageAssetFallback,
  115. "The assetId of the texture to display when the requested image asset is missing.\n"
  116. "@ingroup GFX\n");
  117. smNoImageAssetFallback = StringTable->insert(Con::getVariable("$Core::NoImageAssetFallback"));
  118. }
  119. //-----------------------------------------------------------------------------
  120. void ImageAsset::initPersistFields()
  121. {
  122. docsURL;
  123. // Call parent.
  124. Parent::initPersistFields();
  125. addProtectedField("imageFile", TypeAssetLooseFilePath, Offset(mImageFileName, ImageAsset),
  126. &setImageFileName, &getImageFileName, "Path to the image file.");
  127. addField("useMips", TypeBool, Offset(mUseMips, ImageAsset), "Should the image use mips? (Currently unused).");
  128. addField("isHDRImage", TypeBool, Offset(mIsHDRImage, ImageAsset), "Is the image in an HDR format? (Currently unused)");
  129. addField("imageType", TypeImageAssetType, Offset(mImageType, ImageAsset), "What the main use-case for the image is for.");
  130. }
  131. //------------------------------------------------------------------------------
  132. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  133. U32 ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset)
  134. {
  135. AssetQuery query;
  136. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  137. if (foundAssetcount == 0)
  138. {
  139. //Didn't work, so have us fall back to a placeholder asset
  140. imageAsset->setAssetId(ImageAsset::smNoImageAssetFallback);
  141. if (imageAsset->isNull())
  142. {
  143. //Well that's bad, loading the fallback failed.
  144. Con::warnf("ImageAsset::getAssetByFilename - Finding of asset associated with file %s failed with no fallback asset", fileName);
  145. return AssetErrCode::Failed;
  146. }
  147. //handle noshape not being loaded itself
  148. if ((*imageAsset)->mLoadedState == BadFileReference)
  149. {
  150. Con::warnf("ImageAsset::getAssetByFilename - Finding of associated with file %s failed, and fallback asset reported error of Bad File Reference.", fileName);
  151. return AssetErrCode::BadFileReference;
  152. }
  153. Con::warnf("ImageAsset::getAssetByFilename - Finding of associated with file %s failed, utilizing fallback asset", fileName);
  154. (*imageAsset)->mLoadedState = AssetErrCode::UsingFallback;
  155. return AssetErrCode::UsingFallback;
  156. }
  157. else
  158. {
  159. //acquire and bind the asset, and return it out
  160. imageAsset->setAssetId(query.mAssetList[0]);
  161. return (*imageAsset)->mLoadedState;
  162. }
  163. }
  164. StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
  165. {
  166. if (fileName == StringTable->EmptyString())
  167. return StringTable->EmptyString();
  168. StringTableEntry imageAssetId = ImageAsset::smNoImageAssetFallback;
  169. AssetQuery query;
  170. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  171. if (foundAssetcount != 0)
  172. {
  173. //acquire and bind the asset, and return it out
  174. imageAssetId = query.mAssetList[0];
  175. }
  176. else
  177. {
  178. AssetPtr<ImageAsset> imageAsset = imageAssetId; //ensures the fallback is loaded
  179. }
  180. return imageAssetId;
  181. }
  182. U32 ImageAsset::getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset)
  183. {
  184. (*imageAsset) = assetId;
  185. if (imageAsset->notNull())
  186. {
  187. return (*imageAsset)->mLoadedState;
  188. }
  189. else
  190. {
  191. if (imageAsset->isNull())
  192. {
  193. //Well that's bad, loading the fallback failed.
  194. Con::warnf("ImageAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  195. return AssetErrCode::Failed;
  196. }
  197. //handle noshape not being loaded itself
  198. if ((*imageAsset)->mLoadedState == BadFileReference)
  199. {
  200. Con::warnf("ImageAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  201. return AssetErrCode::BadFileReference;
  202. }
  203. Con::warnf("ImageAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  204. (*imageAsset)->mLoadedState = AssetErrCode::UsingFallback;
  205. return AssetErrCode::UsingFallback;
  206. }
  207. }
  208. //------------------------------------------------------------------------------
  209. void ImageAsset::copyTo(SimObject* object)
  210. {
  211. // Call to parent.
  212. Parent::copyTo(object);
  213. }
  214. void ImageAsset::loadImage()
  215. {
  216. if (mImagePath)
  217. {
  218. if (!Torque::FS::IsFile(mImagePath))
  219. {
  220. Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
  221. mLoadedState = BadFileReference;
  222. return;
  223. }
  224. mLoadedState = Ok;
  225. mIsValidImage = true;
  226. mChangeSignal.trigger();
  227. return;
  228. }
  229. mLoadedState = BadFileReference;
  230. mIsValidImage = false;
  231. }
  232. void ImageAsset::initializeAsset()
  233. {
  234. ResourceManager::get().getChangedSignal().notify(this, &ImageAsset::_onResourceChanged);
  235. mImagePath = getOwned() ? expandAssetFilePath(mImageFileName) : mImagePath;
  236. loadImage();
  237. }
  238. void ImageAsset::onAssetRefresh()
  239. {
  240. mImagePath = getOwned() ? expandAssetFilePath(mImageFileName) : mImagePath;
  241. loadImage();
  242. }
  243. void ImageAsset::_onResourceChanged(const Torque::Path& path)
  244. {
  245. if (path != Torque::Path(mImagePath))
  246. return;
  247. refreshAsset();
  248. //loadImage();
  249. }
  250. void ImageAsset::setImageFileName(const char* pScriptFile)
  251. {
  252. // Sanity!
  253. AssertFatal(pScriptFile != NULL, "Cannot use a NULL image file.");
  254. // Update.
  255. mImageFileName = StringTable->insert(pScriptFile, true);
  256. // Refresh the asset.
  257. refreshAsset();
  258. }
  259. const GBitmap& ImageAsset::getImage()
  260. {
  261. return GBitmap(); //TODO fix this
  262. }
  263. GFXTexHandle ImageAsset::getTexture(GFXTextureProfile* requestedProfile)
  264. {
  265. if (mResourceMap.contains(requestedProfile))
  266. {
  267. mLoadedState = Ok;
  268. return mResourceMap.find(requestedProfile)->value;
  269. }
  270. else
  271. {
  272. //If we don't have an existing map case to the requested format, we'll just create it and insert it in
  273. GFXTexHandle newTex = TEXMGR->createTexture(mImagePath, requestedProfile);
  274. if (newTex)
  275. {
  276. mResourceMap.insert(requestedProfile, newTex);
  277. mLoadedState = Ok;
  278. return newTex;
  279. }
  280. else
  281. mLoadedState = BadFileReference;
  282. }
  283. //if (mTexture.isValid())
  284. // return mTexture;
  285. return nullptr;
  286. }
  287. const char* ImageAsset::getImageInfo()
  288. {
  289. if (mIsValidImage)
  290. {
  291. static const U32 bufSize = 2048;
  292. char* returnBuffer = Con::getReturnBuffer(bufSize);
  293. GFXTexHandle newTex = TEXMGR->createTexture(mImagePath, &GFXStaticTextureSRGBProfile);
  294. if (newTex)
  295. {
  296. dSprintf(returnBuffer, bufSize, "%s %d %d %d", GFXStringTextureFormat[newTex->getFormat()], newTex->getHeight(), newTex->getWidth(), newTex->getDepth());
  297. newTex = nullptr;
  298. }
  299. else
  300. {
  301. dSprintf(returnBuffer, bufSize, "ImageAsset::getImageInfo() - Failed to get image info for %s", getAssetId());
  302. }
  303. return returnBuffer;
  304. }
  305. return "";
  306. }
  307. const char* ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes type)
  308. {
  309. // must match ImageTypes order
  310. static const char* _names[] = {
  311. "Albedo",
  312. "Normal",
  313. "ORMConfig",
  314. "GUI",
  315. "Roughness",
  316. "AO",
  317. "Metalness",
  318. "Glow",
  319. "Particle",
  320. "Decal",
  321. "Cubemap"
  322. };
  323. if (type < 0 || type >= ImageTypeCount)
  324. {
  325. Con::errorf("ImageAsset::getAdapterNameFromType - Invalid ImageType, defaulting to Albedo");
  326. return _names[Albedo];
  327. }
  328. return _names[type];
  329. }
  330. ImageAsset::ImageTypes ImageAsset::getImageTypeFromName(const char* name)
  331. {
  332. if (dStrIsEmpty(name))
  333. {
  334. return (ImageTypes)Albedo;
  335. }
  336. S32 ret = -1;
  337. for (S32 i = 0; i < ImageTypeCount; i++)
  338. {
  339. if (!dStricmp(getImageTypeNameFromType((ImageTypes)i), name))
  340. ret = i;
  341. }
  342. if (ret == -1)
  343. {
  344. Con::errorf("ImageAsset::getImageTypeFromName - Invalid ImageType name, defaulting to Albedo");
  345. ret = Albedo;
  346. }
  347. return (ImageTypes)ret;
  348. }
  349. DefineEngineMethod(ImageAsset, getImagePath, const char*, (), ,
  350. "Gets the image filepath of this asset.\n"
  351. "@return File path of the image file.")
  352. {
  353. return object->getImagePath();
  354. }
  355. DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
  356. "Gets the info and properties of the image.\n"
  357. "@return The info/properties of the image.")
  358. {
  359. return object->getImageInfo();
  360. }
  361. #ifdef TORQUE_TOOLS
  362. DefineEngineStaticMethod(ImageAsset, getAssetIdByFilename, const char*, (const char* filePath), (""),
  363. "Queries the Asset Database to see if any asset exists that is associated with the provided file path.\n"
  364. "@return The AssetId of the associated asset, if any.")
  365. {
  366. return ImageAsset::getAssetIdByFilename(StringTable->insert(filePath));
  367. }
  368. //-----------------------------------------------------------------------------
  369. // GuiInspectorTypeAssetId
  370. //-----------------------------------------------------------------------------
  371. IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetPtr);
  372. ConsoleDocClass(GuiInspectorTypeImageAssetPtr,
  373. "@brief Inspector field type for Shapes\n\n"
  374. "Editor use only.\n\n"
  375. "@internal"
  376. );
  377. void GuiInspectorTypeImageAssetPtr::consoleInit()
  378. {
  379. Parent::consoleInit();
  380. ConsoleBaseType::getType(TypeImageAssetPtr)->setInspectorFieldType("GuiInspectorTypeImageAssetPtr");
  381. }
  382. GuiControl* GuiInspectorTypeImageAssetPtr::constructEditControl()
  383. {
  384. // Create base filename edit controls
  385. GuiControl* retCtrl = Parent::constructEditControl();
  386. if (retCtrl == NULL)
  387. return retCtrl;
  388. retCtrl->getRenderTooltipDelegate().bind(this, &GuiInspectorTypeImageAssetPtr::renderTooltip);
  389. // Change filespec
  390. char szBuffer[512];
  391. const char* previewImage;
  392. if (mInspector->getInspectObject() != nullptr)
  393. {
  394. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ImageAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  395. mInspector->getIdString(), mCaption);
  396. mBrowseButton->setField("Command", szBuffer);
  397. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  398. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  399. }
  400. else
  401. {
  402. //if we don't have a target object, we'll be manipulating the desination value directly
  403. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ImageAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
  404. mInspector->getIdString(), mVariableName);
  405. mBrowseButton->setField("Command", szBuffer);
  406. previewImage = Con::getVariable(mVariableName);
  407. }
  408. mLabel = new GuiTextCtrl();
  409. mLabel->registerObject();
  410. mLabel->setControlProfile(mProfile);
  411. mLabel->setText(mCaption);
  412. addObject(mLabel);
  413. //
  414. GuiTextEditCtrl* editTextCtrl = static_cast<GuiTextEditCtrl*>(retCtrl);
  415. GuiControlProfile* toolEditProfile;
  416. if (Sim::findObject("ToolsGuiTextEditProfile", toolEditProfile))
  417. editTextCtrl->setControlProfile(toolEditProfile);
  418. GuiControlProfile* toolDefaultProfile = nullptr;
  419. Sim::findObject("ToolsGuiDefaultProfile", toolDefaultProfile);
  420. //
  421. mPreviewImage = new GuiBitmapCtrl();
  422. mPreviewImage->registerObject();
  423. if (toolDefaultProfile)
  424. mPreviewImage->setControlProfile(toolDefaultProfile);
  425. updatePreviewImage();
  426. addObject(mPreviewImage);
  427. //
  428. mPreviewBorderButton = new GuiBitmapButtonCtrl();
  429. mPreviewBorderButton->registerObject();
  430. if (toolDefaultProfile)
  431. mPreviewBorderButton->setControlProfile(toolDefaultProfile);
  432. mPreviewBorderButton->_setBitmap(StringTable->insert("ToolsModule:cubemapBtnBorder_n_image"));
  433. mPreviewBorderButton->setField("Command", szBuffer); //clicking the preview does the same thing as the edit button, for simplicity
  434. addObject(mPreviewBorderButton);
  435. //
  436. // Create "Open in Editor" button
  437. /*mEditButton = new GuiBitmapButtonCtrl();
  438. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  439. mEditButton->setField("Command", szBuffer);
  440. mEditButton->setText("Edit");
  441. mEditButton->setSizing(horizResizeLeft, vertResizeAspectTop);
  442. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile");
  443. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  444. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  445. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Image Editor");
  446. mEditButton->registerObject();
  447. addObject(mEditButton);*/
  448. //
  449. mUseHeightOverride = true;
  450. mHeightOverride = 72;
  451. return retCtrl;
  452. }
  453. bool GuiInspectorTypeImageAssetPtr::updateRects()
  454. {
  455. S32 rowSize = 18;
  456. S32 dividerPos, dividerMargin;
  457. mInspector->getDivider(dividerPos, dividerMargin);
  458. Point2I fieldExtent = getExtent();
  459. Point2I fieldPos = getPosition();
  460. mEditCtrlRect.set(0, 0, fieldExtent.x, fieldExtent.y);
  461. mLabel->resize(Point2I(mProfile->mTextOffset.x, 0), Point2I(fieldExtent.x, rowSize));
  462. RectI previewRect = RectI(Point2I(mProfile->mTextOffset.x, rowSize), Point2I(50, 50));
  463. mPreviewBorderButton->resize(previewRect.point, previewRect.extent);
  464. mPreviewImage->resize(previewRect.point, previewRect.extent);
  465. S32 editPos = previewRect.point.x + previewRect.extent.x + 10;
  466. mEdit->resize(Point2I(editPos, rowSize * 1.5), Point2I(fieldExtent.x - editPos - 5, rowSize));
  467. //mEditButton->resize(Point2I(fieldExtent.x - 105, previewRect.point.y + previewRect.extent.y - rowSize), Point2I(100, rowSize));
  468. mBrowseButton->setHidden(true);
  469. return true;
  470. }
  471. bool GuiInspectorTypeImageAssetPtr::renderTooltip(const Point2I& hoverPos, const Point2I& cursorPos, const char* tipText)
  472. {
  473. if (!mAwake)
  474. return false;
  475. GuiCanvas* root = getRoot();
  476. if (!root)
  477. return false;
  478. AssetPtr<ImageAsset> imgAsset;
  479. U32 assetState = ImageAsset::getAssetById(getData(), &imgAsset);
  480. if (imgAsset == NULL || assetState == ImageAsset::Failed)
  481. return false;
  482. StringTableEntry filename = imgAsset->getImagePath();
  483. if (!filename || !filename[0])
  484. return false;
  485. StringTableEntry previewFilename = filename;
  486. if (Con::isFunction("getAssetPreviewImage"))
  487. {
  488. ConsoleValue consoleRet = Con::executef("getAssetPreviewImage", filename);
  489. previewFilename = StringTable->insert(consoleRet.getString());
  490. if (AssetDatabase.isDeclaredAsset(previewFilename))
  491. {
  492. ImageAsset* previewAsset = AssetDatabase.acquireAsset<ImageAsset>(previewFilename);
  493. previewFilename = previewAsset->getImagePath();
  494. }
  495. }
  496. GFXTexHandle texture(previewFilename, &GFXStaticTextureSRGBProfile, avar("%s() - tooltip texture (line %d)", __FUNCTION__, __LINE__));
  497. if (texture.isNull())
  498. return false;
  499. // Render image at a reasonable screen size while
  500. // keeping its aspect ratio...
  501. Point2I screensize = getRoot()->getWindowSize();
  502. Point2I offset = hoverPos;
  503. Point2I tipBounds;
  504. U32 texWidth = texture.getWidth();
  505. U32 texHeight = texture.getHeight();
  506. F32 aspect = (F32)texHeight / (F32)texWidth;
  507. const F32 newWidth = 150.0f;
  508. F32 newHeight = aspect * newWidth;
  509. // Offset below cursor image
  510. offset.y += 20; // TODO: Attempt to fix?: root->getCursorExtent().y;
  511. tipBounds.x = newWidth;
  512. tipBounds.y = newHeight;
  513. // Make sure all of the tooltip will be rendered width the app window,
  514. // 5 is given as a buffer against the edge
  515. if (screensize.x < offset.x + tipBounds.x + 5)
  516. offset.x = screensize.x - tipBounds.x - 5;
  517. if (screensize.y < offset.y + tipBounds.y + 5)
  518. offset.y = hoverPos.y - tipBounds.y - 5;
  519. RectI oldClip = GFX->getClipRect();
  520. RectI rect(offset, tipBounds);
  521. GFX->setClipRect(rect);
  522. GFXDrawUtil* drawer = GFX->getDrawUtil();
  523. drawer->clearBitmapModulation();
  524. GFX->getDrawUtil()->drawBitmapStretch(texture, rect);
  525. GFX->setClipRect(oldClip);
  526. return true;
  527. }
  528. void GuiInspectorTypeImageAssetPtr::updateValue()
  529. {
  530. Parent::updateValue();
  531. updatePreviewImage();
  532. }
  533. void GuiInspectorTypeImageAssetPtr::updatePreviewImage()
  534. {
  535. const char* previewImage;
  536. if (mInspector->getInspectObject() != nullptr)
  537. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  538. else
  539. previewImage = Con::getVariable(mVariableName);
  540. //if what we're working with isn't even a valid asset, don't present like we found a good one
  541. if (!AssetDatabase.isDeclaredAsset(previewImage))
  542. {
  543. mPreviewImage->_setBitmap(StringTable->EmptyString());
  544. return;
  545. }
  546. String imgPreviewAssetId = String(previewImage) + "_PreviewImage";
  547. imgPreviewAssetId.replace(":", "_");
  548. imgPreviewAssetId = "ToolsModule:" + imgPreviewAssetId;
  549. if (AssetDatabase.isDeclaredAsset(imgPreviewAssetId.c_str()))
  550. {
  551. mPreviewImage->setBitmap(StringTable->insert(imgPreviewAssetId.c_str()));
  552. }
  553. else
  554. {
  555. if (AssetDatabase.isDeclaredAsset(previewImage))
  556. {
  557. ImageAsset* imgAsset = AssetDatabase.acquireAsset<ImageAsset>(previewImage);
  558. if (imgAsset && imgAsset->isAssetValid())
  559. {
  560. mPreviewImage->_setBitmap(imgAsset->getAssetId());
  561. }
  562. }
  563. }
  564. if (mPreviewImage->getBitmapAsset().isNull())
  565. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  566. }
  567. void GuiInspectorTypeImageAssetPtr::setPreviewImage(StringTableEntry assetId)
  568. {
  569. //if what we're working with isn't even a valid asset, don't present like we found a good one
  570. if (!AssetDatabase.isDeclaredAsset(assetId))
  571. {
  572. mPreviewImage->_setBitmap(StringTable->EmptyString());
  573. return;
  574. }
  575. String imgPreviewAssetId = String(assetId) + "_PreviewImage";
  576. imgPreviewAssetId.replace(":", "_");
  577. imgPreviewAssetId = "ToolsModule:" + imgPreviewAssetId;
  578. if (AssetDatabase.isDeclaredAsset(imgPreviewAssetId.c_str()))
  579. {
  580. mPreviewImage->setBitmap(StringTable->insert(imgPreviewAssetId.c_str()));
  581. }
  582. else
  583. {
  584. if (AssetDatabase.isDeclaredAsset(assetId))
  585. {
  586. ImageAsset* imgAsset = AssetDatabase.acquireAsset<ImageAsset>(assetId);
  587. if (imgAsset && imgAsset->isAssetValid())
  588. {
  589. mPreviewImage->_setBitmap(imgAsset->getAssetId());
  590. }
  591. }
  592. }
  593. if (mPreviewImage->getBitmapAsset().isNull())
  594. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  595. }
  596. IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetId);
  597. ConsoleDocClass(GuiInspectorTypeImageAssetId,
  598. "@brief Inspector field type for Shapes\n\n"
  599. "Editor use only.\n\n"
  600. "@internal"
  601. );
  602. void GuiInspectorTypeImageAssetId::consoleInit()
  603. {
  604. Parent::consoleInit();
  605. ConsoleBaseType::getType(TypeImageAssetId)->setInspectorFieldType("GuiInspectorTypeImageAssetId");
  606. }
  607. #endif