guiProgressCtrl.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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 "platform/platform.h"
  23. #include "gui/game/guiProgressCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "gfx/gfxDrawUtil.h"
  27. #include "console/engineAPI.h"
  28. IMPLEMENT_CONOBJECT(GuiProgressCtrl);
  29. ConsoleDocClass( GuiProgressCtrl,
  30. "@brief GUI Control which displays a horizontal bar which increases as the progress value of 0.0 - 1.0 increases.\n\n"
  31. "@tsexample\n"
  32. " new GuiProgressCtrl(JS_statusBar)\n"
  33. " {\n"
  34. " //Properties not specific to this control have been omitted from this example.\n"
  35. " };\n\n"
  36. "// Define the value to set the progress bar"
  37. "%value = \"0.5f\"\n\n"
  38. "// Set the value of the progress bar, from 0.0 - 1.0\n"
  39. "%thisGuiProgressCtrl.setValue(%value);\n"
  40. "// Get the value of the progress bar.\n"
  41. "%progress = %thisGuiProgressCtrl.getValue();\n"
  42. "@endtsexample\n\n"
  43. "@see GuiTextCtrl\n"
  44. "@see GuiControl\n\n"
  45. "@ingroup GuiValues\n"
  46. );
  47. GuiProgressCtrl::GuiProgressCtrl()
  48. {
  49. mProgress = 0.0f;
  50. }
  51. const char* GuiProgressCtrl::getScriptValue()
  52. {
  53. static const U32 bufSize = 64;
  54. char * ret = Con::getReturnBuffer(bufSize);
  55. dSprintf(ret, bufSize, "%g", mProgress);
  56. return ret;
  57. }
  58. void GuiProgressCtrl::setScriptValue(const char *value)
  59. {
  60. //set the value
  61. if (! value)
  62. mProgress = 0.0f;
  63. else
  64. mProgress = dAtof(value);
  65. //validate the value
  66. mProgress = mClampF(mProgress, 0.f, 1.f);
  67. setUpdate();
  68. }
  69. void GuiProgressCtrl::onPreRender()
  70. {
  71. const char * var = getVariable();
  72. if(var)
  73. {
  74. F32 value = mClampF(dAtof(var), 0.f, 1.f);
  75. if(value != mProgress)
  76. {
  77. mProgress = value;
  78. setUpdate();
  79. }
  80. }
  81. }
  82. void GuiProgressCtrl::onRender(Point2I offset, const RectI &updateRect)
  83. {
  84. RectI ctrlRect(offset, getExtent());
  85. //draw the progress
  86. S32 width = (S32)((F32)(getWidth()) * mProgress);
  87. if (width > 0)
  88. {
  89. RectI progressRect = ctrlRect;
  90. progressRect.extent.x = width;
  91. GFX->getDrawUtil()->drawRectFill(progressRect, mProfile->mFillColor);
  92. }
  93. //now draw the border
  94. if (mProfile->mBorder)
  95. GFX->getDrawUtil()->drawRect(ctrlRect, mProfile->mBorderColor);
  96. Parent::onRender( offset, updateRect );
  97. //render the children
  98. renderChildControls(offset, updateRect);
  99. }