ImageAsset.cc 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065
  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. static StringTableEntry imageLayerCustomNodeName = StringTable->insert("ImageLayers");
  95. static StringTableEntry layerNodeName = StringTable->insert("ImageLayer");
  96. static StringTableEntry layerImageFile = StringTable->insert("ImageFile");
  97. static StringTableEntry layerPositionName = StringTable->insert("Position");
  98. static StringTableEntry layerBlendColorName = StringTable->insert("BlendColor");
  99. //------------------------------------------------------------------------------
  100. static EnumTable::Enums textureFilterLookup[] =
  101. {
  102. { ImageAsset::FILTER_NEAREST, "NEAREST" },
  103. { ImageAsset::FILTER_BILINEAR, "BILINEAR" },
  104. { ImageAsset::FILTER_INVALID, "DEFAULT" }
  105. };
  106. EnumTable textureFilterTable(sizeof(textureFilterLookup) / sizeof(EnumTable::Enums), &textureFilterLookup[0]);
  107. //------------------------------------------------------------------------------
  108. ImageAsset::TextureFilterMode ImageAsset::getFilterModeEnum(const char* label)
  109. {
  110. // Search for Mnemonic.
  111. for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
  112. if( dStricmp(textureFilterLookup[i].label, label) == 0)
  113. return((ImageAsset::TextureFilterMode)textureFilterLookup[i].index);
  114. // Warn.
  115. Con::warnf( "ImageAsset::getFilterModeEnum() - Invalid filter-mode '%s'", label );
  116. return ImageAsset::FILTER_INVALID;
  117. }
  118. //------------------------------------------------------------------------------
  119. const char* ImageAsset::getFilterModeDescription( ImageAsset::TextureFilterMode filterMode )
  120. {
  121. // Search for Mode.
  122. for(U32 i = 0; i < (sizeof(textureFilterLookup) / sizeof(EnumTable::Enums)); i++)
  123. if( textureFilterLookup[i].index == filterMode )
  124. return textureFilterLookup[i].label;
  125. // Warn.
  126. Con::warnf( "ImageAsset::getFilterModeDescription() - Invalid filter-mode." );
  127. return StringTable->EmptyString;
  128. }
  129. //------------------------------------------------------------------------------
  130. ImageAsset::ImageAsset() : mImageFile(StringTable->EmptyString),
  131. mForce16Bit(false),
  132. mLocalFilterMode(FILTER_INVALID),
  133. mExplicitMode(false),
  134. mCellRowOrder(true),
  135. mCellOffsetX(0),
  136. mCellOffsetY(0),
  137. mCellStrideX(0),
  138. mCellStrideY(0),
  139. mCellCountX(0),
  140. mCellCountY(0),
  141. mCellWidth(0),
  142. mCellHeight(0),
  143. mBlendColor(1.0f, 1.0f, 1.0f),
  144. mImageTextureHandle(NULL)
  145. {
  146. // Set Vector Associations.
  147. VECTOR_SET_ASSOCIATION( mFrames );
  148. VECTOR_SET_ASSOCIATION( mExplicitFrames );
  149. VECTOR_SET_ASSOCIATION(mImageLayers);
  150. }
  151. //------------------------------------------------------------------------------
  152. ImageAsset::~ImageAsset()
  153. {
  154. }
  155. //------------------------------------------------------------------------------
  156. void ImageAsset::initPersistFields()
  157. {
  158. // Call parent.
  159. Parent::initPersistFields();
  160. // Fields.
  161. addGroup("Image Fields");
  162. addProtectedField("ImageFile", TypeAssetLooseFilePath, Offset(mImageFile, ImageAsset), &setImageFile, &getImageFile, &defaultProtectedWriteFn, "");
  163. addProtectedField("Force16bit", TypeBool, Offset(mForce16Bit, ImageAsset), &setForce16Bit, &defaultProtectedGetFn, &writeForce16Bit, "Forces the image into 16 bit mode.");
  164. addProtectedField("FilterMode", TypeEnum, Offset(mLocalFilterMode, ImageAsset), &setFilterMode, &defaultProtectedGetFn, &writeFilterMode, 1, &textureFilterTable);
  165. addProtectedField("ExplicitMode", TypeBool, Offset(mExplicitMode, ImageAsset), &setExplicitMode, &defaultProtectedGetFn, &writeExplicitMode, "");
  166. addProtectedField("CellRowOrder", TypeBool, Offset(mCellRowOrder, ImageAsset), &setCellRowOrder, &defaultProtectedGetFn, &writeCellRowOrder, "If true, cell number are applied left-to-right, top-to-bottom.");
  167. addProtectedField("BlendColor", TypeColorF, Offset(mBlendColor, ImageAsset), &setBlendColor, &defaultProtectedGetFn, &writeBlendColor, "Changes the base color of the texture. You should only use this if you are using layers.");
  168. endGroup("Image Fields");
  169. addGroup("X Values");
  170. addProtectedField("CellWidth", TypeS32, Offset(mCellWidth, ImageAsset), &setCellWidth, &defaultProtectedGetFn, &writeCellWidth, "The width of each cell.");
  171. addProtectedField("CellCountX", TypeS32, Offset(mCellCountX, ImageAsset), &setCellCountX, &defaultProtectedGetFn, &writeCellCountX, "The number of cells in the X direction.");
  172. 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.");
  173. 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.");
  174. endGroup("X Values");
  175. addGroup("Y Values");
  176. addProtectedField("CellHeight", TypeS32, Offset(mCellHeight, ImageAsset), &setCellHeight, &defaultProtectedGetFn, &writeCellHeight, "The height of each cell.");
  177. addProtectedField("CellCountY", TypeS32, Offset(mCellCountY, ImageAsset), &setCellCountY, &defaultProtectedGetFn, &writeCellCountY, "The number of cells in the Y direction.");
  178. 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.");
  179. 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.");
  180. endGroup("Y Values");
  181. }
  182. //------------------------------------------------------------------------------
  183. bool ImageAsset::onAdd()
  184. {
  185. // Call Parent.
  186. if (!Parent::onAdd())
  187. return false;
  188. return true;
  189. }
  190. //------------------------------------------------------------------------------
  191. void ImageAsset::onRemove()
  192. {
  193. // Call Parent.
  194. Parent::onRemove();
  195. }
  196. //------------------------------------------------------------------------------
  197. void ImageAsset::setImageFile( const char* pImageFile )
  198. {
  199. // Sanity!
  200. AssertFatal( pImageFile != NULL, "Cannot use a NULL image file." );
  201. // Fetch image file.
  202. pImageFile = StringTable->insert( pImageFile );
  203. // Ignore no change,
  204. if ( pImageFile == mImageFile )
  205. return;
  206. // Update.
  207. mImageFile = getOwned() ? expandAssetFilePath( pImageFile ) : StringTable->insert( pImageFile );
  208. // Refresh the asset.
  209. refreshAsset();
  210. }
  211. //------------------------------------------------------------------------------
  212. void ImageAsset::copyTo(SimObject* object)
  213. {
  214. // Call to parent.
  215. Parent::copyTo(object);
  216. // Cast to asset.
  217. ImageAsset* pAsset = static_cast<ImageAsset*>(object);
  218. // Sanity!
  219. AssertFatal(pAsset != NULL, "ImageAsset::copyTo() - Object is not the correct type.");
  220. // Copy state.
  221. pAsset->setImageFile( getImageFile() );
  222. pAsset->setForce16Bit( getForce16Bit() );
  223. pAsset->setFilterMode( getFilterMode() );
  224. pAsset->setExplicitMode( getExplicitMode() );
  225. pAsset->setCellRowOrder( getCellRowOrder() );
  226. pAsset->setCellOffsetX( getCellCountX() );
  227. pAsset->setCellOffsetY( getCellCountY() );
  228. pAsset->setCellStrideX( getCellStrideX() );
  229. pAsset->setCellStrideY( getCellStrideY() );
  230. pAsset->setCellCountX( getCellCountX() );
  231. pAsset->setCellCountY( getCellCountY() );
  232. pAsset->setCellWidth( getCellWidth() );
  233. pAsset->setCellHeight( getCellHeight() );
  234. // Finish if not in explicit mode.
  235. if ( !getExplicitMode() )
  236. return;
  237. // Fetch the explicit cell count.
  238. const S32 explicitCellCount = getExplicitCellCount();
  239. // Finish if no explicit cells exist.
  240. if ( explicitCellCount == 0 )
  241. return;
  242. // Copy explicit cells.
  243. pAsset->clearExplicitCells();
  244. for( S32 index = 0; index < explicitCellCount; ++index )
  245. {
  246. // Fetch the cell pixel area.
  247. const FrameArea::PixelArea& pixelArea = getImageFrameArea( index ).mPixelArea;
  248. // Add the explicit cell.
  249. pAsset->addExplicitCell( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, pixelArea.mRegionName );
  250. }
  251. }
  252. //------------------------------------------------------------------------------
  253. void ImageAsset::setForce16Bit( const bool force16Bit )
  254. {
  255. // Ignore no change,
  256. if ( force16Bit == mForce16Bit )
  257. return;
  258. // Update.
  259. mForce16Bit = force16Bit;
  260. // Refresh the asset.
  261. refreshAsset();
  262. }
  263. //------------------------------------------------------------------------------
  264. void ImageAsset::setFilterMode( const ImageAsset::TextureFilterMode filterMode )
  265. {
  266. // Ignore no change,
  267. if ( filterMode == mLocalFilterMode )
  268. return;
  269. // Update.
  270. mLocalFilterMode = filterMode;
  271. // Refresh the asset.
  272. refreshAsset();
  273. }
  274. //------------------------------------------------------------------------------
  275. void ImageAsset::setExplicitMode( const bool explicitMode )
  276. {
  277. // Ignore no change,
  278. if ( explicitMode == mExplicitMode )
  279. return;
  280. // Update.
  281. mExplicitMode = explicitMode;
  282. // Refresh the asset.
  283. refreshAsset();
  284. }
  285. //------------------------------------------------------------------------------
  286. void ImageAsset::setCellRowOrder( const bool cellRowOrder )
  287. {
  288. // Ignore no change.
  289. if ( cellRowOrder == mCellRowOrder )
  290. return;
  291. // Update.
  292. mCellRowOrder = cellRowOrder;
  293. // Refresh the asset.
  294. refreshAsset();
  295. }
  296. //------------------------------------------------------------------------------
  297. void ImageAsset::setCellOffsetX( const S32 cellOffsetX )
  298. {
  299. // Ignore no change.
  300. if ( cellOffsetX == mCellOffsetX )
  301. return;
  302. // Valid?
  303. if ( cellOffsetX < 0 )
  304. {
  305. // No, so warn.
  306. Con::warnf( "Invalid CELL offset X '%d'.", cellOffsetX );
  307. return;
  308. }
  309. // Update.
  310. mCellOffsetX = cellOffsetX;
  311. // Refresh the asset.
  312. refreshAsset();
  313. }
  314. //------------------------------------------------------------------------------
  315. void ImageAsset::setCellOffsetY( const S32 cellOffsetY )
  316. {
  317. // Ignore no change.
  318. if ( cellOffsetY == mCellOffsetY )
  319. return;
  320. // Valid?
  321. if ( cellOffsetY < 0 )
  322. {
  323. // No, so warn.
  324. Con::warnf( "Invalid CELL offset Y '%d'.", cellOffsetY );
  325. return;
  326. }
  327. // Update.
  328. mCellOffsetY = cellOffsetY;
  329. // Refresh the asset.
  330. refreshAsset();
  331. }
  332. //------------------------------------------------------------------------------
  333. void ImageAsset::setCellStrideX( const S32 cellStrideX )
  334. {
  335. // Ignore no change.
  336. if ( cellStrideX == mCellStrideX )
  337. return;
  338. // Valid?
  339. if ( cellStrideX < 0 )
  340. {
  341. // No, so warn.
  342. Con::warnf( "Invalid CELL stride X '%d'.", cellStrideX );
  343. return;
  344. }
  345. // Update.
  346. mCellStrideX = cellStrideX;
  347. // Refresh the asset.
  348. refreshAsset();
  349. }
  350. //------------------------------------------------------------------------------
  351. void ImageAsset::setCellStrideY( const S32 cellStrideY )
  352. {
  353. // Ignore no change.
  354. if ( cellStrideY == mCellStrideY )
  355. return;
  356. // Valid?
  357. if ( cellStrideY < 0 )
  358. {
  359. // No, so warn.
  360. Con::warnf( "Invalid CELL stride Y '%d'.", cellStrideY );
  361. return;
  362. }
  363. // Update.
  364. mCellStrideY = cellStrideY;
  365. // Refresh the asset.
  366. refreshAsset();
  367. }
  368. //------------------------------------------------------------------------------
  369. void ImageAsset::setCellCountX( const S32 cellCountX )
  370. {
  371. // Ignore no change.
  372. if ( cellCountX == mCellCountX )
  373. return;
  374. // Valid?
  375. if ( cellCountX < 0 )
  376. {
  377. // No, so warn.
  378. Con::warnf( "Invalid CELL count X '%d'.", cellCountX );
  379. return;
  380. }
  381. // Update.
  382. mCellCountX = cellCountX;
  383. // Refresh the asset.
  384. refreshAsset();
  385. }
  386. //------------------------------------------------------------------------------
  387. void ImageAsset::setCellCountY( const S32 cellCountY )
  388. {
  389. // Ignore no change.
  390. if ( cellCountY == mCellCountY )
  391. return;
  392. // Valid?
  393. if ( cellCountY < 0 )
  394. {
  395. // No, so warn.
  396. Con::warnf( "Invalid CELL count Y '%d'.", cellCountY );
  397. return;
  398. }
  399. // Update.
  400. mCellCountY = cellCountY;
  401. // Refresh the asset.
  402. refreshAsset();
  403. }
  404. //------------------------------------------------------------------------------
  405. void ImageAsset::setCellWidth( const S32 cellWidth )
  406. {
  407. // Ignore no change.
  408. if ( cellWidth == mCellWidth )
  409. return;
  410. // Valid?
  411. if ( cellWidth < 0 )
  412. {
  413. // No, so warn.
  414. Con::warnf( "Invalid CELL width '%d'.", cellWidth );
  415. return;
  416. }
  417. // Update.
  418. mCellWidth = cellWidth;
  419. // Refresh the asset.
  420. refreshAsset();
  421. }
  422. //------------------------------------------------------------------------------
  423. void ImageAsset::setCellHeight( const S32 cellheight )
  424. {
  425. // Ignore no change.
  426. if ( cellheight == mCellHeight )
  427. return;
  428. // Valid?
  429. if ( cellheight < 0 )
  430. {
  431. // No, so warn.
  432. Con::warnf( "Invalid CELL height '%d'.", cellheight );
  433. return;
  434. }
  435. // Update.
  436. mCellHeight = cellheight;
  437. // Refresh the asset.
  438. refreshAsset();
  439. }
  440. //------------------------------------------------------------------------------
  441. Vector2 ImageAsset::getExplicitCellOffset(const S32 cellIndex)
  442. {
  443. if ( !getExplicitMode() )
  444. {
  445. // No, so warn.
  446. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  447. return NULL;
  448. }
  449. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  450. return(thisCell.mPixelOffset);
  451. }
  452. //------------------------------------------------------------------------------
  453. S32 ImageAsset::getExplicitCellWidth(const S32 cellIndex)
  454. {
  455. if ( !getExplicitMode() )
  456. {
  457. // No, so warn.
  458. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  459. return (0);
  460. }
  461. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  462. return(thisCell.mPixelWidth);
  463. }
  464. //------------------------------------------------------------------------------
  465. S32 ImageAsset::getExplicitCellHeight(const S32 cellIndex)
  466. {
  467. if ( !getExplicitMode() )
  468. {
  469. // No, so warn.
  470. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  471. return (0);
  472. }
  473. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  474. return(thisCell.mPixelHeight);
  475. }
  476. //------------------------------------------------------------------------------
  477. StringTableEntry ImageAsset::getExplicitCellName(const S32 cellIndex)
  478. {
  479. if ( !getExplicitMode() )
  480. {
  481. // No, so warn.
  482. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  483. return NULL;
  484. }
  485. ImageAsset::FrameArea::PixelArea thisCell = mExplicitFrames.at(cellIndex);
  486. return(thisCell.mRegionName);
  487. }
  488. //------------------------------------------------------------------------------
  489. S32 ImageAsset::getExplicitCellIndex(const char* regionName)
  490. {
  491. if ( !getExplicitMode() )
  492. {
  493. // No, so warn.
  494. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  495. return -1;
  496. }
  497. // Set up a frame counter
  498. S32 frameCounter = 0;
  499. // Interate through the vector
  500. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  501. {
  502. // Grab the current pixelArea
  503. const FrameArea::PixelArea& pixelArea = *frameItr;
  504. // Check to see if the name matches the argument
  505. if (!dStrcmp(pixelArea.mRegionName, regionName))
  506. {
  507. // Found it, so return the frame
  508. return frameCounter;
  509. }
  510. else
  511. {
  512. ++frameCounter;
  513. }
  514. }
  515. // Didn't find it, so return -1
  516. return -1;
  517. }
  518. //------------------------------------------------------------------------------
  519. bool ImageAsset::containsNamedRegion(const char* regionName)
  520. {
  521. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  522. {
  523. // Grab the current pixelArea
  524. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  525. // Check to see if the name matches the argument
  526. if (!dStrcmp(pixelArea.mRegionName, regionName))
  527. {
  528. // Found it, so erase it and return success
  529. return true;
  530. }
  531. }
  532. return false;
  533. }
  534. //------------------------------------------------------------------------------
  535. bool ImageAsset::clearExplicitCells( void )
  536. {
  537. // Are we in explicit mode?
  538. if ( !getExplicitMode() )
  539. {
  540. // No, so warn.
  541. Con::warnf( "ImageAsset() - Cannot perform explicit cell operation when not in explicit mode." );
  542. return false;
  543. }
  544. // Clear explicit frames.
  545. mExplicitFrames.clear();
  546. // Refresh the asset.
  547. refreshAsset();
  548. return true;
  549. }
  550. //------------------------------------------------------------------------------
  551. bool ImageAsset::addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  552. {
  553. // Are we in explicit mode?
  554. if ( !getExplicitMode() )
  555. {
  556. // No, so warn.
  557. Con::warnf( "ImageAsset::addExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  558. return false;
  559. }
  560. // Fetch the original image dimensions.
  561. const S32 imageWidth = getImageWidth();
  562. const S32 imageHeight = getImageHeight();
  563. // The region name cannot be empty
  564. if ( regionName == StringTable->EmptyString )
  565. {
  566. Con::warnf( "ImageAsset::addExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  567. U32 currentIndex = mExplicitFrames.size();
  568. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  569. dSscanf(regionName, "%i", currentIndex);
  570. }
  571. // The Cell Offset X needs to be within the image.
  572. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  573. {
  574. // Warn.
  575. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  576. return false;
  577. }
  578. // The Cell Offset Y needs to be within the image.
  579. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  580. {
  581. // Warn.
  582. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  583. return false;
  584. }
  585. // The Cell Width needs to be within the image.
  586. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  587. {
  588. // Warn.
  589. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  590. return false;
  591. }
  592. // The Cell Height needs to be within the image.
  593. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  594. {
  595. // Warn.
  596. Con::warnf( "ImageAsset::addExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  597. return false;
  598. }
  599. // Store frame.
  600. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  601. mExplicitFrames.push_back( pixelArea );
  602. // Refresh the asset.
  603. refreshAsset();
  604. return true;
  605. }
  606. //------------------------------------------------------------------------------
  607. bool ImageAsset::insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  608. {
  609. // Are we in explicit mode?
  610. if ( !getExplicitMode() )
  611. {
  612. // No, so warn.
  613. Con::warnf( "ImageAsset::insertExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  614. return false;
  615. }
  616. // Fetch the original image dimensions.
  617. const S32 imageWidth = getImageWidth();
  618. const S32 imageHeight = getImageHeight();
  619. // Fetch the explicit frame count.
  620. const S32 explicitFramelCount = mExplicitFrames.size();
  621. // Region cannot be empty
  622. if ( regionName == StringTable->EmptyString )
  623. {
  624. Con::warnf( "ImageAsset::insertExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  625. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFramelCount );
  626. dSscanf(regionName, "%i", explicitFramelCount);
  627. }
  628. // The cell index needs to be in range.
  629. if ( cellIndex < 0 )
  630. {
  631. // Warn.
  632. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  633. return false;
  634. }
  635. // The Cell Offset X needs to be within the image.
  636. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  637. {
  638. // Warn.
  639. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  640. return false;
  641. }
  642. // The Cell Offset Y needs to be within the image.
  643. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  644. {
  645. // Warn.
  646. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  647. return false;
  648. }
  649. // The Cell Width needs to be within the image.
  650. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  651. {
  652. // Warn.
  653. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  654. return false;
  655. }
  656. // The Cell Height needs to be within the image.
  657. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  658. {
  659. // Warn.
  660. Con::warnf( "ImageAsset::insertExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  661. return false;
  662. }
  663. // Configure frame.
  664. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  665. // Insert frame appropriately.
  666. if ( cellIndex >= explicitFramelCount )
  667. {
  668. mExplicitFrames.push_back( pixelArea );
  669. }
  670. else
  671. {
  672. mExplicitFrames.insert( cellIndex );
  673. mExplicitFrames[cellIndex] = pixelArea;
  674. }
  675. // Refresh the asset.
  676. refreshAsset();
  677. return true;
  678. }
  679. //------------------------------------------------------------------------------
  680. bool ImageAsset::setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName )
  681. {
  682. // Are we in explicit mode?
  683. if ( !getExplicitMode() )
  684. {
  685. // No, so warn.
  686. Con::warnf( "ImageAsset::setExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  687. return false;
  688. }
  689. // Fetch the original image dimensions.
  690. const S32 imageWidth = getImageWidth();
  691. const S32 imageHeight = getImageHeight();
  692. // Fetch the explicit frame count.
  693. const S32 explicitFrameCount = mExplicitFrames.size();
  694. // Region cannot be empty
  695. if ( regionName == StringTable->EmptyString )
  696. {
  697. Con::warnf( "ImageAsset::setExplicitCell() - Cell name of '%s' is invalid or was not set.", regionName );
  698. Con::warnf( "- Setting to the next index in the frame list: '%i'", explicitFrameCount );
  699. dSscanf(regionName, "%i", explicitFrameCount);
  700. }
  701. // The cell index needs to be in range.
  702. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  703. {
  704. // Warn.
  705. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  706. return false;
  707. }
  708. // The Cell Offset X needs to be within the image.
  709. if ( cellOffsetX < 0 || cellOffsetX >= imageWidth )
  710. {
  711. // Warn.
  712. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetX of %d.", cellOffsetX );
  713. return false;
  714. }
  715. // The Cell Offset Y needs to be within the image.
  716. if ( cellOffsetY < 0 || cellOffsetY >= imageHeight )
  717. {
  718. // Warn.
  719. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell OffsetY of %d.", cellOffsetY );
  720. return false;
  721. }
  722. // The Cell Width needs to be within the image.
  723. if ( cellWidth <= 0 || (cellOffsetX+cellWidth) > imageWidth )
  724. {
  725. // Warn.
  726. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellWidth );
  727. return false;
  728. }
  729. // The Cell Height needs to be within the image.
  730. if ( cellHeight <= 0 || (cellOffsetY+cellHeight) > imageHeight )
  731. {
  732. // Warn.
  733. Con::warnf( "ImageAsset::setExplicitCell() - Invalid Cell Width of %d.", cellHeight );
  734. return false;
  735. }
  736. // Configure frame.
  737. FrameArea::PixelArea pixelArea( cellOffsetX, cellOffsetY, cellWidth, cellHeight, regionName );
  738. // Set cell.
  739. mExplicitFrames[cellIndex] = pixelArea;
  740. // Refresh the asset.
  741. refreshAsset();
  742. return true;
  743. }
  744. //------------------------------------------------------------------------------
  745. bool ImageAsset::removeExplicitCell( const S32 cellIndex )
  746. {
  747. // Are we in explicit mode?
  748. if ( !getExplicitMode() )
  749. {
  750. // No, so warn.
  751. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  752. return false;
  753. }
  754. // Fetch the explicit frame count.
  755. const S32 explicitFrameCount = mExplicitFrames.size();
  756. // The cell index needs to be in range.
  757. if ( cellIndex < 0 || cellIndex >= explicitFrameCount )
  758. {
  759. // Warn.
  760. Con::warnf( "ImageAsset::removeExplicitCell() - Invalid Cell Index of %d.", cellIndex );
  761. return false;
  762. }
  763. // Remove cell.
  764. mExplicitFrames.erase(cellIndex);
  765. // Refresh the asset.
  766. refreshAsset();
  767. return true;
  768. }
  769. //------------------------------------------------------------------------------
  770. bool ImageAsset::removeExplicitCell( const char* regionName )
  771. {
  772. // Are we in explicit mode?
  773. if ( !getExplicitMode() )
  774. {
  775. // No, so warn.
  776. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot perform explicit cell operation when not in explicit mode." );
  777. return false;
  778. }
  779. // Interate through the vector
  780. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  781. {
  782. // Grab the current pixelArea
  783. const FrameArea::PixelArea& pixelArea = *frameItr;
  784. // Check to see if the name matches the argument
  785. if (!dStrcmp(pixelArea.mRegionName, regionName))
  786. {
  787. // Found it, so erase it and return success
  788. mExplicitFrames.erase(frameItr);
  789. return true;
  790. }
  791. }
  792. // Didn't find it, so warn
  793. Con::warnf( "ImageAsset::removeExplicitCell() - Cannot find %s cell to remove.", regionName );
  794. return false;
  795. }
  796. //------------------------------------------------------------------------------
  797. ImageAsset::FrameArea& ImageAsset::getCellByName( const char* cellName)
  798. {
  799. // If the cellName was empty
  800. if (cellName == StringTable->EmptyString)
  801. {
  802. // Warn and return a bad frame
  803. Con::warnf( "ImageAsset::getCellByName() - Empty cell name was passed." );
  804. return BadFrameArea;
  805. }
  806. for( typeFrameAreaVector::iterator frameItr = mFrames.begin(); frameItr != mFrames.end(); ++frameItr )
  807. {
  808. // Grab the current pixelArea
  809. const FrameArea::PixelArea& pixelArea = frameItr->mPixelArea;
  810. // Check to see if the name matches the argument
  811. if (!dStrcmp(pixelArea.mRegionName, cellName))
  812. {
  813. // Found it, so erase it and return success
  814. return *frameItr;
  815. }
  816. }
  817. // Didn't find it, so warn and return a bad frame
  818. Con::warnf( "ImageAsset::getCellByName() - Cannot find %s cell.", cellName );
  819. return BadFrameArea;
  820. }
  821. //------------------------------------------------------------------------------
  822. void ImageAsset::setTextureFilter( const TextureFilterMode filterMode )
  823. {
  824. // Finish if no texture.
  825. if ( mImageTextureHandle.IsNull() )
  826. return;
  827. // Select Hardware Filter Mode.
  828. GLint glFilterMode;
  829. switch( filterMode )
  830. {
  831. // Nearest ("none").
  832. case FILTER_NEAREST:
  833. {
  834. glFilterMode = GL_NEAREST;
  835. } break;
  836. // Bilinear ("smooth").
  837. case FILTER_BILINEAR:
  838. {
  839. glFilterMode = GL_LINEAR;
  840. } break;
  841. // Huh?
  842. default:
  843. // Oh well...
  844. glFilterMode = GL_LINEAR;
  845. };
  846. // Set the texture objects filter mode.
  847. mImageTextureHandle.setFilter( glFilterMode );
  848. }
  849. //------------------------------------------------------------------------------
  850. void ImageAsset::initializeAsset( void )
  851. {
  852. // Call parent.
  853. Parent::initializeAsset();
  854. // Ensure the image-file is expanded.
  855. mImageFile = expandAssetFilePath( mImageFile );
  856. // Time to load the layer files too.
  857. for (auto& layer : mImageLayers)
  858. {
  859. const char* path = expandAssetFilePath(layer.mImageFile);
  860. layer.LoadImage(path);
  861. }
  862. // Calculate the image.
  863. calculateImage();
  864. // Redraw the image.
  865. redrawImage();
  866. }
  867. //------------------------------------------------------------------------------
  868. void ImageAsset::onAssetRefresh( void )
  869. {
  870. // Ignore if not yet added to the sim.
  871. if ( !isProperlyAdded() )
  872. return;
  873. // Call parent.
  874. Parent::onAssetRefresh();
  875. // Compile image.
  876. calculateImage();
  877. // Redraw the image.
  878. redrawImage();
  879. }
  880. //-----------------------------------------------------------------------------
  881. void ImageAsset::onTamlPreWrite( void )
  882. {
  883. // Call parent.
  884. Parent::onTamlPreWrite();
  885. // Ensure the image-file is collapsed.
  886. mImageFile = collapseAssetFilePath( mImageFile );
  887. }
  888. //-----------------------------------------------------------------------------
  889. void ImageAsset::onTamlPostWrite( void )
  890. {
  891. // Call parent.
  892. Parent::onTamlPostWrite();
  893. // Ensure the image-file is expanded.
  894. mImageFile = expandAssetFilePath( mImageFile );
  895. }
  896. //------------------------------------------------------------------------------
  897. void ImageAsset::calculateImage( void )
  898. {
  899. // Debug Profiling.
  900. PROFILE_SCOPE(ImageAsset_CalculateImage);
  901. // Clear frames.
  902. mFrames.clear();
  903. // If we have an existing texture and we're setting to the same bitmap then force the texture manager
  904. // to refresh the texture itself.
  905. if ( !mImageTextureHandle.IsNull() && dStricmp(mImageTextureHandle.getTextureKey(), mImageFile) == 0 )
  906. TextureManager::refresh( mImageFile );
  907. // Get image texture.
  908. mImageTextureHandle.set( mImageFile, mImageLayers.size() > 0 ? TextureHandle::BitmapKeepTexture : TextureHandle::BitmapTexture, true, getForce16Bit() );
  909. // Is the texture valid?
  910. if ( mImageTextureHandle.IsNull() )
  911. {
  912. // No, so warn.
  913. Con::warnf( "Image '%s' could not load texture '%s'.", getAssetId(), mImageFile );
  914. return;
  915. }
  916. // Is the local filter mode specified?
  917. if ( mLocalFilterMode != FILTER_INVALID )
  918. {
  919. // Yes, so set filter mode.
  920. setTextureFilter( mLocalFilterMode );
  921. }
  922. else
  923. {
  924. TextureFilterMode filterMode = FILTER_NEAREST;
  925. // No, so fetch the global filter.
  926. const char* pGlobalFilter = Con::getVariable( "$pref::T2D::imageAssetGlobalFilterMode" );
  927. // Fetch the global filter mode.
  928. if ( pGlobalFilter != NULL && dStrlen(pGlobalFilter) > 0 )
  929. filterMode = getFilterModeEnum( pGlobalFilter );
  930. // If global filter mode is invalid then use local filter mode.
  931. if ( filterMode == FILTER_INVALID )
  932. filterMode = FILTER_NEAREST;
  933. // Set filter mode.
  934. setTextureFilter( filterMode );
  935. }
  936. // Calculate according to mode.
  937. if ( mExplicitMode )
  938. {
  939. calculateExplicitMode();
  940. }
  941. else
  942. {
  943. calculateImplicitMode();
  944. }
  945. }
  946. //------------------------------------------------------------------------------
  947. void ImageAsset::calculateImplicitMode( void )
  948. {
  949. // Debug Profiling.
  950. PROFILE_SCOPE(ImageAsset_CalculateImplicitMode);
  951. // Sanity!
  952. AssertFatal( !mExplicitMode, "Cannot calculate implicit cells when in explicit mode." );
  953. // Fetch the texture object.
  954. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  955. // Calculate texel scales.
  956. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  957. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  958. // Fetch the original image dimensions.
  959. const S32 imageWidth = getImageWidth();
  960. const S32 imageHeight = getImageHeight();
  961. // Set full-frame as default.
  962. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  963. mFrames.push_back( frameArea );
  964. // Finish if no cell counts are specified. This is how we default to full-frame mode.
  965. if ( mCellCountX < 1 || mCellCountY < 1 )
  966. return;
  967. // The cell width needs to be at maximum the image width!
  968. if ( mCellWidth < 1 || mCellWidth > imageWidth )
  969. {
  970. // Warn.
  971. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Width of %d.", mCellWidth );
  972. return;
  973. }
  974. // The cell height needs to be at maximum the image height!
  975. if ( mCellHeight < 1 || mCellHeight > imageHeight )
  976. {
  977. // Warn.
  978. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell Height of %d.", mCellHeight );
  979. return;
  980. }
  981. // The Cell Offset X needs to be within the image.
  982. if ( mCellOffsetX < 0 || mCellOffsetX >= imageWidth )
  983. {
  984. // Warn.
  985. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX of %d.", mCellOffsetX );
  986. return;
  987. }
  988. // The Cell Offset Y needs to be within the image.
  989. if ( mCellOffsetY < 0 || mCellOffsetY >= imageHeight )
  990. {
  991. // Warn.
  992. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY of %d.", mCellOffsetY );
  993. return;
  994. }
  995. // Are we using Cell-StrideX?
  996. S32 cellStepX;
  997. if ( mCellStrideX != 0 )
  998. {
  999. // Yes, so set stepX to be StrideX.
  1000. cellStepX = mCellStrideX;
  1001. }
  1002. else
  1003. {
  1004. // No, so set stepY to be Cell Width.
  1005. cellStepX = mCellWidth;
  1006. }
  1007. // Are we using Cell-StrideY?
  1008. S32 cellStepY;
  1009. if ( mCellStrideY != 0 )
  1010. {
  1011. // Yes, so set stepY to be StrideY.
  1012. cellStepY = mCellStrideY;
  1013. }
  1014. else
  1015. {
  1016. // No, so set stepY to be Cell Height.
  1017. cellStepY = mCellHeight;
  1018. }
  1019. // Calculate Final Cell Position X.
  1020. S32 cellFinalPositionX = mCellOffsetX + ((mCellCountX-((cellStepX<0)?1:0))*cellStepX);
  1021. // Off Left?
  1022. if ( cellFinalPositionX < 0 )
  1023. {
  1024. // Warn.
  1025. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image left-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  1026. return;
  1027. }
  1028. // Off Right?
  1029. else if ( cellFinalPositionX > imageWidth )
  1030. {
  1031. // Warn.
  1032. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetX(%d)/Width(%d)/CountX(%d); off image right-hand-side.", mCellOffsetX, mCellWidth, mCellCountX );
  1033. return;
  1034. }
  1035. // Calculate Final Cell Position Y.
  1036. S32 cellFinalPositionY = mCellOffsetY + ((mCellCountY-((cellStepY<0)?1:0))*cellStepY);
  1037. // Off Top?
  1038. if ( cellFinalPositionY < 0 )
  1039. {
  1040. // Warn.
  1041. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image top-side.", mCellOffsetY, mCellHeight, mCellCountY );
  1042. return;
  1043. }
  1044. // Off Bottom?
  1045. else if ( cellFinalPositionY > imageHeight )
  1046. {
  1047. // Warn.
  1048. Con::warnf( "ImageAsset::calculateImage() - Invalid Cell OffsetY(%d)/Height(%d)/CountY(%d); off image bottom-side.", mCellOffsetY, mCellHeight, mCellCountY );
  1049. return;
  1050. }
  1051. // Clear default frame.
  1052. mFrames.clear();
  1053. // Cell Row Order?
  1054. if ( mCellRowOrder )
  1055. {
  1056. // Yes, so RowRow Order.
  1057. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  1058. {
  1059. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  1060. {
  1061. // Set frame area.
  1062. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  1063. // Store fame.
  1064. mFrames.push_back( frameArea );
  1065. }
  1066. }
  1067. return;
  1068. }
  1069. // No, so Column Order.
  1070. for ( S32 x = 0, cellPositionX = mCellOffsetX; x < mCellCountX; x++, cellPositionX+=cellStepX )
  1071. {
  1072. for ( S32 y = 0, cellPositionY = mCellOffsetY; y < mCellCountY; y++, cellPositionY+=cellStepY )
  1073. {
  1074. // Set frame area.
  1075. frameArea.setArea( cellPositionX, cellPositionY, mCellWidth, mCellHeight, texelWidthScale, texelHeightScale );
  1076. // Store fame.
  1077. mFrames.push_back( frameArea );
  1078. }
  1079. }
  1080. }
  1081. //------------------------------------------------------------------------------
  1082. void ImageAsset::calculateExplicitMode( void )
  1083. {
  1084. // Debug Profiling.
  1085. PROFILE_SCOPE(ImageAsset_CalculateExplicitMode);
  1086. // Sanity!
  1087. AssertFatal( mExplicitMode, "Cannot calculate explicit cells when not in explicit mode." );
  1088. // Fetch the texture object.
  1089. TextureObject* pTextureObject = ((TextureObject*)mImageTextureHandle);
  1090. // Calculate texel scales.
  1091. const F32 texelWidthScale = 1.0f / (F32)pTextureObject->getTextureWidth();
  1092. const F32 texelHeightScale = 1.0f / (F32)pTextureObject->getTextureHeight();
  1093. // Fetch the original image dimensions.
  1094. const S32 imageWidth = getImageWidth();
  1095. const S32 imageHeight = getImageHeight();
  1096. // Clear default frame.
  1097. mFrames.clear();
  1098. // Are any explicit frames set.
  1099. if ( mExplicitFrames.size() == 0 )
  1100. {
  1101. // No, so set full-frame as default.
  1102. FrameArea frameArea( 0, 0, imageWidth, imageHeight, texelWidthScale, texelHeightScale );
  1103. mFrames.push_back( frameArea );
  1104. return;
  1105. }
  1106. // Iterate explicit frames.
  1107. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1108. {
  1109. // Fetch pixel area.
  1110. const FrameArea::PixelArea& pixelArea = *frameItr;
  1111. // Set frame area.
  1112. FrameArea frameArea( pixelArea.mPixelOffset.x, pixelArea.mPixelOffset.y, pixelArea.mPixelWidth, pixelArea.mPixelHeight, texelWidthScale, texelHeightScale, pixelArea.mRegionName );
  1113. // Store frame.
  1114. mFrames.push_back( frameArea );
  1115. }
  1116. }
  1117. void ImageAsset::completeLayerChange(const bool doRedraw)
  1118. {
  1119. if (doRedraw)
  1120. {
  1121. redrawImage();
  1122. if (getOwned())
  1123. {
  1124. refreshAsset();
  1125. }
  1126. }
  1127. }
  1128. void ImageAsset::redrawImage()
  1129. {
  1130. if (mImageLayers.size() > 0)
  1131. {
  1132. GBitmap* map = mImageTextureHandle.getBitmap();
  1133. if (map == nullptr)
  1134. {
  1135. // This should reload the texture and keep it in memory
  1136. mImageTextureHandle.set(mImageFile, TextureHandle::BitmapKeepTexture, true, getForce16Bit());
  1137. map = mImageTextureHandle.getBitmap();
  1138. if (map == nullptr)
  1139. {
  1140. // We still can't come up with a bitmap, so warn and return
  1141. Con::warnf("ImageAsset::redrawImage() - Unable to load bitmap for image '%s'.", mImageFile);
  1142. return;
  1143. }
  1144. mImageLayers[0].LoadImage(mImageLayers[0].mImageFile);
  1145. }
  1146. map->clearImage(mBlendColor);
  1147. for (auto &layer : mImageLayers)
  1148. {
  1149. map->mergeLayer(layer.mPosition, layer.mBitmap, layer.mBlendColor);
  1150. }
  1151. mImageTextureHandle.refresh();
  1152. }
  1153. }
  1154. //------------------------------------------------------------------------------
  1155. bool ImageAsset::setFilterMode( void* obj, const char* data )
  1156. {
  1157. static_cast<ImageAsset*>(obj)->setFilterMode(getFilterModeEnum(data));
  1158. return false;
  1159. }
  1160. void ImageAsset::addLayer(const char* imagePath, const Point2I position, const ColorF blendColor, const bool doRedraw)
  1161. {
  1162. insertLayer(mImageLayers.size(), imagePath, position, blendColor, doRedraw);
  1163. }
  1164. void ImageAsset::insertLayer(const U32 index, const char* imagePath, const Point2I position, const ColorF blendColor, const bool doRedraw)
  1165. {
  1166. if (mImageLayers.size() == 0)
  1167. {
  1168. Point2I p(0, 0);
  1169. ImageLayer baseLayer(mImageFile, p, mBlendColor);
  1170. mImageLayers.push_back(baseLayer);
  1171. if (getOwned())
  1172. {
  1173. baseLayer.LoadImage(mImageFile);
  1174. }
  1175. }
  1176. ImageLayer layer(imagePath, position, blendColor);
  1177. if (getOwned())
  1178. {
  1179. const char* path = expandAssetFilePath(layer.mImageFile);
  1180. layer.LoadImage(path);
  1181. }
  1182. if (index == 0 || index >= mImageLayers.size())
  1183. {
  1184. mImageLayers.push_back(layer);
  1185. }
  1186. else
  1187. {
  1188. typeImageLayerVector::iterator iter = mImageLayers.begin() + index;
  1189. mImageLayers.insert(iter, layer);
  1190. }
  1191. completeLayerChange(doRedraw);
  1192. }
  1193. void ImageAsset::removeLayer(const U32 index, const bool doRedraw)
  1194. {
  1195. if (index < 1 || index >= mImageLayers.size())
  1196. {
  1197. Con::warnf("ImageAsset::removeLayer() - Invalid Index of %d.", index);
  1198. return;
  1199. }
  1200. mImageLayers.erase(index);
  1201. if (mImageLayers.size() == 1)
  1202. {
  1203. mImageLayers.clear();
  1204. }
  1205. completeLayerChange(doRedraw);
  1206. }
  1207. void ImageAsset::moveLayerForward(const U32 index, const bool doRedraw)
  1208. {
  1209. if (index < 1 || index >= mImageLayers.size())
  1210. {
  1211. Con::warnf("ImageAsset::moveLayerForward() - Invalid Index of %d.", index);
  1212. return;
  1213. }
  1214. if (index == mImageLayers.size() - 1)
  1215. {
  1216. return;
  1217. }
  1218. ImageLayer layer = mImageLayers[index];
  1219. mImageLayers.erase(index);
  1220. mImageLayers.insert(mImageLayers.begin() + index + 1, layer);
  1221. completeLayerChange(doRedraw);
  1222. }
  1223. void ImageAsset::moveLayerBackward(const U32 index, const bool doRedraw)
  1224. {
  1225. if (index < 1 || index >= mImageLayers.size())
  1226. {
  1227. Con::warnf("ImageAsset::moveLayerBackward() - Invalid Index of %d.", index);
  1228. return;
  1229. }
  1230. if (index == 1)
  1231. {
  1232. return;
  1233. }
  1234. moveLayerForward(index - 1, doRedraw);
  1235. }
  1236. void ImageAsset::moveLayerToFront(const U32 index, const bool doRedraw)
  1237. {
  1238. if (index < 1 || index >= mImageLayers.size())
  1239. {
  1240. Con::warnf("ImageAsset::moveLayerToFront() - Invalid Index of %d.", index);
  1241. return;
  1242. }
  1243. if (index == mImageLayers.size() - 1)
  1244. {
  1245. return;
  1246. }
  1247. ImageLayer layer = mImageLayers[index];
  1248. mImageLayers.erase(index);
  1249. mImageLayers.push_back(layer);//The back of the array is the front of the image
  1250. completeLayerChange(doRedraw);
  1251. }
  1252. void ImageAsset::moveLayerToBack(const U32 index, const bool doRedraw)
  1253. {
  1254. if (index < 1 || index >= mImageLayers.size())
  1255. {
  1256. Con::warnf("ImageAsset::moveLayerToBack() - Invalid Index of %d.", index);
  1257. return;
  1258. }
  1259. if (index == 1)
  1260. {
  1261. return;
  1262. }
  1263. ImageLayer layer = mImageLayers[index];
  1264. mImageLayers.erase(index);
  1265. mImageLayers.insert(mImageLayers.begin() + 1, layer);//The front of the array is the back of the image
  1266. completeLayerChange(doRedraw);
  1267. }
  1268. void ImageAsset::setLayerImage(const U32 index, const char* imagePath, const bool doRedraw)
  1269. {
  1270. if (index < 1 || index >= mImageLayers.size())
  1271. {
  1272. Con::warnf("ImageAsset::setLayerImage() - Invalid Index of %d.", index);
  1273. return;
  1274. }
  1275. if (getOwned())
  1276. {
  1277. const char* path = expandAssetFilePath(imagePath);
  1278. mImageLayers[index].mImageFile = StringTable->insert(imagePath);
  1279. mImageLayers[index].LoadImage(path);
  1280. }
  1281. else
  1282. {
  1283. mImageLayers[index].mImageFile = StringTable->insert(imagePath);
  1284. }
  1285. completeLayerChange(doRedraw);
  1286. }
  1287. void ImageAsset::setLayerPosition(const U32 index, const Point2I position, const bool doRedraw)
  1288. {
  1289. if (index < 1 || index >= mImageLayers.size())
  1290. {
  1291. Con::warnf("ImageAsset::setLayerPosition() - Invalid Index of %d.", index);
  1292. return;
  1293. }
  1294. mImageLayers[index].mPosition.set(position.x, position.y);
  1295. completeLayerChange(doRedraw);
  1296. }
  1297. void ImageAsset::setLayerBlendColor(const U32 index, const ColorF blendColor, const bool doRedraw)
  1298. {
  1299. if (index < 1 || index >= mImageLayers.size())
  1300. {
  1301. Con::warnf("ImageAsset::setLayerBlendColor() - Invalid Index of %d.", index);
  1302. return;
  1303. }
  1304. mImageLayers[index].mBlendColor.set(blendColor.red, blendColor.green, blendColor.blue, blendColor.alpha);
  1305. completeLayerChange(doRedraw);
  1306. }
  1307. const char* ImageAsset::getLayerImage(const U32 index)
  1308. {
  1309. if (index < 1 || index >= mImageLayers.size())
  1310. {
  1311. Con::warnf("ImageAsset::getLayerImage() - Invalid Index of %d.", index);
  1312. return StringTable->EmptyString;
  1313. }
  1314. return mImageLayers[index].mImageFile;
  1315. }
  1316. const Point2I ImageAsset::getLayerPosition(const U32 index)
  1317. {
  1318. if (index < 1 || index >= mImageLayers.size())
  1319. {
  1320. Con::warnf("ImageAsset::getLayerPosition() - Invalid Index of %d.", index);
  1321. return Point2I();
  1322. }
  1323. return mImageLayers[index].mPosition;
  1324. }
  1325. const ColorF ImageAsset::getLayerBlendColor(const U32 index)
  1326. {
  1327. if (index < 1 || index >= mImageLayers.size())
  1328. {
  1329. Con::warnf("ImageAsset::getLayerBlendColor() - Invalid Index of %d.", index);
  1330. return ColorF();
  1331. }
  1332. return mImageLayers[index].mBlendColor;
  1333. }
  1334. void ImageAsset::setBlendColor(const ColorF color, const bool doRedraw)
  1335. {
  1336. mBlendColor.set(color.red, color.green, color.blue, color.alpha);
  1337. if (mImageLayers.size() == 0)
  1338. {
  1339. Con::warnf("ImageAsset::setBlendColor() - The ImageAsset blend color will not be applied if layers are not being used.");
  1340. return;
  1341. }
  1342. mImageLayers[0].mBlendColor.set(color.red, color.green, color.blue, color.alpha);
  1343. completeLayerChange(doRedraw);
  1344. }
  1345. const ColorF ImageAsset::getBlendColor()
  1346. {
  1347. return mBlendColor;
  1348. }
  1349. //------------------------------------------------------------------------------
  1350. void ImageAsset::onTamlCustomWrite( TamlCustomNodes& customNodes )
  1351. {
  1352. // Debug Profiling.
  1353. PROFILE_SCOPE(ImageAsset_OnTamlCustomWrite);
  1354. // Call parent.
  1355. Parent::onTamlCustomWrite( customNodes );
  1356. if (mExplicitMode && mExplicitFrames.size() > 0)
  1357. {
  1358. // Add cell custom node.
  1359. TamlCustomNode* pCustomCellNodes = customNodes.addNode( cellCustomNodeCellsName );
  1360. // Iterate explicit frames.
  1361. for( typeExplicitFrameAreaVector::iterator frameItr = mExplicitFrames.begin(); frameItr != mExplicitFrames.end(); ++frameItr )
  1362. {
  1363. // Fetch pixel area.
  1364. const FrameArea::PixelArea& pixelArea = *frameItr;
  1365. // Add cell alias.
  1366. TamlCustomNode* pCellNode = pCustomCellNodes->addNode( cellNodeName );
  1367. // Add cell properties.
  1368. pCellNode->addField( cellRegionName, pixelArea.mRegionName );
  1369. pCellNode->addField( cellOffsetName, pixelArea.mPixelOffset );
  1370. pCellNode->addField( cellWidthName, pixelArea.mPixelWidth );
  1371. pCellNode->addField( cellHeightName, pixelArea.mPixelHeight );
  1372. }
  1373. }
  1374. if (mImageLayers.size() > 0)
  1375. {
  1376. TamlCustomNode* pCustomLayerNodes = customNodes.addNode(imageLayerCustomNodeName);
  1377. for (typeImageLayerVector::iterator frameItr = mImageLayers.begin() + 1; frameItr != mImageLayers.end(); ++frameItr)
  1378. {
  1379. const ImageLayer& layer = *frameItr;
  1380. TamlCustomNode* pLayerNode = pCustomLayerNodes->addNode(layerNodeName);
  1381. pLayerNode->addField(layerImageFile, layer.mImageFile);
  1382. pLayerNode->addField(layerPositionName, layer.mPosition);
  1383. pLayerNode->addField(layerBlendColorName, layer.mBlendColor);
  1384. }
  1385. }
  1386. }
  1387. //-----------------------------------------------------------------------------
  1388. void ImageAsset::onTamlCustomRead(const TamlCustomNodes& customNodes)
  1389. {
  1390. // Debug Profiling.
  1391. PROFILE_SCOPE(ImageAsset_OnTamlCustomRead);
  1392. // Call parent.
  1393. Parent::onTamlCustomRead(customNodes);
  1394. // Load the explicit cells
  1395. loadTamlExplicitCells(customNodes);
  1396. // Load image layers
  1397. loadTamlImageLayers(customNodes);
  1398. }
  1399. void ImageAsset::loadTamlExplicitCells(const TamlCustomNodes& customNodes)
  1400. {
  1401. // Find cell custom node.
  1402. const TamlCustomNode* pCustomCellNodes = customNodes.findNode( cellCustomNodeCellsName );
  1403. // Continue if we have explicit cells.
  1404. if ( pCustomCellNodes != NULL )
  1405. {
  1406. // Set explicit mode.
  1407. mExplicitMode = true;
  1408. // Fetch children cell nodes.
  1409. const TamlCustomNodeVector& cellNodes = pCustomCellNodes->getChildren();
  1410. // Iterate cells.
  1411. for( TamlCustomNodeVector::const_iterator cellNodeItr = cellNodes.begin(); cellNodeItr != cellNodes.end(); ++cellNodeItr )
  1412. {
  1413. // Fetch cell node.
  1414. TamlCustomNode* pCellNode = *cellNodeItr;
  1415. // Fetch node name.
  1416. StringTableEntry nodeName = pCellNode->getNodeName();
  1417. // Is this a valid alias?
  1418. if ( nodeName != cellNodeName )
  1419. {
  1420. // No, so warn.
  1421. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, cellNodeName );
  1422. continue;
  1423. }
  1424. Point2I cellOffset(-1, -1);
  1425. S32 cellWidth = 0;
  1426. S32 cellHeight = 0;
  1427. const char* regionName = StringTable->EmptyString;
  1428. // Fetch fields.
  1429. const TamlCustomFieldVector& fields = pCellNode->getFields();
  1430. // Iterate property fields.
  1431. for ( TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr )
  1432. {
  1433. // Fetch field.
  1434. const TamlCustomField* pField = *fieldItr;
  1435. // Fetch field name.
  1436. StringTableEntry fieldName = pField->getFieldName();
  1437. // Check common fields.
  1438. if ( fieldName == cellRegionName )
  1439. {
  1440. regionName = pField->getFieldValue();
  1441. }
  1442. else if ( fieldName == cellOffsetName )
  1443. {
  1444. pField->getFieldValue( cellOffset );
  1445. }
  1446. else if ( fieldName == cellOffsetXName )
  1447. {
  1448. pField->getFieldValue( cellOffset.x );
  1449. }
  1450. else if ( fieldName == cellOffsetYName )
  1451. {
  1452. pField->getFieldValue( cellOffset.y );
  1453. }
  1454. else if ( fieldName == cellWidthName )
  1455. {
  1456. pField->getFieldValue( cellWidth );
  1457. }
  1458. else if ( fieldName == cellHeightName )
  1459. {
  1460. pField->getFieldValue( cellHeight );
  1461. }
  1462. else
  1463. {
  1464. // Unknown name so warn.
  1465. Con::warnf( "ImageAsset::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName );
  1466. continue;
  1467. }
  1468. }
  1469. // Does the region have a name
  1470. if ( regionName == StringTable->EmptyString )
  1471. {
  1472. // No, so warn and set it to the next index
  1473. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell name of '%s' is invalid or was not set.", regionName );
  1474. U32 currentIndex = mExplicitFrames.size();
  1475. Con::warnf( "- Setting to the next index in the frame list: '%i'", currentIndex );
  1476. dSscanf(regionName, "%i", currentIndex);
  1477. }
  1478. // Is cell offset valid?
  1479. if ( cellOffset.x < 0 || cellOffset.y < 0 )
  1480. {
  1481. // No, so warn.
  1482. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell offset of '(%d,%d)' is invalid or was not set.", cellOffset.x, cellOffset.y );
  1483. continue;
  1484. }
  1485. // Is cell width valid?
  1486. if ( cellWidth <= 0 )
  1487. {
  1488. // No, so warn.
  1489. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell width of '%d' is invalid or was not set.", cellWidth );
  1490. continue;
  1491. }
  1492. // Is cell height valid?
  1493. if ( cellHeight <= 0 )
  1494. {
  1495. // No, so warn.
  1496. Con::warnf( "ImageAsset::onTamlCustomRead() - Cell height of '%d' is invalid or was not set.", cellHeight );
  1497. continue;
  1498. }
  1499. // Add explicit frame.
  1500. FrameArea::PixelArea pixelArea( cellOffset.x, cellOffset.y, cellWidth, cellHeight, regionName );
  1501. mExplicitFrames.push_back( pixelArea );
  1502. }
  1503. }
  1504. }
  1505. void ImageAsset::loadTamlImageLayers(const TamlCustomNodes& customNodes)
  1506. {
  1507. // Find layer custom node.
  1508. const TamlCustomNode* pCustomLayerNodes = customNodes.findNode(imageLayerCustomNodeName);
  1509. // Continue if we have explicit layers.
  1510. if (pCustomLayerNodes != NULL)
  1511. {
  1512. // Fetch children layer nodes.
  1513. const TamlCustomNodeVector& layerNodes = pCustomLayerNodes->getChildren();
  1514. // Iterate layers.
  1515. for (TamlCustomNodeVector::const_iterator layerNodeItr = layerNodes.begin(); layerNodeItr != layerNodes.end(); ++layerNodeItr)
  1516. {
  1517. // Fetch layer node.
  1518. TamlCustomNode* pLayerNode = *layerNodeItr;
  1519. // Fetch node name.
  1520. StringTableEntry nodeName = pLayerNode->getNodeName();
  1521. // Is this a valid alias?
  1522. if (nodeName != layerNodeName)
  1523. {
  1524. // No, so warn.
  1525. Con::warnf("ImageAsset::onTamlCustomRead() - Encountered an unknown custom name of '%s'. Only '%s' is valid.", nodeName, layerNodeName);
  1526. continue;
  1527. }
  1528. Point2I layerPosition(0, 0);
  1529. const char* imagePath = StringTable->EmptyString;
  1530. ColorF blendColor = ColorF(1, 1, 1, 1);
  1531. // Fetch fields.
  1532. const TamlCustomFieldVector& fields = pLayerNode->getFields();
  1533. // Iterate property fields.
  1534. for (TamlCustomFieldVector::const_iterator fieldItr = fields.begin(); fieldItr != fields.end(); ++fieldItr)
  1535. {
  1536. // Fetch field.
  1537. const TamlCustomField* pField = *fieldItr;
  1538. // Fetch field name.
  1539. StringTableEntry fieldName = pField->getFieldName();
  1540. // Check common fields.
  1541. if (fieldName == layerImageFile)
  1542. {
  1543. imagePath = pField->getFieldValue();
  1544. }
  1545. else if (fieldName == layerPositionName)
  1546. {
  1547. pField->getFieldValue(layerPosition);
  1548. }
  1549. else if (fieldName == layerBlendColorName)
  1550. {
  1551. pField->getFieldValue(blendColor);
  1552. }
  1553. else
  1554. {
  1555. // Unknown name so warn.
  1556. Con::warnf("ImageAsset::onTamlCustomRead() - Encountered an unknown custom field name of '%s'.", fieldName);
  1557. continue;
  1558. }
  1559. }
  1560. // Is the image name valid?
  1561. if (imagePath == StringTable->EmptyString)
  1562. {
  1563. // No, so warn
  1564. Con::warnf("ImageAsset::onTamlCustomRead() - Image File of '%s' is invalid or was not set.", imagePath);
  1565. }
  1566. // Add image layer.
  1567. addLayer(imagePath, layerPosition, blendColor, false);
  1568. }
  1569. }
  1570. }
  1571. //-----------------------------------------------------------------------------
  1572. static void WriteCustomTamlSchema( const AbstractClassRep* pClassRep, TiXmlElement* pParentElement )
  1573. {
  1574. // Sanity!
  1575. AssertFatal( pClassRep != NULL, "ImageAsset::WriteCustomTamlSchema() - ClassRep cannot be NULL." );
  1576. AssertFatal( pParentElement != NULL, "ImageAsset::WriteCustomTamlSchema() - Parent Element cannot be NULL." );
  1577. char buffer[1024];
  1578. //---Cells---
  1579. // Create ImageAsset node element.
  1580. TiXmlElement* pImageAssetNodeElement = new TiXmlElement( "xs:element" );
  1581. dSprintf( buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), cellCustomNodeCellsName );
  1582. pImageAssetNodeElement->SetAttribute( "name", buffer );
  1583. pImageAssetNodeElement->SetAttribute( "minOccurs", 0 );
  1584. pImageAssetNodeElement->SetAttribute( "maxOccurs", 1 );
  1585. pParentElement->LinkEndChild( pImageAssetNodeElement );
  1586. // Create complex type.
  1587. TiXmlElement* pImageAssetNodeComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1588. pImageAssetNodeElement->LinkEndChild( pImageAssetNodeComplexTypeElement );
  1589. // Create choice element.
  1590. TiXmlElement* pImageAssetNodeChoiceElement = new TiXmlElement( "xs:choice" );
  1591. pImageAssetNodeChoiceElement->SetAttribute( "minOccurs", 0 );
  1592. pImageAssetNodeChoiceElement->SetAttribute( "maxOccurs", "unbounded" );
  1593. pImageAssetNodeComplexTypeElement->LinkEndChild( pImageAssetNodeChoiceElement );
  1594. // Create ImageAsset element.
  1595. TiXmlElement* pImageAssetElement = new TiXmlElement( "xs:element" );
  1596. pImageAssetElement->SetAttribute( "name", cellNodeName );
  1597. pImageAssetElement->SetAttribute( "minOccurs", 0 );
  1598. pImageAssetElement->SetAttribute( "maxOccurs", 1 );
  1599. pImageAssetNodeChoiceElement->LinkEndChild( pImageAssetElement );
  1600. // Create complex type Element.
  1601. TiXmlElement* pImageAssetComplexTypeElement = new TiXmlElement( "xs:complexType" );
  1602. pImageAssetElement->LinkEndChild( pImageAssetComplexTypeElement );
  1603. // Create "RegionName" attribute.
  1604. TiXmlElement* pImageRegionName = new TiXmlElement( "xs:attribute" );
  1605. pImageRegionName->SetAttribute( "name", cellRegionName );
  1606. pImageRegionName->SetAttribute( "type", "xs:string" );
  1607. pImageAssetComplexTypeElement->LinkEndChild( pImageRegionName );
  1608. // Create "Offset" attribute.
  1609. TiXmlElement* pImageAssetOffset = new TiXmlElement( "xs:attribute" );
  1610. pImageAssetOffset->SetAttribute( "name", cellOffsetName );
  1611. pImageAssetOffset->SetAttribute( "type", "Point2I_ConsoleType" );
  1612. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetOffset );
  1613. // Create "Width" attribute.
  1614. TiXmlElement* pImageAssetWidth = new TiXmlElement( "xs:attribute" );
  1615. pImageAssetWidth->SetAttribute( "name", cellWidthName );
  1616. pImageAssetWidth->SetAttribute( "type", "xs:unsignedInt" );
  1617. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetWidth );
  1618. // Create "Height" attribute.
  1619. TiXmlElement* pImageAssetHeight = new TiXmlElement( "xs:attribute" );
  1620. pImageAssetHeight->SetAttribute( "name", cellHeightName );
  1621. pImageAssetHeight->SetAttribute( "type", "xs:unsignedInt" );
  1622. pImageAssetComplexTypeElement->LinkEndChild( pImageAssetHeight );
  1623. //---Layers---
  1624. char buffer2[1024];
  1625. // Create ImageAsset node element.
  1626. TiXmlElement* pImageAssetNodeElementL = new TiXmlElement("xs:element");
  1627. dSprintf(buffer, sizeof(buffer2), "%s.%s", pClassRep->getClassName(), imageLayerCustomNodeName);
  1628. pImageAssetNodeElementL->SetAttribute("name", buffer2);
  1629. pImageAssetNodeElementL->SetAttribute("minOccurs", 0);
  1630. pImageAssetNodeElementL->SetAttribute("maxOccurs", 1);
  1631. pParentElement->LinkEndChild(pImageAssetNodeElementL);
  1632. // Create complex type.
  1633. TiXmlElement* pImageAssetNodeComplexTypeElementL = new TiXmlElement("xs:complexType");
  1634. pImageAssetNodeElementL->LinkEndChild(pImageAssetNodeComplexTypeElementL);
  1635. // Create choice element.
  1636. TiXmlElement* pImageAssetNodeChoiceElementL = new TiXmlElement("xs:choice");
  1637. pImageAssetNodeChoiceElementL->SetAttribute("minOccurs", 0);
  1638. pImageAssetNodeChoiceElementL->SetAttribute("maxOccurs", "unbounded");
  1639. pImageAssetNodeComplexTypeElementL->LinkEndChild(pImageAssetNodeChoiceElementL);
  1640. // Create ImageAsset element.
  1641. TiXmlElement* pImageAssetElementL = new TiXmlElement("xs:element");
  1642. pImageAssetElementL->SetAttribute("name", layerNodeName);
  1643. pImageAssetElementL->SetAttribute("minOccurs", 0);
  1644. pImageAssetElementL->SetAttribute("maxOccurs", 1);
  1645. pImageAssetNodeChoiceElementL->LinkEndChild(pImageAssetElementL);
  1646. // Create complex type Element.
  1647. TiXmlElement* pImageAssetComplexTypeElementL = new TiXmlElement("xs:complexType");
  1648. pImageAssetElementL->LinkEndChild(pImageAssetComplexTypeElementL);
  1649. // Create "ImageFile" attribute.
  1650. TiXmlElement* pLayerImageFile = new TiXmlElement("xs:attribute");
  1651. pLayerImageFile->SetAttribute("name", layerImageFile);
  1652. pLayerImageFile->SetAttribute("type", "xs:string");
  1653. pImageAssetComplexTypeElementL->LinkEndChild(pLayerImageFile);
  1654. // Create "Position" attribute.
  1655. TiXmlElement* pLayerPosition = new TiXmlElement("xs:attribute");
  1656. pLayerPosition->SetAttribute("name", layerPositionName);
  1657. pLayerPosition->SetAttribute("type", "Point2I_ConsoleType");
  1658. pImageAssetComplexTypeElementL->LinkEndChild(pLayerPosition);
  1659. // Create "BlendColor" attribute.
  1660. TiXmlElement* pLayerBlendColor = new TiXmlElement("xs:attribute");
  1661. pLayerBlendColor->SetAttribute("name", layerBlendColorName);
  1662. pLayerBlendColor->SetAttribute("type", "ColorF_ConsoleType");
  1663. pImageAssetComplexTypeElementL->LinkEndChild(pLayerBlendColor);
  1664. }
  1665. //------------------------------------------------------------------------------
  1666. IMPLEMENT_CONOBJECT_SCHEMA(ImageAsset, WriteCustomTamlSchema);