ImageAsset.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. inline void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight )
  65. {
  66. mPixelOffset.set( pixelFrameOffsetX, pixelFrameOffsetY );
  67. mPixelWidth = pixelFrameWidth;
  68. mPixelHeight = pixelFrameHeight;
  69. };
  70. Point2I mPixelOffset;
  71. U32 mPixelWidth;
  72. U32 mPixelHeight;
  73. };
  74. /// Frame Texel Area.
  75. class TexelArea
  76. {
  77. public:
  78. TexelArea() {}
  79. TexelArea( const PixelArea& pixelArea, const F32 texelWidthScale, const F32 texelHeightScale )
  80. {
  81. setArea( pixelArea, texelWidthScale, texelHeightScale );
  82. }
  83. void setArea( const PixelArea& pixelArea, const F32 texelWidthScale, const F32 texelHeightScale )
  84. {
  85. mTexelLower.Set( pixelArea.mPixelOffset.x * texelWidthScale, pixelArea.mPixelOffset.y * texelHeightScale );
  86. mTexelWidth = pixelArea.mPixelWidth * texelWidthScale;
  87. mTexelHeight = pixelArea.mPixelHeight * texelHeightScale;
  88. mTexelUpper.Set( mTexelLower.x + mTexelWidth, mTexelLower.y + mTexelHeight );
  89. }
  90. void setFlip( const bool flipX, const bool flipY )
  91. {
  92. if ( flipX ) mSwap( mTexelLower.x, mTexelUpper.x );
  93. if ( flipY ) mSwap( mTexelLower.y, mTexelUpper.y );
  94. }
  95. Vector2 mTexelLower;
  96. Vector2 mTexelUpper;
  97. F32 mTexelWidth;
  98. F32 mTexelHeight;
  99. };
  100. public:
  101. FrameArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale )
  102. {
  103. setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight, texelWidthScale, texelHeightScale );
  104. }
  105. void setArea( const S32 pixelFrameOffsetX, const S32 pixelFrameOffsetY, const U32 pixelFrameWidth, const U32 pixelFrameHeight, const F32 texelWidthScale, const F32 texelHeightScale )
  106. {
  107. mPixelArea.setArea( pixelFrameOffsetX, pixelFrameOffsetY, pixelFrameWidth, pixelFrameHeight );
  108. mTexelArea.setArea( mPixelArea, texelWidthScale, texelHeightScale );
  109. }
  110. PixelArea mPixelArea;
  111. TexelArea mTexelArea;
  112. };
  113. private:
  114. typedef Vector<FrameArea> typeFrameAreaVector;
  115. typedef Vector<FrameArea::PixelArea> typeExplicitFrameAreaVector;
  116. /// Configuration.
  117. StringTableEntry mImageFile;
  118. bool mForce16Bit;
  119. TextureFilterMode mLocalFilterMode;
  120. bool mExplicitMode;
  121. bool mCellRowOrder;
  122. S32 mCellOffsetX;
  123. S32 mCellOffsetY;
  124. S32 mCellStrideX;
  125. S32 mCellStrideY;
  126. S32 mCellWidth;
  127. S32 mCellHeight;
  128. S32 mCellCountX;
  129. S32 mCellCountY;
  130. /// Imagery.
  131. typeFrameAreaVector mFrames;
  132. typeExplicitFrameAreaVector mExplicitFrames;
  133. TextureHandle mImageTextureHandle;
  134. public:
  135. ImageAsset();
  136. virtual ~ImageAsset();
  137. /// Core.
  138. static void initPersistFields();
  139. virtual bool onAdd();
  140. virtual void onRemove();
  141. virtual void copyTo(SimObject* object);
  142. void setImageFile( const char* pImageFile );
  143. inline StringTableEntry getImageFile( void ) const { return mImageFile; };
  144. void setForce16Bit( const bool force16Bit );
  145. inline bool getForce16Bit( void ) const { return mForce16Bit; }
  146. void setFilterMode( const TextureFilterMode filterMode );
  147. TextureFilterMode getFilterMode( void ) const { return mLocalFilterMode; }
  148. void setExplicitMode( const bool explicitMode );
  149. bool getExplicitMode( void ) const { return mExplicitMode; }
  150. void setCellRowOrder( const bool cellRowOrder );
  151. inline bool getCellRowOrder( void ) const { return mCellRowOrder; }
  152. void setCellOffsetX( const S32 cellOffsetX );
  153. inline S32 getCellOffsetX( void ) const { return mCellOffsetX; }
  154. void setCellOffsetY( const S32 cellOffsetY );
  155. inline S32 getCellOffsetY( void ) const { return mCellOffsetY; }
  156. void setCellStrideX( const S32 cellStrideX );
  157. inline S32 getCellStrideX( void ) const { return mCellStrideX; }
  158. void setCellStrideY( const S32 cellStrideY );
  159. inline S32 getCellStrideY( void ) const { return mCellStrideY; }
  160. void setCellCountX( const S32 cellCountX );
  161. inline S32 getCellCountX( void ) const { return mCellCountX; }
  162. void setCellCountY( const S32 cellCountY );
  163. inline S32 getCellCountY( void ) const { return mCellCountY; }
  164. void setCellWidth( const S32 cellWidth );
  165. inline S32 getCellWidth( void ) const { return mCellWidth; }
  166. void setCellHeight( const S32 cellheight );
  167. S32 getCellHeight( void) const { return mCellHeight; }
  168. inline TextureHandle& getImageTexture( void ) { return mImageTextureHandle; }
  169. inline S32 getImageWidth( void ) const { return mImageTextureHandle.getWidth(); }
  170. inline S32 getImageHeight( void ) const { return mImageTextureHandle.getHeight(); }
  171. inline U32 getFrameCount( void ) const { return (U32)mFrames.size(); };
  172. inline const FrameArea& getImageFrameArea( U32 frame ) const { clampFrame(frame); return mFrames[frame]; };
  173. inline const void bindImageTexture( void) { glBindTexture( GL_TEXTURE_2D, getImageTexture().getGLName() ); };
  174. virtual bool isAssetValid( void ) const { return !mImageTextureHandle.IsNull(); }
  175. /// Explicit cell control.
  176. bool clearExplicitCells( void );
  177. bool addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight );
  178. bool insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight );
  179. bool removeExplicitCell( const S32 cellIndex );
  180. bool setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight );
  181. inline S32 getExplicitCellCount( void ) const { return mExplicitFrames.size(); }
  182. static TextureFilterMode getFilterModeEnum(const char* label);
  183. static const char* getFilterModeDescription( TextureFilterMode filterMode );
  184. /// Declare Console Object.
  185. DECLARE_CONOBJECT(ImageAsset);
  186. private:
  187. inline void clampFrame( U32& frame ) const { const U32 totalFrames = getFrameCount(); if ( frame >= totalFrames ) frame = (totalFrames == 0 ? 0 : totalFrames-1 ); };
  188. void calculateImage( void );
  189. void calculateImplicitMode( void );
  190. void calculateExplicitMode( void );
  191. void setTextureFilter( const TextureFilterMode filterMode );
  192. protected:
  193. virtual void initializeAsset( void );
  194. virtual void onAssetRefresh( void );
  195. /// Taml callbacks.
  196. virtual void onTamlPreWrite( void );
  197. virtual void onTamlPostWrite( void );
  198. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  199. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  200. protected:
  201. static void textureEventCallback( const U32 eventCode, void *userData );
  202. static bool setImageFile( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setImageFile(data); return false; }
  203. static const char* getImageFile(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFile(); }
  204. static bool writeImageFile( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getImageFile() != StringTable->EmptyString; }
  205. static bool setForce16Bit( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setForce16Bit(dAtob(data)); return false; }
  206. static bool writeForce16Bit( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getForce16Bit() == true; }
  207. static bool setFilterMode( void* obj, const char* data );
  208. static bool writeFilterMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getFilterMode() != FILTER_BILINEAR; }
  209. static bool setExplicitMode( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setExplicitMode(dAtob(data)); return false; }
  210. static bool setCellRowOrder( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellRowOrder(dAtob(data)); return false; }
  211. static bool writeCellRowOrder( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && !pImageAsset->getCellRowOrder(); }
  212. static bool setCellOffsetX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetX(dAtoi(data)); return false; }
  213. static bool writeCellOffsetX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetX() != 0; }
  214. static bool setCellOffsetY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetY(dAtoi(data)); return false; }
  215. static bool writeCellOffsetY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetY() != 0; }
  216. static bool setCellStrideX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideX(dAtoi(data)); return false; }
  217. static bool writeCellStrideX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideX() != 0; }
  218. static bool setCellStrideY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideY(dAtoi(data)); return false; }
  219. static bool writeCellStrideY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideY() != 0; }
  220. static bool setCellCountX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountX(dAtoi(data)); return false; }
  221. static bool writeCellCountX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountX() != 0; }
  222. static bool setCellCountY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountY(dAtoi(data)); return false; }
  223. static bool writeCellCountY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountY() != 0; }
  224. static bool setCellWidth( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellWidth(dAtoi(data)); return false; }
  225. static bool writeCellWidth( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellWidth() != 0; }
  226. static bool setCellHeight( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellHeight(dAtoi(data)); return false; }
  227. static bool writeCellHeight( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellHeight() != 0; }
  228. };
  229. //-----------------------------------------------------------------------------
  230. extern ImageAsset::FrameArea BadFrameArea;
  231. #endif // _IMAGE_ASSET_H_