ImageAsset.cc 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  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 _CONSOLE_H_
  23. #include "console/console.h"
  24. #endif
  25. #ifndef _CONSOLEINTERNAL_H_
  26. #include "console/consoleInternal.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _PLATFORM_H_
  32. #include "platform/platform.h"
  33. #endif
  34. #ifndef _GBITMAP_H_
  35. #include "graphics/gBitmap.h"
  36. #endif
  37. #ifndef _UTILITY_H_
  38. #include "2d/core/Utility.h"
  39. #endif
  40. #ifndef _SCENE_OBJECT_H_
  41. #include "2d/sceneobject/SceneObject.h"
  42. #endif
  43. #ifndef _IMAGE_ASSET_H_
  44. #include "2d/assets/ImageAsset.h"
  45. #endif
  46. // Script bindings.
  47. #include "ImageAsset_ScriptBinding.h"
  48. // Debug Profiling.
  49. #include "debug/profiler.h"
  50. //------------------------------------------------------------------------------
  51. ImageAsset::FrameArea BadFrameArea(0, 0, 0, 0, 0.0f, 0.0f);
  52. //------------------------------------------------------------------------------
  53. ConsoleType( imageAssetPtr, TypeImageAssetPtr, sizeof(AssetPtr<ImageAsset>), ASSET_ID_FIELD_PREFIX )
  54. //-----------------------------------------------------------------------------
  55. ConsoleGetType( TypeImageAssetPtr )
  56. {
  57. // Fetch asset Id.
  58. return (*((AssetPtr<ImageAsset>*)dptr)).getAssetId();
  59. }
  60. //-----------------------------------------------------------------------------
  61. ConsoleSetType( TypeImageAssetPtr )
  62. {
  63. // Was a single argument specified?
  64. if( argc == 1 )
  65. {
  66. // Yes, so fetch field value.
  67. const char* pFieldValue = argv[0];
  68. // Fetch asset pointer.
  69. AssetPtr<ImageAsset>* pAssetPtr = dynamic_cast<AssetPtr<ImageAsset>*>((AssetPtrBase*)(dptr));
  70. // Is the asset pointer the correct type?
  71. if ( pAssetPtr == NULL )
  72. {
  73. // No, so fail.
  74. Con::warnf( "(TypeImageAssetPtr) - Failed to set asset Id '%d'.", pFieldValue );
  75. return;
  76. }
  77. // Set asset.
  78. pAssetPtr->setAssetId( pFieldValue );
  79. return;
  80. }
  81. // Warn.
  82. Con::warnf( "(TypeImageAssetPtr) - Cannot set multiple args to a single asset." );
  83. }
  84. //------------------------------------------------------------------------------
  85. static StringTableEntry cellCustomNodeCellsName = StringTable->insert( "Cells" );
  86. static StringTableEntry cellNodeName = StringTable->insert( "Cell" );
  87. static StringTableEntry cellRegionName = StringTable->insert( "RegionName" );
  88. static StringTableEntry cellOffsetName = StringTable->insert( "Offset" );
  89. static StringTableEntry cellOffsetXName = StringTable->insert( "OffsetX" );
  90. static StringTableEntry cellOffsetYName = StringTable->insert( "OffsetY" );
  91. static StringTableEntry cellWidthName = StringTable->insert( "Width" );
  92. static StringTableEntry cellHeightName = StringTable->insert( "Height" );
  93. static StringTableEntry cellNameEntryName = StringTable->insert( "Name" );
  94. //------------------------------------------------------------------------------
  95. static EnumTable::Enums textureFilterLookup[] =
  96. {
  97. { ImageAsset::FILTER_NEAREST, "NEAREST" },
  98. { ImageAsset::FILTER_BILINEAR, "BILINEAR" },
  99. };
  100. EnumTable textureFilterTable(sizeof(textureFilterLookup) / sizeof(EnumTable::Enums), &textureFilterLookup[0]);
  101. //------------------------------------------------------------------------------
  102. ImageAsset::TextureFilterMode ImageAsset::getFilterModeEnum(const char* label)
  103. {
  104. // Search for Mnemonic.
  105. for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
  106. if( dStricmp(textureFilterLookup[i].label, label) == 0)
  107. return((ImageAsset::TextureFilterMode)textureFilterLookup[i].index);
  108. // Warn.
  109. Con::warnf( "ImageAsset::getFilterModeEnum() - Invalid filter-mode '%s'", label );
  110. return ImageAsset::FILTER_INVALID;
  111. }
  112. //------------------------------------------------------------------------------
  113. const char* ImageAsset::getFilterModeDescription( ImageAsset::TextureFilterMode filterMode )
  114. {
  115. // Search for Mode.
  116. for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
  117. if( textureFilterLookup[i].index == filterMode )
  118. return textureFilterLookup[i].label;
  119. // Warn.
  120. Con::warnf( "ImageAsset::getFilterModeDescription() - Invalid filter-mode." );
  121. return StringTable->EmptyString;
  122. }
  123. //------------------------------------------------------------------------------
  124. ImageAsset::ImageAsset() : mImageFile(StringTable->EmptyString),
  125. mForce16Bit(false),
  126. mLocalFilterMode(FILTER_INVALID),
  127. mExplicitMode(false),
  128. mCellRowOrder(true),
  129. mCellOffsetX(0),
  130. mCellOffsetY(0),
  131. mCellStrideX(0),
  132. mCellStrideY(0),
  133. mCellCountX(0),
  134. mCellCountY(0),
  135. mCellWidth(0),
  136. mCellHeight(0),
  137. mImageTextureHandle(NULL)
  138. {
  139. // Set Vector Associations.
  140. VECTOR_SET_ASSOCIATION( mFrames );
  141. VECTOR_SET_ASSOCIATION( mExplicitFrames );
  142. }
  143. //------------------------------------------------------------------------------
  144. ImageAsset::~ImageAsset()
  145. {
  146. }
  147. //------------------------------------------------------------------------------
  148. void ImageAsset::initPersistFields()
  149. {
  150. // Call parent.
  151. Parent::initPersistFields();
  152. // Fields.
  153. addProtectedField("ImageFile", TypeAssetLooseFilePath, Offset(mImageFile, ImageAsset), &setImageFile, &getImageFile, &defaultProtectedWriteFn, "");
  154. addProtectedField("Force16bit", TypeBool, Offset(mForce16Bit, ImageAsset), &setForce16Bit, &defaultProtectedGetFn, &writeForce16Bit, "");
  155. addProtectedField("FilterMode", TypeEnum, Offset(mLocalFilterMode, ImageAsset), &setFilterMode, &defaultProtectedGetFn, &writeFilterMode, 1, &textureFilterTable);
  156. addProtectedField("ExplicitMode", TypeBool, Offset(mExplicitMode, ImageAsset), &setExplicitMode, &defaultProtectedGetFn, &defaultProtectedNotWriteFn, "");
  157. addProtectedField("CellRowOrder", TypeBool, Offset(mCellRowOrder, ImageAsset), &setCellRowOrder, &defaultProtectedGetFn, &writeCellRowOrder, "");
  158. addProtectedField("CellOffsetX", TypeS32, Offset(mCellOffsetX, ImageAsset), &setCellOffsetX, &defaultProtectedGetFn, &writeCellOffsetX, "");
  159. addProtectedField("CellOffsetY", TypeS32, Offset(mCellOffsetY, ImageAsset), &setCellOffsetY, &defaultProtectedGetFn, &writeCellOffsetY, "");
  160. addProtectedField("CellStrideX", TypeS32, Offset(mCellStrideX, ImageAsset), &setCellStrideX, &defaultProtectedGetFn, &writeCellStrideX, "");
  161. addProtectedField("CellStrideY", TypeS32, Offset(mCellStrideY, ImageAsset), &setCellStrideY, &defaultProtectedGetFn, &writeCellStrideY, "");
  162. addProtectedField("CellCountX", TypeS32, Offset(mCellCountX, ImageAsset), &setCellCountX, &defaultProtectedGetFn, &writeCellCountX, "");
  163. addProtectedField("CellCountY", TypeS32, Offset(mCellCountY, ImageAsset), &setCellCountY, &defaultProtectedGetFn, &writeCellCountY, "");
  164. addProtectedField("CellWidth", TypeS32, Offset(mCellWidth, ImageAsset), &setCellWidth, &defaultProtectedGetFn, &writeCellWidth, "");
  165. addProtectedField("CellHeight", TypeS32, Offset(mCellHeight, ImageAsset), &setCellHeight, &defaultProtectedGetFn, &writeCellHeight, "");
  166. }
  167. //------------------------------------------------------------------------------
  168. bool ImageAsset::onAdd()
  169. {
  170. // Call Parent.
  171. if (!Parent::onAdd())
  172. return false;
  173. return true;
  174. }
  175. //------------------------------------------------------------------------------
  176. void ImageAsset::onRemove()
  177. {
  178. // Call Parent.
  179. Parent::onRemove();
  180. }
  181. //------------------------------------------------------------------------------
  182. void ImageAsset::setImageFile( const char* pImageFile )
  183. {
  184. // Sanity!
  185. AssertFatal( pImageFile != NULL, "Cannot use a NULL image file." );
  186. // Fetch image file.
  187. pImageFile = StringTable->insert( pImageFile );
  188. // Ignore no change,
  189. if ( pImageFile == mImageFile )
  190. return;
  191. // Update.
  192. mImageFile = getOwned() ? expandAssetFilePath( pImageFile ) : StringTable->insert( pImageFile );
  193. // Refresh the asset.
  194. refreshAsset();
  195. }
  196. //------------------------------------------------------------------------------
  197. void ImageAsset::copyTo(SimObject* object)
  198. {
  199. // Call to parent.
  200. Parent::copyTo(object);
  201. // Cast to asset.
  202. ImageAsset* pAsset = static_cast<ImageAsset*>(object);
  203. // Sanity!
  204. AssertFatal(pAsset != NULL, "ImageAsset::copyTo() - Object is not the correct type.");
  205. // Copy state.
  206. pAsset->setImageFile( getImageFile() );
  207. pAsset->setForce16Bit( getForce16Bit() );
  208. pAsset->setFilterMode( getFilterMode() );
  209. pAsset->setExplicitMode( getExplicitMode() );
  210. pAsset->setCellRowOrder( getCellRowOrder() );
  211. pAsset->setCellOffsetX( getCellCountX() );
  212. pAsset->setCellOffsetY( getCellCountY() );
  213. pAsset->setCellStrideX( getCellStrideX() );
  214. pAsset->setCellStrideY( getCellStrideY() );
  215. pAsset->setCellCountX( getCellCountX() );
  216. pAsset->setCellCountY( getCellCountY() );
  217. pAsset->setCellWidth( getCellWidth() );
  218. pAsset->setCellHeight( getCellHeight() );
  219. // Finish if not in explicit mode.
  220. if ( !getExplicitMode() )
  221. return;
  222. // Fetch the explicit cell count.
  223. const S32 explicitCellCount = getExplicitCellCount();
  224. // Finish if no explicit cells exist.
  225. if ( explicitCellCount == 0 )
  226. return;
  227. // Copy explicit cells.
  228. pAsset->clearExplicitCells();
  229. for( S32 index = 0; index < explicitCellCount; ++index )
  230. {
  231. // Fetch the cell pixel area.
  232. const FrameArea::PixelArea& pixelArea = getImageFrameArea( index ).mPixelArea;
  233. // Add the explicit cell.
  234. pAsset->addExplicitCell( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, pixelArea.mRegionName );
  235. }
  236. }
  237. //------------------------------------------------------------------------------
  238. void ImageAsset::setForce16Bit( const bool force16Bit )
  239. {
  240. // Ignore no change,
  241. if ( force16Bit == mForce16Bit )
  242. return;
  243. // Update.
  244. mForce16Bit = force16Bit;
  245. // Refresh the asset.
  246. refreshAsset();
  247. }
  248. //------------------------------------------------------------------------------
  249. void ImageAsset::setFilterMode( const ImageAsset::TextureFilterMode filterMode )
  250. {
  251. // Ignore no change,
  252. if ( filterMode == mLocalFilterMode )
  253. return;
  254. // Invalid filter mode?
  255. if ( filterMode == FILTER_INVALID )
  256. {
  257. // Yes, so warn.
  258. Con::warnf( "Cannot set an invalid filter mode." );
  259. return;
  260. }
  261. // Update.
  262. mLocalFilterMode = filterMode;
  263. // Refresh the asset.
  264. refreshAsset();
  265. }
  266. //------------------------------------------------------------------------------
  267. void ImageAsset::setExplicitMode( const bool explicitMode )
  268. {
  269. // Ignore no change,
  270. if ( explicitMode == mExplicitMode )
  271. return;
  272. // Update.
  273. mExplicitMode = explicitMode;
  274. // Refresh the asset.
  275. refreshAsset();
  276. }
  277. //------------------------------------------------------------------------------
  278. void ImageAsset::setCellRowOrder( const bool cellRowOrder )
  279. {
  280. // Ignore no change.
  281. if ( cellRowOrder == mCellRowOrder )
  282. return;
  283. // Update.
  284. mCellRowOrder = cellRowOrder;
  285. // Refresh the asset.
  286. refreshAsset();
  287. }
  288. //------------------------------------------------------------------------------
  289. void ImageAsset::setCellOffsetX( const S32 cellOffsetX )
  290. {
  291. // Ignore no change.
  292. if ( cellOffsetX == mCellOffsetX )
  293. return;
  294. // Valid?
  295. if ( cellOffsetX < 0 )
  296. {
  297. // No, so warn.
  298. Con::warnf( "Invalid CELL offset X '%d'.", cellOffsetX );
  299. return;
  300. }
  301. // Update.
  302. mCellOffsetX = cellOffsetX;
  303. // Refresh the asset.
  304. refreshAsset();
  305. }
  306. //------------------------------------------------------------------------------
  307. void ImageAsset::setCellOffsetY( const S32 cellOffsetY )
  308. {
  309. // Ignore no change.
  310. if ( cellOffsetY == mCellOffsetY )
  311. return;
  312. // Valid?
  313. if ( cellOffsetY < 0 )
  314. {
  315. // No, so warn.
  316. Con::warnf( "Invalid CELL offset Y '%d'.", cellOffsetY );
  317. return;
  318. }
  319. // Update.
  320. mCellOffsetY = cellOffsetY;
  321. // Refresh the asset.
  322. refreshAsset();
  323. }
  324. //------------------------------------------------------------------------------
  325. void ImageAsset::setCellStrideX( const S32 cellStrideX )
  326. {
  327. // Ignore no change.
  328. if ( cellStrideX == mCellStrideX )
  329. return;
  330. // Valid?
  331. if ( cellStrideX < 0 )
  332. {
  333. // No, so warn.
  334. Con::warnf( "Invalid CELL stride X '%d'.", cellStrideX );
  335. return;
  336. }
  337. // Update.
  338. mCellStrideX = cellStrideX;
  339. // Refresh the asset.
  340. refreshAsset();
  341. }
  342. //------------------------------------------------------------------------------
  343. void ImageAsset::setCellStrideY( const S32 cellStrideY )
  344. {
  345. // Ignore no change.
  346. if ( cellStrideY == mCellStrideY )
  347. return;
  348. // Valid?
  349. if ( cellStrideY < 0 )
  350. {
  351. // No, so warn.
  352. Con::warnf( "Invalid CELL stride Y '%d'.", cellStrideY );
  353. return;
  354. }
  355. // Update.
  356. mCellStrideY = cellStrideY;
  357. // Refresh the asset.
  358. refreshAsset();
  359. }
  360. //------------------------------------------------------------------------------
  361. void ImageAsset::setCellCountX( const S32 cellCountX )
  362. {
  363. // Ignore no change.
  364. if ( cellCountX == mCellCountX )
  365. return;
  366. // Valid?
  367. if ( cellCountX < 0 )
  368. {
  369. // No, so warn.
  370. Con::warnf( "Invalid CELL count X '%d'.", cellCountX );
  371. return;
  372. }
  373. // Update.
  374. mCellCountX = cellCountX;
  375. // Refresh the asset.
  376. refreshAsset();
  377. }
  378. //------------------------------------------------------------------------------
  379. void ImageAsset::setCellCountY( const S32 cellCountY )
  380. {
  381. // Ignore no change.
  382. if ( cellCountY == mCellCountY )
  383. return;
  384. // Valid?
  385. if ( cellCountY < 0 )
  386. {
  387. // No, so warn.
  388. Con::warnf( "Invalid CELL count Y '%d'.", cellCountY );
  389. return;
  390. }
  391. // Update.
  392. mCellCountY = cellCountY;
  393. // Refresh the asset.
  394. refreshAsset();
  395. }
  396. //------------------------------------------------------------------------------
  397. void ImageAsset::setCellWidth( const S32 cellWidth )
  398. {
  399. // Ignore no change.
  400. if ( cellWidth == mCellWidth )
  401. return;
  402. // Valid?
  403. if ( cellWidth < 0 )
  404. {
  405. // No, so warn.
  406. Con::warnf( "Invalid CELL width '%d'.", cellWidth );
  407. return;
  408. }
  409. // Update.
  410. mCellWidth = cellWidth;
  411. // Refresh the asset.
  412. refreshAsset();
  413. }
  414. //------------------------------------------------------------------------------
  415. void ImageAsset::setCellHeight( const S32 cellheight )
  416. {
  417. // Ignore no change.
  418. if ( cellheight == mCellHeight )
  419. return;
  420. // Valid?
  421. if ( cellheight < 0 )
  422. {
  423. // No, so warn.
  424. Con::warnf( "Invalid CELL height '%d'.", cellheight );
  425. return;
  426. }
  427. // Update.
  428. mCellHeight = cellheight;
  429. // Refresh the asset.
  430. refreshAsset();
  431. }
  432. //------------------------------------------------------------------------------
  433. S32 ImageAsset::getExplicitCellWidth(const S32 cellIndex)
  434. {
  435. if ( !getExplicitMode() )
  436. {
  437. // No, so warn.
  438. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  439. return (0);
  440. }
  441. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  442. return(thisCell.mPixelWidth);
  443. }
  444. //------------------------------------------------------------------------------
  445. S32 ImageAsset::getExplicitCellHeight(const S32 cellIndex)
  446. {
  447. if ( !getExplicitMode() )
  448. {
  449. // No, so warn.
  450. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  451. return (0);
  452. }
  453. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  454. return(thisCell.mPixelHeight);
  455. }
  456. //------------------------------------------------------------------------------
  457. bool ImageAsset::containsNamedRegion(const char* regionName)
  458. {
  459. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  460. {
  461. // Grab the current pixelArea
  462. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  463. // Check to see if the name matches the argument
  464. if (!dStrcmp(pixelArea.mRegionName, regionName))
  465. {
  466. // Found it, so erase it and return success
  467. return true;
  468. }
  469. }
  470. return false;
  471. }
  472. //------------------------------------------------------------------------------
  473. bool ImageAsset::clearExplicitCells( void )
  474. {
  475. // Are we in explicit mode?
  476. if ( !getExplicitMode() )
  477. {
  478. // No, so warn.
  479. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  480. return false;
  481. }
  482. // Clear explicit frames.
  483. mExplicitFrames.clear();
  484. // Refresh the asset.
  485. refreshAsset();
  486. return true;
  487. }
  488. //------------------------------------------------------------------------------
  489. bool ImageAsset::addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  490. {
  491. // Are we in explicit mode?
  492. if ( !getExplicitMode() )
  493. {
  494. // No, so warn.
  495. Con::warnf( "ImageAsset::addExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  496. return false;
  497. }
  498. // Fetch the original image dimensions.
  499. const S32 imageWidth = getImageWidth();
  500. const S32 imageHeight = getImageHeight();
  501. // The region name cannot be empty
  502. if ( regionName == StringTable->EmptyString )
  503. {
  504. Con::warnf( "ImageAsset::addExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  505. U32 currentIndex = mExplicitFrames.size();
  506. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  507. dSscanf(regionName, "%i", currentIndex);
  508. }
  509. // The Cell Offset X needs to be within the image.
  510. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  511. {
  512. // Warn.
  513. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  514. return false;
  515. }
  516. // The Cell Offset Y needs to be within the image.
  517. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  518. {
  519. // Warn.
  520. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  521. return false;
  522. }
  523. // The Cell Width needs to be within the image.
  524. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  525. {
  526. // Warn.
  527. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  528. return false;
  529. }
  530. // The Cell Height needs to be within the image.
  531. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  532. {
  533. // Warn.
  534. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  535. return false;
  536. }
  537. // Store frame.
  538. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  539. mExplicitFrames.push_back( pixelArea );
  540. // Refresh the asset.
  541. refreshAsset();
  542. return true;
  543. }
  544. //------------------------------------------------------------------------------
  545. bool ImageAsset::insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  546. {
  547. // Are we in explicit mode?
  548. if ( !getExplicitMode() )
  549. {
  550. // No, so warn.
  551. Con::warnf( "ImageAsset::insertExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  552. return false;
  553. }
  554. // Fetch the original image dimensions.
  555. const S32 imageWidth = getImageWidth();
  556. const S32 imageHeight = getImageHeight();
  557. // Fetch the explicit frame count.
  558. const S32 explicitFramelCount = mExplicitFrames.size();
  559. // Region cannot be empty
  560. if ( regionName == StringTable->EmptyString )
  561. {
  562. Con::warnf( "ImageAsset::insertExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  563. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFramelCount );
  564. dSscanf(regionName, "%i", explicitFramelCount);
  565. }
  566. // The cell index needs to be in range.
  567. if ( cellIndex < 0 )
  568. {
  569. // Warn.
  570. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  571. return false;
  572. }
  573. // The Cell Offset X needs to be within the image.
  574. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  575. {
  576. // Warn.
  577. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  578. return false;
  579. }
  580. // The Cell Offset Y needs to be within the image.
  581. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  582. {
  583. // Warn.
  584. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  585. return false;
  586. }
  587. // The Cell Width needs to be within the image.
  588. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  589. {
  590. // Warn.
  591. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  592. return false;
  593. }
  594. // The Cell Height needs to be within the image.
  595. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  596. {
  597. // Warn.
  598. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  599. return false;
  600. }
  601. // Configure frame.
  602. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  603. // Insert frame appropriately.
  604. if ( cellIndex >= explicitFramelCount )
  605. {
  606. mExplicitFrames.push_back( pixelArea );
  607. }
  608. else
  609. {
  610. mExplicitFrames.insert( cellIndex );
  611. mExplicitFrames[cellIndex] = pixelArea;
  612. }
  613. // Refresh the asset.
  614. refreshAsset();
  615. return true;
  616. }
  617. //------------------------------------------------------------------------------
  618. bool ImageAsset::setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  619. {
  620. // Are we in explicit mode?
  621. if ( !getExplicitMode() )
  622. {
  623. // No, so warn.
  624. Con::warnf( "ImageAsset::setExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  625. return false;
  626. }
  627. // Fetch the original image dimensions.
  628. const S32 imageWidth = getImageWidth();
  629. const S32 imageHeight = getImageHeight();
  630. // Fetch the explicit frame count.
  631. const S32 explicitFrameCount = mExplicitFrames.size();
  632. // Region cannot be empty
  633. if ( regionName == StringTable->EmptyString )
  634. {
  635. Con::warnf( "ImageAsset::setExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  636. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFrameCount );
  637. dSscanf(regionName, "%i", explicitFrameCount);
  638. }
  639. // The cell index needs to be in range.
  640. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  641. {
  642. // Warn.
  643. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  644. return false;
  645. }
  646. // The Cell Offset X needs to be within the image.
  647. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  648. {
  649. // Warn.
  650. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  651. return false;
  652. }
  653. // The Cell Offset Y needs to be within the image.
  654. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  655. {
  656. // Warn.
  657. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  658. return false;
  659. }
  660. // The Cell Width needs to be within the image.
  661. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  662. {
  663. // Warn.
  664. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  665. return false;
  666. }
  667. // The Cell Height needs to be within the image.
  668. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  669. {
  670. // Warn.
  671. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  672. return false;
  673. }
  674. // Configure frame.
  675. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  676. // Set cell.
  677. mExplicitFrames[cellIndex] = pixelArea;
  678. // Refresh the asset.
  679. refreshAsset();
  680. return true;
  681. }
  682. //------------------------------------------------------------------------------
  683. bool ImageAsset::removeExplicitCell( const S32 cellIndex )
  684. {
  685. // Are we in explicit mode?
  686. if ( !getExplicitMode() )
  687. {
  688. // No, so warn.
  689. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  690. return false;
  691. }
  692. // Fetch the explicit frame count.
  693. const S32 explicitFrameCount = mExplicitFrames.size();
  694. // The cell index needs to be in range.
  695. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  696. {
  697. // Warn.
  698. Con::warnf( "ImageAsset::removeExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  699. return false;
  700. }
  701. // Remove cell.
  702. mExplicitFrames.erase(cellIndex);
  703. // Refresh the asset.
  704. refreshAsset();
  705. return true;
  706. }
  707. //------------------------------------------------------------------------------
  708. bool ImageAsset::removeExplicitCell( const char* regionName )
  709. {
  710. // Are we in explicit mode?
  711. if ( !getExplicitMode() )
  712. {
  713. // No, so warn.
  714. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  715. return false;
  716. }
  717. // Interate through the vector
  718. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  719. {
  720. // Grab the current pixelArea
  721. const FrameArea::PixelArea& pixelArea = *frameItr;
  722. // Check to see if the name matches the argument
  723. if (pixelArea.mRegionName == regionName)
  724. {
  725. // Found it, so erase it and return success
  726. mExplicitFrames.erase(frameItr);
  727. return true;
  728. }
  729. }
  730. // Didn't find it, so warn
  731. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot find %s cell to remove.", regionName );
  732. return false;
  733. }
  734. //------------------------------------------------------------------------------
  735. ImageAsset::FrameArea& ImageAsset::getCellByName( const char* cellName)
  736. {
  737. // If the cellName was empty
  738. if (cellName == StringTable->EmptyString)
  739. {
  740. // Warn and return a bad frame
  741. Con::warnf( "ImageAsset::getCellByName() - Empty cell name was passed." );
  742. return BadFrameArea;
  743. }
  744. // Interate through the vector
  745. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  746. {
  747. // Grab the current pixelArea
  748. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  749. // Check to see if the name matches the argument
  750. if (pixelArea.mRegionName == cellName)
  751. {
  752. // Found it, so erase it and return success
  753. return *frameItr;
  754. }
  755. }
  756. // Didn't find it, so warn and return a bad frame
  757. Con::warnf( "ImageAsset::getCellByName() - Cannot find %s cell.", cellName );
  758. return BadFrameArea;
  759. }
  760. //------------------------------------------------------------------------------
  761. void ImageAsset::setTextureFilter( const TextureFilterMode filterMode )
  762. {
  763. // Finish if no texture.
  764. if ( mImageTextureHandle.IsNull() )
  765. return;
  766. // Select Hardware Filter Mode.
  767. GLint glFilterMode;
  768. switch( filterMode )
  769. {
  770. // Nearest ("none").
  771. case FILTER_NEAREST:
  772. {
  773. glFilterMode = GL_NEAREST;
  774. } break;
  775. // Bilinear ("smooth").
  776. case FILTER_BILINEAR:
  777. {
  778. glFilterMode = GL_LINEAR;
  779. } break;
  780. // Huh?
  781. default:
  782. // Oh well...
  783. glFilterMode = GL_LINEAR;
  784. };
  785. // Set the texture objects filter mode.
  786. mImageTextureHandle.setFilter( glFilterMode );
  787. }
  788. //------------------------------------------------------------------------------
  789. void ImageAsset::initializeAsset( void )
  790. {
  791. // Call parent.
  792. Parent::initializeAsset();
  793. // Ensure the image-file is expanded.
  794. mImageFile = expandAssetFilePath( mImageFile );
  795. // Calculate the image.
  796. calculateImage();
  797. }
  798. //------------------------------------------------------------------------------
  799. void ImageAsset::onAssetRefresh( void )
  800. {
  801. // Ignore if not yet added to the sim.
  802. if ( !isProperlyAdded() )
  803. return;
  804. // Call parent.
  805. Parent::onAssetRefresh();
  806. // Compile image.
  807. calculateImage();
  808. }
  809. //-----------------------------------------------------------------------------
  810. void ImageAsset::onTamlPreWrite( void )
  811. {
  812. // Call parent.
  813. Parent::onTamlPreWrite();
  814. // Ensure the image-file is collapsed.
  815. mImageFile = collapseAssetFilePath( mImageFile );
  816. }
  817. //-----------------------------------------------------------------------------
  818. void ImageAsset::onTamlPostWrite( void )
  819. {
  820. // Call parent.
  821. Parent::onTamlPostWrite();
  822. // Ensure the image-file is expanded.
  823. mImageFile = expandAssetFilePath( mImageFile );
  824. }
  825. //------------------------------------------------------------------------------
  826. void ImageAsset::calculateImage( void )
  827. {
  828. // Debug Profiling.
  829. PROFILE_SCOPE(ImageAsset_CalculateImage);
  830. // Clear frames.
  831. mFrames.clear();
  832. // If we have an existing texture and we're setting to the same bitmap then force the texture manager
  833. // to refresh the texture itself.
  834. if ( !mImageTextureHandle.IsNull() && dStricmp(mImageTextureHandle.getTextureKey(), mImageFile) == 0 )
  835. TextureManager::refresh( mImageFile );
  836. // Get image texture.
  837. mImageTextureHandle.set( mImageFile, TextureHandle::BitmapTexture, true, getForce16Bit() );
  838. // Is the texture valid?
  839. if ( mImageTextureHandle.IsNull() )
  840. {
  841. // No, so warn.
  842. Con::warnf( "Image '%s' could not load texture '%s'.", getAssetId(), mImageFile );
  843. return;
  844. }
  845. // Is the local filter mode specified?
  846. if ( mLocalFilterMode != FILTER_INVALID )
  847. {
  848. // Yes, so set filter mode.
  849. setTextureFilter( mLocalFilterMode );
  850. }
  851. else
  852. {
  853. TextureFilterMode filterMode = FILTER_NEAREST;
  854. // No, so fetch the global filter.
  855. const char* pGlobalFilter = Con::getVariable( "$pref::T2D::imageAssetGlobalFilterMode" );
  856. // Fetch the global filter mode.
  857. if ( pGlobalFilter != NULL && dStrlen(pGlobalFilter) > 0 )
  858. filterMode = getFilterModeEnum( pGlobalFilter );
  859. // If global filter mode is invalid then use local filter mode.
  860. if ( filterMode == FILTER_INVALID )
  861. filterMode = FILTER_NEAREST;
  862. // Set filter mode.
  863. setTextureFilter( filterMode );
  864. }
  865. // Calculate according to mode.
  866. if ( mExplicitMode )
  867. {
  868. calculateExplicitMode();
  869. }
  870. else
  871. {
  872. calculateImplicitMode();
  873. }
  874. }
  875. //------------------------------------------------------------------------------
  876. void ImageAsset::calculateImplicitMode( void )
  877. {
  878. // Debug Profiling.
  879. PROFILE_SCOPE(ImageAsset_CalculateImplicitMode);
  880. // Sanity!
  881. AssertFatal( !mExplicitMode, "Cannot calculate implicit cells when in explicit mode." );
  882. // Fetch the texture object.
  883. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  884. // Calculate texel scales.
  885. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  886. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  887. // Fetch the original image dimensions.
  888. const S32 imageWidth = getImageWidth();
  889. const S32 imageHeight = getImageHeight();
  890. // Set full-frame as default.
  891. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  892. mFrames.push_back( frameArea );
  893. // Finish if no cell counts are specified. This is how we default to full-frame mode.
  894. if ( mCellCountX < 1 || mCellCountY < 1 )
  895. return;
  896. // The cell width needs to be at maximum the image width!
  897. if ( mCellWidth < 1 || mCellWidth > imageWidth )
  898. {
  899. // Warn.
  900. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Width of %d.", mCellWidth );
  901. return;
  902. }
  903. // The cell height needs to be at maximum the image height!
  904. if ( mCellHeight < 1 || mCellHeight > imageHeight )
  905. {
  906. // Warn.
  907. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Height of %d.", mCellHeight );
  908. return;
  909. }
  910. // The Cell Offset X needs to be within the image.
  911. if ( mCellOffsetX < 0 || mCellOffsetX >= imageWidth )
  912. {
  913. // Warn.
  914. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX of %d.", mCellOffsetX );
  915. return;
  916. }
  917. // The Cell Offset Y needs to be within the image.
  918. if ( mCellOffsetY < 0 || mCellOffsetY >= imageHeight )
  919. {
  920. // Warn.
  921. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY of %d.", mCellOffsetY );
  922. return;
  923. }
  924. // Are we using Cell-StrideX?
  925. S32 cellStepX;
  926. if ( mCellStrideX != 0 )
  927. {
  928. // Yes, so set stepX to be StrideX.
  929. cellStepX = mCellStrideX;
  930. }
  931. else
  932. {
  933. // No, so set stepY to be Cell Width.
  934. cellStepX = mCellWidth;
  935. }
  936. // Are we using Cell-StrideY?
  937. S32 cellStepY;
  938. if ( mCellStrideY != 0 )
  939. {
  940. // Yes, so set stepY to be StrideY.
  941. cellStepY = mCellStrideY;
  942. }
  943. else
  944. {
  945. // No, so set stepY to be Cell Height.
  946. cellStepY = mCellHeight;
  947. }
  948. // Calculate Final Cell Position X.
  949. S32 cellFinalPositionX = mCellOffsetX + ((mCellCountX-((cellStepX<0)?1:0))*cellStepX);
  950. // Off Left?
  951. if ( cellFinalPositionX < 0 )
  952. {
  953. // Warn.
  954. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image left-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  955. return;
  956. }
  957. // Off Right?
  958. else if ( cellFinalPositionX > imageWidth )
  959. {
  960. // Warn.
  961. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image right-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  962. return;
  963. }
  964. // Calculate Final Cell Position Y.
  965. S32 cellFinalPositionY = mCellOffsetY + ((mCellCountY-((cellStepY<0)?1:0))*cellStepY);
  966. // Off Top?
  967. if ( cellFinalPositionY < 0 )
  968. {
  969. // Warn.
  970. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image top-side.", mCellOffsetY, mCellHeight, mCellCountY );
  971. return;
  972. }
  973. // Off Bottom?
  974. else if ( cellFinalPositionY > imageHeight )
  975. {
  976. // Warn.
  977. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image bottom-side.", mCellOffsetY, mCellHeight, mCellCountY );
  978. return;
  979. }
  980. // Clear default frame.
  981. mFrames.clear();
  982. // Cell Row Order?
  983. if ( mCellRowOrder )
  984. {
  985. // Yes, so RowRow Order.
  986. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  987. {
  988. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  989. {
  990. // Set frame area.
  991. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  992. // Store fame.
  993. mFrames.push_back( frameArea );
  994. }
  995. }
  996. return;
  997. }
  998. // No, so Column Order.
  999. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  1000. {
  1001. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  1002. {
  1003. // Set frame area.
  1004. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  1005. // Store fame.
  1006. mFrames.push_back( frameArea );
  1007. }
  1008. }
  1009. }
  1010. //------------------------------------------------------------------------------
  1011. void ImageAsset::calculateExplicitMode( void )
  1012. {
  1013. // Debug Profiling.
  1014. PROFILE_SCOPE(ImageAsset_CalculateExplicitMode);
  1015. // Sanity!
  1016. AssertFatal( mExplicitMode, "Cannot calculate explicit cells when not in explicit mode." );
  1017. // Fetch the texture object.
  1018. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  1019. // Calculate texel scales.
  1020. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  1021. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  1022. // Fetch the original image dimensions.
  1023. const S32 imageWidth = getImageWidth();
  1024. const S32 imageHeight = getImageHeight();
  1025. // Clear default frame.
  1026. mFrames.clear();
  1027. // Are any explicit frames set.
  1028. if ( mExplicitFrames.size() == 0 )
  1029. {
  1030. // No, so set full-frame as default.
  1031. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  1032. mFrames.push_back( frameArea );
  1033. return;
  1034. }
  1035. // Iterate explicit frames.
  1036. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1037. {
  1038. // Fetch pixel area.
  1039. const FrameArea::PixelArea& pixelArea = *frameItr;
  1040. // Set frame area.
  1041. FrameArea frameArea( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, texelWidthScale, texelHeightScale, pixelArea.mRegionName );
  1042. // Store frame.
  1043. mFrames.push_back( frameArea );
  1044. }
  1045. }
  1046. //------------------------------------------------------------------------------
  1047. bool ImageAsset::setFilterMode( void* obj, const char* data )
  1048. {
  1049. static_cast<ImageAsset*>(obj)->setFilterMode(getFilterModeEnum(data));
  1050. return false;
  1051. }
  1052. //------------------------------------------------------------------------------
  1053. void ImageAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
  1054. {
  1055. // Debug Profiling.
  1056. PROFILE_SCOPE(ImageAsset_OnTamlCustomWrite);
  1057. // Call parent.
  1058. Parent::onTamlCustomWrite( customNodes );
  1059. // Finish if not in explicit mode.
  1060. if ( !mExplicitMode )
  1061. return;
  1062. if (mExplicitFrames.size() > 0)
  1063. {
  1064. // Add cell custom node.
  1065. TamlCustomNode* pCustomCellNodes = customNodes.addNode( cellCustomNodeCellsName );
  1066. // Iterate explicit frames.
  1067. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1068. {
  1069. // Fetch pixel area.
  1070. const FrameArea::PixelArea& pixelArea = *frameItr;
  1071. // Add cell alias.
  1072. TamlCustomNode* pCellNode = pCustomCellNodes->addNode( cellNodeName );
  1073. // Add cell properties.
  1074. pCellNode->addField( cellRegionName, pixelArea.mRegionName );
  1075. pCellNode->addField( cellOffsetName, pixelArea.mPixelOffset );
  1076. pCellNode->addField( cellWidthName, pixelArea.mPixelWidth );
  1077. pCellNode->addField( cellHeightName, pixelArea.mPixelHeight );
  1078. }
  1079. }
  1080. }
  1081. //-----------------------------------------------------------------------------
  1082. void ImageAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
  1083. {
  1084. // Debug Profiling.
  1085. PROFILE_SCOPE(ImageAsset_OnTamlCustomRead);
  1086. // Call parent.
  1087. Parent::onTamlCustomRead( customNodes );
  1088. // Find cell custom node.
  1089. const TamlCustomNode* pCustomCellNodes = customNodes.findNode( cellCustomNodeCellsName );
  1090. // Continue if we have explicit cells.
  1091. if ( pCustomCellNodes != NULL )
  1092. {
  1093. // Set explicit mode.
  1094. mExplicitMode = true;
  1095. // Fetch children cell nodes.
  1096. const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
  1097. // Iterate cells.
  1098. for( TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr )
  1099. {
  1100. // Fetch cell node.
  1101. TamlCustomNode* pCellNode = *cellNodeItr;
  1102. // Fetch node name.
  1103. StringTableEntry nodeName = pCellNode->getNodeName();
  1104. // Is this a valid alias?
  1105. if ( nodeName != cellNodeName )
  1106. {
  1107. // No, so warn.
  1108. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, cellNodeName );
  1109. continue;
  1110. }
  1111. Point2I cellOffset(-1, -1);
  1112. S32 cellWidth = 0;
  1113. S32 cellHeight = 0;
  1114. const char* regionName = StringTable->EmptyString;
  1115. // Fetch fields.
  1116. const TamlCustomFieldVector& fields = pCellNode->getFields();
  1117. // Iterate property fields.
  1118. for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
  1119. {
  1120. // Fetch field.
  1121. const TamlCustomField* pField = *fieldItr;
  1122. // Fetch field name.
  1123. StringTableEntry fieldName = pField->getFieldName();
  1124. // Check common fields.
  1125. if ( fieldName == cellRegionName )
  1126. {
  1127. regionName = pField->getFieldValue();
  1128. }
  1129. else if ( fieldName == cellOffsetName )
  1130. {
  1131. pField->getFieldValue( cellOffset );
  1132. }
  1133. else if ( fieldName == cellOffsetXName )
  1134. {
  1135. pField->getFieldValue( cellOffset.x );
  1136. }
  1137. else if ( fieldName == cellOffsetYName )
  1138. {
  1139. pField->getFieldValue( cellOffset.y );
  1140. }
  1141. else if ( fieldName == cellWidthName )
  1142. {
  1143. pField->getFieldValue( cellWidth );
  1144. }
  1145. else if ( fieldName == cellHeightName )
  1146. {
  1147. pField->getFieldValue( cellHeight );
  1148. }
  1149. else
  1150. {
  1151. // Unknown name so warn.
  1152. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName );
  1153. continue;
  1154. }
  1155. }
  1156. // Does the region have a name
  1157. if ( regionName == StringTable->EmptyString )
  1158. {
  1159. // No, so warn and set it to the next index
  1160. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell name of '%s' is invalid or was not set.", regionName );
  1161. U32 currentIndex = mExplicitFrames.size();
  1162. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  1163. dSscanf(regionName, "%i", currentIndex);
  1164. }
  1165. // Is cell offset valid?
  1166. if ( cellOffset.x < 0 || cellOffset.y < 0 )
  1167. {
  1168. // No, so warn.
  1169. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell offset of '(%d,%d)' is invalid or was not set.", cellOffset.x, cellOffset.y );
  1170. continue;
  1171. }
  1172. // Is cell width valid?
  1173. if ( cellWidth <= 0 )
  1174. {
  1175. // No, so warn.
  1176. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell width of '%d' is invalid or was not set.", cellWidth );
  1177. continue;
  1178. }
  1179. // Is cell height valid?
  1180. if ( cellHeight <= 0 )
  1181. {
  1182. // No, so warn.
  1183. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell height of '%d' is invalid or was not set.", cellHeight );
  1184. continue;
  1185. }
  1186. // Add explicit frame.
  1187. FrameArea::PixelArea pixelArea( cellOffset.x, cellOffset.y, cellWidth, cellHeight, regionName );
  1188. mExplicitFrames.push_back( pixelArea );
  1189. }
  1190. }
  1191. }
  1192. //-----------------------------------------------------------------------------
  1193. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  1194. {
  1195. // Sanity!
  1196. AssertFatal( pClassRep != NULL, "ImageAsset::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  1197. AssertFatal( pParentElement != NULL, "ImageAsset::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  1198. char buffer[1024];
  1199. // Create ImageAsset node element.
  1200. TiXmlElement* pImageAssetNodeElement = new TiXmlElement( "xs:element" );
  1201. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), cellCustomNodeCellsName );
  1202. pImageAssetNodeElement->SetAttribute( "name", buffer );
  1203. pImageAssetNodeElement->SetAttribute( "minOccurs", 0 );
  1204. pImageAssetNodeElement->SetAttribute( "maxOccurs", 1 );
  1205. pParentElement->LinkEndChild( pImageAssetNodeElement );
  1206. // Create complex type.
  1207. TiXmlElement* pImageAssetNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1208. pImageAssetNodeElement->LinkEndChild( pImageAssetNodeComplexTypeElement );
  1209. // Create choice element.
  1210. TiXmlElement* pImageAssetNodeChoiceElement = new TiXmlElement( "xs:choice" );
  1211. pImageAssetNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  1212. pImageAssetNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  1213. pImageAssetNodeComplexTypeElement->LinkEndChild( pImageAssetNodeChoiceElement );
  1214. // Create ImageAsset element.
  1215. TiXmlElement* pImageAssetElement = new TiXmlElement( "xs:element" );
  1216. pImageAssetElement->SetAttribute( "name", cellNodeName );
  1217. pImageAssetElement->SetAttribute( "minOccurs", 0 );
  1218. pImageAssetElement->SetAttribute( "maxOccurs", 1 );
  1219. pImageAssetNodeChoiceElement->LinkEndChild( pImageAssetElement );
  1220. // Create complex type Element.
  1221. TiXmlElement* pImageAssetComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1222. pImageAssetElement->LinkEndChild( pImageAssetComplexTypeElement );
  1223. // Create "RegionName" attribute.
  1224. TiXmlElement* pImageRegionName = new TiXmlElement( "xs:attribute" );
  1225. pImageRegionName->SetAttribute( "name", cellRegionName );
  1226. pImageRegionName->SetAttribute( "type", "xs:string" );
  1227. pImageAssetComplexTypeElement->LinkEndChild( pImageRegionName );
  1228. // Create "Offset" attribute.
  1229. TiXmlElement* pImageAssetOffset = new TiXmlElement( "xs:attribute" );
  1230. pImageAssetOffset->SetAttribute( "name", cellOffsetName );
  1231. pImageAssetOffset->SetAttribute( "type", "Point2I_ConsoleType" );
  1232. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetOffset );
  1233. // Create "Width" attribute.
  1234. TiXmlElement* pImageAssetWidth = new TiXmlElement( "xs:attribute" );
  1235. pImageAssetWidth->SetAttribute( "name", cellWidthName );
  1236. pImageAssetWidth->SetAttribute( "type", "xs:unsignedInt" );
  1237. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetWidth );
  1238. // Create "Height" attribute.
  1239. TiXmlElement* pImageAssetHeight = new TiXmlElement( "xs:attribute" );
  1240. pImageAssetHeight->SetAttribute( "name", cellHeightName );
  1241. pImageAssetHeight->SetAttribute( "type", "xs:unsignedInt" );
  1242. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetHeight );
  1243. }
  1244. //------------------------------------------------------------------------------
  1245. IMPLEMENT_CONOBJECT_SCHEMA(ImageAsset, WriteCustomTamlSchema);