guiProgressCtrl.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. //validate the value
  67. target = mClampF(target, 0.f, 1.f);
  68. if (time == 0)
  69. {
  70. mCurrent = target;
  71. setProcessTicks(false);
  72. F32 old = mCurrent;
  73. if (isMethod("onDisplayChange") && old != mCurrent)
  74. Con::executef(this, 2, "onDisplayChange", Con::getFloatArg(mCurrent));
  75. }
  76. mAnimationLength = time;
  77. if (target != mCurrent)
  78. {
  79. mStart = mCurrent;
  80. mEnd = target;
  81. startFluidAnimation();
  82. setProcessTicks(true);
  83. }
  84. setUpdate();
  85. }
  86. void GuiProgressCtrl::resetProgress()
  87. {
  88. F32 old = mCurrent;
  89. mCurrent = 0.0f;
  90. setProcessTicks(false);
  91. if (isMethod("onDisplayChange") && old != mCurrent)
  92. Con::executef(this, 2, "onDisplayChange", Con::getFloatArg(mCurrent));
  93. setUpdate();
  94. }
  95. void GuiProgressCtrl::onRender(Point2I offset, const RectI &updateRect)
  96. {
  97. //Render the background box
  98. RectI backRect = applyMargins(offset, mBounds.extent, NormalState, mProfile);
  99. renderUniversalRect(backRect, mProfile, NormalState);
  100. if (mCurrent == 0.0f)
  101. {
  102. return;
  103. }
  104. GuiControlState currentState = SelectedState;
  105. if(mCurrent < 1.0f)
  106. {
  107. currentState = HighlightState;
  108. }
  109. //Render the progress bar
  110. RectI ctrlRect = applyMargins(offset, mBounds.extent, currentState, mProfile);
  111. ctrlRect.extent.x = ctrlRect.extent.x * mCurrent;
  112. renderUniversalRect(ctrlRect, mProfile, currentState);
  113. //Render Text
  114. dglSetBitmapModulation(getFontColor(mProfile, currentState));
  115. RectI fillRect = applyBorders(ctrlRect.point, ctrlRect.extent, currentState, mProfile);
  116. RectI contentRect = applyPadding(fillRect.point, fillRect.extent, currentState, mProfile);
  117. if (contentRect.isValidRect())
  118. {
  119. renderText(contentRect.point, contentRect.extent, mText, mProfile);
  120. }
  121. }
  122. void GuiProgressCtrl::processTick()
  123. {
  124. bool shouldWeContinue = false;
  125. //Expanding
  126. shouldWeContinue |= animateProgressBar();
  127. if (!shouldWeContinue)
  128. {
  129. setProcessTicks(false);
  130. if (isMethod("onProgressComplete"))
  131. Con::executef(this, 1, "onProgressComplete");
  132. }
  133. }
  134. bool GuiProgressCtrl::animateProgressBar()
  135. {
  136. F32 progress = getProgress(32.0f);
  137. setUpdate();
  138. F32 old = mCurrent;
  139. mCurrent = processValue(progress, mStart, mEnd);
  140. if (isMethod("onDisplayChange") && old != mCurrent)
  141. Con::executef(this, 2, "onDisplayChange", Con::getFloatArg(mCurrent));
  142. if (mAnimationProgress >= 1.0f)
  143. {
  144. mAnimationProgress = 1.0f;
  145. return false;
  146. }
  147. return true;
  148. }