Game Development Community

GuiBitmapCtrl Color Overlay/Transparency

by Andrew Mac · in Torque 3D Professional · 09/03/2013 (9:46 am) · 2 replies

I couldn't find a way to control color/transparency in GUI bitmap images without actually editing the image itself, so I dug into the cpp code and found it was actually quite easy to enable. I figured I'd post it on here in case anyone else goes looking for similar functionality.

Find GuiBitmapCtrl::onRender function in Engine/gui/controls/guiBitmapCtrl.cpp (should be line 166) and replace it with the following:

void GuiBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
{
   if (mTextureObject)
   {
	  GFX->getDrawUtil()->setBitmapModulation( mProfile->mFillColor );
	  if(mWrap)
		{
         // We manually draw each repeat because non power of two textures will 
         // not tile correctly when rendered with GFX->drawBitmapTile(). The non POT
         // bitmap will be padded by the hardware, and we'll see lots of slack
         // in the texture. So... lets do what we must: draw each repeat by itself:
 			GFXTextureObject* texture = mTextureObject;
			RectI srcRegion;
			RectI dstRegion;
			float xdone = ((float)getExtent().x/(float)texture->mBitmapSize.x)+1;
			float ydone = ((float)getExtent().y/(float)texture->mBitmapSize.y)+1;

			int xshift = mStartPoint.x%texture->mBitmapSize.x;
			int yshift = mStartPoint.y%texture->mBitmapSize.y;
			for(int y = 0; y < ydone; ++y)
				for(int x = 0; x < xdone; ++x)
				{
		 			srcRegion.set(0,0,texture->mBitmapSize.x,texture->mBitmapSize.y);
  					dstRegion.set( ((texture->mBitmapSize.x*x)+offset.x)-xshift,
								      ((texture->mBitmapSize.y*y)+offset.y)-yshift,
								      texture->mBitmapSize.x,
								      texture->mBitmapSize.y);
               GFX->getDrawUtil()->drawBitmapStretchSR(texture,dstRegion, srcRegion, GFXBitmapFlip_None, GFXTextureFilterLinear);
				}

		}
	  else
        {
         RectI rect(offset, getExtent());
         GFX->getDrawUtil()->drawBitmapStretch(mTextureObject, rect, GFXBitmapFlip_None, GFXTextureFilterLinear, false);
        }
	  GFX->getDrawUtil()->clearBitmapModulation();
   }

   if (mProfile->mBorder || !mTextureObject)
   {
      RectI rect(offset.x, offset.y, getExtent().x, getExtent().y);
      GFX->getDrawUtil()->drawRect(rect, mProfile->mBorderColor);
   }

   renderChildControls(offset, updateRect);
}

I set the overlay color to the element's profile's fillColor. Now, if you have a bunch of already existing GuiBitmapCtrl's they may not show up due to GuiDefaultProfile having fill color set to transparent. I just created a new profile, GuiBitmapProfile, with fillColor set to fully opaque white and went from there.

#1
09/03/2013 (1:55 pm)
Seems pretty handy - have to play around and see what I can do with it. Thanks!
#2
09/03/2013 (6:06 pm)
I did that a while back, feel free to use this if you need to as well.