guiBitmapBarCtrl.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/controls/guiBitmapBarCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. IMPLEMENT_CONOBJECT(GuiBitmapBarCtrl);
  30. GuiBitmapBarCtrl::GuiBitmapBarCtrl(void)
  31. {
  32. mPercent = 100.0;
  33. mVertical = false;
  34. mFlipClip = false;
  35. }
  36. void GuiBitmapBarCtrl::initPersistFields()
  37. {
  38. addField("percent", TypeF32, Offset(mPercent, GuiBitmapBarCtrl),
  39. "% shown");
  40. addField("vertical", TypeBool, Offset(mVertical, GuiBitmapBarCtrl),
  41. "If true, the bitmap is clipped vertically.");
  42. addField("flipClip", TypeBool, Offset(mFlipClip, GuiBitmapBarCtrl),
  43. "If true, the bitmap is clipped in the oposite direction.");
  44. Parent::initPersistFields();
  45. removeField("wrap");
  46. }
  47. void GuiBitmapBarCtrl::onRender(Point2I offset, const RectI &updateRect)
  48. {
  49. if (mBitmap)
  50. {
  51. GFX->getDrawUtil()->clearBitmapModulation();
  52. GFX->getDrawUtil()->setBitmapModulation(mColor);
  53. F32 pct = (mPercent / 100.0);
  54. GFXTextureObject* texture = mBitmap;
  55. Point2I modifiedSRC;
  56. modifiedSRC.x = mVertical ? (F32)texture->mBitmapSize.x : (F32)(texture->mBitmapSize.x*pct);
  57. modifiedSRC.y = mVertical ? (F32)(texture->mBitmapSize.y*pct) : (F32)texture->mBitmapSize.y;
  58. RectI srcRegion;
  59. Point2I offsetSRC = Point2I::Zero;
  60. if (mFlipClip)
  61. {
  62. offsetSRC.x = texture->mBitmapSize.x - modifiedSRC.x;
  63. offsetSRC.y = texture->mBitmapSize.y - modifiedSRC.y;
  64. }
  65. srcRegion.set(offsetSRC, modifiedSRC);
  66. RectI destRegion;
  67. Point2I modifiedDest;
  68. modifiedDest.x = mVertical ? (F32)updateRect.len_x() : (F32)(updateRect.len_x()*pct);
  69. modifiedDest.y = mVertical ? (F32)(updateRect.len_y()*pct) : (F32)updateRect.len_y();
  70. Point2I offsetDest = Point2I::Zero;
  71. if (mFlipClip)
  72. {
  73. offsetDest.x = updateRect.len_x() - modifiedDest.x;
  74. offsetDest.y = updateRect.len_y() - modifiedDest.y;
  75. }
  76. offsetDest += offset;
  77. destRegion.set(offsetDest, modifiedDest);
  78. GFX->getDrawUtil()->drawBitmapStretchSR(texture, destRegion, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
  79. }
  80. if (mProfile->mBorder || !mBitmap)
  81. {
  82. RectI rect(offset, getExtent());
  83. GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
  84. }
  85. renderChildControls(offset, updateRect);
  86. }