guiBitmapCtrl.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/controls/guiBitmapCtrl.h"
  24. #include "console/console.h"
  25. #include "console/consoleTypes.h"
  26. #include "console/engineAPI.h"
  27. #include "gfx/gfxDevice.h"
  28. #include "gfx/gfxDrawUtil.h"
  29. #include "materials/matTextureTarget.h"
  30. IMPLEMENT_CONOBJECT(GuiBitmapCtrl);
  31. ConsoleDocClass( GuiBitmapCtrl,
  32. "@brief A gui control that is used to display an image.\n\n"
  33. "The image is stretched to the constraints of the control by default. However, the control can also\n"
  34. "tile the image as well.\n\n"
  35. "The image itself is stored inside the GuiBitmapCtrl::bitmap field. The boolean value that decides\n"
  36. "whether the image is stretched or tiled is stored inside the GuiBitmapCtrl::wrap field.\n"
  37. "@tsexample\n"
  38. "// Create a tiling GuiBitmapCtrl that displays \"myImage.png\"\n"
  39. "%bitmapCtrl = new GuiBitmapCtrl()\n"
  40. "{\n"
  41. " bitmap = \"myImage.png\";\n"
  42. " wrap = \"true\";\n"
  43. "};\n"
  44. "@endtsexample\n\n"
  45. "@ingroup GuiControls"
  46. );
  47. GuiBitmapCtrl::GuiBitmapCtrl(void)
  48. : mStartPoint( 0, 0 ),
  49. mColor(ColorI::WHITE),
  50. mAngle(0),
  51. mWrap( false )
  52. {
  53. INIT_ASSET(Bitmap);
  54. }
  55. bool GuiBitmapCtrl::setBitmapName( void *object, const char *index, const char *data )
  56. {
  57. // Prior to this, you couldn't do bitmap.bitmap = "foo.jpg" and have it work.
  58. // With protected console types you can now call the setBitmap function and
  59. // make it load the image.
  60. static_cast<GuiBitmapCtrl *>( object )->setBitmap( data );
  61. // Return false because the setBitmap method will assign 'mBitmapName' to the
  62. // argument we are specifying in the call.
  63. return false;
  64. }
  65. void GuiBitmapCtrl::initPersistFields()
  66. {
  67. addGroup( "Bitmap" );
  68. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiBitmapCtrl, The bitmap file to display in the control);
  69. addField("color", TypeColorI, Offset(mColor, GuiBitmapCtrl),"color mul");
  70. addField( "wrap", TypeBool, Offset( mWrap, GuiBitmapCtrl ),
  71. "If true, the bitmap is tiled inside the control rather than stretched to fit." );
  72. addField("angle", TypeF32, Offset(mAngle, GuiBitmapCtrl), "rotation");
  73. endGroup( "Bitmap" );
  74. Parent::initPersistFields();
  75. }
  76. bool GuiBitmapCtrl::onWake()
  77. {
  78. if (! Parent::onWake())
  79. return false;
  80. setActive(true);
  81. if (mBitmapName != StringTable->insert("texhandle"))
  82. setBitmap(getBitmap());
  83. return true;
  84. }
  85. void GuiBitmapCtrl::onSleep()
  86. {
  87. if ( mBitmapName != StringTable->insert("texhandle") )
  88. mBitmap = NULL;
  89. Parent::onSleep();
  90. }
  91. //-------------------------------------
  92. void GuiBitmapCtrl::inspectPostApply()
  93. {
  94. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  95. // set it's extent to be exactly the size of the bitmap (if present)
  96. Parent::inspectPostApply();
  97. if (!mWrap && (getExtent().x == 0) && (getExtent().y == 0) && mBitmap)
  98. {
  99. setExtent( mBitmap->getWidth(), mBitmap->getHeight());
  100. }
  101. }
  102. void GuiBitmapCtrl::setBitmap( const char *name, bool resize )
  103. {
  104. if ( !isAwake() )
  105. return;
  106. _setBitmap(StringTable->insert(name));
  107. if (mBitmap && resize)
  108. {
  109. setExtent(mBitmap->getWidth(), mBitmap->getHeight());
  110. updateSizing();
  111. }
  112. setUpdate();
  113. }
  114. void GuiBitmapCtrl::updateSizing()
  115. {
  116. if(!getParent())
  117. return;
  118. // updates our bounds according to our horizSizing and verSizing rules
  119. RectI fakeBounds( getPosition(), getParent()->getExtent());
  120. parentResized( fakeBounds, fakeBounds);
  121. }
  122. void GuiBitmapCtrl::setBitmapHandle(GFXTexHandle handle, bool resize)
  123. {
  124. mBitmap = handle;
  125. mBitmapName = StringTable->insert("texhandle");
  126. // Resize the control to fit the bitmap
  127. if (resize)
  128. {
  129. setExtent(mBitmap->getWidth(), mBitmap->getHeight());
  130. updateSizing();
  131. }
  132. }
  133. void GuiBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
  134. {
  135. if (mBitmap)
  136. {
  137. GFX->getDrawUtil()->clearBitmapModulation();
  138. GFX->getDrawUtil()->setBitmapModulation(mColor);
  139. if(mWrap)
  140. {
  141. // We manually draw each repeat because non power of two textures will
  142. // not tile correctly when rendered with GFX->drawBitmapTile(). The non POT
  143. // bitmap will be padded by the hardware, and we'll see lots of slack
  144. // in the texture. So... lets do what we must: draw each repeat by itself:
  145. GFXTextureObject* texture = mBitmap;
  146. RectI srcRegion;
  147. RectI dstRegion;
  148. F32 xdone = ((F32)getExtent().x/(F32)texture->mBitmapSize.x)+1;
  149. F32 ydone = ((F32)getExtent().y/(F32)texture->mBitmapSize.y)+1;
  150. S32 xshift = mStartPoint.x%texture->mBitmapSize.x;
  151. S32 yshift = mStartPoint.y%texture->mBitmapSize.y;
  152. for(S32 y = 0; y < ydone; ++y)
  153. for(S32 x = 0; x < xdone; ++x)
  154. {
  155. srcRegion.set(0,0,texture->mBitmapSize.x,texture->mBitmapSize.y);
  156. dstRegion.set( ((texture->mBitmapSize.x*x)+offset.x)-xshift,
  157. ((texture->mBitmapSize.y*y)+offset.y)-yshift,
  158. texture->mBitmapSize.x,
  159. texture->mBitmapSize.y);
  160. GFX->getDrawUtil()->drawBitmapStretchSR(texture, dstRegion, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear, mAngle);
  161. }
  162. }
  163. else
  164. {
  165. RectI rect(offset, getExtent());
  166. GFX->getDrawUtil()->drawBitmapStretch(mBitmap, rect, GFXBitmapFlip_None, GFXTextureFilterLinear, false, mAngle);
  167. }
  168. }
  169. if (mProfile->mBorder || !mBitmap)
  170. {
  171. RectI rect(offset.x, offset.y, getExtent().x, getExtent().y);
  172. GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
  173. }
  174. renderChildControls(offset, updateRect);
  175. }
  176. void GuiBitmapCtrl::setValue(S32 x, S32 y)
  177. {
  178. if (mBitmap)
  179. {
  180. x += mBitmap->getWidth() / 2;
  181. y += mBitmap->getHeight() / 2;
  182. }
  183. while (x < 0)
  184. x += 256;
  185. mStartPoint.x = x % 256;
  186. while (y < 0)
  187. y += 256;
  188. mStartPoint.y = y % 256;
  189. }
  190. DefineEngineMethod( GuiBitmapCtrl, setValue, void, ( S32 x, S32 y ),,
  191. "Set the offset of the bitmap within the control.\n"
  192. "@param x The x-axis offset of the image.\n"
  193. "@param y The y-axis offset of the image.\n")
  194. {
  195. object->setValue(x, y);
  196. }
  197. //
  198. static ConsoleDocFragment _sGuiBitmapCtrlSetBitmap1(
  199. "@brief Assign an image to the control.\n\n"
  200. "Child controls with resize according to their layout settings.\n"
  201. "@param filename The filename of the image.\n"
  202. "@param resize Optional parameter. If true, the GUI will resize to fit the image.",
  203. "GuiBitmapCtrl", // The class to place the method in; use NULL for functions.
  204. "void setBitmap( String filename, bool resize );" ); // The definition string.
  205. static ConsoleDocFragment _sGuiBitmapCtrlSetBitmap2(
  206. "@brief Assign an image to the control.\n\n"
  207. "Child controls will resize according to their layout settings.\n"
  208. "@param filename The filename of the image.\n"
  209. "@param resize A boolean value that decides whether the ctrl refreshes or not.",
  210. "GuiBitmapCtrl", // The class to place the method in; use NULL for functions.
  211. "void setBitmap( String filename );" ); // The definition string.
  212. //"Set the bitmap displayed in the control. Note that it is limited in size, to 256x256."
  213. DefineEngineMethod( GuiBitmapCtrl, setBitmap, void, ( const char * fileRoot, bool resize), ( false),
  214. "( String filename | String filename, bool resize ) Assign an image to the control.\n\n"
  215. "@hide" )
  216. {
  217. char filename[1024];
  218. Con::expandScriptFilename(filename, sizeof(filename), fileRoot);
  219. object->setBitmap(filename, resize );
  220. }
  221. DefineEngineMethod(GuiBitmapCtrl, getBitmap, const char*, (),,
  222. "Gets the current bitmap set for this control.\n\n"
  223. "@hide")
  224. {
  225. return object->getBitmap();
  226. }
  227. DefineEngineMethod( GuiBitmapCtrl, setNamedTexture, bool, (String namedtexture),,
  228. "@brief Set a texture as the image.\n\n"
  229. "@param namedtexture The name of the texture (NamedTexTarget).\n"
  230. "@return true if the texture exists." )
  231. {
  232. GFXTexHandle theTex;
  233. NamedTexTarget *namedTarget = NULL;
  234. namedTarget = NamedTexTarget::find(namedtexture.c_str());
  235. if ( namedTarget )
  236. {
  237. theTex = namedTarget->getTexture( 0 );
  238. }
  239. if ( theTex.isValid() )
  240. {
  241. object->setBitmapHandle( theTex , false );
  242. return true; //a new texture was set correctly
  243. }
  244. return false; //we couldn't change the texture
  245. }