2
0

guiBitmapBorderCtrl.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "gui/guiControl.h"
  23. #include "graphics/dgl.h"
  24. /// Renders a skinned border.
  25. class GuiBitmapBorderCtrl : public GuiControl
  26. {
  27. typedef GuiControl Parent;
  28. enum {
  29. BorderTopLeft,
  30. BorderTopRight,
  31. BorderTop,
  32. BorderLeft,
  33. BorderRight,
  34. BorderBottomLeft,
  35. BorderBottom,
  36. BorderBottomRight,
  37. NumBitmaps
  38. };
  39. RectI *mBitmapBounds; ///< bmp is [3*n], bmpHL is [3*n + 1], bmpNA is [3*n + 2]
  40. TextureHandle mTextureHandle;
  41. public:
  42. bool onWake();
  43. void onSleep();
  44. void onRender(Point2I offset, const RectI &updateRect);
  45. DECLARE_CONOBJECT(GuiBitmapBorderCtrl);
  46. };
  47. IMPLEMENT_CONOBJECT(GuiBitmapBorderCtrl);
  48. bool GuiBitmapBorderCtrl::onWake()
  49. {
  50. if (! Parent::onWake())
  51. return false;
  52. //get the texture for the close, minimize, and maximize buttons
  53. mTextureHandle = mProfile->mTextureHandle;
  54. bool result = mProfile->constructBitmapArray() >= NumBitmaps;
  55. AssertFatal(result, "Failed to create the bitmap array");
  56. if(!result)
  57. return false;
  58. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  59. return true;
  60. }
  61. void GuiBitmapBorderCtrl::onSleep()
  62. {
  63. mTextureHandle = NULL;
  64. Parent::onSleep();
  65. }
  66. void GuiBitmapBorderCtrl::onRender(Point2I offset, const RectI &updateRect)
  67. {
  68. renderChildControls( offset, updateRect );
  69. dglSetClipRect(updateRect);
  70. //draw the outline
  71. RectI winRect;
  72. winRect.point = offset;
  73. winRect.extent = mBounds.extent;
  74. winRect.point.x += mBitmapBounds[BorderLeft].extent.x;
  75. winRect.point.y += mBitmapBounds[BorderTop].extent.y;
  76. winRect.extent.x -= mBitmapBounds[BorderLeft].extent.x + mBitmapBounds[BorderRight].extent.x;
  77. winRect.extent.y -= mBitmapBounds[BorderTop].extent.y + mBitmapBounds[BorderBottom].extent.y;
  78. if(mProfile->mOpaque)
  79. dglDrawRectFill(winRect, mProfile->mFillColor);
  80. dglClearBitmapModulation();
  81. dglDrawBitmapSR(mTextureHandle, offset, mBitmapBounds[BorderTopLeft]);
  82. dglDrawBitmapSR(mTextureHandle, Point2I(offset.x + mBounds.extent.x - mBitmapBounds[BorderTopRight].extent.x, offset.y),
  83. mBitmapBounds[BorderTopRight]);
  84. RectI destRect;
  85. destRect.point.x = offset.x + mBitmapBounds[BorderTopLeft].extent.x;
  86. destRect.point.y = offset.y;
  87. destRect.extent.x = mBounds.extent.x - mBitmapBounds[BorderTopLeft].extent.x - mBitmapBounds[BorderTopRight].extent.x;
  88. destRect.extent.y = mBitmapBounds[BorderTop].extent.y;
  89. RectI stretchRect = mBitmapBounds[BorderTop];
  90. stretchRect.inset(1,0);
  91. dglDrawBitmapStretchSR(mTextureHandle, destRect, stretchRect);
  92. destRect.point.x = offset.x;
  93. destRect.point.y = offset.y + mBitmapBounds[BorderTopLeft].extent.y;
  94. destRect.extent.x = mBitmapBounds[BorderLeft].extent.x;
  95. destRect.extent.y = mBounds.extent.y - mBitmapBounds[BorderTopLeft].extent.y - mBitmapBounds[BorderBottomLeft].extent.y;
  96. stretchRect = mBitmapBounds[BorderLeft];
  97. stretchRect.inset(0,1);
  98. dglDrawBitmapStretchSR(mTextureHandle, destRect, stretchRect);
  99. destRect.point.x = offset.x + mBounds.extent.x - mBitmapBounds[BorderRight].extent.x;
  100. destRect.extent.x = mBitmapBounds[BorderRight].extent.x;
  101. destRect.point.y = offset.y + mBitmapBounds[BorderTopRight].extent.y;
  102. destRect.extent.y = mBounds.extent.y - mBitmapBounds[BorderTopRight].extent.y - mBitmapBounds[BorderBottomRight].extent.y;
  103. stretchRect = mBitmapBounds[BorderRight];
  104. stretchRect.inset(0,1);
  105. dglDrawBitmapStretchSR(mTextureHandle, destRect, stretchRect);
  106. dglDrawBitmapSR(mTextureHandle, offset + Point2I(0, mBounds.extent.y - mBitmapBounds[BorderBottomLeft].extent.y), mBitmapBounds[BorderBottomLeft]);
  107. dglDrawBitmapSR(mTextureHandle, offset + mBounds.extent - mBitmapBounds[BorderBottomRight].extent, mBitmapBounds[BorderBottomRight]);
  108. destRect.point.x = offset.x + mBitmapBounds[BorderBottomLeft].extent.x;
  109. destRect.extent.x = mBounds.extent.x - mBitmapBounds[BorderBottomLeft].extent.x - mBitmapBounds[BorderBottomRight].extent.x;
  110. destRect.point.y = offset.y + mBounds.extent.y - mBitmapBounds[BorderBottom].extent.y;
  111. destRect.extent.y = mBitmapBounds[BorderBottom].extent.y;
  112. stretchRect = mBitmapBounds[BorderBottom];
  113. stretchRect.inset(1,0);
  114. dglDrawBitmapStretchSR(mTextureHandle, destRect, stretchRect);
  115. }