guiChunkedBitmapCtrl.cpp 6.2 KB

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