guiProgressCtrl.cc 4.2 KB

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