guiSpriteCtrl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 _GUISPRITECTRL_H_
  23. #define _GUISPRITECTRL_H_
  24. #ifndef _IMAGE_FRAME_PROVIDER_H
  25. #include "2d/core/ImageFrameProvider.h"
  26. #endif
  27. //-----------------------------------------------------------------------------
  28. class GuiSpriteCtrl : public GuiControl, public ImageFrameProvider
  29. {
  30. private:
  31. typedef GuiControl Parent;
  32. protected:
  33. StringTableEntry mImageAssetId;
  34. U32 mFrame;
  35. StringTableEntry mNamedImageFrameId;
  36. StringTableEntry mAnimationAssetId;
  37. StringTableEntry mBitmapName;
  38. TextureHandle mBitmapTextureHandle;
  39. Vector<RectI> mBitmapArrayRects;
  40. bool mTileImage; //If true, the image will tile over the content area
  41. Point2I mPositionOffset; //Offset from the set location of the image
  42. FluidColorI mImageColor; //The blend color used on the image
  43. Point2I mImageSize; //The size of the image, reduced, if needed, to fit the content area
  44. bool mFullSize; //If true, the image will take all available space
  45. bool mConstrainProportions; //If true, the image will maintain its aspect ratio
  46. bool mClampImage; //If true, the image will be forced into the control's content area.
  47. bool mSingleFrameBitmap; //If true and bitmaps are used, this will assume there's only one frame when scanning the bitmap
  48. Fluid mFluidMoveTo;
  49. Point2I mStartOffset;
  50. Point2I mTargetOffset;
  51. Fluid mFluidGrowTo;
  52. Point2I mStartSize;
  53. Point2I mTargetSize;
  54. public:
  55. GuiSpriteCtrl();
  56. virtual ~GuiSpriteCtrl();
  57. static void initPersistFields();
  58. bool onWake();
  59. void onSleep();
  60. virtual void copyTo(SimObject* object);
  61. //Rendering
  62. void onRender(Point2I offset, const RectI &updateRect);
  63. void RenderImage(RectI &bounds, Point2I &offset, Point2I &size);
  64. void RenderTiledImage(RectI &bounds, Point2I &start, Point2I &size);
  65. //positioning and sizing
  66. Point2I constrain(Point2I &point, bool grow = true);
  67. Point2I constrainLockX(Point2I &point);
  68. Point2I constrainLockY(Point2I &point);
  69. F32 getAspectRatio();
  70. Point2I applyAlignment(RectI &bounds, Point2I &size);
  71. //Animation Functions
  72. void moveTo(S32 x, S32 y, S32 time, EasingFunction ease = Linear);
  73. void growTo(S32 x, S32 y, S32 time, EasingFunction ease = Linear);
  74. void fadeTo(const ColorI &color, S32 time, EasingFunction ease = Linear);
  75. bool animateMoveTo();
  76. bool animateGrowTo();
  77. bool animateFadeTo();
  78. // Static and Animated Assets.
  79. inline bool setImage( const char* pImageAssetId ) { return setImage( pImageAssetId, mImageFrame ); }
  80. virtual bool setImage( const char* pImageAssetId, const U32 frame );
  81. virtual bool setImage( const char* pImageAssetId, const char* pNamedFrame );
  82. inline StringTableEntry getImage( void ) const{ return mImageAssetId; }
  83. virtual bool setImageFrame( const U32 frame );
  84. inline U32 getImageFrame( void ) const { return mFrame; }
  85. virtual bool setNamedImageFrame ( const char* namedFrame );
  86. inline StringTableEntry getNamedImageFrame( void ) const { return mNamedImageFrameId; }
  87. virtual bool setAnimation( const char* pAnimationAssetId );
  88. inline StringTableEntry getAnimation( void ) const { return mAnimationAssetId; }
  89. //Accessors
  90. inline bool getTileImage() { return mTileImage; }
  91. inline void setTileImage(bool setting) { mTileImage = setting; }
  92. inline Point2I getPositionOffset() { return mPositionOffset; }
  93. inline void setPositionOffset(S32 x, S32 y) { mPositionOffset = Point2I(x, y); }
  94. inline ColorI getImageColor() { return ColorI(mImageColor); }
  95. inline void setImageColor(ColorI color) { mImageColor = FluidColorI(color); }
  96. inline Point2I getImageSize() { return mImageSize; }
  97. inline void setImageSize(Point2I size) { mImageSize = size; }
  98. inline bool getFullSize() { return mFullSize; }
  99. inline void setFullSize(bool isFull) { mFullSize = isFull; }
  100. inline bool getClampImage() { return mClampImage; }
  101. inline void setClampImage(bool clamp) { mClampImage = clamp; }
  102. inline bool getConstrainProportions() { return mConstrainProportions; }
  103. inline void setConstrainProportions(bool setting) { mConstrainProportions = setting; }
  104. inline bool getSingleFrameBitmap() { return mSingleFrameBitmap; }
  105. inline void setSingleFrameBitmap(bool isSingleFrame) { mSingleFrameBitmap = isSingleFrame; }
  106. //Bitmap members
  107. void setBitmap(const char *name);
  108. inline void setBitmap(const TextureHandle &handle) { mBitmapTextureHandle = handle; }
  109. inline StringTableEntry getBitmapName(void) const { return mBitmapName; }
  110. U32 constructBitmapArray();
  111. inline U32 getBitmapFrame() { return mSingleFrameBitmap ? 1 : constructBitmapArray(); }
  112. inline bool usesAsset() { return (mImageAssetId != StringTable->EmptyString) || (mAnimationAssetId != StringTable->EmptyString); }
  113. // Declare type.
  114. DECLARE_CONOBJECT(GuiSpriteCtrl);
  115. protected:
  116. virtual bool update(const F32 elapsedTime);
  117. virtual void onAnimationEnd( void );
  118. virtual void processTick();
  119. virtual void interpolateTick(F32 delta) {};
  120. virtual void advanceTime(F32 timeDelta) {};
  121. RectI getSourceRect(TextureObject* texture);
  122. protected:
  123. static bool setImage(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setImage( data ); return false; }
  124. static bool writeImage(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if( !pCastObject->usesAsset() || !pCastObject->isStaticFrameProvider() ) return false; return pCastObject->mImageAssetId != StringTable->EmptyString; }
  125. static bool setImageFrame(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setImageFrame( dAtoi(data) ); return false; }
  126. static bool writeImageFrame(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if( !pCastObject->usesAsset() ) { return pCastObject->getBitmapFrame(); } if ( !pCastObject->isStaticFrameProvider() || pCastObject->isUsingNamedImageFrame() ) return false; return pCastObject->getImageFrame() > 0; }
  127. static bool setNamedImageFrame(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setNamedImageFrame(data); return false; }
  128. static bool writeNamedImageFrame(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if( !pCastObject->usesAsset() || !pCastObject->isStaticFrameProvider() || !pCastObject->isUsingNamedImageFrame() ) return false; return pCastObject->mNamedImageFrameId != StringTable->EmptyString; }
  129. static bool setAnimation(void* obj, const char* data) { static_cast<GuiSpriteCtrl*>(obj)->setAnimation(data); return false; };
  130. static bool writeAnimation(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if( !pCastObject->usesAsset() || pCastObject->isStaticFrameProvider() ) return false; return pCastObject->mAnimationAssetId != StringTable->EmptyString; }
  131. static bool setBitmapName(void *obj, const char *data) { static_cast<GuiSpriteCtrl *>(obj)->setBitmap(data); return false; }
  132. static const char *getBitmapName(void *obj, const char *data) { return static_cast<GuiSpriteCtrl*>(obj)->getBitmapName(); }
  133. static bool writeBitmapName(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); if( pCastObject->usesAsset() ) return false; return pCastObject->mBitmapName != StringTable->EmptyString; }
  134. static bool writeSingleFrameBitmap(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return !pCastObject->mSingleFrameBitmap && !pCastObject->usesAsset() && pCastObject->mBitmapName != StringTable->EmptyString; }
  135. static bool writeTileImage(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return pCastObject->mTileImage; }
  136. static bool writePositionOffset(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return !pCastObject->mPositionOffset.isZero(); }
  137. static bool writeImageColor(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return pCastObject->mImageColor.red != 255 || pCastObject->mImageColor.green != 255 || pCastObject->mImageColor.blue != 255 || pCastObject->mImageColor.alpha != 255; }
  138. static bool writeImageSize(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return pCastObject->mImageSize.x != 10 || pCastObject->mImageSize.y != 10; }
  139. static bool writeFullSize(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return !pCastObject->mFullSize; }
  140. static bool writeClampImage(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return !pCastObject->mClampImage; }
  141. static bool writeConstrainProportions(void* obj, StringTableEntry pFieldName) { GuiSpriteCtrl* pCastObject = static_cast<GuiSpriteCtrl*>(obj); return !pCastObject->mConstrainProportions; }
  142. };
  143. #endif //_GUISPRITECTRL_H_