Game Development Community

dev|Pro Game Development Curriculum

T3D - guiTextEditSliderBitmapCtrl FIX and Clean Up

by Jean-louis Amadi · 03/24/2012 (4:30 am) · 0 comments

Now, the gui is displayed correctly and its height is given by the height of the first part of the skin, also you can use it without the incoherent stretch of the bitmap, that we had previously. The line that separe the numbers of the bitmap do not pass over the border zone of the zone of edition as before.

Pictures

img833.imageshack.us/img833/9328/numericsliderctrl.jpg



img406.imageshack.us/img406/7411/numericsliderctrlexampl.jpg

Codes(choose the better install for you)


Engine/source/gui/controls/guiTextEditSliderBitmapCtrl.h
Engine/source/gui/controls/guiTextEditSliderBitmapCtrl.cpp
Rebuild in debug and release, enjoy!

Copy/Paste the code above in the respective files and overwrite.
OR Merge(if already modified).
OR create 2 new files with the name of your choice and copy/paste the code above in the respective files.

guiTextEditSliderBitmapCtrl.h

//-----------------------------------------------------------------------------
// Torque
// Copyright GarageGames, LLC 2011
//-----------------------------------------------------------------------------

#ifndef _GUITEXTEDITSLIDERBITMAPCTRL_H_
#define _GUITEXTEDITSLIDERBITMAPCTRL_H_

#ifndef _GUITYPES_H_
#include "gui/core/guiTypes.h"
#endif
#ifndef _GUITEXTEDITCTRL_H_
#include "gui/controls/guiTextEditCtrl.h"
#endif

class GuiTextEditSliderBitmapCtrl : public GuiTextEditCtrl
{
   typedef GuiTextEditCtrl Parent;

public:

   enum CtrlArea
   {
      None,
      Slider,
      ArrowUp,
      ArrowDown
   };

   GuiTextEditSliderBitmapCtrl();
   ~GuiTextEditSliderBitmapCtrl();

   DECLARE_CONOBJECT(GuiTextEditSliderBitmapCtrl);
   DECLARE_CATEGORY( "Gui Values" );
   DECLARE_DESCRIPTION( "A text control that display a numeric value and bitmapped up/down sliders." );

   static void initPersistFields();

   virtual void getText(char *dest);  // dest must be of size
                                      // StructDes::MAX_STRING_LEN + 1

   virtual void setText(const char *txt);

   void setValue();
   void checkRange();
   void checkIncValue();
   void timeInc(U32 elapseTime);

   virtual bool onKeyDown(const GuiEvent &event);
   virtual void onMouseDown(const GuiEvent &event);
   virtual void onMouseDragged(const GuiEvent &event);
   virtual void onMouseUp(const GuiEvent &event);
   virtual bool onMouseWheelUp(const GuiEvent &event);
   virtual bool onMouseWheelDown(const GuiEvent &event);
	
   bool onWake();
   virtual void onPreRender();
   virtual void onRender(Point2I offset, const RectI &updateRect);

    //[GameAlchemyPack--Begin]
	//void setBitmap(const char *name);
	//[GameAlchemyPack--End]

protected:

   Point2F mRange;
   F32 mIncAmount;
   F32 mValue;
   F32 mIncCounter;
   F32 mMulInc;
   StringTableEntry mFormat;
   U32 mMouseDownTime;
   bool mFocusOnMouseWheel;
	S32 mNumberOfBitmaps;
	 //[GameAlchemyPack--Begin]
	//StringTableEntry mBitmapName;
	 //[GameAlchemyPack--End]

   CtrlArea mTextAreaHit;
};

#endif //_GUITEXTEDITSLIDERBITMAPCTRL_H_

guiTextEditSliderBitmapCtrl.cpp

//-----------------------------------------------------------------------------
// Torque
// Copyright GarageGames, LLC 2011
//-----------------------------------------------------------------------------

