guiBitmapCtrl.cpp 9.5 KB

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