Game Development Community

dev|Pro Game Development Curriculum

GuiSkinnedButtonCtrl (TGEA) - A basic skinnable button control

by Nathan Bowhay - ESAL · 03/24/2009 (5:42 pm) · 2 comments

This was originally done by Rob Parton that can be found here: www.garagegames.com/community/resources/view/11299

I just ported it to TGEA so I figured I would save others the work if they needed it.

If you want an example image use the resources link above and download the .zip file.

guiSkinnedButtonCtrl.h
// Skinned Button Control for TGEA
// by Rob Parton
// TGEA port by Nathan Bowhay

#ifndef _GUISKINNEDBUTTONCTRL_H_
#define _GUISKINNEDBUTTONCTRL_H_

#ifndef _GUIBUTTONBASECTRL_H_
#include "gui/buttons/guiButtonBaseCtrl.h"
#endif

///////////////////////////////////////////////////////////
class GuiSkinnedButtonCtrl: public GuiButtonBaseCtrl
{

  typedef GuiButtonBaseCtrl Parent;
  RectI *mBitmapBounds;

protected:
    GFXTexHandle mTexture;

	enum {
      ButtonStateNormal,
      ButtonStateHilite,
      ButtonStateNotActive,
	  ButtonStateDepressed
    };

    static const int NumBitmaps = 12;

  public:
	// Declare a console object in Torque
    DECLARE_CONOBJECT(GuiSkinnedButtonCtrl);
    GuiSkinnedButtonCtrl();

    void onSleep();
    bool onWake();
	// rendering
    void onRender(Point2I offset, const RectI &updateRect);

};

#endif

guiSkinnedButtonCtrl.cpp
// Skinned Button Control for TGEA
// by Rob Parton
// TGEA port by Nathan Bowhay

#include "console/console.h"
#include "gfx/gfxDevice.h"
#include "console/consoleTypes.h"
#include "gui/core/guiCanvas.h"
#include "gui/core/guiDefaultControlRender.h"
#include "gui/buttons/guiSkinnedButtonCtrl.h"

//////////////////////////////////////////////////////

IMPLEMENT_CONOBJECT(GuiSkinnedButtonCtrl);

GuiSkinnedButtonCtrl::GuiSkinnedButtonCtrl()
{
	mButtonText = StringTable->insert("Button");
}

void GuiSkinnedButtonCtrl::onSleep()
{
	mTexture = NULL;
	Parent::onSleep();
}

bool GuiSkinnedButtonCtrl::onWake()
{
   // if the parent control didn't wake up, what am I doing up?
   if (! Parent::onWake())
      return false;

   //get the texture for the button.
	mTexture = mProfile->mTextureObject;
   bool result = mProfile->constructBitmapArray() >= NumBitmaps;
   AssertFatal(result, "Failed to create the bitmap array");
   if(!result)
      return false;

   // set bitmap bounds
   mBitmapBounds = mProfile->mBitmapArrayRects.address();

   // this sets the button so the height is never higher than the actual bitmap
   // because vertical stretching of a skinned button is hideous.
   // TODO: Make this optional
	this->setHeight(mBitmapBounds[0].extent.y);

   return true;
}

void GuiSkinnedButtonCtrl::onRender(Point2I offset, const RectI &updateRect)
{
	bool bHighlight = mMouseOver;
	bool bDepressed = mDepressed;

	ColorI fontColor = mActive ? (bHighlight ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;

	RectI ctrlRect(offset, getExtent());

	// Figure out which bitmap index we start on
	int idm = mActive ? (bHighlight ? (bDepressed ? ButtonStateDepressed: ButtonStateHilite) : ButtonStateNormal) : ButtonStateNotActive;
	idm *= 3;
   if(mStateOn)
      idm = 9;

	// Draw Background dependant on mode
	GFX->getDrawUtil()->clearBitmapModulation();

	// Left Backing
	GFX->getDrawUtil()->drawBitmapSR(mTexture,offset,mBitmapBounds[0+idm]);

	// Right Backing
	GFX->getDrawUtil()->drawBitmapSR(mTexture, Point2I(offset.x + getWidth() - mBitmapBounds[2+idm].extent.x,offset.y), mBitmapBounds[2+idm]);

	// Background Span
	RectI destRect;
	destRect.point.x = offset.x + mBitmapBounds[0+idm].extent.x;
	destRect.point.y = offset.y;
	destRect.extent.x = getWidth() - mBitmapBounds[0+idm].extent.x - mBitmapBounds[2+idm].extent.x;
	destRect.extent.y = mBitmapBounds[1+idm].extent.y;

	RectI stretchRect = mBitmapBounds[1+idm];
	stretchRect.inset(1,0);
	GFX->getDrawUtil()->drawBitmapStretchSR(mTexture,destRect, stretchRect);

	// text!
	GFX->getDrawUtil()->setBitmapModulation(fontColor);
	renderJustifiedText(offset, getExtent(), mButtonText);

	// render child controls
	renderChildControls(offset, updateRect);
}

#1
04/08/2009 (7:28 am)
Nathan I am sorry but there is already GuiBitmapButtonTextCtrl.
#2
04/13/2009 (1:28 pm)
Yeah there is and I have used it, but it works a bit different then this. Unless they changed the way it works in TGEA. Cause the old one (TGE) worked a lot different then this button (the original resource done by someone else that I ported), you couldn't resize it and not stretch the image (didn't use a bitmap array), which is the reason the original person made the resource and why we ended up using it.