guiFadeinBitmapCtrl.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. docsURL;
  80. addGroup( "Fading" );
  81. addField( "fadeColor", TypeColorF, Offset( mFadeColor, GuiFadeinBitmapCtrl ),
  82. "Color to fade in from and fade out to." );
  83. addField( "fadeInTime", TypeS32, Offset( mFadeInTime, GuiFadeinBitmapCtrl ),
  84. "Milliseconds for the bitmap to fade in." );
  85. addField( "waitTime", TypeS32, Offset( mWaitTime, GuiFadeinBitmapCtrl ),
  86. "Milliseconds to wait after fading in before fading out the bitmap." );
  87. addField( "fadeOutTime", TypeS32, Offset( mFadeOutTime, GuiFadeinBitmapCtrl ),
  88. "Milliseconds for the bitmap to fade out." );
  89. addField( "fadeInEase", TypeEaseF, Offset( mFadeInEase, GuiFadeinBitmapCtrl ),
  90. "Easing curve for fade-in." );
  91. addField( "fadeOutEase", TypeEaseF, Offset( mFadeOutEase, GuiFadeinBitmapCtrl ),
  92. "Easing curve for fade-out." );
  93. addField( "done", TypeBool, Offset( mDone, GuiFadeinBitmapCtrl ),
  94. "Whether the fade cycle has finished running." );
  95. endGroup( "Fading" );
  96. Parent::initPersistFields();
  97. }
  98. //-----------------------------------------------------------------------------
  99. void GuiFadeinBitmapCtrl::onPreRender()
  100. {
  101. Parent::onPreRender();
  102. setUpdate();
  103. }
  104. //-----------------------------------------------------------------------------
  105. void GuiFadeinBitmapCtrl::onMouseDown(const GuiEvent &)
  106. {
  107. click_callback();
  108. }
  109. //-----------------------------------------------------------------------------
  110. bool GuiFadeinBitmapCtrl::onKeyDown(const GuiEvent &)
  111. {
  112. click_callback();
  113. return true;
  114. }
  115. bool GuiFadeinBitmapCtrl::onGamepadButtonDown(const GuiEvent& event)
  116. {
  117. click_callback();
  118. return true;
  119. }
  120. //-----------------------------------------------------------------------------
  121. bool GuiFadeinBitmapCtrl::onWake()
  122. {
  123. if( !Parent::onWake() )
  124. return false;
  125. // Reset reference time.
  126. mStartTime = 0;
  127. return true;
  128. }
  129. //-----------------------------------------------------------------------------
  130. void GuiFadeinBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  131. {
  132. Parent::onRender(offset, updateRect);
  133. // Set reference time if we haven't already. This is done here when rendering
  134. // starts so that we begin counting from the time the control is actually
  135. // visible rather than from its onWake() (which may be a considerable time
  136. // before the control actually gets to render).
  137. if( !mStartTime )
  138. mStartTime = Platform::getRealMilliseconds();
  139. // Compute overlay alpha.
  140. U32 elapsed = Platform::getRealMilliseconds() - mStartTime;
  141. U32 alpha;
  142. if( elapsed < mFadeInTime )
  143. {
  144. // fade-in
  145. alpha = 255.f * ( 1.0f - mFadeInEase.getValue( elapsed, 0.f, 1.f, mFadeInTime ) );
  146. }
  147. else if( elapsed < ( mFadeInTime + mWaitTime ) )
  148. {
  149. // wait
  150. alpha = 0;
  151. }
  152. else if( elapsed < ( mFadeInTime + mWaitTime + mFadeOutTime ) )
  153. {
  154. // fade out
  155. elapsed -= ( mFadeInTime + mWaitTime );
  156. alpha = mFadeOutEase.getValue( elapsed, 0.f, 255.f, mFadeOutTime );
  157. }
  158. else
  159. {
  160. // done state
  161. alpha = mFadeOutTime ? 255 : 0;
  162. mDone = true;
  163. // Trigger onDone callback except when in Gui Editor.
  164. if( !smDesignTime )
  165. onDone_callback();
  166. }
  167. // Render overlay on top of bitmap.
  168. ColorI color = mFadeColor.toColorI();
  169. color.alpha = alpha;
  170. GFX->getDrawUtil()->drawRectFill( offset, getExtent() + offset, color );
  171. }