#include "platform/platform.h"
#include "gui/controls/guiTextEditSliderBitmapCtrl.h"

#include "console/consoleTypes.h"
#include "console/console.h"
#include "gui/core/guiCanvas.h"
#include "gfx/gfxDevice.h"
#include "gfx/gfxDrawUtil.h"


IMPLEMENT_CONOBJECT(GuiTextEditSliderBitmapCtrl);

ConsoleDocClass( GuiTextEditSliderBitmapCtrl,
   "@brief GUI Control which displays a numerical value which can be increased "
   "or decreased using a pair of bitmap up/down buttons. nn"

   "This control uses the bitmap specified in it's profile "
   "(GuiControlProfile::bitmapName). It takes this image and breaks up aspects "
   "of it to render the up and down arrows. It is also important to set "
   "GuiControlProfile::hasBitmapArray to true on the profile as well.nn"

   "The bitmap referenced should be broken up into a 1 x 4 grid (using the top "
   "left color pixel as a border color between each of the images) in which it "
   "will map to the following places:n"
   "<ol>n"
   "<li>Up arrow active</li>n"
   "<li>Up arrow inactive</li>n"
   "<li>Down arrow active</li>n"
   "<li>Down arrow inactive</li>n"
   "</ol>nn"

   "<pre>n"
   "1n"
   "2n"
   "3n"
   "4</pre>nn"

   "@tsexamplen"
   "singleton GuiControlProfile (SliderBitmapGUIProfile)n"
   "{n"
   "   bitmap = "core/art/gui/images/sliderArray";n"
   "   hasBitmapArray = true;n"
   "   opaque = false;n"
   "};nn"

   "new GuiTextEditSliderBitmapCtrl()n"
   "{n"
   "   profile = "SliderBitmapGUIProfile";n"
   "   format = "%3.2f";n"
   "   range = "-1e+03 1e+03";n"
   "   increment = "0.1";n"
   "   focusOnMouseWheel = "0";n"
   "   bitmap = "";n"
   "   //Properties not specific to this control have been omitted from this example.n"
   "};n"
   "@endtsexamplenn"

   "@see GuiTextEditSliderCtrlnn"
   "@see GuiTextEditCtrlnn"

   "@ingroup GuiCoren"
);


GuiTextEditSliderBitmapCtrl::GuiTextEditSliderBitmapCtrl()
{
   mRange.set(0.0f, 1.0f);
   mIncAmount = 1.0f;
   mValue = 0.0f;
   mMulInc = 0;
   mIncCounter = 0.0f;
   mFormat = StringTable->insert("%3.2f");
   mTextAreaHit = None;
   mFocusOnMouseWheel = false;
   //[GameAlchemyPack--Begin]
   //mBitmapName = StringTable->insert( "" );
   //[GameAlchemyPack--End]
}

GuiTextEditSliderBitmapCtrl::~GuiTextEditSliderBitmapCtrl()
{
}

void GuiTextEditSliderBitmapCtrl::initPersistFields()
{
   addField("format",    TypeString,  Offset(mFormat, GuiTextEditSliderBitmapCtrl), "Character format type to place in the control.n");
   addField("range",     TypePoint2F, Offset(mRange, GuiTextEditSliderBitmapCtrl), "Maximum vertical and horizontal range to allow in the control.n");
   addField("increment", TypeF32,     Offset(mIncAmount,     GuiTextEditSliderBitmapCtrl), "How far to increment the slider on each step.n");
   addField("focusOnMouseWheel", TypeBool, Offset(mFocusOnMouseWheel, GuiTextEditSliderBitmapCtrl), "If true, the control will accept giving focus to the user when the mouse wheel is used.n");
   //[GameAlchemyPack--Begin]
   //addField("bitmap",    TypeFilename,Offset(mBitmapName, GuiTextEditSliderBitmapCtrl), "Unused" );
   //[GameAlchemyPack--End]

   Parent::initPersistFields();
}

