VFadeControl.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //-----------------------------------------------------------------------------
  2. // Verve
  3. // Copyright (C) 2014 - Violent Tulip
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //-----------------------------------------------------------------------------
  23. #include "Verve/GUI/VFadeControl.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. #include "math/mMathFn.h"
  27. //-----------------------------------------------------------------------------
  28. IMPLEMENT_CONOBJECT( VFadeControl );
  29. //-----------------------------------------------------------------------------
  30. VFadeControl::VFadeControl( void ) :
  31. mActive( false ),
  32. mFadeType( k_TypeInvalid ),
  33. mElapsedTime( 0 ),
  34. mDuration( 1000 ),
  35. mLastTime( 0 )
  36. {
  37. // Void.
  38. }
  39. //-----------------------------------------------------------------------------
  40. //
  41. // Render Methods.
  42. //
  43. //-----------------------------------------------------------------------------
  44. void VFadeControl::onRender( Point2I pOffset, const RectI &pUpdateRect )
  45. {
  46. Parent::onRender( pOffset, pUpdateRect );
  47. if ( mFadeType == k_TypeInvalid )
  48. {
  49. // Invalid Fade State.
  50. return;
  51. }
  52. // Fetch Time.
  53. const U32 time = Platform::getRealMilliseconds();
  54. // Fetch Delta.
  55. const U32 delta = ( time - mLastTime );
  56. // Store Time.
  57. mLastTime = time;
  58. if ( mActive )
  59. {
  60. // Update Elapsed Time.
  61. mElapsedTime += delta;
  62. }
  63. F32 alpha = 1.f - mClampF( F32( mElapsedTime ) / F32( mDuration ), 0.f, 1.f );
  64. if ( mFadeType == k_TypeOut )
  65. {
  66. // Flip.
  67. alpha = 1.f - alpha;
  68. }
  69. if ( alpha > 0.f )
  70. {
  71. // Render.
  72. GFX->getDrawUtil()->drawRectFill( pOffset, pOffset + getExtent(), ColorI( 0, 0, 0, alpha * 255 ) );
  73. }
  74. if ( mElapsedTime >= mDuration )
  75. {
  76. // Stop.
  77. mActive = false;
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. //
  82. // Control Methods.
  83. //
  84. //-----------------------------------------------------------------------------
  85. void VFadeControl::start( eFadeType pType, S32 pDuration )
  86. {
  87. mActive = true;
  88. mFadeType = pType;
  89. mElapsedTime = 0;
  90. mDuration = pDuration;
  91. mLastTime = Platform::getRealMilliseconds();
  92. }
  93. void VFadeControl::pause( void )
  94. {
  95. mActive = false;
  96. }