guiFadeinBitmapCtrl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/game/guiFadeinBitmapCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "math/mathTypes.h"
  28. #include "console/engineAPI.h"
  29. IMPLEMENT_CONOBJECT( GuiFadeinBitmapCtrl );
  30. ConsoleDocClass( GuiFadeinBitmapCtrl,
  31. "@brief A GUI control which renders a black square over a bitmap image. The black square will fade out, then fade back in after a determined time.\n"
  32. "This control is especially useful for transitions and splash screens.\n\n"
  33. "@tsexample\n"
  34. "new GuiFadeinBitmapCtrl()\n"
  35. " {\n"
  36. " fadeinTime = \"1000\";\n"
  37. " waitTime = \"2000\";\n"
  38. " fadeoutTime = \"1000\";\n"
  39. " done = \"1\";\n"
  40. " // Additional GUI properties that are not specific to GuiFadeinBitmapCtrl have been omitted from this example.\n"
  41. " };\n"
  42. "@endtsexample\n\n"
  43. "@see GuiBitmapCtrl\n\n"
  44. "@ingroup GuiCore\n"
  45. );
  46. IMPLEMENT_CALLBACK( GuiFadeinBitmapCtrl, click, void, (),(),
  47. "@brief Informs the script level that this object received a Click event from the cursor or keyboard.\n\n"
  48. "@tsexample\n"
  49. "GuiFadeInBitmapCtrl::click(%this)\n"
  50. " {\n"
  51. " // Code to run when click occurs\n"
  52. " }\n"
  53. "@endtsexample\n\n"
  54. "@see GuiCore\n\n"
  55. );
  56. IMPLEMENT_CALLBACK( GuiFadeinBitmapCtrl, onDone, void, (),(),
  57. "@brief Informs the script level that this object has completed is fade cycle.\n\n"
  58. "@tsexample\n"
  59. "GuiFadeInBitmapCtrl::onDone(%this)\n"
  60. " {\n"
  61. " // Code to run when the fade cycle completes\n"
  62. " }\n"
  63. "@endtsexample\n\n"
  64. "@see GuiCore\n\n"
  65. );
  66. //-----------------------------------------------------------------------------
  67. GuiFadeinBitmapCtrl::GuiFadeinBitmapCtrl()
  68. : mFadeColor( 0.f, 0.f, 0.f ),
  69. mStartTime( 0 ),
  70. mFadeInTime( 1000 ),
  71. mWaitTime( 2000 ),
  72. mFadeOutTime( 1000 ),
  73. mDone( false )
  74. {
  75. }
  76. //-----------------------------------------------------------------------------
  77. void GuiFadeinBitmapCtrl::initPersistFields()
  78. {
  79. addGroup( "Fading" );
  80. addField( "fadeColor", TypeColorF, Offset( mFadeColor, GuiFadeinBitmapCtrl ),
  81. "Color to fade in from and fade out to." );
  82. addField( "fadeInTime", TypeS32, Offset( mFadeInTime, GuiFadeinBitmapCtrl ),
  83. "Milliseconds for the bitmap to fade in." );
  84. addField( "waitTime", TypeS32, Offset( mWaitTime, GuiFadeinBitmapCtrl ),
  85. "Milliseconds to wait after fading in before fading out the bitmap." );
  86. addField( "fadeOutTime", TypeS32, Offset( mFadeOutTime, GuiFadeinBitmapCtrl ),
  87. "Milliseconds for the bitmap to fade out." );
  88. addField( "fadeInEase", TypeEaseF, Offset( mFadeInEase, GuiFadeinBitmapCtrl ),
  89. "Easing curve for fade-in." );
  90. addField( "fadeOutEase", TypeEaseF, Offset( mFadeOutEase, GuiFadeinBitmapCtrl ),
  91. "Easing curve for fade-out." );
  92. addField( "done", TypeBool, Offset( mDone, GuiFadeinBitmapCtrl ),
  93. "Whether the fade cycle has finished running." );
  94. endGroup( "Fading" );
  95. Parent::initPersistFields();
  96. }
  97. //-----------------------------------------------------------------------------
  98. void GuiFadeinBitmapCtrl::onPreRender()
  99. {
  100. Parent::onPreRender();
  101. setUpdate();
  102. }
  103. //-----------------------------------------------------------------------------
  104. void GuiFadeinBitmapCtrl::onMouseDown(const GuiEvent &)
  105. {
  106. click_callback();
  107. }
  108. //-----------------------------------------------------------------------------
  109. bool GuiFadeinBitmapCtrl::onKeyDown(const GuiEvent &)
  110. {
  111. click_callback();
  112. return true;
  113. }
  114. bool GuiFadeinBitmapCtrl::onGamepadButtonDown(const GuiEvent& event)
  115. {
  116. click_callback();
  117. return true;
  118. }
  119. //-----------------------------------------------------------------------------
  120. bool GuiFadeinBitmapCtrl::onWake()
  121. {
  122. if( !Parent::onWake() )
  123. return false;
  124. // Reset reference time.
  125. mStartTime = 0;
  126. return true;
  127. }
  128. //-----------------------------------------------------------------------------
  129. void GuiFadeinBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  130. {
  131. Parent::onRender(offset, updateRect);
  132. // Set reference time if we haven't already. This is done here when rendering
  133. // starts so that we begin counting from the time the control is actually
  134. // visible rather than from its onWake() (which may be a considerable time
  135. // before the control actually gets to render).
  136. if( !mStartTime )
  137. mStartTime = Platform::getRealMilliseconds();
  138. // Compute overlay alpha.
  139. U32 elapsed = Platform::getRealMilliseconds() - mStartTime;
  140. U32 alpha;
  141. if( elapsed < mFadeInTime )
  142. {
  143. // fade-in
  144. alpha = 255.f * ( 1.0f - mFadeInEase.getValue( elapsed, 0.f, 1.f, mFadeInTime ) );
  145. }
  146. else if( elapsed < ( mFadeInTime + mWaitTime ) )
  147. {
  148. // wait
  149. alpha = 0;
  150. }
  151. else if( elapsed < ( mFadeInTime + mWaitTime + mFadeOutTime ) )
  152. {
  153. // fade out
  154. elapsed -= ( mFadeInTime + mWaitTime );
  155. alpha = mFadeOutEase.getValue( elapsed, 0.f, 255.f, mFadeOutTime );
  156. }
  157. else
  158. {
  159. // done state
  160. alpha = mFadeOutTime ? 255 : 0;
  161. mDone = true;
  162. // Trigger onDone callback except when in Gui Editor.
  163. if( !smDesignTime )
  164. onDone_callback();
  165. }
  166. // Render overlay on top of bitmap.
  167. ColorI color = mFadeColor.toColorI();
  168. color.alpha = alpha;
  169. GFX->getDrawUtil()->drawRectFill( offset, getExtent() + offset, color );
  170. }