void GuiTextEditSliderBitmapCtrl::getText(char *dest)
{
   Parent::getText(dest);
}

void GuiTextEditSliderBitmapCtrl::setText(const char *txt)
{
   mValue = dAtof(txt);
   checkRange();
   setValue();
}

bool GuiTextEditSliderBitmapCtrl::onKeyDown(const GuiEvent &event)
{
   return Parent::onKeyDown(event);
}

void GuiTextEditSliderBitmapCtrl::checkRange()
{
   if(mValue < mRange.x)
      mValue = mRange.x;
   else if(mValue > mRange.y)
      mValue = mRange.y;
}

void GuiTextEditSliderBitmapCtrl::setValue()
{
   char buf[20];
   // For some reason this sprintf is failing to convert
   // a floating point number to anything with %d, so cast it.
   if( dStricmp( mFormat, "%d" ) == 0 )
      dSprintf(buf,sizeof(buf),mFormat, (S32)mValue);
   else
      dSprintf(buf,sizeof(buf),mFormat, mValue);
   Parent::setText(buf);
}

void GuiTextEditSliderBitmapCtrl::onMouseDown(const GuiEvent &event)
{
   // If we're not active then skip out.
   if ( !mActive || !mAwake || !mVisible )
   {
      Parent::onMouseDown(event);
      return;
   }

   char txt[20];
   Parent::getText(txt);
   mValue = dAtof(txt);

   mMouseDownTime = Sim::getCurrentTime();
   GuiControl *parent = getParent();
   if(!parent)
      return;
   Point2I camPos  = event.mousePoint;
   Point2I point = parent->localToGlobalCoord(getPosition());

   //[GameAlchemyPack--Begin]
   Point2I sizeBitmap(mProfile->mTextureObject.getWidthHeight());
   //if(camPos.x > point.x + getExtent().x - 14)
   //[GameAlchemyPack--End]
   if(camPos.x > point.x + getExtent().x - sizeBitmap.x)
   {
      if(camPos.y > point.y + (getExtent().y/2))
      {
         mValue -=mIncAmount;
         mTextAreaHit = ArrowDown;
         mMulInc = -0.15f;
      }
      else
      {
         mValue +=mIncAmount;
         mTextAreaHit = ArrowUp;
         mMulInc = 0.15f;
      }

      checkRange();
      setValue();
      mouseLock();

      // We should get the focus and set the 
      // cursor to the start of the text to 
      // mimic the standard Windows behavior.
      setFirstResponder();
      mCursorPos = mBlockStart = mBlockEnd = 0;
      setUpdate();

      return;
   }

   Parent::onMouseDown(event);
}

void GuiTextEditSliderBitmapCtrl::onMouseDragged(const GuiEvent &event)
{
   // If we're not active then skip out.
   if ( !mActive || !mAwake || !mVisible )
   {
      Parent::onMouseDragged(event);
      return;
   }

   if(mTextAreaHit == None || mTextAreaHit == Slider)
   {
      mTextAreaHit = Slider;
      GuiControl *parent = getParent();
      if(!parent)
         return;
      Point2I camPos = event.mousePoint;
      Point2I point = parent->localToGlobalCoord(getPosition());
      F32 maxDis = 100;
      F32 val;
      if(camPos.y < point.y)
      {
         if((F32)point.y < maxDis)
            maxDis = (F32)point.y;

         val = point.y - maxDis;
         
         if(point.y > 0)
            mMulInc= 1.0f-(((float)camPos.y - val) / maxDis);
         else
            mMulInc = 1.0f;
         
         checkIncValue();
         
         return;
      }
      else if(camPos.y > point.y + getExtent().y)
      {
         GuiCanvas *root = getRoot();
         val = (F32)(root->getHeight() - (point.y + getHeight()));
         if(val < maxDis)
            maxDis = val;
         if( val > 0)
            mMulInc= -(F32)(camPos.y - (point.y + getHeight()))/maxDis;
         else
            mMulInc = -1.0f;
         checkIncValue();
         return;
      }
      mTextAreaHit = None;
      Parent::onMouseDragged(event);
   }
}

