Game Development Community

dev|Pro Game Development Curriculum

Blinking cursor for guiMLTextEditCtrl

by Nathan Bowhay - ESAL · 08/06/2009 (2:21 pm) · 1 comments

I just grabbed this code from guiTextEditCtrl and moved it over, but here it is:

The files we will work with are of course guiMLTextEditCtrl.cpp and guiMLTextEditCtrl.h and they are in EngineSourceguicontrols.

First the .h:
On line 20 after "StringTableEntry mEscapeCommand;" add:
// for animating the cursor
   S32      mNumFramesElapsed;
   U32      mTimeLastCursorFlipped;
   bool     mCursorOn;

On line 33 after "// rendering" add:
void onPreRender();

On line 45 after "static void initPersistFields();" add:
virtual void setFirstResponder();
   virtual void onLoseFirstResponder();

Now for the .cpp:
On line 26 after "mVertMoveAnchorValid = false;" in the ctor add:
mCursorOn = false;
   mNumFramesElapsed = 0;

On line 69 after the onWake method add:
void GuiMLTextEditCtrl::setFirstResponder()
{
   Parent::setFirstResponder();

   GuiCanvas *root = getRoot();
   if (root != NULL)
   {
		root->enableKeyboardTranslation();
  

	   // If the native OS accelerator keys are not disabled
		// then some key events like Delete, ctrl+V, etc may
		// not make it down to us.
		root->setNativeAcceleratorsEnabled( false );
   }
}

void GuiMLTextEditCtrl::onLoseFirstResponder()
{
   GuiCanvas *root = getRoot();
   root->setNativeAcceleratorsEnabled( true );
   root->disableKeyboardTranslation();

   // Redraw the control:
   setUpdate();
}

On line 411 before the onRender method add:
void GuiMLTextEditCtrl::onPreRender()
{
	Parent::onPreRender();
	if ( isFirstResponder() )
	{
		U32 timeElapsed = Platform::getVirtualMilliseconds() - mTimeLastCursorFlipped;
		mNumFramesElapsed++;
		if ( ( timeElapsed > 500 ) && ( mNumFramesElapsed > 3 ) )
		{
			mCursorOn = !mCursorOn;
			mTimeLastCursorFlipped = Platform::getVirtualMilliseconds();
			mNumFramesElapsed = 0;
			setUpdate();
		}
	}
}

and finally on line 434 in onRender change:
if (isFirstResponder())
to
if (isFirstResponder() && mCursorOn)

And that is it you should have a nice little blinking cursor now!