guiIdleCamFadeBitmapCtrl.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. #include "platform/platform.h"
  23. #include "gui/controls/guiBitmapCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. class GuiIdleCamFadeBitmapCtrl : public GuiBitmapCtrl
  28. {
  29. typedef GuiBitmapCtrl Parent;
  30. public:
  31. DECLARE_CONOBJECT(GuiIdleCamFadeBitmapCtrl);
  32. DECLARE_CATEGORY( "Gui Images" );
  33. U32 wakeTime;
  34. bool done;
  35. U32 fadeinTime;
  36. U32 fadeoutTime;
  37. bool doFadeIn;
  38. bool doFadeOut;
  39. GuiIdleCamFadeBitmapCtrl()
  40. {
  41. wakeTime = 0;
  42. fadeinTime = 1000;
  43. fadeoutTime = 1000;
  44. done = false;
  45. doFadeIn = false;
  46. doFadeOut = false;
  47. }
  48. void onPreRender()
  49. {
  50. Parent::onPreRender();
  51. setUpdate();
  52. }
  53. void onMouseDown(const GuiEvent &)
  54. {
  55. Con::executef(this, "click");
  56. }
  57. bool onKeyDown(const GuiEvent &)
  58. {
  59. Con::executef(this, "click");
  60. return true;
  61. }
  62. bool onWake()
  63. {
  64. if(!Parent::onWake())
  65. return false;
  66. wakeTime = Platform::getRealMilliseconds();
  67. return true;
  68. }
  69. void fadeIn()
  70. {
  71. wakeTime = Platform::getRealMilliseconds();
  72. doFadeIn = true;
  73. doFadeOut = false;
  74. done = false;
  75. }
  76. void fadeOut()
  77. {
  78. wakeTime = Platform::getRealMilliseconds();
  79. doFadeIn = false;
  80. doFadeOut = true;
  81. done = false;
  82. }
  83. void onRender(Point2I offset, const RectI &updateRect)
  84. {
  85. U32 elapsed = Platform::getRealMilliseconds() - wakeTime;
  86. U32 alpha;
  87. if (doFadeOut && elapsed < fadeoutTime)
  88. {
  89. // fade out
  90. alpha = 255 - (255 * (F32(elapsed) / F32(fadeoutTime)));
  91. }
  92. else if (doFadeIn && elapsed < fadeinTime)
  93. {
  94. // fade in
  95. alpha = 255 * F32(elapsed) / F32(fadeinTime);
  96. }
  97. else
  98. {
  99. // done state
  100. alpha = doFadeIn ? 255 : 0;
  101. done = true;
  102. }
  103. ColorI color(255,255,255,alpha);
  104. if (mTextureObject)
  105. {
  106. GFX->getDrawUtil()->setBitmapModulation(color);
  107. if(mWrap)
  108. {
  109. GFXTextureObject* texture = mTextureObject;
  110. RectI srcRegion;
  111. RectI dstRegion;
  112. F32 xdone = ((F32)getExtent().x/(F32)texture->mBitmapSize.x)+1;
  113. F32 ydone = ((F32)getExtent().y/(F32)texture->mBitmapSize.y)+1;
  114. S32 xshift = mStartPoint.x%texture->mBitmapSize.x;
  115. S32 yshift = mStartPoint.y%texture->mBitmapSize.y;
  116. for(S32 y = 0; y < ydone; ++y)
  117. for(S32 x = 0; x < xdone; ++x)
  118. {
  119. srcRegion.set(0,0,texture->mBitmapSize.x,texture->mBitmapSize.y);
  120. dstRegion.set( ((texture->mBitmapSize.x*x)+offset.x)-xshift,
  121. ((texture->mBitmapSize.y*y)+offset.y)-yshift,
  122. texture->mBitmapSize.x,
  123. texture->mBitmapSize.y);
  124. GFX->getDrawUtil()->drawBitmapStretchSR(texture,dstRegion, srcRegion);
  125. }
  126. }
  127. else
  128. {
  129. RectI rect(offset, getExtent());
  130. GFX->getDrawUtil()->drawBitmapStretch(mTextureObject, rect);
  131. }
  132. }
  133. if (mProfile->mBorder || !mTextureObject)
  134. {
  135. RectI rect(offset.x, offset.y, getExtent().x, getExtent().y);
  136. ColorI borderCol(mProfile->mBorderColor);
  137. borderCol.alpha = alpha;
  138. GFX->getDrawUtil()->drawRect(rect, borderCol);
  139. }
  140. renderChildControls(offset, updateRect);
  141. }
  142. static void initPersistFields()
  143. {
  144. addField("fadeinTime", TypeS32, Offset(fadeinTime, GuiIdleCamFadeBitmapCtrl));
  145. addField("fadeoutTime", TypeS32, Offset(fadeoutTime, GuiIdleCamFadeBitmapCtrl));
  146. addField("done", TypeBool, Offset(done, GuiIdleCamFadeBitmapCtrl));
  147. Parent::initPersistFields();
  148. }
  149. };
  150. IMPLEMENT_CONOBJECT(GuiIdleCamFadeBitmapCtrl);
  151. ConsoleDocClass( GuiIdleCamFadeBitmapCtrl,
  152. "@brief GUI that will fade the current view in and out.\n\n"
  153. "Main difference between this and FadeinBitmap is this appears to "
  154. "fade based on the source texture.\n\n"
  155. "This is going to be deprecated, and any useful code ported to FadeinBitmap\n\n"
  156. "@internal");
  157. ConsoleMethod(GuiIdleCamFadeBitmapCtrl, fadeIn, void, 2, 2, "()"
  158. "@internal")
  159. {
  160. object->fadeIn();
  161. }
  162. ConsoleMethod(GuiIdleCamFadeBitmapCtrl, fadeOut, void, 2, 2, "()"
  163. "@internal")
  164. {
  165. object->fadeOut();
  166. }