guiChunkedBitmapCtrl.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #include "guiChunkedBitmapCtrl.h"
  32. IMPLEMENT_CONOBJECT(GuiChunkedBitmapCtrl);
  33. ConsoleDocClass( GuiChunkedBitmapCtrl,
  34. "@brief This is a control that will render a specified bitmap or a bitmap specified in a referenced variable.\n\n"
  35. "This control allows you to either set a bitmap with the \"bitmap\" field or with the setBitmap method. You can also choose "
  36. "to reference a variable in the \"variable\" field such as \"$image\" and then set \"useVariable\" to true. This will cause it to "
  37. "synchronize the variable with the bitmap displayed (if the variable holds a valid image). You can then change the variable and "
  38. "effectively changed the displayed image.\n\n"
  39. "@tsexample\n"
  40. "$image = \"anotherbackground.png\";\n"
  41. "new GuiChunkedBitmapCtrl(ChunkedBitmap)\n"
  42. "{\n"
  43. " bitmap = \"background.png\";\n"
  44. " variable = \"$image\";\n"
  45. " useVariable = false;\n"
  46. "}\n\n"
  47. "// This will result in the control rendering \"background.png\"\n"
  48. "// If we now set the useVariable to true it will now render \"anotherbackground.png\"\n"
  49. "ChunkedBitmap.useVariable = true;\n"
  50. "@endtsexample\n\n"
  51. "@see GuiControl::variable\n\n"
  52. "@ingroup GuiImages\n"
  53. );
  54. void GuiChunkedBitmapCtrl::initPersistFields()
  55. {
  56. addGroup("GuiChunkedBitmapCtrl");
  57. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiChunkedBitmapCtrl, "This is the bitmap to render to the control.");
  58. addField( "useVariable", TypeBool, Offset( mUseVariable, GuiChunkedBitmapCtrl ), "This decides whether to use the \"bitmap\" file "
  59. "or a bitmap stored in \"variable\"");
  60. addField( "tile", TypeBool, Offset( mTile, GuiChunkedBitmapCtrl ), "This is no longer in use", AbstractClassRep::FIELD_HideInInspectors);
  61. endGroup("GuiChunkedBitmapCtrl");
  62. Parent::initPersistFields();
  63. }
  64. DefineEngineMethod( GuiChunkedBitmapCtrl, setBitmap, void, (const char* filename),,
  65. "@brief Set the image rendered in this control.\n\n"
  66. "@param filename The image name you want to set\n"
  67. "@tsexample\n"
  68. "ChunkedBitmap.setBitmap(\"images/background.png\");"
  69. "@endtsexample\n\n")
  70. {
  71. object->setBitmap( filename );
  72. }
  73. GuiChunkedBitmapCtrl::GuiChunkedBitmapCtrl()
  74. {
  75. INIT_ASSET(Bitmap);
  76. mUseVariable = false;
  77. mTile = false;
  78. }
  79. void GuiChunkedBitmapCtrl::setBitmap(const char *name)
  80. {
  81. bool awake = mAwake;
  82. if(awake)
  83. onSleep();
  84. _setBitmap(StringTable->insert(name));
  85. if(awake)
  86. onWake();
  87. setUpdate();
  88. }
  89. bool GuiChunkedBitmapCtrl::onWake()
  90. {
  91. if(!Parent::onWake())
  92. return false;
  93. if( !mBitmap
  94. && ( ( mBitmapName && mBitmapName[ 0 ] )
  95. || ( mUseVariable && mConsoleVariable && mConsoleVariable[ 0 ] ) ) )
  96. {
  97. if ( mUseVariable )
  98. mBitmap.set( Con::getVariable( mConsoleVariable ), &GFXDefaultGUIProfile, avar("%s() - mTexHandle (line %d)", __FUNCTION__, __LINE__) );
  99. else
  100. mBitmap.set( mBitmapName, &GFXDefaultGUIProfile, avar("%s() - mTexHandle (line %d)", __FUNCTION__, __LINE__) );
  101. }
  102. return true;
  103. }
  104. void GuiChunkedBitmapCtrl::onSleep()
  105. {
  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(LinearColorF(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( mBitmap )
  142. {
  143. RectI boundsRect( offset, getExtent());
  144. GFX->getDrawUtil()->drawBitmapStretch(mBitmap, boundsRect, GFXBitmapFlip_None, GFXTextureFilterLinear );
  145. }
  146. renderChildControls(offset, updateRect);
  147. }