guiBitmapBorderCtrl.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/core/guiControl.h"
  24. #include "gfx/gfxDevice.h"
  25. #include "gfx/gfxDrawUtil.h"
  26. // [rene 11/09/09] Why does this not use the bitmap array from its profile?
  27. /// Renders a skinned border.
  28. class GuiBitmapBorderCtrl : public GuiControl
  29. {
  30. typedef GuiControl Parent;
  31. enum {
  32. BorderTopLeft,
  33. BorderTopRight,
  34. BorderTop,
  35. BorderLeft,
  36. BorderRight,
  37. BorderBottomLeft,
  38. BorderBottom,
  39. BorderBottomRight,
  40. NumBitmaps
  41. };
  42. RectI *mBitmapBounds; ///< bmp is [3*n], bmpHL is [3*n + 1], bmpNA is [3*n + 2]
  43. GFXTexHandle mTextureObject;
  44. public:
  45. bool onWake();
  46. void onSleep();
  47. void onRender(Point2I offset, const RectI &updateRect);
  48. DECLARE_CONOBJECT(GuiBitmapBorderCtrl);
  49. DECLARE_CATEGORY( "Gui Images" );
  50. DECLARE_DESCRIPTION( "A control that renders a skinned border." );
  51. };
  52. IMPLEMENT_CONOBJECT(GuiBitmapBorderCtrl);
  53. ConsoleDocClass( GuiBitmapBorderCtrl,
  54. "@brief A control that renders a skinned border specified in its profile.\n\n"
  55. "This control uses the bitmap specified in it's profile (GuiControlProfile::bitmapName). It takes this image and breaks up aspects of it "
  56. "to skin the border of this control with. It is also important to set GuiControlProfile::hasBitmapArray to true on the profile as well.\n\n"
  57. "The bitmap referenced should be broken up into a 3 x 3 grid (using the top left color pixel as a border color between each of the images) "
  58. "in which it will map to the following places:\n"
  59. "1 = Top Left Corner\n"
  60. "2 = Top Right Corner\n"
  61. "3 = Top Center\n"
  62. "4 = Left Center\n"
  63. "5 = Right Center\n"
  64. "6 = Bottom Left Corner\n"
  65. "7 = Bottom Center\n"
  66. "8 = Bottom Right Corner\n"
  67. "0 = Nothing\n\n"
  68. "1 2 3\n"
  69. "4 5 0\n"
  70. "6 7 8\n\n"
  71. "@tsexample\n"
  72. "singleton GuiControlProfile (BorderGUIProfile)\n"
  73. "{\n"
  74. " bitmap = \"core/art/gui/images/borderArray\";\n"
  75. " hasBitmapArray = true;\n"
  76. " opaque = false;\n"
  77. "};\n\n"
  78. "new GuiBitmapBorderCtrl(BitmapBorderGUI)\n"
  79. "{\n"
  80. " profile = \"BorderGUIProfile\";\n"
  81. " position = \"0 0\";\n"
  82. " extent = \"400 40\";\n"
  83. " visible = \"1\";\n"
  84. "};"
  85. "@endtsexample\n\n"
  86. "@see GuiControlProfile::bitmapName\n"
  87. "@see GuiControlProfile::hasBitmapArray\n\n"
  88. "@ingroup GuiImages"
  89. );
  90. bool GuiBitmapBorderCtrl::onWake()
  91. {
  92. if (! Parent::onWake())
  93. return false;
  94. //get the texture for the close, minimize, and maximize buttons
  95. mBitmapBounds = NULL;
  96. mTextureObject = mProfile->getBitmapResource();
  97. if( mProfile->constructBitmapArray() >= NumBitmaps )
  98. mBitmapBounds = mProfile->mBitmapArrayRects.address();
  99. else
  100. Con::errorf( "GuiBitmapBorderCtrl: Could not construct bitmap array for profile '%s'", mProfile->getName() );
  101. return true;
  102. }
  103. void GuiBitmapBorderCtrl::onSleep()
  104. {
  105. mTextureObject = NULL;
  106. mBitmapBounds = NULL;
  107. Parent::onSleep();
  108. }
  109. void GuiBitmapBorderCtrl::onRender(Point2I offset, const RectI &updateRect)
  110. {
  111. renderChildControls( offset, updateRect );
  112. if( mBitmapBounds )
  113. {
  114. GFX->setClipRect(updateRect);
  115. GFXDrawUtil* drawUtil = GFX->getDrawUtil();
  116. //draw the outline
  117. RectI winRect;
  118. winRect.point = offset;
  119. winRect.extent = getExtent();
  120. winRect.point.x += mBitmapBounds[BorderLeft].extent.x;
  121. winRect.point.y += mBitmapBounds[BorderTop].extent.y;
  122. winRect.extent.x -= mBitmapBounds[BorderLeft].extent.x + mBitmapBounds[BorderRight].extent.x;
  123. winRect.extent.y -= mBitmapBounds[BorderTop].extent.y + mBitmapBounds[BorderBottom].extent.y;
  124. if(mProfile->mOpaque)
  125. drawUtil->drawRectFill(winRect, mProfile->mFillColor);
  126. drawUtil->clearBitmapModulation();
  127. drawUtil->drawBitmapSR(mTextureObject, offset, mBitmapBounds[BorderTopLeft]);
  128. drawUtil->drawBitmapSR(mTextureObject, Point2I(offset.x + getWidth() - mBitmapBounds[BorderTopRight].extent.x, offset.y),
  129. mBitmapBounds[BorderTopRight]);
  130. RectI destRect;
  131. destRect.point.x = offset.x + mBitmapBounds[BorderTopLeft].extent.x;
  132. destRect.point.y = offset.y;
  133. destRect.extent.x = getWidth() - mBitmapBounds[BorderTopLeft].extent.x - mBitmapBounds[BorderTopRight].extent.x;
  134. destRect.extent.y = mBitmapBounds[BorderTop].extent.y;
  135. RectI stretchRect = mBitmapBounds[BorderTop];
  136. stretchRect.inset(1,0);
  137. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  138. destRect.point.x = offset.x;
  139. destRect.point.y = offset.y + mBitmapBounds[BorderTopLeft].extent.y;
  140. destRect.extent.x = mBitmapBounds[BorderLeft].extent.x;
  141. destRect.extent.y = getHeight() - mBitmapBounds[BorderTopLeft].extent.y - mBitmapBounds[BorderBottomLeft].extent.y;
  142. stretchRect = mBitmapBounds[BorderLeft];
  143. stretchRect.inset(0,1);
  144. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  145. destRect.point.x = offset.x + getWidth() - mBitmapBounds[BorderRight].extent.x;
  146. destRect.extent.x = mBitmapBounds[BorderRight].extent.x;
  147. destRect.point.y = offset.y + mBitmapBounds[BorderTopRight].extent.y;
  148. destRect.extent.y = getHeight() - mBitmapBounds[BorderTopRight].extent.y - mBitmapBounds[BorderBottomRight].extent.y;
  149. stretchRect = mBitmapBounds[BorderRight];
  150. stretchRect.inset(0,1);
  151. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  152. drawUtil->drawBitmapSR(mTextureObject, offset + Point2I(0, getHeight() - mBitmapBounds[BorderBottomLeft].extent.y), mBitmapBounds[BorderBottomLeft]);
  153. drawUtil->drawBitmapSR(mTextureObject, offset + getExtent() - mBitmapBounds[BorderBottomRight].extent, mBitmapBounds[BorderBottomRight]);
  154. destRect.point.x = offset.x + mBitmapBounds[BorderBottomLeft].extent.x;
  155. destRect.extent.x = getWidth() - mBitmapBounds[BorderBottomLeft].extent.x - mBitmapBounds[BorderBottomRight].extent.x;
  156. destRect.point.y = offset.y + getHeight() - mBitmapBounds[BorderBottom].extent.y;
  157. destRect.extent.y = mBitmapBounds[BorderBottom].extent.y;
  158. stretchRect = mBitmapBounds[BorderBottom];
  159. stretchRect.inset(1,0);
  160. drawUtil->drawBitmapStretchSR(mTextureObject, destRect, stretchRect);
  161. }
  162. }