guiProgressCtrl.cc 4.2 KB

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