Torque3D Documentation / _generateds / guiBitmapCtrl.cpp

guiBitmapCtrl.cpp

Engine/source/gui/controls/guiBitmapCtrl.cpp

More...

Public Variables

_sGuiBitmapCtrlSetBitmap2 ("@brief Assign an image to the control.\n\n" "Child controls will resize according to their layout settings.\n" "@param filename The filename of the image.\n" "@param resize A boolean value that decides whether the ctrl refreshes or not.", "GuiBitmapCtrl", "void setBitmap( String filename );")

Public Functions

_sGuiBitmapCtrlSetBitmap1("@brief Assign an image <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "Child controls with resize according <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> their layout <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">settings.\n</a>" "@param filename The filename of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" "@param resize Optional parameter. If true, the GUI will resize <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> fit the image." , "GuiBitmapCtrl" , "void setBitmap( <a href="/coding/class/classstring/">String</a> filename, bool resize );" )
ConsoleDocClass(GuiBitmapCtrl , "@brief A gui <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> that is used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display an <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "The image is stretched <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the constraints of the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> by default. However)
DefineEngineMethod(GuiBitmapCtrl , setBitmap , void , (const char *fileRoot, bool resize) , (false) , "( String filename | <a href="/coding/class/classstring/">String</a> filename, bool resize ) Assign an image <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "@hide" )
DefineEngineMethod(GuiBitmapCtrl , setNamedTexture , bool , (String namedtexture) , "@brief Set <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> texture as the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "@param namedtexture The name of the texture (<a href="/coding/class/classnamedtextarget/">NamedTexTarget</a>).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@return true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the texture exists." )
DefineEngineMethod(GuiBitmapCtrl , setValue , void , (S32 x, S32 y) , "Set the offset of the bitmap within the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n</a>" "@param x The x-axis offset of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" "@param y The y-axis offset of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" )

Detailed Description

Public Variables

ConsoleDocFragment _sGuiBitmapCtrlSetBitmap2 ("@brief Assign an image to the control.\n\n" "Child controls will resize according to their layout settings.\n" "@param filename The filename of the image.\n" "@param resize A boolean value that decides whether the ctrl refreshes or not.", "GuiBitmapCtrl", "void setBitmap( String filename );")

Public Functions

_sGuiBitmapCtrlSetBitmap1("@brief Assign an image <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "Child controls with resize according <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> their layout <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">settings.\n</a>" "@param filename The filename of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" "@param resize Optional parameter. If true, the GUI will resize <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> fit the image." , "GuiBitmapCtrl" , "void setBitmap( <a href="/coding/class/classstring/">String</a> filename, bool resize );" )

ConsoleDocClass(GuiBitmapCtrl , "@brief A gui <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> that is used <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display an <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "The image is stretched <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the constraints of the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> by default. However)

DefineEngineMethod(GuiBitmapCtrl , setBitmap , void , (const char *fileRoot, bool resize) , (false) , "( String filename | <a href="/coding/class/classstring/">String</a> filename, bool resize ) Assign an image <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n\n</a>" "@hide" )

DefineEngineMethod(GuiBitmapCtrl , setNamedTexture , bool , (String namedtexture) , "@brief Set <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> texture as the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n\n</a>" "@param namedtexture The name of the texture (<a href="/coding/class/classnamedtextarget/">NamedTexTarget</a>).\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@return true <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the texture exists." )

DefineEngineMethod(GuiBitmapCtrl , setValue , void , (S32 x, S32 y) , "Set the offset of the bitmap within the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">control.\n</a>" "@param x The x-axis offset of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" "@param y The y-axis offset of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">image.\n</a>" )

IMPLEMENT_CONOBJECT(GuiBitmapCtrl )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#include "platform/platform.h"
 25#include "gui/controls/guiBitmapCtrl.h"
 26
 27#include "console/console.h"
 28#include "console/consoleTypes.h"
 29#include "console/engineAPI.h"
 30#include "gfx/gfxDevice.h"
 31#include "gfx/gfxDrawUtil.h"
 32
 33#include "materials/matTextureTarget.h"
 34
 35
 36IMPLEMENT_CONOBJECT(GuiBitmapCtrl);
 37
 38ConsoleDocClass( GuiBitmapCtrl,
 39   "@brief A gui control that is used to display an image.\n\n"
 40   
 41   "The image is stretched to the constraints of the control by default. However, the control can also\n"
 42   "tile the image as well.\n\n"
 43
 44   "The image itself is stored inside the GuiBitmapCtrl::bitmap field. The boolean value that decides\n"
 45   "whether the image is stretched or tiled is stored inside the GuiBitmapCtrl::wrap field.\n"
 46   
 47   "@tsexample\n"
 48   "// Create a tiling GuiBitmapCtrl that displays \"myImage.png\"\n"
 49   "%bitmapCtrl = new GuiBitmapCtrl()\n"
 50   "{\n"
 51   "   bitmap = \"myImage.png\";\n"
 52   "   wrap = \"true\";\n"
 53   "};\n"
 54   "@endtsexample\n\n"
 55   
 56   "@ingroup GuiControls"
 57);
 58
 59GuiBitmapCtrl::GuiBitmapCtrl(void)
 60 : mBitmapName(),
 61   mStartPoint( 0, 0 ),
 62   mColor(ColorI::WHITE),
 63   mWrap( false )
 64{  
 65}
 66
 67bool GuiBitmapCtrl::setBitmapName( void *object, const char *index, const char *data )
 68{
 69   // Prior to this, you couldn't do bitmap.bitmap = "foo.jpg" and have it work.
 70   // With protected console types you can now call the setBitmap function and
 71   // make it load the image.
 72   static_cast<GuiBitmapCtrl *>( object )->setBitmap( data );
 73
 74   // Return false because the setBitmap method will assign 'mBitmapName' to the
 75   // argument we are specifying in the call.
 76   return false;
 77}
 78
 79void GuiBitmapCtrl::initPersistFields()
 80{
 81   addGroup( "Bitmap" );
 82   
 83      addProtectedField( "bitmap", TypeImageFilename, Offset( mBitmapName, GuiBitmapCtrl ),
 84         &setBitmapName, &defaultProtectedGetFn,
 85         "The bitmap file to display in the control.");
 86      addField("color", TypeColorI, Offset(mColor, GuiBitmapCtrl),"color mul");
 87      addField( "wrap",   TypeBool,     Offset( mWrap, GuiBitmapCtrl ),
 88         "If true, the bitmap is tiled inside the control rather than stretched to fit." );
 89      
 90   endGroup( "Bitmap" );
 91
 92   Parent::initPersistFields();
 93}
 94
 95bool GuiBitmapCtrl::onWake()
 96{
 97   if (! Parent::onWake())
 98      return false;
 99   setActive(true);
100   setBitmap(mBitmapName);
101   return true;
102}
103
104void GuiBitmapCtrl::onSleep()
105{
106   if ( !mBitmapName.equal("texhandle", String::NoCase) )
107      mTextureObject = NULL;
108
109   Parent::onSleep();
110}
111
112//-------------------------------------
113void GuiBitmapCtrl::inspectPostApply()
114{
115   // if the extent is set to (0,0) in the gui editor and appy hit, this control will
116   // set it's extent to be exactly the size of the bitmap (if present)
117   Parent::inspectPostApply();
118
119   if (!mWrap && (getExtent().x == 0) && (getExtent().y == 0) && mTextureObject)
120   {
121      setExtent( mTextureObject->getWidth(), mTextureObject->getHeight());
122   }
123}
124
125void GuiBitmapCtrl::setBitmap( const char *name, bool resize )
126{
127   mBitmapName = name;
128   if ( !isAwake() )
129      return;
130
131   if ( mBitmapName.isNotEmpty() )
132   {
133      if ( !mBitmapName.equal("texhandle", String::NoCase) )
134         mTextureObject.set( mBitmapName, &GFXDefaultGUIProfile, avar("%s() - mTextureObject (line %d)", __FUNCTION__, __LINE__) );
135
136      // Resize the control to fit the bitmap
137      if ( mTextureObject && resize )
138      {
139         setExtent( mTextureObject->getWidth(), mTextureObject->getHeight() );
140         updateSizing();
141      }
142   }
143   else
144      mTextureObject = NULL;
145
146   setUpdate();
147}
148
149void GuiBitmapCtrl::updateSizing()
150{
151   if(!getParent())
152      return;
153   // updates our bounds according to our horizSizing and verSizing rules
154   RectI fakeBounds( getPosition(), getParent()->getExtent());
155   parentResized( fakeBounds, fakeBounds);
156}
157
158void GuiBitmapCtrl::setBitmapHandle(GFXTexHandle handle, bool resize)
159{
160   mTextureObject = handle;
161
162   mBitmapName = String("texhandle");
163
164   // Resize the control to fit the bitmap
165   if (resize) 
166   {
167      setExtent(mTextureObject->getWidth(), mTextureObject->getHeight());
168      updateSizing();
169   }
170}
171
172void GuiBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
173{
174   if (mTextureObject)
175   {
176      GFX->getDrawUtil()->clearBitmapModulation();
177      GFX->getDrawUtil()->setBitmapModulation(mColor);
178      if(mWrap)
179      {
180         // We manually draw each repeat because non power of two textures will 
181         // not tile correctly when rendered with GFX->drawBitmapTile(). The non POT
182         // bitmap will be padded by the hardware, and we'll see lots of slack
183         // in the texture. So... lets do what we must: draw each repeat by itself:
184         GFXTextureObject* texture = mTextureObject;
185         RectI srcRegion;
186         RectI dstRegion;
187         F32 xdone = ((F32)getExtent().x/(F32)texture->mBitmapSize.x)+1;
188         F32 ydone = ((F32)getExtent().y/(F32)texture->mBitmapSize.y)+1;
189
190         S32 xshift = mStartPoint.x</a>%texture-><a href="/coding/class/classgfxtextureobject/#classgfxtextureobject_1ac2d6a6fef6cde71207314fe42cfcdce1">mBitmapSize.x;
191         S32 yshift = mStartPoint.y</a>%texture-><a href="/coding/class/classgfxtextureobject/#classgfxtextureobject_1ac2d6a6fef6cde71207314fe42cfcdce1">mBitmapSize.y;
192         for(S32 y = 0; y < ydone; ++y)
193            for(S32 x = 0; x < xdone; ++x)
194            {
195               srcRegion.set(0,0,texture->mBitmapSize.x,texture->mBitmapSize.y);
196               dstRegion.set( ((texture->mBitmapSize.x*x)+offset.x)-xshift,
197                              ((texture->mBitmapSize.y*y)+offset.y)-yshift,
198                              texture->mBitmapSize.x,
199                              texture->mBitmapSize.y);
200               GFX->getDrawUtil()->drawBitmapStretchSR(texture,dstRegion, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear);
201            }
202
203      }
204      else
205      {
206         RectI rect(offset, getExtent());
207         GFX->getDrawUtil()->drawBitmapStretch(mTextureObject, rect, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
208      }
209   }
210
211   if (mProfile->mBorder || !mTextureObject)
212   {
213      RectI rect(offset.x, offset.y, getExtent().x, getExtent().y);
214      GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
215   }
216
217   renderChildControls(offset, updateRect);
218}
219
220void GuiBitmapCtrl::setValue(S32 x, S32 y)
221{
222   if (mTextureObject)
223   {
224      x += mTextureObject->getWidth() / 2;
225      y += mTextureObject->getHeight() / 2;
226   }
227   while (x < 0)
228      x += 256;
229   mStartPoint.x = x % 256;
230
231   while (y < 0)
232      y += 256;
233   mStartPoint.y = y % 256;
234}
235
236DefineEngineMethod( GuiBitmapCtrl, setValue, void, ( S32 x, S32 y ),,
237   "Set the offset of the bitmap within the control.\n"
238   "@param x The x-axis offset of the image.\n"
239   "@param y The y-axis offset of the image.\n")
240{
241   object->setValue(x, y);
242}
243
244
245//
246
247static ConsoleDocFragment _sGuiBitmapCtrlSetBitmap1(
248   "@brief Assign an image to the control.\n\n"
249   "Child controls with resize according to their layout settings.\n"
250   "@param filename The filename of the image.\n"
251   "@param resize Optional parameter. If true, the GUI will resize to fit the image.",
252   "GuiBitmapCtrl", // The class to place the method in; use NULL for functions.
253   "void setBitmap( String filename, bool resize );" ); // The definition string.
254
255static ConsoleDocFragment _sGuiBitmapCtrlSetBitmap2(
256   "@brief Assign an image to the control.\n\n"
257   "Child controls will resize according to their layout settings.\n"
258   "@param filename The filename of the image.\n"
259   "@param resize A boolean value that decides whether the ctrl refreshes or not.",
260   "GuiBitmapCtrl", // The class to place the method in; use NULL for functions.
261   "void setBitmap( String filename );" ); // The definition string.
262
263
264//"Set the bitmap displayed in the control. Note that it is limited in size, to 256x256."
265DefineEngineMethod( GuiBitmapCtrl, setBitmap, void, ( const char * fileRoot, bool resize), ( false),
266   "( String filename | String filename, bool resize ) Assign an image to the control.\n\n"
267   "@hide" )
268{
269   char filename[1024];
270   Con::expandScriptFilename(filename, sizeof(filename), fileRoot);
271   object->setBitmap(filename, resize );
272}
273
274DefineEngineMethod( GuiBitmapCtrl, setNamedTexture, bool, (String namedtexture),,
275   "@brief Set a texture as the image.\n\n"
276   "@param namedtexture The name of the texture (NamedTexTarget).\n"
277   "@return true if the texture exists." )
278{
279   GFXTexHandle theTex;
280   NamedTexTarget *namedTarget = NULL;
281   namedTarget = NamedTexTarget::find(namedtexture.c_str());
282   if ( namedTarget )
283   {
284      theTex = namedTarget->getTexture( 0 );
285   }
286   
287   if ( theTex.isValid() )
288   {
289      object->setBitmapHandle( theTex , false );
290      return true; //a new texture was set correctly
291   }
292   return false; //we couldn't change the texture
293}
294