ImageAsset.cc 44 KB

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