ImageAsset.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 _IMAGE_ASSET_H_
  23. #define _IMAGE_ASSET_H_
  24. #ifndef _ASSET_BASE_H_
  25. #include "assets/assetBase.h"
  26. #endif
  27. #ifndef _VECTOR_H_
  28. #include "collection/vector.h"
  29. #endif
  30. #ifndef _VECTOR2_H_
  31. #include "2d/core/Vector2.h"
  32. #endif
  33. #ifndef _TEXTURE_MANAGER_H_
  34. #include "graphics/TextureManager.h"
  35. #endif
  36. //-----------------------------------------------------------------------------
  37. DefineConsoleType( TypeImageAssetPtr )
  38. //-----------------------------------------------------------------------------
  39. class ImageAsset : public AssetBase
  40. {
  41. private:
  42. typedef AssetBase Parent;
  43. public:
  44. /// Texture Filter Mode.
  45. enum TextureFilterMode
  46. {
  47. FILTER_NEAREST,
  48. FILTER_BILINEAR,
  49. FILTER_INVALID,
  50. };
  51. /// Frame area.
  52. class FrameArea
  53. {
  54. public:
  55. /// Frame Pixel Area.
  56. class PixelArea
  57. {
  58. public:
  59. PixelArea() {}
  60. PixelArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight )
  61. {
  62. setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight );
  63. }
  64. PixelArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const char* regionName )
  65. {
  66. setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight, regionName );
  67. }
  68. inline void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight )
  69. {
  70. mPixelOffset.set( pixelFrameOffsetX, pixelFrameOffsetY );
  71. mPixelWidth = pixelFrameWidth;
  72. mPixelHeight = pixelFrameHeight;
  73. };
  74. inline void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const char* regionName )
  75. {
  76. mPixelOffset.set( pixelFrameOffsetX, pixelFrameOffsetY );
  77. mPixelWidth = pixelFrameWidth;
  78. mPixelHeight = pixelFrameHeight;
  79. mRegionName = StringTable->insert(regionName);
  80. };
  81. Point2I mPixelOffset;
  82. U32 mPixelWidth;
  83. U32 mPixelHeight;
  84. StringTableEntry mRegionName;
  85. };
  86. /// Frame Texel Area.
  87. class TexelArea
  88. {
  89. public:
  90. TexelArea() {}
  91. TexelArea( const PixelArea& pixelArea, const F32 texelWidthScale, const F32 texelHeightScale )
  92. {
  93. setArea( pixelArea, texelWidthScale, texelHeightScale );
  94. }
  95. void setArea( const PixelArea& pixelArea, const F32 texelWidthScale, const F32 texelHeightScale )
  96. {
  97. mTexelLower.Set( pixelArea.mPixelOffset.x * texelWidthScale, pixelArea.mPixelOffset.y * texelHeightScale );
  98. mTexelWidth = pixelArea.mPixelWidth * texelWidthScale;
  99. mTexelHeight = pixelArea.mPixelHeight * texelHeightScale;
  100. mTexelUpper.Set( mTexelLower.x + mTexelWidth, mTexelLower.y + mTexelHeight );
  101. }
  102. void setFlip( const bool flipX, const bool flipY )
  103. {
  104. if ( flipX ) mSwap( mTexelLower.x, mTexelUpper.x );
  105. if ( flipY ) mSwap( mTexelLower.y, mTexelUpper.y );
  106. }
  107. Vector2 mTexelLower;
  108. Vector2 mTexelUpper;
  109. F32 mTexelWidth;
  110. F32 mTexelHeight;
  111. };
  112. public:
  113. FrameArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale )
  114. {
  115. setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight, texelWidthScale, texelHeightScale );
  116. }
  117. FrameArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale, const char* regionName )
  118. {
  119. setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight, texelWidthScale, texelHeightScale, regionName);
  120. }
  121. FrameArea()
  122. {
  123. setArea(0, 0, 0, 0, 0.0f, 0.0f);
  124. }
  125. void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale )
  126. {
  127. mPixelArea.setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight );
  128. mTexelArea.setArea( mPixelArea, texelWidthScale, texelHeightScale );
  129. }
  130. void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale, const char* regionName )
  131. {
  132. mPixelArea.setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight, regionName );
  133. mTexelArea.setArea( mPixelArea, texelWidthScale, texelHeightScale );
  134. }
  135. PixelArea mPixelArea;
  136. TexelArea mTexelArea;
  137. };
  138. private:
  139. typedef Vector<FrameArea> typeFrameAreaVector;
  140. typedef Vector<FrameArea::PixelArea> typeExplicitFrameAreaVector;
  141. /// Configuration.
  142. StringTableEntry mImageFile;
  143. bool mForce16Bit;
  144. TextureFilterMode mLocalFilterMode;
  145. bool mExplicitMode;
  146. bool mCellRowOrder;
  147. S32 mCellOffsetX;
  148. S32 mCellOffsetY;
  149. S32 mCellStrideX;
  150. S32 mCellStrideY;
  151. S32 mCellWidth;
  152. S32 mCellHeight;
  153. S32 mCellCountX;
  154. S32 mCellCountY;
  155. /// Imagery.
  156. typeFrameAreaVector mFrames;
  157. typeExplicitFrameAreaVector mExplicitFrames;
  158. TextureHandle mImageTextureHandle;
  159. public:
  160. ImageAsset();
  161. virtual ~ImageAsset();
  162. /// Core.
  163. static void initPersistFields();
  164. virtual bool onAdd();
  165. virtual void onRemove();
  166. virtual void copyTo(SimObject* object);
  167. void setImageFile( const char* pImageFile );
  168. inline StringTableEntry getImageFile( void ) const { return mImageFile; };
  169. void setForce16Bit( const bool force16Bit );
  170. inline bool getForce16Bit( void ) const { return mForce16Bit; }
  171. void setFilterMode( const TextureFilterMode filterMode );
  172. TextureFilterMode getFilterMode( void ) const { return mLocalFilterMode; }
  173. void setExplicitMode( const bool explicitMode );
  174. bool getExplicitMode( void ) const { return mExplicitMode; }
  175. void setCellRowOrder( const bool cellRowOrder );
  176. inline bool getCellRowOrder( void ) const { return mCellRowOrder; }
  177. void setCellOffsetX( const S32 cellOffsetX );
  178. inline S32 getCellOffsetX( void ) const { return mCellOffsetX; }
  179. void setCellOffsetY( const S32 cellOffsetY );
  180. inline S32 getCellOffsetY( void ) const { return mCellOffsetY; }
  181. void setCellStrideX( const S32 cellStrideX );
  182. inline S32 getCellStrideX( void ) const { return mCellStrideX; }
  183. void setCellStrideY( const S32 cellStrideY );
  184. inline S32 getCellStrideY( void ) const { return mCellStrideY; }
  185. void setCellCountX( const S32 cellCountX );
  186. inline S32 getCellCountX( void ) const { return mCellCountX; }
  187. void setCellCountY( const S32 cellCountY );
  188. inline S32 getCellCountY( void ) const { return mCellCountY; }
  189. void setCellWidth( const S32 cellWidth );
  190. inline S32 getCellWidth( void ) const { return mCellWidth; }
  191. void setCellHeight( const S32 cellheight );
  192. inline S32 getCellHeight( void) const { return mCellHeight; }
  193. Vector2 getExplicitCellOffset(const S32 cellIndex);
  194. S32 getExplicitCellWidth(const S32 cellIndex);
  195. S32 getExplicitCellHeight(const S32 cellIndex);
  196. StringTableEntry getExplicitCellName(const S32 cellIndex);
  197. S32 getExplicitCellIndex(const char* regionName);
  198. bool containsNamedRegion(const char* regionName);
  199. inline TextureHandle& getImageTexture( void ) { return mImageTextureHandle; }
  200. inline S32 getImageWidth( void ) const { return mImageTextureHandle.getWidth(); }
  201. inline S32 getImageHeight( void ) const { return mImageTextureHandle.getHeight(); }
  202. inline U32 getFrameCount( void ) const { return (U32)mFrames.size(); };
  203. inline bool containsFrame( const char* namedFrame ) { return containsNamedRegion(namedFrame); };
  204. FrameArea& getCellByName(const char* cellName);
  205. inline const FrameArea& getImageFrameArea( U32 frame ) const { clampFrame(frame); return mFrames[frame]; };
  206. inline const FrameArea& getImageFrameArea( const char* namedFrame) { return getCellByName(namedFrame); };
  207. inline const void bindImageTexture( void) { glBindTexture( GL_TEXTURE_2D, getImageTexture().getGLName() ); };
  208. virtual bool isAssetValid( void ) const { return !mImageTextureHandle.IsNull(); }
  209. /// Explicit cell control.
  210. bool clearExplicitCells( void );
  211. bool addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  212. bool insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  213. bool removeExplicitCell( const S32 cellIndex );
  214. bool removeExplicitCell( const char* regionName );
  215. bool setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  216. inline S32 getExplicitCellCount( void ) const { return mExplicitFrames.size(); }
  217. static TextureFilterMode getFilterModeEnum(const char* label);
  218. static const char* getFilterModeDescription( TextureFilterMode filterMode );
  219. inline void forceCalculation( void ) { calculateImage(); }
  220. /// Declare Console Object.
  221. DECLARE_CONOBJECT(ImageAsset);
  222. private:
  223. inline void clampFrame( U32& frame ) const { const U32 totalFrames = getFrameCount(); if ( frame >= totalFrames ) frame = (totalFrames == 0 ? 0 : totalFrames-1 ); };
  224. void calculateImage( void );
  225. void calculateImplicitMode( void );
  226. void calculateExplicitMode( void );
  227. void setTextureFilter( const TextureFilterMode filterMode );
  228. protected:
  229. virtual void initializeAsset( void );
  230. virtual void onAssetRefresh( void );
  231. /// Taml callbacks.
  232. virtual void onTamlPreWrite( void );
  233. virtual void onTamlPostWrite( void );
  234. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  235. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  236. protected:
  237. static void textureEventCallback( const U32 eventCode, void *userData );
  238. static bool setImageFile( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setImageFile(data); return false; }
  239. static const char* getImageFile(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFile(); }
  240. static bool writeImageFile( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getImageFile() != StringTable->EmptyString; }
  241. static bool setForce16Bit( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setForce16Bit(dAtob(data)); return false; }
  242. static bool writeForce16Bit( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getForce16Bit() == true; }
  243. static bool setFilterMode( void* obj, const char* data );
  244. static bool writeFilterMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getFilterMode() != FILTER_BILINEAR; }
  245. static bool setExplicitMode( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setExplicitMode(dAtob(data)); return false; }
  246. static bool writeExplicitMode(void* obj, StringTableEntry pFieldName) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return pImageAsset->getExplicitMode(); }
  247. static bool setCellRowOrder( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellRowOrder(dAtob(data)); return false; }
  248. static bool writeCellRowOrder( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && !pImageAsset->getCellRowOrder(); }
  249. static bool setCellOffsetX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetX(dAtoi(data)); return false; }
  250. static bool writeCellOffsetX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetX() != 0; }
  251. static bool setCellOffsetY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetY(dAtoi(data)); return false; }
  252. static bool writeCellOffsetY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetY() != 0; }
  253. static bool setCellStrideX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideX(dAtoi(data)); return false; }
  254. static bool writeCellStrideX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideX() != 0; }
  255. static bool setCellStrideY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideY(dAtoi(data)); return false; }
  256. static bool writeCellStrideY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideY() != 0; }
  257. static bool setCellCountX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountX(dAtoi(data)); return false; }
  258. static bool writeCellCountX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountX() != 0; }
  259. static bool setCellCountY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountY(dAtoi(data)); return false; }
  260. static bool writeCellCountY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountY() != 0; }
  261. static bool setCellWidth( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellWidth(dAtoi(data)); return false; }
  262. static bool writeCellWidth( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellWidth() != 0; }
  263. static bool setCellHeight( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellHeight(dAtoi(data)); return false; }
  264. static bool writeCellHeight( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellHeight() != 0; }
  265. };
  266. //-----------------------------------------------------------------------------
  267. extern ImageAsset::FrameArea BadFrameArea;
  268. #endif // _IMAGE_ASSET_H_