ImageAsset.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. class ImageLayer
  140. {
  141. public:
  142. ImageLayer() {}
  143. ImageLayer(const char* imageFile, const Point2I position, const ColorF blendColor) :
  144. mPosition(position), mBlendColor(blendColor), mBitmap(nullptr)
  145. {
  146. mImageFile = StringTable->insert(imageFile);
  147. }
  148. void LoadImage(const char* path)
  149. {
  150. mBitmap = GBitmap::load(path);
  151. }
  152. StringTableEntry mImageFile;
  153. Point2I mPosition;
  154. ColorF mBlendColor;
  155. GBitmap* mBitmap;
  156. };
  157. private:
  158. typedef Vector<FrameArea> typeFrameAreaVector;
  159. typedef Vector<FrameArea::PixelArea> typeExplicitFrameAreaVector;
  160. typedef Vector<ImageLayer> typeImageLayerVector;
  161. /// Configuration.
  162. StringTableEntry mImageFile;
  163. bool mForce16Bit;
  164. TextureFilterMode mLocalFilterMode;
  165. bool mExplicitMode;
  166. bool mCellRowOrder;
  167. S32 mCellOffsetX;
  168. S32 mCellOffsetY;
  169. S32 mCellStrideX;
  170. S32 mCellStrideY;
  171. S32 mCellWidth;
  172. S32 mCellHeight;
  173. S32 mCellCountX;
  174. S32 mCellCountY;
  175. /// Imagery.
  176. typeFrameAreaVector mFrames;
  177. typeExplicitFrameAreaVector mExplicitFrames;
  178. TextureHandle mImageTextureHandle;
  179. typeImageLayerVector mImageLayers;
  180. ColorF mBlendColor;
  181. public:
  182. ImageAsset();
  183. virtual ~ImageAsset();
  184. /// Core.
  185. static void initPersistFields();
  186. virtual bool onAdd();
  187. virtual void onRemove();
  188. virtual void copyTo(SimObject* object);
  189. void setImageFile( const char* pImageFile );
  190. inline StringTableEntry getImageFile( void ) const { return mImageFile; };
  191. inline StringTableEntry getRelativeImageFile(void) const { return collapseAssetFilePath(mImageFile); };
  192. void setForce16Bit( const bool force16Bit );
  193. inline bool getForce16Bit( void ) const { return mForce16Bit; }
  194. void setFilterMode( const TextureFilterMode filterMode );
  195. TextureFilterMode getFilterMode( void ) const { return mLocalFilterMode; }
  196. void setExplicitMode( const bool explicitMode );
  197. bool getExplicitMode( void ) const { return mExplicitMode; }
  198. void setCellRowOrder( const bool cellRowOrder );
  199. inline bool getCellRowOrder( void ) const { return mCellRowOrder; }
  200. void setCellOffsetX( const S32 cellOffsetX );
  201. inline S32 getCellOffsetX( void ) const { return mCellOffsetX; }
  202. void setCellOffsetY( const S32 cellOffsetY );
  203. inline S32 getCellOffsetY( void ) const { return mCellOffsetY; }
  204. void setCellStrideX( const S32 cellStrideX );
  205. inline S32 getCellStrideX( void ) const { return mCellStrideX; }
  206. void setCellStrideY( const S32 cellStrideY );
  207. inline S32 getCellStrideY( void ) const { return mCellStrideY; }
  208. void setCellCountX( const S32 cellCountX );
  209. inline S32 getCellCountX( void ) const { return mCellCountX; }
  210. void setCellCountY( const S32 cellCountY );
  211. inline S32 getCellCountY( void ) const { return mCellCountY; }
  212. void setCellWidth( const S32 cellWidth );
  213. inline S32 getCellWidth( void ) const { return mCellWidth; }
  214. void setCellHeight( const S32 cellheight );
  215. inline S32 getCellHeight( void) const { return mCellHeight; }
  216. Vector2 getExplicitCellOffset(const S32 cellIndex);
  217. S32 getExplicitCellWidth(const S32 cellIndex);
  218. S32 getExplicitCellHeight(const S32 cellIndex);
  219. StringTableEntry getExplicitCellName(const S32 cellIndex);
  220. S32 getExplicitCellIndex(const char* regionName);
  221. bool containsNamedRegion(const char* regionName);
  222. inline TextureHandle& getImageTexture( void ) { return mImageTextureHandle; }
  223. inline S32 getImageWidth( void ) const { return mImageTextureHandle.getWidth(); }
  224. inline S32 getImageHeight( void ) const { return mImageTextureHandle.getHeight(); }
  225. inline U32 getFrameCount( void ) const { return (U32)mFrames.size(); };
  226. inline bool containsFrame( const char* namedFrame ) { return containsNamedRegion(namedFrame); };
  227. FrameArea& getCellByName(const char* cellName);
  228. inline const FrameArea& getImageFrameArea( U32 frame ) const { clampFrame(frame); return mFrames[frame]; };
  229. inline const FrameArea& getImageFrameArea( const char* namedFrame) { return getCellByName(namedFrame); };
  230. inline const void bindImageTexture( void) { glBindTexture( GL_TEXTURE_2D, getImageTexture().getGLName() ); };
  231. virtual bool isAssetValid( void ) const { return !mImageTextureHandle.IsNull(); }
  232. /// Explicit cell control.
  233. bool clearExplicitCells( void );
  234. bool addExplicitCell( const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  235. bool insertExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  236. bool removeExplicitCell( const S32 cellIndex );
  237. bool removeExplicitCell( const char* regionName );
  238. bool setExplicitCell( const S32 cellIndex, const S32 cellOffsetX, const S32 cellOffsetY, const S32 cellWidth, const S32 cellHeight, const char* regionName );
  239. inline S32 getExplicitCellCount( void ) const { return mExplicitFrames.size(); }
  240. static TextureFilterMode getFilterModeEnum(const char* label);
  241. static const char* getFilterModeDescription( TextureFilterMode filterMode );
  242. inline void forceCalculation( void ) { calculateImage(); }
  243. /// Layers
  244. void addLayer(const char* imagePath, const Point2I position, const ColorF blendColor, const bool doRedraw = true);
  245. void insertLayer(const U32 index, const char* imagePath, const Point2I position, const ColorF blendColor, const bool doRedraw = true);
  246. void removeLayer(const U32 index, const bool doRedraw = true);
  247. void moveLayerForward(const U32 index, const bool doRedraw = true);
  248. void moveLayerBackward(const U32 index, const bool doRedraw = true);
  249. void moveLayerToFront(const U32 index, const bool doRedraw = true);
  250. void moveLayerToBack(const U32 index, const bool doRedraw = true);
  251. void setLayerImage(const U32 index, const char* imagePath, const bool doRedraw = true);
  252. void setLayerPosition(const U32 index, const Point2I position, const bool doRedraw = true);
  253. void setLayerBlendColor(const U32 index, const ColorF color, const bool doRedraw = true);
  254. const U32 getLayerCount() { return getMax(mImageLayers.size() - 1, 0); } //We pretend layer 0 doesn't exist as it is internally created
  255. const char* getLayerImage(const U32 index);
  256. const Point2I getLayerPosition(const U32 index);
  257. const ColorF getLayerBlendColor(const U32 index);
  258. void forceRedrawImage() { redrawImage(); }
  259. void setBlendColor(const ColorF color, const bool doRedraw = true);
  260. const ColorF getBlendColor();
  261. /// Declare Console Object.
  262. DECLARE_CONOBJECT(ImageAsset);
  263. private:
  264. inline void clampFrame( U32& frame ) const { const U32 totalFrames = getFrameCount(); if ( frame >= totalFrames ) frame = (totalFrames == 0 ? 0 : totalFrames-1 ); };
  265. void calculateImage( void );
  266. void calculateImplicitMode( void );
  267. void calculateExplicitMode( void );
  268. void setTextureFilter( const TextureFilterMode filterMode );
  269. void completeLayerChange(const bool doRedraw);
  270. void redrawImage();
  271. protected:
  272. virtual void initializeAsset( void );
  273. virtual void onAssetRefresh( void );
  274. /// Taml callbacks.
  275. virtual void onTamlPreWrite( void );
  276. virtual void onTamlPostWrite( void );
  277. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  278. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  279. virtual void loadTamlExplicitCells(const TamlCustomNodes& customNodes);
  280. virtual void loadTamlImageLayers(const TamlCustomNodes& customNodes);
  281. protected:
  282. static void textureEventCallback( const U32 eventCode, void *userData );
  283. static bool setImageFile( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setImageFile(data); return false; }
  284. static const char* getImageFile(void* obj, const char* data) { return static_cast<ImageAsset*>(obj)->getImageFile(); }
  285. static bool writeImageFile( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getImageFile() != StringTable->EmptyString; }
  286. static bool setForce16Bit( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setForce16Bit(dAtob(data)); return false; }
  287. static bool writeForce16Bit( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getForce16Bit() == true; }
  288. static bool setFilterMode( void* obj, const char* data );
  289. static bool writeFilterMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ImageAsset*>(obj)->getFilterMode() != FILTER_INVALID; }
  290. static bool setExplicitMode( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setExplicitMode(dAtob(data)); return false; }
  291. static bool writeExplicitMode(void* obj, StringTableEntry pFieldName) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return pImageAsset->getExplicitMode(); }
  292. static bool setCellRowOrder( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellRowOrder(dAtob(data)); return false; }
  293. static bool writeCellRowOrder( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && !pImageAsset->getCellRowOrder(); }
  294. static bool setCellOffsetX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetX(dAtoi(data)); return false; }
  295. static bool writeCellOffsetX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetX() != 0; }
  296. static bool setCellOffsetY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellOffsetY(dAtoi(data)); return false; }
  297. static bool writeCellOffsetY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellOffsetY() != 0; }
  298. static bool setCellStrideX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideX(dAtoi(data)); return false; }
  299. static bool writeCellStrideX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideX() != 0; }
  300. static bool setCellStrideY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellStrideY(dAtoi(data)); return false; }
  301. static bool writeCellStrideY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellStrideY() != 0; }
  302. static bool setCellCountX( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountX(dAtoi(data)); return false; }
  303. static bool writeCellCountX( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountX() != 0; }
  304. static bool setCellCountY( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellCountY(dAtoi(data)); return false; }
  305. static bool writeCellCountY( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellCountY() != 0; }
  306. static bool setCellWidth( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellWidth(dAtoi(data)); return false; }
  307. static bool writeCellWidth( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellWidth() != 0; }
  308. static bool setCellHeight( void* obj, const char* data ) { static_cast<ImageAsset*>(obj)->setCellHeight(dAtoi(data)); return false; }
  309. static bool writeCellHeight( void* obj, StringTableEntry pFieldName ) { ImageAsset* pImageAsset = static_cast<ImageAsset*>(obj); return !pImageAsset->getExplicitMode() && pImageAsset->getCellHeight() != 0; }
  310. static bool setBlendColor(void* obj, const char* data)
  311. {
  312. if (Utility::mGetStringElementCount(data) < 4)
  313. {
  314. Con::warnf("ImageAsset::BlendColor - Blend colors require four values (red/green/blue/alpha).");
  315. return false;
  316. }
  317. static_cast<ImageAsset*>(obj)->setBlendColor(
  318. ColorF(dAtof(Utility::mGetStringElement(data, 0)),
  319. dAtof(Utility::mGetStringElement(data, 1)),
  320. dAtof(Utility::mGetStringElement(data, 2)),
  321. dAtof(Utility::mGetStringElement(data, 3))), true);
  322. return false;
  323. }
  324. static bool writeBlendColor(void* obj, StringTableEntry pFieldName) { return static_cast<ImageAsset*>(obj)->getBlendColor() != ColorF(1.0f, 1.0f, 1.0f, 1.0f); }
  325. };
  326. //-----------------------------------------------------------------------------
  327. extern ImageAsset::FrameArea BadFrameArea;
  328. #endif // _IMAGE_ASSET_H_