ImageAsset.cc 52 KB

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