void GuiTextEditSliderBitmapCtrl::onMouseUp(const GuiEvent &event)
{
   // If we're not active then skip out.
   if ( !mActive || !mAwake || !mVisible )
   {
      Parent::onMouseUp(event);
      return;
   }

   mMulInc = 0.0f;
   mouseUnlock();

   if ( mTextAreaHit != None )

  //if we released the mouse within this control, then the parent will call
  //the mConsoleCommand other wise we have to call it.
   Parent::onMouseUp(event);

   //if we didn't release the mouse within this control, then perform the action
   // if (!cursorInControl())
   execConsoleCallback();   

   execAltConsoleCallback();

   mTextAreaHit = None;
}

bool GuiTextEditSliderBitmapCtrl::onMouseWheelUp(const GuiEvent &event)
{
   if ( !mActive || !mAwake || !mVisible )
      return Parent::onMouseWheelUp(event);

   if ( !isFirstResponder() && !mFocusOnMouseWheel )
      return false;

   mValue += mIncAmount;

   checkRange();
   setValue();
   
   setFirstResponder();
   mCursorPos = mBlockStart = mBlockEnd = 0;
   setUpdate();

   return true;
}

bool GuiTextEditSliderBitmapCtrl::onMouseWheelDown(const GuiEvent &event)
{
   if ( !mActive || !mAwake || !mVisible )
      return Parent::onMouseWheelDown(event);

   if ( !isFirstResponder() && !mFocusOnMouseWheel )
      return false;

   mValue -= mIncAmount;

   checkRange();
   setValue();

   setFirstResponder();
   mCursorPos = mBlockStart = mBlockEnd = 0;
   setUpdate();

   return true;
}

void GuiTextEditSliderBitmapCtrl::checkIncValue()
{
   if(mMulInc > 1.0f)
      mMulInc = 1.0f;
   else if(mMulInc < -1.0f)
      mMulInc = -1.0f;
}

void GuiTextEditSliderBitmapCtrl::timeInc(U32 elapseTime)
{
   S32 numTimes = elapseTime / 750;
   if(mTextAreaHit != Slider && numTimes > 0)
   {
      if(mTextAreaHit == ArrowUp)
         mMulInc = 0.15f * numTimes;
      else
         mMulInc = -0.15f * numTimes;

      checkIncValue();
   }
}
bool GuiTextEditSliderBitmapCtrl::onWake()
{
   if(!Parent::onWake())
      return false;

	mNumberOfBitmaps = mProfile->constructBitmapArray();
	//[GameAlchemyPack--Begin]
	if(mNumberOfBitmaps == 0)
		Con::warnf("No image provided for GuiTextEditSliderBitmapCtrl; do not render");
	//[GameAlchemyPack--End]

   return true;
}

void GuiTextEditSliderBitmapCtrl::onPreRender()
{
	//[GameAlchemyPack--Begin]
	if( mProfile->mTextureObject.isValid() && !mProfile->mTextureObject.isNull() )
	{
		setHeight( (mProfile->mTextureObject.getHeight()/2) + 2 );
	}
	//[GameAlchemyPack--End]
   if (isFirstResponder())
   {
      U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
      mNumFramesElapsed++;
      if ((timeElapsed > 500) && (mNumFramesElapsed > 3))
      {
         mCursorOn = !mCursorOn;
         mTimeLastCursorFlipped = Sim::getCurrentTime();
         mNumFramesElapsed = 0;
         setUpdate();
      }

      //update the cursor if the text is scrolling
      if (mDragHit)
      {
         if ((mScrollDir < 0) && (mCursorPos > 0))
         {
            mCursorPos--;
         }
         else if ((mScrollDir > 0) && (mCursorPos < (S32)dStrlen(mText)))
         {
            mCursorPos++;
         }
      }
   }
}

