Scroller.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 _SCROLLER_H_
  23. #define _SCROLLER_H_
  24. #ifndef _SPRITE_BASE_H_
  25. #include "2d/core/SpriteBase.h"
  26. #endif
  27. #ifndef _TEXTURE_MANAGER_H_
  28. #include "graphics/TextureManager.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. class Scroller : public SpriteBase
  32. {
  33. typedef SpriteBase Parent;
  34. private:
  35. F32 mRepeatX;
  36. F32 mRepeatY;
  37. F32 mScrollX;
  38. F32 mScrollY;
  39. F32 mTextureOffsetX;
  40. F32 mTextureOffsetY;
  41. Vector2 mPreTickTextureOffset;
  42. Vector2 mPostTickTextureOffset;
  43. Vector2 mRenderTickTextureOffset;
  44. /// Scroll split region.
  45. struct ScrollSplitRegion
  46. {
  47. /// Vertexes.
  48. F32 mVertSplitLowerX1;
  49. F32 mVertSplitLowerX2;
  50. F32 mVertSplitUpperX1;
  51. F32 mVertSplitUpperX2;
  52. F32 mVertSplitLowerY1;
  53. F32 mVertSplitLowerY2;
  54. F32 mVertSplitUpperY1;
  55. F32 mVertSplitUpperY2;
  56. F32 mTexSplitLowerX1;
  57. F32 mTexSplitLowerX2;
  58. F32 mTexSplitUpperX1;
  59. F32 mTexSplitUpperX2;
  60. F32 mTexSplitLowerY1;
  61. F32 mTexSplitLowerY2;
  62. F32 mTexSplitUpperY1;
  63. F32 mTexSplitUpperY2;
  64. inline void addVertexOffset( const F32 offsetX, const F32 offsetY )
  65. {
  66. mVertSplitLowerX1 += offsetX;
  67. mVertSplitLowerX2 += offsetX;
  68. mVertSplitUpperX1 += offsetX;
  69. mVertSplitUpperX2 += offsetX;
  70. mVertSplitLowerY1 += offsetY;
  71. mVertSplitLowerY2 += offsetY;
  72. mVertSplitUpperY1 += offsetY;
  73. mVertSplitUpperY2 += offsetY;
  74. }
  75. };
  76. public:
  77. Scroller();
  78. virtual ~Scroller();
  79. static void initPersistFields();
  80. inline void setRepeatX( const F32 repeatX ) { setRepeat(repeatX, getRepeatY()); };
  81. inline void setRepeatY( const F32 repeatY ) { setRepeat(getRepeatX(), repeatY); };
  82. void setRepeat( const F32 repeatX, const F32 repeatY );
  83. void setScrollX(F32 x) { setScroll(x, getScrollY()); };
  84. void setScrollY(F32 y) { setScroll(getScrollX(), y); };
  85. void setScroll( F32 scrollX = 0.0f, F32 scrollY = 0.0f );
  86. inline void setScrollPositionX( F32 scrollX ) { setScrollPosition(scrollX, getScrollPositionY()); };
  87. inline void setScrollPositionY( F32 scrollY ) { setScrollPosition(getScrollPositionX(), scrollY); };
  88. void setScrollPosition( F32 scrollX, F32 scrollY );
  89. inline F32 getRepeatX() { return mRepeatX; };
  90. inline F32 getRepeatY() { return mRepeatY; };
  91. inline F32 getScrollX() { return mScrollX; };
  92. inline F32 getScrollY() { return mScrollY; };
  93. inline F32 getScrollPositionX() { return mTextureOffsetX; };
  94. inline F32 getScrollPositionY() { return mTextureOffsetY; };
  95. /// Tick Processing.
  96. void resetTickScrollPositions( void );
  97. void updateTickScrollPosition( void );
  98. virtual void interpolateObject( const F32 timeDelta );
  99. virtual bool onAdd();
  100. virtual void onRemove();
  101. virtual void integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  102. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  103. virtual void setAngle( const F32 radians ) { Parent::setAngle( 0.0f ); }; // Stop angle being changed.
  104. virtual void setFixedAngle( const bool fixed ) { Parent::setFixedAngle( true ); } // Always fixed angle.
  105. virtual void copyTo(SimObject* object);
  106. /// Declare Console Object.
  107. DECLARE_CONOBJECT(Scroller);
  108. private:
  109. void renderRegionSplitX( BatchRender* pBatchRenderer, TextureHandle& texture, const ScrollSplitRegion& splitRegion );
  110. void renderRegionSplitY( BatchRender* pBatchRenderer, TextureHandle& texture, const ScrollSplitRegion& splitRegion );
  111. void renderRegionSplitXY( BatchRender* pBatchRenderer, TextureHandle& texture, const ScrollSplitRegion& splitRegion );
  112. void renderRegionNoSplit( BatchRender* pBatchRenderer, TextureHandle& texture, const ScrollSplitRegion& splitRegion );
  113. protected:
  114. static bool setRepeatX(void* obj, const char* data) { static_cast<Scroller*>(obj)->setRepeatX( dAtof(data) ); return false; }
  115. static bool writeRepeatX( void* obj, StringTableEntry pFieldName ) { return mNotEqual( static_cast<Scroller*>(obj)->mRepeatX, 1.0f); }
  116. static bool setRepeatY(void* obj, const char* data) { static_cast<Scroller*>(obj)->setRepeatY( dAtof(data) ); return false; }
  117. static bool writeRepeatY( void* obj, StringTableEntry pFieldName ) { return mNotEqual( static_cast<Scroller*>(obj)->mRepeatY, 1.0f); }
  118. static bool writeScrollX( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<Scroller*>(obj)->mScrollX); }
  119. static bool writeScrollY( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<Scroller*>(obj)->mScrollY); }
  120. static bool writeScrollPositionX( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<Scroller*>(obj)->mTextureOffsetX); }
  121. static bool writeScrollPositionY( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<Scroller*>(obj)->mTextureOffsetY); }
  122. };
  123. #endif // _SCROLLER_H_