guiChunkedBitmapCtrl.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 "console/console.h"
  24. #include "console/consoleTypes.h"
  25. #include "gfx/bitmap/gBitmap.h"
  26. #include "gui/core/guiControl.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxTextureHandle.h"
  29. #include "gfx/gfxDrawUtil.h"
  30. #include "console/engineAPI.h"
  31. class GuiChunkedBitmapCtrl : public GuiControl
  32. {
  33. private:
  34. typedef GuiControl Parent;
  35. void renderRegion(const Point2I &offset, const Point2I &extent);
  36. protected:
  37. StringTableEntry mBitmapName;
  38. GFXTexHandle mTexHandle;
  39. bool mUseVariable;
  40. bool mTile;
  41. public:
  42. //creation methods
  43. DECLARE_CONOBJECT(GuiChunkedBitmapCtrl);
  44. DECLARE_CATEGORY( "Gui Images" );
  45. GuiChunkedBitmapCtrl();
  46. static void initPersistFields();
  47. //Parental methods
  48. bool onWake();
  49. void onSleep();
  50. void setBitmap(const char *name);
  51. void onRender(Point2I offset, const RectI &updateRect);
  52. };
  53. IMPLEMENT_CONOBJECT(GuiChunkedBitmapCtrl);
  54. ConsoleDocClass( GuiChunkedBitmapCtrl,
  55. "@brief This is a control that will render a specified bitmap or a bitmap specified in a referenced variable.\n\n"
  56. "This control allows you to either set a bitmap with the \"bitmap\" field or with the setBitmap method. You can also choose "
  57. "to reference a variable in the \"variable\" field such as \"$image\" and then set \"useVariable\" to true. This will cause it to "
  58. "synchronize the variable with the bitmap displayed (if the variable holds a valid image). You can then change the variable and "
  59. "effectively changed the displayed image.\n\n"
  60. "@tsexample\n"
  61. "$image = \"anotherbackground.png\";\n"
  62. "new GuiChunkedBitmapCtrl(ChunkedBitmap)\n"
  63. "{\n"
  64. " bitmap = \"background.png\";\n"
  65. " variable = \"$image\";\n"
  66. " useVariable = false;\n"
  67. "}\n\n"
  68. "// This will result in the control rendering \"background.png\"\n"
  69. "// If we now set the useVariable to true it will now render \"anotherbackground.png\"\n"
  70. "ChunkedBitmap.useVariable = true;\n"
  71. "@endtsexample\n\n"
  72. "@see GuiControl::variable\n\n"
  73. "@ingroup GuiImages\n"
  74. );
  75. void GuiChunkedBitmapCtrl::initPersistFields()
  76. {
  77. addGroup("GuiChunkedBitmapCtrl");
  78. addField( "bitmap", TypeFilename, Offset( mBitmapName, GuiChunkedBitmapCtrl ), "This is the bitmap to render to the control." );
  79. addField( "useVariable", TypeBool, Offset( mUseVariable, GuiChunkedBitmapCtrl ), "This decides whether to use the \"bitmap\" file "
  80. "or a bitmap stored in \"variable\"");
  81. addField( "tile", TypeBool, Offset( mTile, GuiChunkedBitmapCtrl ), "This is no longer in use");
  82. endGroup("GuiChunkedBitmapCtrl");
  83. Parent::initPersistFields();
  84. }
  85. DefineEngineMethod( GuiChunkedBitmapCtrl, setBitmap, void, (const char* filename),,
  86. "@brief Set the image rendered in this control.\n\n"
  87. "@param filename The image name you want to set\n"
  88. "@tsexample\n"
  89. "ChunkedBitmap.setBitmap(\"images/background.png\");"
  90. "@endtsexample\n\n")
  91. {
  92. object->setBitmap( filename );
  93. }
  94. GuiChunkedBitmapCtrl::GuiChunkedBitmapCtrl()
  95. {
  96. mBitmapName = StringTable->insert("");
  97. mUseVariable = false;
  98. mTile = false;
  99. }
  100. void GuiChunkedBitmapCtrl::setBitmap(const char *name)
  101. {
  102. bool awake = mAwake;
  103. if(awake)
  104. onSleep();
  105. mBitmapName = StringTable->insert(name);
  106. if(awake)
  107. onWake();
  108. setUpdate();
  109. }
  110. bool GuiChunkedBitmapCtrl::onWake()
  111. {
  112. if(!Parent::onWake())
  113. return false;
  114. if( !mTexHandle
  115. && ( ( mBitmapName && mBitmapName[ 0 ] )
  116. || ( mUseVariable && mConsoleVariable && mConsoleVariable[ 0 ] ) ) )
  117. {
  118. if ( mUseVariable )
  119. mTexHandle.set( Con::getVariable( mConsoleVariable ), &GFXDefaultGUIProfile, avar("%s() - mTexHandle (line %d)", __FUNCTION__, __LINE__) );
  120. else
  121. mTexHandle.set( mBitmapName, &GFXDefaultGUIProfile, avar("%s() - mTexHandle (line %d)", __FUNCTION__, __LINE__) );
  122. }
  123. return true;
  124. }
  125. void GuiChunkedBitmapCtrl::onSleep()
  126. {
  127. mTexHandle = NULL;
  128. Parent::onSleep();
  129. }
  130. void GuiChunkedBitmapCtrl::renderRegion(const Point2I &offset, const Point2I &extent)
  131. {
  132. /*
  133. U32 widthCount = mTexHandle.getTextureCountWidth();
  134. U32 heightCount = mTexHandle.getTextureCountHeight();
  135. if(!widthCount || !heightCount)
  136. return;
  137. F32 widthScale = F32(extent.x) / F32(mTexHandle.getWidth());
  138. F32 heightScale = F32(extent.y) / F32(mTexHandle.getHeight());
  139. GFX->setBitmapModulation(ColorF(1,1,1));
  140. for(U32 i = 0; i < widthCount; i++)
  141. {
  142. for(U32 j = 0; j < heightCount; j++)
  143. {
  144. GFXTexHandle t = mTexHandle.getSubTexture(i, j);
  145. RectI stretchRegion;
  146. stretchRegion.point.x = (S32)(i * 256 * widthScale + offset.x);
  147. stretchRegion.point.y = (S32)(j * 256 * heightScale + offset.y);
  148. if(i == widthCount - 1)
  149. stretchRegion.extent.x = extent.x + offset.x - stretchRegion.point.x;
  150. else
  151. stretchRegion.extent.x = (S32)((i * 256 + t.getWidth() ) * widthScale + offset.x - stretchRegion.point.x);
  152. if(j == heightCount - 1)
  153. stretchRegion.extent.y = extent.y + offset.y - stretchRegion.point.y;
  154. else
  155. stretchRegion.extent.y = (S32)((j * 256 + t.getHeight()) * heightScale + offset.y - stretchRegion.point.y);
  156. GFX->drawBitmapStretch(t, stretchRegion);
  157. }
  158. }
  159. */
  160. }
  161. void GuiChunkedBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  162. {
  163. if( mTexHandle )
  164. {
  165. RectI boundsRect( offset, getExtent());
  166. GFX->getDrawUtil()->drawBitmapStretch( mTexHandle, boundsRect, GFXBitmapFlip_None, GFXTextureFilterLinear );
  167. }
  168. renderChildControls(offset, updateRect);
  169. }