guiProgressCtrl.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/guiDefaultControlRender.h"
  26. #include "gui/guiProgressCtrl.h"
  27. #include "gui/guiProgressCtrl_ScriptBinding.h"
  28. IMPLEMENT_CONOBJECT(GuiProgressCtrl);
  29. GuiProgressCtrl::GuiProgressCtrl()
  30. {
  31. mBounds.extent.set(230, 24);
  32. mCurrent = 0.0f;
  33. mStart = 0.0f;
  34. mEnd = 0.0f;
  35. mIsContainer = false;
  36. setAnimationLength(250);
  37. setEasingFunction(EaseInOut);
  38. setField("profile", "GuiProgressProfile");
  39. }
  40. void GuiProgressCtrl::initPersistFields()
  41. {
  42. Parent::initPersistFields();
  43. addField("animationTime", TypeS32, Offset(mAnimationLength, GuiProgressCtrl));
  44. }
  45. const char* GuiProgressCtrl::getScriptValue()
  46. {
  47. char * ret = Con::getReturnBuffer(64);
  48. dSprintf(ret, 64, "%g", mCurrent);
  49. return ret;
  50. }
  51. void GuiProgressCtrl::setScriptValue(const char *value)
  52. {
  53. //set the value
  54. F32 target = 0.0f;
  55. if (value)
  56. target = dAtof(value);
  57. setCurrentProgress(target);
  58. }
  59. void GuiProgressCtrl::setCurrentProgress(F32 target)
  60. {
  61. setCurrentProgress(target, mAnimationLength);
  62. }
  63. void GuiProgressCtrl::setCurrentProgress(F32 target, S32 time)
  64. {
  65. mAnimationLength = time;
  66. //validate the value
  67. target = mClampF(target, 0.f, 1.f);
  68. if (target != mCurrent)
  69. {
  70. mStart = mCurrent;
  71. mEnd = target;
  72. startFluidAnimation();
  73. setProcessTicks(true);
  74. }
  75. setUpdate();
  76. }
  77. void GuiProgressCtrl::resetProgress()
  78. {
  79. setCurrentProgress(0.0f);
  80. }
  81. void GuiProgressCtrl::onRender(Point2I offset, const RectI &updateRect)
  82. {
  83. //Render the background box
  84. RectI backRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  85. renderUniversalRect(backRect, mProfile, NormalState);
  86. if (mCurrent == 0.0f)
  87. {
  88. return;
  89. }
  90. GuiControlState currentState = SelectedState;
  91. if(mCurrent < 1.0f)
  92. {
  93. currentState = HighlightState;
  94. }
  95. //Render the progress bar
  96. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  97. ctrlRect.extent.x = ctrlRect.extent.x * mCurrent;
  98. renderUniversalRect(ctrlRect, mProfile, currentState);
  99. //Render Text
  100. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  101. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  102. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  103. if (contentRect.isValidRect())
  104. {
  105. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  106. }
  107. }
  108. void GuiProgressCtrl::processTick()
  109. {
  110. bool shouldWeContinue = false;
  111. //Expanding
  112. shouldWeContinue |= animateProgressBar();
  113. if (!shouldWeContinue)
  114. {
  115. setProcessTicks(false);
  116. }
  117. }
  118. bool GuiProgressCtrl::animateProgressBar()
  119. {
  120. F32 progress = getProgress(32.0f);
  121. setUpdate();
  122. mCurrent = processValue(progress, mStart, mEnd);
  123. if (mAnimationProgress >= 1.0f)
  124. {
  125. mAnimationProgress = 1.0f;
  126. return false;
  127. }
  128. return true;
  129. }