guiIdleCamFadeBitmapCtrl.cpp 5.6 KB

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