guiIdleCamFadeBitmapCtrl.cpp 5.7 KB

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