2
0

guiFadeinBitmapCtrl.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/console.h"
  23. #include "console/consoleTypes.h"
  24. #include "graphics/dgl.h"
  25. #include "gui/guiBitmapCtrl.h"
  26. class GuiFadeinBitmapCtrl : public GuiBitmapCtrl
  27. {
  28. typedef GuiBitmapCtrl Parent;
  29. public:
  30. U32 wakeTime;
  31. bool done;
  32. U32 fadeinTime;
  33. U32 waitTime;
  34. U32 fadeoutTime;
  35. GuiFadeinBitmapCtrl()
  36. {
  37. wakeTime = 0;
  38. fadeinTime = 1000;
  39. waitTime = 2000;
  40. fadeoutTime = 1000;
  41. done = false;
  42. }
  43. void onPreRender()
  44. {
  45. Parent::onPreRender();
  46. setUpdate();
  47. }
  48. void onMouseDown(const GuiEvent &)
  49. {
  50. Con::executef(this, 1, "click");
  51. }
  52. bool onKeyDown(const GuiEvent &)
  53. {
  54. Con::executef(this, 1, "click");
  55. return true;
  56. }
  57. DECLARE_CONOBJECT(GuiFadeinBitmapCtrl);
  58. bool onWake()
  59. {
  60. if(!Parent::onWake())
  61. return false;
  62. wakeTime = Platform::getRealMilliseconds();
  63. done = false;
  64. return true;
  65. }
  66. void onRender(Point2I offset, const RectI &updateRect)
  67. {
  68. Parent::onRender(offset, updateRect);
  69. U32 elapsed = Platform::getRealMilliseconds() - wakeTime;
  70. U32 alpha;
  71. if (elapsed < fadeinTime)
  72. {
  73. // fade-in
  74. alpha = (U32)(255 - (255 * (F32(elapsed) / F32(fadeinTime))));
  75. }
  76. else if (elapsed < (fadeinTime+waitTime))
  77. {
  78. // wait
  79. alpha = 0;
  80. }
  81. else if (elapsed < (fadeinTime+waitTime+fadeoutTime))
  82. {
  83. // fade out
  84. elapsed -= (fadeinTime+waitTime);
  85. alpha = (U32)(255 * F32(elapsed) / F32(fadeoutTime));
  86. }
  87. else
  88. {
  89. // done state
  90. alpha = fadeoutTime ? 255 : 0;
  91. done = true;
  92. }
  93. ColorI color(0,0,0,alpha);
  94. dglDrawRectFill(offset, mBounds.extent + offset, color);
  95. }
  96. static void initPersistFields()
  97. {
  98. Parent::initPersistFields();
  99. addField("fadeinTime", TypeS32, Offset(fadeinTime, GuiFadeinBitmapCtrl));
  100. addField("waitTime", TypeS32, Offset(waitTime, GuiFadeinBitmapCtrl));
  101. addField("fadeoutTime", TypeS32, Offset(fadeoutTime, GuiFadeinBitmapCtrl));
  102. addField("done", TypeBool, Offset(done, GuiFadeinBitmapCtrl));
  103. }
  104. };
  105. IMPLEMENT_CONOBJECT(GuiFadeinBitmapCtrl);