ImageAsset.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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. // Debug Profiling.
  39. #include "platform/profiler.h"
  40. #include "T3D/assets/assetImporter.h"
  41. //-----------------------------------------------------------------------------
  42. IMPLEMENT_CONOBJECT(ImageAsset);
  43. ConsoleType(ImageAssetPtr, TypeImageAssetPtr, String, ASSET_ID_FIELD_PREFIX)
  44. //-----------------------------------------------------------------------------
  45. ConsoleGetType(TypeImageAssetPtr)
  46. {
  47. // Fetch asset Id.
  48. return *((StringTableEntry*)dptr);
  49. }
  50. //-----------------------------------------------------------------------------
  51. ConsoleSetType(TypeImageAssetPtr)
  52. {
  53. // Was a single argument specified?
  54. if (argc == 1)
  55. {
  56. // Yes, so fetch field value.
  57. const char* pFieldValue = argv[0];
  58. // Fetch asset Id.
  59. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  60. // Update asset value.
  61. *assetId = StringTable->insert(pFieldValue);
  62. return;
  63. }
  64. // Warn.
  65. Con::warnf("(TypeImageAssetPtr) - Cannot set multiple args to a single asset.");
  66. }
  67. ConsoleType(assetIdString, TypeImageAssetId, String, ASSET_ID_FIELD_PREFIX)
  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. // Yes, so fetch field value.
  79. const char* pFieldValue = argv[0];
  80. // Fetch asset Id.
  81. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  82. // Update asset value.
  83. *assetId = StringTable->insert(pFieldValue);
  84. return;
  85. }
  86. // Warn.
  87. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  88. }
  89. //-----------------------------------------------------------------------------
  90. ImplementEnumType(ImageAssetType,
  91. "Type of mesh data available in a shape.\n"
  92. "@ingroup gameObjects")
  93. { ImageAsset::Albedo, "Albedo", "" },
  94. { ImageAsset::Normal, "Normal", "" },
  95. { ImageAsset::ORMConfig, "ORMConfig", "" },
  96. { ImageAsset::GUI, "GUI", "" },
  97. { ImageAsset::Roughness, "Roughness", "" },
  98. { ImageAsset::AO, "AO", "" },
  99. { ImageAsset::Metalness, "Metalness", "" },
  100. { ImageAsset::Glow, "Glow", "" },
  101. { ImageAsset::Particle, "Particle", "" },
  102. { ImageAsset::Decal, "Decal", "" },
  103. { ImageAsset::Cubemap, "Cubemap", "" },
  104. EndImplementEnumType;
  105. //-----------------------------------------------------------------------------
  106. ImageAsset::ImageAsset() : AssetBase(), mImage(nullptr), mUseMips(true), mIsHDRImage(false), mIsValidImage(false), mImageType(Albedo)
  107. {
  108. mImageFileName = StringTable->EmptyString();
  109. mImagePath = StringTable->EmptyString();
  110. }
  111. //-----------------------------------------------------------------------------
  112. ImageAsset::~ImageAsset()
  113. {
  114. }
  115. //-----------------------------------------------------------------------------
  116. void ImageAsset::initPersistFields()
  117. {
  118. // Call parent.
  119. Parent::initPersistFields();
  120. addProtectedField("imageFile", TypeAssetLooseFilePath, Offset(mImageFileName, ImageAsset),
  121. &setImageFileName, &getImageFileName, "Path to the image file.");
  122. addField("useMips", TypeBool, Offset(mUseMips, ImageAsset), "Should the image use mips? (Currently unused).");
  123. addField("isHDRImage", TypeBool, Offset(mIsHDRImage, ImageAsset), "Is the image in an HDR format? (Currently unused)");
  124. addField("imageType", TypeImageAssetType, Offset(mImageType, ImageAsset), "What the main use-case for the image is for.");
  125. }
  126. //------------------------------------------------------------------------------
  127. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  128. bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset)
  129. {
  130. AssetQuery query;
  131. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  132. if (foundAssetcount == 0)
  133. {
  134. //Didn't find any assets
  135. //If possible, see if we can run an in-place import and the get the asset from that
  136. #if TORQUE_DEBUG
  137. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  138. #endif
  139. AssetImporter* autoAssetImporter;
  140. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  141. {
  142. autoAssetImporter = new AssetImporter();
  143. autoAssetImporter->registerObject("autoAssetImporter");
  144. }
  145. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  146. if (resultingAssetId != StringTable->EmptyString())
  147. {
  148. imageAsset->setAssetId(resultingAssetId);
  149. if (!imageAsset->isNull())
  150. return true;
  151. }
  152. //Didn't work, so have us fall back to a placeholder asset
  153. imageAsset->setAssetId(StringTable->insert("Core_Rendering:noImage"));
  154. if (!imageAsset->isNull())
  155. return true;
  156. //That didn't work, so fail out
  157. return false;
  158. }
  159. else
  160. {
  161. //acquire and bind the asset, and return it out
  162. imageAsset->setAssetId(query.mAssetList[0]);
  163. return true;
  164. }
  165. }
  166. StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
  167. {
  168. StringTableEntry imageAssetId = StringTable->EmptyString();
  169. AssetQuery query;
  170. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  171. if (foundAssetcount == 0)
  172. {
  173. //Didn't find any assets
  174. //If possible, see if we can run an in-place import and the get the asset from that
  175. #if TORQUE_DEBUG
  176. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  177. #endif
  178. AssetImporter* autoAssetImporter;
  179. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  180. {
  181. autoAssetImporter = new AssetImporter();
  182. autoAssetImporter->registerObject("autoAssetImporter");
  183. }
  184. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  185. if (resultingAssetId != StringTable->EmptyString())
  186. {
  187. imageAssetId = resultingAssetId;
  188. return imageAssetId;
  189. }
  190. //Didn't work, so have us fall back to a placeholder asset
  191. imageAssetId = StringTable->insert("Core_Rendering:noImage");
  192. }
  193. else
  194. {
  195. //acquire and bind the asset, and return it out
  196. imageAssetId = query.mAssetList[0];
  197. }
  198. return imageAssetId;
  199. }
  200. bool ImageAsset::getAssetById(StringTableEntry assetId, AssetPtr<ImageAsset>* imageAsset)
  201. {
  202. (*imageAsset) = assetId;
  203. if (!imageAsset->isNull())
  204. return true;
  205. //Didn't work, so have us fall back to a placeholder asset
  206. StringTableEntry noImageId = StringTable->insert("Core_Rendering:noMaterial");
  207. imageAsset->setAssetId(noImageId);
  208. if (!imageAsset->isNull())
  209. return true;
  210. return false;
  211. }
  212. //------------------------------------------------------------------------------
  213. void ImageAsset::copyTo(SimObject* object)
  214. {
  215. // Call to parent.
  216. Parent::copyTo(object);
  217. }
  218. void ImageAsset::loadImage()
  219. {
  220. SAFE_DELETE(mImage);
  221. if (mImagePath)
  222. {
  223. if (!Platform::isFile(mImagePath))
  224. {
  225. Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
  226. return;
  227. }
  228. mImage.set(mImagePath, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  229. if (mImage)
  230. {
  231. mIsValidImage = true;
  232. return;
  233. }
  234. }
  235. mIsValidImage = false;
  236. }
  237. void ImageAsset::initializeAsset()
  238. {
  239. mImagePath = expandAssetFilePath(mImageFileName);
  240. loadImage();
  241. }
  242. void ImageAsset::onAssetRefresh()
  243. {
  244. mImagePath = expandAssetFilePath(mImageFileName);
  245. loadImage();
  246. }
  247. void ImageAsset::setImageFileName(const char* pScriptFile)
  248. {
  249. // Sanity!
  250. AssertFatal(pScriptFile != NULL, "Cannot use a NULL image file.");
  251. // Update.
  252. mImageFileName = StringTable->insert(pScriptFile);
  253. }
  254. GFXTexHandle ImageAsset::getImage(GFXTextureProfile requestedProfile)
  255. {
  256. /*if (mResourceMap.contains(requestedProfile))
  257. {
  258. return mResourceMap.find(requestedProfile)->value;
  259. }
  260. else
  261. {
  262. //If we don't have an existing map case to the requested format, we'll just create it and insert it in
  263. GFXTexHandle newImage;
  264. newImage.set(mImageFileName, &requestedProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  265. mResourceMap.insert(requestedProfile, newImage);
  266. return newImage;
  267. }*/
  268. if (mImage.isValid())
  269. return mImage;
  270. return nullptr;
  271. }
  272. const char* ImageAsset::getImageInfo()
  273. {
  274. if (mIsValidImage)
  275. {
  276. static const U32 bufSize = 2048;
  277. char* returnBuffer = Con::getReturnBuffer(bufSize);
  278. dSprintf(returnBuffer, bufSize, "%s %d %d %d", GFXStringTextureFormat[mImage.getFormat()], mImage.getHeight(), mImage.getWidth(), mImage.getDepth());
  279. return returnBuffer;
  280. }
  281. return "";
  282. }
  283. const char* ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes type)
  284. {
  285. // must match ImageTypes order
  286. static const char* _names[] = {
  287. "Albedo",
  288. "Normal",
  289. "ORMConfig",
  290. "GUI",
  291. "Roughness",
  292. "AO",
  293. "Metalness",
  294. "Glow",
  295. "Particle",
  296. "Decal",
  297. "Cubemap"
  298. };
  299. if (type < 0 || type >= ImageTypeCount)
  300. {
  301. Con::errorf("ImageAsset::getAdapterNameFromType - Invalid ImageType, defaulting to Albedo");
  302. return _names[Albedo];
  303. }
  304. return _names[type];
  305. }
  306. ImageAsset::ImageTypes ImageAsset::getImageTypeFromName(const char* name)
  307. {
  308. S32 ret = -1;
  309. for (S32 i = 0; i < ImageTypeCount; i++)
  310. {
  311. if (!dStricmp(getImageTypeNameFromType((ImageTypes)i), name))
  312. ret = i;
  313. }
  314. if (ret == -1)
  315. {
  316. Con::errorf("ImageAsset::getImageTypeFromName - Invalid ImageType name, defaulting to Albedo");
  317. ret = Albedo;
  318. }
  319. return (ImageTypes)ret;
  320. }
  321. DefineEngineMethod(ImageAsset, getImagePath, const char*, (), ,
  322. "Creates an instance of the given GameObject given the asset definition.\n"
  323. "@return The GameObject entity created from the asset.")
  324. {
  325. return object->getImagePath();
  326. }
  327. DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
  328. "Creates an instance of the given GameObject given the asset definition.\n"
  329. "@return The GameObject entity created from the asset.")
  330. {
  331. return object->getImageInfo();
  332. }
  333. //-----------------------------------------------------------------------------
  334. // GuiInspectorTypeAssetId
  335. //-----------------------------------------------------------------------------
  336. IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetPtr);
  337. ConsoleDocClass(GuiInspectorTypeImageAssetPtr,
  338. "@brief Inspector field type for Shapes\n\n"
  339. "Editor use only.\n\n"
  340. "@internal"
  341. );
  342. void GuiInspectorTypeImageAssetPtr::consoleInit()
  343. {
  344. Parent::consoleInit();
  345. ConsoleBaseType::getType(TypeImageAssetPtr)->setInspectorFieldType("GuiInspectorTypeImageAssetPtr");
  346. }
  347. GuiControl* GuiInspectorTypeImageAssetPtr::constructEditControl()
  348. {
  349. // Create base filename edit controls
  350. GuiControl* retCtrl = Parent::constructEditControl();
  351. if (retCtrl == NULL)
  352. return retCtrl;
  353. // Change filespec
  354. char szBuffer[512];
  355. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ImageAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  356. mInspector->getInspectObject()->getIdString(), mCaption);
  357. mBrowseButton->setField("Command", szBuffer);
  358. const char* id = mInspector->getInspectObject()->getIdString();
  359. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  360. // Create "Open in ShapeEditor" button
  361. mImageEdButton = new GuiBitmapButtonCtrl();
  362. dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAssetId(%d.getText());", retCtrl->getId());
  363. mImageEdButton->setField("Command", szBuffer);
  364. char bitmapName[512] = "tools/worldEditor/images/toolbar/shape-editor";
  365. mImageEdButton->setBitmap(bitmapName);
  366. mImageEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  367. mImageEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  368. mImageEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  369. mImageEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
  370. mImageEdButton->registerObject();
  371. addObject(mImageEdButton);
  372. return retCtrl;
  373. }
  374. bool GuiInspectorTypeImageAssetPtr::updateRects()
  375. {
  376. S32 dividerPos, dividerMargin;
  377. mInspector->getDivider(dividerPos, dividerMargin);
  378. Point2I fieldExtent = getExtent();
  379. Point2I fieldPos = getPosition();
  380. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  381. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  382. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  383. if (mBrowseButton != NULL)
  384. {
  385. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  386. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  387. }
  388. if (mImageEdButton != NULL)
  389. {
  390. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  391. resized |= mImageEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
  392. }
  393. return resized;
  394. }
  395. IMPLEMENT_CONOBJECT(GuiInspectorTypeImageAssetId);
  396. ConsoleDocClass(GuiInspectorTypeImageAssetId,
  397. "@brief Inspector field type for Shapes\n\n"
  398. "Editor use only.\n\n"
  399. "@internal"
  400. );
  401. void GuiInspectorTypeImageAssetId::consoleInit()
  402. {
  403. Parent::consoleInit();
  404. ConsoleBaseType::getType(TypeImageAssetId)->setInspectorFieldType("GuiInspectorTypeImageAssetId");
  405. }