2
0

ImageAsset.cc 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  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, &writeExplicitMode, "");
  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. Vector2 ImageAsset::getExplicitCellOffset(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 NULL;
  440. }
  441. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  442. return(thisCell.mPixelOffset);
  443. }
  444. //------------------------------------------------------------------------------
  445. S32 ImageAsset::getExplicitCellWidth(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.mPixelWidth);
  455. }
  456. //------------------------------------------------------------------------------
  457. S32 ImageAsset::getExplicitCellHeight(const S32 cellIndex)
  458. {
  459. if ( !getExplicitMode() )
  460. {
  461. // No, so warn.
  462. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  463. return (0);
  464. }
  465. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  466. return(thisCell.mPixelHeight);
  467. }
  468. //------------------------------------------------------------------------------
  469. StringTableEntry ImageAsset::getExplicitCellName(const S32 cellIndex)
  470. {
  471. if ( !getExplicitMode() )
  472. {
  473. // No, so warn.
  474. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  475. return NULL;
  476. }
  477. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  478. return(thisCell.mRegionName);
  479. }
  480. //------------------------------------------------------------------------------
  481. S32 ImageAsset::getExplicitCellIndex(const char* regionName)
  482. {
  483. if ( !getExplicitMode() )
  484. {
  485. // No, so warn.
  486. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  487. return NULL;
  488. }
  489. // Set up a frame counter
  490. S32 frameCounter = 0;
  491. // Interate through the vector
  492. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  493. {
  494. // Grab the current pixelArea
  495. const FrameArea::PixelArea& pixelArea = *frameItr;
  496. // Check to see if the name matches the argument
  497. if (!dStrcmp(pixelArea.mRegionName, regionName))
  498. {
  499. // Found it, so return the frame
  500. return frameCounter;
  501. }
  502. else
  503. {
  504. ++frameCounter;
  505. }
  506. }
  507. // Didn't find it, so warn
  508. Con::warnf( "ImageAsset::getExplicitCellIndex() - Cannot find %s cell.", regionName );
  509. return NULL;
  510. }
  511. //------------------------------------------------------------------------------
  512. bool ImageAsset::containsNamedRegion(const char* regionName)
  513. {
  514. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  515. {
  516. // Grab the current pixelArea
  517. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  518. // Check to see if the name matches the argument
  519. if (!dStrcmp(pixelArea.mRegionName, regionName))
  520. {
  521. // Found it, so erase it and return success
  522. return true;
  523. }
  524. }
  525. return false;
  526. }
  527. //------------------------------------------------------------------------------
  528. bool ImageAsset::clearExplicitCells( void )
  529. {
  530. // Are we in explicit mode?
  531. if ( !getExplicitMode() )
  532. {
  533. // No, so warn.
  534. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  535. return false;
  536. }
  537. // Clear explicit frames.
  538. mExplicitFrames.clear();
  539. // Refresh the asset.
  540. refreshAsset();
  541. return true;
  542. }
  543. //------------------------------------------------------------------------------
  544. bool ImageAsset::addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  545. {
  546. // Are we in explicit mode?
  547. if ( !getExplicitMode() )
  548. {
  549. // No, so warn.
  550. Con::warnf( "ImageAsset::addExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  551. return false;
  552. }
  553. // Fetch the original image dimensions.
  554. const S32 imageWidth = getImageWidth();
  555. const S32 imageHeight = getImageHeight();
  556. // The region name cannot be empty
  557. if ( regionName == StringTable->EmptyString )
  558. {
  559. Con::warnf( "ImageAsset::addExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  560. U32 currentIndex = mExplicitFrames.size();
  561. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  562. dSscanf(regionName, "%i", currentIndex);
  563. }
  564. // The Cell Offset X needs to be within the image.
  565. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  566. {
  567. // Warn.
  568. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  569. return false;
  570. }
  571. // The Cell Offset Y needs to be within the image.
  572. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  573. {
  574. // Warn.
  575. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  576. return false;
  577. }
  578. // The Cell Width needs to be within the image.
  579. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  580. {
  581. // Warn.
  582. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  583. return false;
  584. }
  585. // The Cell Height needs to be within the image.
  586. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  587. {
  588. // Warn.
  589. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  590. return false;
  591. }
  592. // Store frame.
  593. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  594. mExplicitFrames.push_back( pixelArea );
  595. // Refresh the asset.
  596. refreshAsset();
  597. return true;
  598. }
  599. //------------------------------------------------------------------------------
  600. bool ImageAsset::insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  601. {
  602. // Are we in explicit mode?
  603. if ( !getExplicitMode() )
  604. {
  605. // No, so warn.
  606. Con::warnf( "ImageAsset::insertExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  607. return false;
  608. }
  609. // Fetch the original image dimensions.
  610. const S32 imageWidth = getImageWidth();
  611. const S32 imageHeight = getImageHeight();
  612. // Fetch the explicit frame count.
  613. const S32 explicitFramelCount = mExplicitFrames.size();
  614. // Region cannot be empty
  615. if ( regionName == StringTable->EmptyString )
  616. {
  617. Con::warnf( "ImageAsset::insertExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  618. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFramelCount );
  619. dSscanf(regionName, "%i", explicitFramelCount);
  620. }
  621. // The cell index needs to be in range.
  622. if ( cellIndex < 0 )
  623. {
  624. // Warn.
  625. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  626. return false;
  627. }
  628. // The Cell Offset X needs to be within the image.
  629. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  630. {
  631. // Warn.
  632. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  633. return false;
  634. }
  635. // The Cell Offset Y needs to be within the image.
  636. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  637. {
  638. // Warn.
  639. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  640. return false;
  641. }
  642. // The Cell Width needs to be within the image.
  643. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  644. {
  645. // Warn.
  646. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  647. return false;
  648. }
  649. // The Cell Height needs to be within the image.
  650. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  651. {
  652. // Warn.
  653. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  654. return false;
  655. }
  656. // Configure frame.
  657. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  658. // Insert frame appropriately.
  659. if ( cellIndex >= explicitFramelCount )
  660. {
  661. mExplicitFrames.push_back( pixelArea );
  662. }
  663. else
  664. {
  665. mExplicitFrames.insert( cellIndex );
  666. mExplicitFrames[cellIndex] = pixelArea;
  667. }
  668. // Refresh the asset.
  669. refreshAsset();
  670. return true;
  671. }
  672. //------------------------------------------------------------------------------
  673. bool ImageAsset::setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  674. {
  675. // Are we in explicit mode?
  676. if ( !getExplicitMode() )
  677. {
  678. // No, so warn.
  679. Con::warnf( "ImageAsset::setExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  680. return false;
  681. }
  682. // Fetch the original image dimensions.
  683. const S32 imageWidth = getImageWidth();
  684. const S32 imageHeight = getImageHeight();
  685. // Fetch the explicit frame count.
  686. const S32 explicitFrameCount = mExplicitFrames.size();
  687. // Region cannot be empty
  688. if ( regionName == StringTable->EmptyString )
  689. {
  690. Con::warnf( "ImageAsset::setExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  691. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFrameCount );
  692. dSscanf(regionName, "%i", explicitFrameCount);
  693. }
  694. // The cell index needs to be in range.
  695. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  696. {
  697. // Warn.
  698. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  699. return false;
  700. }
  701. // The Cell Offset X needs to be within the image.
  702. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  703. {
  704. // Warn.
  705. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  706. return false;
  707. }
  708. // The Cell Offset Y needs to be within the image.
  709. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  710. {
  711. // Warn.
  712. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  713. return false;
  714. }
  715. // The Cell Width needs to be within the image.
  716. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  717. {
  718. // Warn.
  719. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  720. return false;
  721. }
  722. // The Cell Height needs to be within the image.
  723. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  724. {
  725. // Warn.
  726. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  727. return false;
  728. }
  729. // Configure frame.
  730. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  731. // Set cell.
  732. mExplicitFrames[cellIndex] = pixelArea;
  733. // Refresh the asset.
  734. refreshAsset();
  735. return true;
  736. }
  737. //------------------------------------------------------------------------------
  738. bool ImageAsset::removeExplicitCell( const S32 cellIndex )
  739. {
  740. // Are we in explicit mode?
  741. if ( !getExplicitMode() )
  742. {
  743. // No, so warn.
  744. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  745. return false;
  746. }
  747. // Fetch the explicit frame count.
  748. const S32 explicitFrameCount = mExplicitFrames.size();
  749. // The cell index needs to be in range.
  750. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  751. {
  752. // Warn.
  753. Con::warnf( "ImageAsset::removeExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  754. return false;
  755. }
  756. // Remove cell.
  757. mExplicitFrames.erase(cellIndex);
  758. // Refresh the asset.
  759. refreshAsset();
  760. return true;
  761. }
  762. //------------------------------------------------------------------------------
  763. bool ImageAsset::removeExplicitCell( const char* regionName )
  764. {
  765. // Are we in explicit mode?
  766. if ( !getExplicitMode() )
  767. {
  768. // No, so warn.
  769. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  770. return false;
  771. }
  772. // Interate through the vector
  773. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  774. {
  775. // Grab the current pixelArea
  776. const FrameArea::PixelArea& pixelArea = *frameItr;
  777. // Check to see if the name matches the argument
  778. if (!dStrcmp(pixelArea.mRegionName, regionName))
  779. {
  780. // Found it, so erase it and return success
  781. mExplicitFrames.erase(frameItr);
  782. return true;
  783. }
  784. }
  785. // Didn't find it, so warn
  786. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot find %s cell to remove.", regionName );
  787. return false;
  788. }
  789. //------------------------------------------------------------------------------
  790. ImageAsset::FrameArea& ImageAsset::getCellByName( const char* cellName)
  791. {
  792. // If the cellName was empty
  793. if (cellName == StringTable->EmptyString)
  794. {
  795. // Warn and return a bad frame
  796. Con::warnf( "ImageAsset::getCellByName() - Empty cell name was passed." );
  797. return BadFrameArea;
  798. }
  799. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  800. {
  801. // Grab the current pixelArea
  802. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  803. // Check to see if the name matches the argument
  804. if (!dStrcmp(pixelArea.mRegionName, cellName))
  805. {
  806. // Found it, so erase it and return success
  807. return *frameItr;
  808. }
  809. }
  810. // Didn't find it, so warn and return a bad frame
  811. Con::warnf( "ImageAsset::getCellByName() - Cannot find %s cell.", cellName );
  812. return BadFrameArea;
  813. }
  814. //------------------------------------------------------------------------------
  815. void ImageAsset::setTextureFilter( const TextureFilterMode filterMode )
  816. {
  817. // Finish if no texture.
  818. if ( mImageTextureHandle.IsNull() )
  819. return;
  820. // Select Hardware Filter Mode.
  821. GLint glFilterMode;
  822. switch( filterMode )
  823. {
  824. // Nearest ("none").
  825. case FILTER_NEAREST:
  826. {
  827. glFilterMode = GL_NEAREST;
  828. } break;
  829. // Bilinear ("smooth").
  830. case FILTER_BILINEAR:
  831. {
  832. glFilterMode = GL_LINEAR;
  833. } break;
  834. // Huh?
  835. default:
  836. // Oh well...
  837. glFilterMode = GL_LINEAR;
  838. };
  839. // Set the texture objects filter mode.
  840. mImageTextureHandle.setFilter( glFilterMode );
  841. }
  842. //------------------------------------------------------------------------------
  843. void ImageAsset::initializeAsset( void )
  844. {
  845. // Call parent.
  846. Parent::initializeAsset();
  847. // Ensure the image-file is expanded.
  848. mImageFile = expandAssetFilePath( mImageFile );
  849. // Calculate the image.
  850. calculateImage();
  851. }
  852. //------------------------------------------------------------------------------
  853. void ImageAsset::onAssetRefresh( void )
  854. {
  855. // Ignore if not yet added to the sim.
  856. if ( !isProperlyAdded() )
  857. return;
  858. // Call parent.
  859. Parent::onAssetRefresh();
  860. // Compile image.
  861. calculateImage();
  862. }
  863. //-----------------------------------------------------------------------------
  864. void ImageAsset::onTamlPreWrite( void )
  865. {
  866. // Call parent.
  867. Parent::onTamlPreWrite();
  868. // Ensure the image-file is collapsed.
  869. mImageFile = collapseAssetFilePath( mImageFile );
  870. }
  871. //-----------------------------------------------------------------------------
  872. void ImageAsset::onTamlPostWrite( void )
  873. {
  874. // Call parent.
  875. Parent::onTamlPostWrite();
  876. // Ensure the image-file is expanded.
  877. mImageFile = expandAssetFilePath( mImageFile );
  878. }
  879. //------------------------------------------------------------------------------
  880. void ImageAsset::calculateImage( void )
  881. {
  882. // Debug Profiling.
  883. PROFILE_SCOPE(ImageAsset_CalculateImage);
  884. // Clear frames.
  885. mFrames.clear();
  886. // If we have an existing texture and we're setting to the same bitmap then force the texture manager
  887. // to refresh the texture itself.
  888. if ( !mImageTextureHandle.IsNull() && dStricmp(mImageTextureHandle.getTextureKey(), mImageFile) == 0 )
  889. TextureManager::refresh( mImageFile );
  890. // Get image texture.
  891. mImageTextureHandle.set( mImageFile, TextureHandle::BitmapTexture, true, getForce16Bit() );
  892. // Is the texture valid?
  893. if ( mImageTextureHandle.IsNull() )
  894. {
  895. // No, so warn.
  896. Con::warnf( "Image '%s' could not load texture '%s'.", getAssetId(), mImageFile );
  897. return;
  898. }
  899. // Is the local filter mode specified?
  900. if ( mLocalFilterMode != FILTER_INVALID )
  901. {
  902. // Yes, so set filter mode.
  903. setTextureFilter( mLocalFilterMode );
  904. }
  905. else
  906. {
  907. TextureFilterMode filterMode = FILTER_NEAREST;
  908. // No, so fetch the global filter.
  909. const char* pGlobalFilter = Con::getVariable( "$pref::T2D::imageAssetGlobalFilterMode" );
  910. // Fetch the global filter mode.
  911. if ( pGlobalFilter != NULL && dStrlen(pGlobalFilter) > 0 )
  912. filterMode = getFilterModeEnum( pGlobalFilter );
  913. // If global filter mode is invalid then use local filter mode.
  914. if ( filterMode == FILTER_INVALID )
  915. filterMode = FILTER_NEAREST;
  916. // Set filter mode.
  917. setTextureFilter( filterMode );
  918. }
  919. // Calculate according to mode.
  920. if ( mExplicitMode )
  921. {
  922. calculateExplicitMode();
  923. }
  924. else
  925. {
  926. calculateImplicitMode();
  927. }
  928. }
  929. //------------------------------------------------------------------------------
  930. void ImageAsset::calculateImplicitMode( void )
  931. {
  932. // Debug Profiling.
  933. PROFILE_SCOPE(ImageAsset_CalculateImplicitMode);
  934. // Sanity!
  935. AssertFatal( !mExplicitMode, "Cannot calculate implicit cells when in explicit mode." );
  936. // Fetch the texture object.
  937. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  938. // Calculate texel scales.
  939. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  940. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  941. // Fetch the original image dimensions.
  942. const S32 imageWidth = getImageWidth();
  943. const S32 imageHeight = getImageHeight();
  944. // Set full-frame as default.
  945. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  946. mFrames.push_back( frameArea );
  947. // Finish if no cell counts are specified. This is how we default to full-frame mode.
  948. if ( mCellCountX < 1 || mCellCountY < 1 )
  949. return;
  950. // The cell width needs to be at maximum the image width!
  951. if ( mCellWidth < 1 || mCellWidth > imageWidth )
  952. {
  953. // Warn.
  954. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Width of %d.", mCellWidth );
  955. return;
  956. }
  957. // The cell height needs to be at maximum the image height!
  958. if ( mCellHeight < 1 || mCellHeight > imageHeight )
  959. {
  960. // Warn.
  961. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Height of %d.", mCellHeight );
  962. return;
  963. }
  964. // The Cell Offset X needs to be within the image.
  965. if ( mCellOffsetX < 0 || mCellOffsetX >= imageWidth )
  966. {
  967. // Warn.
  968. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX of %d.", mCellOffsetX );
  969. return;
  970. }
  971. // The Cell Offset Y needs to be within the image.
  972. if ( mCellOffsetY < 0 || mCellOffsetY >= imageHeight )
  973. {
  974. // Warn.
  975. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY of %d.", mCellOffsetY );
  976. return;
  977. }
  978. // Are we using Cell-StrideX?
  979. S32 cellStepX;
  980. if ( mCellStrideX != 0 )
  981. {
  982. // Yes, so set stepX to be StrideX.
  983. cellStepX = mCellStrideX;
  984. }
  985. else
  986. {
  987. // No, so set stepY to be Cell Width.
  988. cellStepX = mCellWidth;
  989. }
  990. // Are we using Cell-StrideY?
  991. S32 cellStepY;
  992. if ( mCellStrideY != 0 )
  993. {
  994. // Yes, so set stepY to be StrideY.
  995. cellStepY = mCellStrideY;
  996. }
  997. else
  998. {
  999. // No, so set stepY to be Cell Height.
  1000. cellStepY = mCellHeight;
  1001. }
  1002. // Calculate Final Cell Position X.
  1003. S32 cellFinalPositionX = mCellOffsetX + ((mCellCountX-((cellStepX<0)?1:0))*cellStepX);
  1004. // Off Left?
  1005. if ( cellFinalPositionX < 0 )
  1006. {
  1007. // Warn.
  1008. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image left-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  1009. return;
  1010. }
  1011. // Off Right?
  1012. else if ( cellFinalPositionX > imageWidth )
  1013. {
  1014. // Warn.
  1015. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image right-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  1016. return;
  1017. }
  1018. // Calculate Final Cell Position Y.
  1019. S32 cellFinalPositionY = mCellOffsetY + ((mCellCountY-((cellStepY<0)?1:0))*cellStepY);
  1020. // Off Top?
  1021. if ( cellFinalPositionY < 0 )
  1022. {
  1023. // Warn.
  1024. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image top-side.", mCellOffsetY, mCellHeight, mCellCountY );
  1025. return;
  1026. }
  1027. // Off Bottom?
  1028. else if ( cellFinalPositionY > imageHeight )
  1029. {
  1030. // Warn.
  1031. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image bottom-side.", mCellOffsetY, mCellHeight, mCellCountY );
  1032. return;
  1033. }
  1034. // Clear default frame.
  1035. mFrames.clear();
  1036. // Cell Row Order?
  1037. if ( mCellRowOrder )
  1038. {
  1039. // Yes, so RowRow Order.
  1040. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  1041. {
  1042. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  1043. {
  1044. // Set frame area.
  1045. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  1046. // Store fame.
  1047. mFrames.push_back( frameArea );
  1048. }
  1049. }
  1050. return;
  1051. }
  1052. // No, so Column Order.
  1053. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  1054. {
  1055. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  1056. {
  1057. // Set frame area.
  1058. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  1059. // Store fame.
  1060. mFrames.push_back( frameArea );
  1061. }
  1062. }
  1063. }
  1064. //------------------------------------------------------------------------------
  1065. void ImageAsset::calculateExplicitMode( void )
  1066. {
  1067. // Debug Profiling.
  1068. PROFILE_SCOPE(ImageAsset_CalculateExplicitMode);
  1069. // Sanity!
  1070. AssertFatal( mExplicitMode, "Cannot calculate explicit cells when not in explicit mode." );
  1071. // Fetch the texture object.
  1072. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  1073. // Calculate texel scales.
  1074. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  1075. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  1076. // Fetch the original image dimensions.
  1077. const S32 imageWidth = getImageWidth();
  1078. const S32 imageHeight = getImageHeight();
  1079. // Clear default frame.
  1080. mFrames.clear();
  1081. // Are any explicit frames set.
  1082. if ( mExplicitFrames.size() == 0 )
  1083. {
  1084. // No, so set full-frame as default.
  1085. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  1086. mFrames.push_back( frameArea );
  1087. return;
  1088. }
  1089. // Iterate explicit frames.
  1090. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1091. {
  1092. // Fetch pixel area.
  1093. const FrameArea::PixelArea& pixelArea = *frameItr;
  1094. // Set frame area.
  1095. FrameArea frameArea( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, texelWidthScale, texelHeightScale, pixelArea.mRegionName );
  1096. // Store frame.
  1097. mFrames.push_back( frameArea );
  1098. }
  1099. }
  1100. //------------------------------------------------------------------------------
  1101. bool ImageAsset::setFilterMode( void* obj, const char* data )
  1102. {
  1103. static_cast<ImageAsset*>(obj)->setFilterMode(getFilterModeEnum(data));
  1104. return false;
  1105. }
  1106. //------------------------------------------------------------------------------
  1107. void ImageAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
  1108. {
  1109. // Debug Profiling.
  1110. PROFILE_SCOPE(ImageAsset_OnTamlCustomWrite);
  1111. // Call parent.
  1112. Parent::onTamlCustomWrite( customNodes );
  1113. // Finish if not in explicit mode.
  1114. if ( !mExplicitMode )
  1115. return;
  1116. if (mExplicitFrames.size() > 0)
  1117. {
  1118. // Add cell custom node.
  1119. TamlCustomNode* pCustomCellNodes = customNodes.addNode( cellCustomNodeCellsName );
  1120. // Iterate explicit frames.
  1121. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1122. {
  1123. // Fetch pixel area.
  1124. const FrameArea::PixelArea& pixelArea = *frameItr;
  1125. // Add cell alias.
  1126. TamlCustomNode* pCellNode = pCustomCellNodes->addNode( cellNodeName );
  1127. // Add cell properties.
  1128. pCellNode->addField( cellRegionName, pixelArea.mRegionName );
  1129. pCellNode->addField( cellOffsetName, pixelArea.mPixelOffset );
  1130. pCellNode->addField( cellWidthName, pixelArea.mPixelWidth );
  1131. pCellNode->addField( cellHeightName, pixelArea.mPixelHeight );
  1132. }
  1133. }
  1134. }
  1135. //-----------------------------------------------------------------------------
  1136. void ImageAsset::onTamlCustomRead( const TamlCustomNodes& customNodes )
  1137. {
  1138. // Debug Profiling.
  1139. PROFILE_SCOPE(ImageAsset_OnTamlCustomRead);
  1140. // Call parent.
  1141. Parent::onTamlCustomRead( customNodes );
  1142. // Find cell custom node.
  1143. const TamlCustomNode* pCustomCellNodes = customNodes.findNode( cellCustomNodeCellsName );
  1144. // Continue if we have explicit cells.
  1145. if ( pCustomCellNodes != NULL )
  1146. {
  1147. // Set explicit mode.
  1148. mExplicitMode = true;
  1149. // Fetch children cell nodes.
  1150. const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
  1151. // Iterate cells.
  1152. for( TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr )
  1153. {
  1154. // Fetch cell node.
  1155. TamlCustomNode* pCellNode = *cellNodeItr;
  1156. // Fetch node name.
  1157. StringTableEntry nodeName = pCellNode->getNodeName();
  1158. // Is this a valid alias?
  1159. if ( nodeName != cellNodeName )
  1160. {
  1161. // No, so warn.
  1162. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, cellNodeName );
  1163. continue;
  1164. }
  1165. Point2I cellOffset(-1, -1);
  1166. S32 cellWidth = 0;
  1167. S32 cellHeight = 0;
  1168. const char* regionName = StringTable->EmptyString;
  1169. // Fetch fields.
  1170. const TamlCustomFieldVector& fields = pCellNode->getFields();
  1171. // Iterate property fields.
  1172. for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
  1173. {
  1174. // Fetch field.
  1175. const TamlCustomField* pField = *fieldItr;
  1176. // Fetch field name.
  1177. StringTableEntry fieldName = pField->getFieldName();
  1178. // Check common fields.
  1179. if ( fieldName == cellRegionName )
  1180. {
  1181. regionName = pField->getFieldValue();
  1182. }
  1183. else if ( fieldName == cellOffsetName )
  1184. {
  1185. pField->getFieldValue( cellOffset );
  1186. }
  1187. else if ( fieldName == cellOffsetXName )
  1188. {
  1189. pField->getFieldValue( cellOffset.x );
  1190. }
  1191. else if ( fieldName == cellOffsetYName )
  1192. {
  1193. pField->getFieldValue( cellOffset.y );
  1194. }
  1195. else if ( fieldName == cellWidthName )
  1196. {
  1197. pField->getFieldValue( cellWidth );
  1198. }
  1199. else if ( fieldName == cellHeightName )
  1200. {
  1201. pField->getFieldValue( cellHeight );
  1202. }
  1203. else
  1204. {
  1205. // Unknown name so warn.
  1206. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName );
  1207. continue;
  1208. }
  1209. }
  1210. // Does the region have a name
  1211. if ( regionName == StringTable->EmptyString )
  1212. {
  1213. // No, so warn and set it to the next index
  1214. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell name of '%s' is invalid or was not set.", regionName );
  1215. U32 currentIndex = mExplicitFrames.size();
  1216. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  1217. dSscanf(regionName, "%i", currentIndex);
  1218. }
  1219. // Is cell offset valid?
  1220. if ( cellOffset.x < 0 || cellOffset.y < 0 )
  1221. {
  1222. // No, so warn.
  1223. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell offset of '(%d,%d)' is invalid or was not set.", cellOffset.x, cellOffset.y );
  1224. continue;
  1225. }
  1226. // Is cell width valid?
  1227. if ( cellWidth <= 0 )
  1228. {
  1229. // No, so warn.
  1230. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell width of '%d' is invalid or was not set.", cellWidth );
  1231. continue;
  1232. }
  1233. // Is cell height valid?
  1234. if ( cellHeight <= 0 )
  1235. {
  1236. // No, so warn.
  1237. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell height of '%d' is invalid or was not set.", cellHeight );
  1238. continue;
  1239. }
  1240. // Add explicit frame.
  1241. FrameArea::PixelArea pixelArea( cellOffset.x, cellOffset.y, cellWidth, cellHeight, regionName );
  1242. mExplicitFrames.push_back( pixelArea );
  1243. }
  1244. }
  1245. }
  1246. //-----------------------------------------------------------------------------
  1247. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  1248. {
  1249. // Sanity!
  1250. AssertFatal( pClassRep != NULL, "ImageAsset::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  1251. AssertFatal( pParentElement != NULL, "ImageAsset::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  1252. char buffer[1024];
  1253. // Create ImageAsset node element.
  1254. TiXmlElement* pImageAssetNodeElement = new TiXmlElement( "xs:element" );
  1255. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), cellCustomNodeCellsName );
  1256. pImageAssetNodeElement->SetAttribute( "name", buffer );
  1257. pImageAssetNodeElement->SetAttribute( "minOccurs", 0 );
  1258. pImageAssetNodeElement->SetAttribute( "maxOccurs", 1 );
  1259. pParentElement->LinkEndChild( pImageAssetNodeElement );
  1260. // Create complex type.
  1261. TiXmlElement* pImageAssetNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1262. pImageAssetNodeElement->LinkEndChild( pImageAssetNodeComplexTypeElement );
  1263. // Create choice element.
  1264. TiXmlElement* pImageAssetNodeChoiceElement = new TiXmlElement( "xs:choice" );
  1265. pImageAssetNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  1266. pImageAssetNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  1267. pImageAssetNodeComplexTypeElement->LinkEndChild( pImageAssetNodeChoiceElement );
  1268. // Create ImageAsset element.
  1269. TiXmlElement* pImageAssetElement = new TiXmlElement( "xs:element" );
  1270. pImageAssetElement->SetAttribute( "name", cellNodeName );
  1271. pImageAssetElement->SetAttribute( "minOccurs", 0 );
  1272. pImageAssetElement->SetAttribute( "maxOccurs", 1 );
  1273. pImageAssetNodeChoiceElement->LinkEndChild( pImageAssetElement );
  1274. // Create complex type Element.
  1275. TiXmlElement* pImageAssetComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1276. pImageAssetElement->LinkEndChild( pImageAssetComplexTypeElement );
  1277. // Create "RegionName" attribute.
  1278. TiXmlElement* pImageRegionName = new TiXmlElement( "xs:attribute" );
  1279. pImageRegionName->SetAttribute( "name", cellRegionName );
  1280. pImageRegionName->SetAttribute( "type", "xs:string" );
  1281. pImageAssetComplexTypeElement->LinkEndChild( pImageRegionName );
  1282. // Create "Offset" attribute.
  1283. TiXmlElement* pImageAssetOffset = new TiXmlElement( "xs:attribute" );
  1284. pImageAssetOffset->SetAttribute( "name", cellOffsetName );
  1285. pImageAssetOffset->SetAttribute( "type", "Point2I_ConsoleType" );
  1286. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetOffset );
  1287. // Create "Width" attribute.
  1288. TiXmlElement* pImageAssetWidth = new TiXmlElement( "xs:attribute" );
  1289. pImageAssetWidth->SetAttribute( "name", cellWidthName );
  1290. pImageAssetWidth->SetAttribute( "type", "xs:unsignedInt" );
  1291. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetWidth );
  1292. // Create "Height" attribute.
  1293. TiXmlElement* pImageAssetHeight = new TiXmlElement( "xs:attribute" );
  1294. pImageAssetHeight->SetAttribute( "name", cellHeightName );
  1295. pImageAssetHeight->SetAttribute( "type", "xs:unsignedInt" );
  1296. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetHeight );
  1297. }
  1298. //------------------------------------------------------------------------------
  1299. IMPLEMENT_CONOBJECT_SCHEMA(ImageAsset, WriteCustomTamlSchema);