ImageAsset.cc 53 KB

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