ImageAsset.h 18 KB

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