guiProgressCtrl.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. char * ret = Con::getReturnBuffer(64);
  54. dSprintf(ret, 64, "%g", mProgress);
  55. return ret;
  56. }
  57. void GuiProgressCtrl::setScriptValue(const char *value)
  58. {
  59. //set the value
  60. if (! value)
  61. mProgress = 0.0f;
  62. else
  63. mProgress = dAtof(value);
  64. //validate the value
  65. mProgress = mClampF(mProgress, 0.f, 1.f);
  66. setUpdate();
  67. }
  68. void GuiProgressCtrl::onPreRender()
  69. {
  70. const char * var = getVariable();
  71. if(var)
  72. {
  73. F32 value = mClampF(dAtof(var), 0.f, 1.f);
  74. if(value != mProgress)
  75. {
  76. mProgress = value;
  77. setUpdate();
  78. }
  79. }
  80. }
  81. void GuiProgressCtrl::onRender(Point2I offset, const RectI &updateRect)
  82. {
  83. RectI ctrlRect(offset, getExtent());
  84. //draw the progress
  85. S32 width = (S32)((F32)(getWidth()) * mProgress);
  86. if (width > 0)
  87. {
  88. RectI progressRect = ctrlRect;
  89. progressRect.extent.x = width;
  90. GFX->getDrawUtil()->drawRectFill(progressRect, mProfile->mFillColor);
  91. }
  92. //now draw the border
  93. if (mProfile->mBorder)
  94. GFX->getDrawUtil()->drawRect(ctrlRect, mProfile->mBorderColor);
  95. Parent::onRender( offset, updateRect );
  96. //render the children
  97. renderChildControls(offset, updateRect);
  98. }