guiBitmapBarCtrl.cpp 3.7 KB

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