void GuiTextEditSliderBitmapCtrl::onRender(Point2I offset, const RectI &updateRect)
{
   if(mTextAreaHit != None)
   {
      U32 elapseTime = Sim::getCurrentTime() - mMouseDownTime;
      if(elapseTime > 750 || mTextAreaHit == Slider)
      {
         timeInc(elapseTime);
         mIncCounter += mMulInc;
         if(mIncCounter >= 1.0f || mIncCounter <= -1.0f)
         {
            mValue = (mMulInc > 0.0f) ? mValue+mIncAmount : mValue-mIncAmount;
            mIncCounter = (mIncCounter > 0.0f) ? mIncCounter-1 : mIncCounter+1;
            checkRange();
            setValue();
            mCursorPos = 0;
         }
      }
   }

   
	Parent::onRender(offset, updateRect);

	//[GameAlchemyPack--Begin]
	GFX->getDrawUtil()->clearBitmapModulation();
	//[GameAlchemyPack--End]

	if( mProfile->mTextureObject.isValid() && !mProfile->mTextureObject.isNull() )
	{
	// Arrow placement coordinates
	//[GameAlchemyPack--Begin]
   Point2I sizeBitmap(mProfile->mTextureObject.getWidthHeight());

   //Point2I arrowUpStart(offset.x + getWidth() - 14, offset.y + 1 );
   Point2I arrowUpStart(offset.x + getWidth() - sizeBitmap.x-2, offset.y + 1 );
   //Point2I arrowUpEnd(13, getExtent().y/2);
   Point2I arrowUpEnd(sizeBitmap.x, sizeBitmap.y/4);

	//Point2I arrowDownStart(offset.x + getWidth() - 14, offset.y + 1 + getExtent().y/2);
	Point2I arrowDownStart(offset.x + getWidth() - sizeBitmap.x-2, offset.y + 1 + sizeBitmap.y/4);
	//Point2I arrowDownEnd(13, getExtent().y/2);
	Point2I arrowDownEnd(sizeBitmap.x, sizeBitmap.y/4);
	
	// Draw the line that splits the number and bitmaps
	GFX->getDrawUtil()->drawLine(Point2I(offset.x + getWidth() - sizeBitmap.x -4, offset.y + 1 ),
		Point2I(arrowUpStart.x -2, arrowUpStart.y + getExtent().y - 1),
		mProfile->mBorderColor);

	/*GFX->getDrawUtil()->drawLine(Point2I(offset.x + getWidth() - 14 -2, offset.y + 1 ),
		Point2I(arrowUpStart.x -2, arrowUpStart.y + getExtent().y),
		mProfile->mBorderColor);*/

	//GFX->getDrawUtil()->clearBitmapModulation();
	//[GameAlchemyPack--End]
	
	//[GameAlchemyPack--Begin]
	//if(mNumberOfBitmaps == 0)
	//	Con::warnf("No image provided for GuiTextEditSliderBitmapCtrl; do not render");
	//else
	//{//[GameAlchemyPack--End]
		
		// This control needs 4 images in order to render correctly
		if(mTextAreaHit == ArrowUp)
			GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[0] );
		else
			GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowUpStart,arrowUpEnd), mProfile->mBitmapArrayRects[1] );

		if(mTextAreaHit == ArrowDown)
			GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[2] );
		else
			GFX->getDrawUtil()->drawBitmapStretchSR( mProfile->mTextureObject, RectI(arrowDownStart,arrowDownEnd), mProfile->mBitmapArrayRects[3] );
	}
}

//[GameAlchemyPack--Begin]
/*void GuiTextEditSliderBitmapCtrl::setBitmap(const char *name)
{
   bool awake = mAwake;
   if(awake)
      onSleep();

   mBitmapName = StringTable->insert(name);
   if(awake)
      onWake();
   setUpdate();
}*/
//[GameAlchemyPack--End]