CGUIInOutFader.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #include "CGUIInOutFader.h"
  5. #ifdef _IRR_COMPILE_WITH_GUI_
  6. #include "IGUIEnvironment.h"
  7. #include "IVideoDriver.h"
  8. #include "os.h"
  9. namespace irr
  10. {
  11. namespace gui
  12. {
  13. //! constructor
  14. CGUIInOutFader::CGUIInOutFader(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
  15. : IGUIInOutFader(environment, parent, id, rectangle)
  16. {
  17. #ifdef _DEBUG
  18. setDebugName("CGUIInOutFader");
  19. #endif
  20. Action = EFA_NOTHING;
  21. StartTime = 0;
  22. EndTime = 0;
  23. setColor(video::SColor(0,0,0,0));
  24. }
  25. //! draws the element and its children
  26. void CGUIInOutFader::draw()
  27. {
  28. if (!IsVisible || !Action)
  29. return;
  30. u32 now = os::Timer::getTime();
  31. if (now > EndTime && Action == EFA_FADE_IN)
  32. {
  33. Action = EFA_NOTHING;
  34. return;
  35. }
  36. video::IVideoDriver* driver = Environment->getVideoDriver();
  37. if (driver)
  38. {
  39. f32 d;
  40. if (now > EndTime)
  41. d = 0.0f;
  42. else
  43. d = (EndTime - now) / (f32)(EndTime - StartTime);
  44. video::SColor newCol = FullColor.getInterpolated(TransColor, d);
  45. driver->draw2DRectangle(newCol, AbsoluteRect, &AbsoluteClippingRect);
  46. }
  47. IGUIElement::draw();
  48. }
  49. //! Gets the color to fade out to or to fade in from.
  50. video::SColor CGUIInOutFader::getColor() const
  51. {
  52. return Color[1];
  53. }
  54. //! Sets the color to fade out to or to fade in from.
  55. void CGUIInOutFader::setColor(video::SColor color)
  56. {
  57. video::SColor s = color;
  58. video::SColor d = color;
  59. s.setAlpha ( 255 );
  60. d.setAlpha ( 0 );
  61. setColor ( s,d );
  62. /*
  63. Color[0] = color;
  64. FullColor = Color[0];
  65. TransColor = Color[0];
  66. if (Action == EFA_FADE_OUT)
  67. {
  68. FullColor.setAlpha(0);
  69. TransColor.setAlpha(255);
  70. }
  71. else
  72. if (Action == EFA_FADE_IN)
  73. {
  74. FullColor.setAlpha(255);
  75. TransColor.setAlpha(0);
  76. }
  77. */
  78. }
  79. void CGUIInOutFader::setColor(video::SColor source, video::SColor dest)
  80. {
  81. Color[0] = source;
  82. Color[1] = dest;
  83. if (Action == EFA_FADE_OUT)
  84. {
  85. FullColor = Color[1];
  86. TransColor = Color[0];
  87. }
  88. else
  89. if (Action == EFA_FADE_IN)
  90. {
  91. FullColor = Color[0];
  92. TransColor = Color[1];
  93. }
  94. }
  95. //! Returns if the fade in or out process is done.
  96. bool CGUIInOutFader::isReady() const
  97. {
  98. u32 now = os::Timer::getTime();
  99. bool ret = (now > EndTime);
  100. _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
  101. return ret;
  102. }
  103. //! Starts the fade in process.
  104. void CGUIInOutFader::fadeIn(u32 time)
  105. {
  106. StartTime = os::Timer::getTime();
  107. EndTime = StartTime + time;
  108. Action = EFA_FADE_IN;
  109. setColor(Color[0],Color[1]);
  110. }
  111. //! Starts the fade out process.
  112. void CGUIInOutFader::fadeOut(u32 time)
  113. {
  114. StartTime = os::Timer::getTime();
  115. EndTime = StartTime + time;
  116. Action = EFA_FADE_OUT;
  117. setColor(Color[0],Color[1]);
  118. }
  119. //! Writes attributes of the element.
  120. void CGUIInOutFader::serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options=0) const
  121. {
  122. IGUIInOutFader::serializeAttributes(out,options);
  123. out->addColor ("FullColor", FullColor);
  124. out->addColor ("TransColor", TransColor);
  125. }
  126. //! Reads attributes of the element
  127. void CGUIInOutFader::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0)
  128. {
  129. IGUIInOutFader::deserializeAttributes(in,options);
  130. FullColor = in->getAttributeAsColor("FullColor");
  131. TransColor = in->getAttributeAsColor("TransColor");
  132. }
  133. } // end namespace gui
  134. } // end namespace irr
  135. #endif // _IRR_COMPILE_WITH_GUI_