T2D Text Display Class
by Greg Lincoln · 03/25/2005 (1:19 pm) · 14 comments
Download Code File
fxTextObject2D is a simple text display class that works pretty much like a tooltip. It remains the same size regardless of camera zoom, but it does apply a location offset to stay in the correct place. It uses Torque's built in text routine, which supports very nice anti-aliased display and proportional fonts.
NOTE: I am very new at working with T2D and the Torque platform/OpenGL. I'm sure there's some stupidities in this code, and a bug or two. It works for me but YMMV. If you find anything I did wrong, please let me know so I can learn. :)
Installing the new class is pretty simple. Just stick the .cc and .h file in your T2D project. There's also one small change you need to make to fxSceneWindow2D.h. Add this method to the public methods section.
Compile your new T2D and you should be good to go.
Here's a usage sample:
fxTextObject2D is a simple text display class that works pretty much like a tooltip. It remains the same size regardless of camera zoom, but it does apply a location offset to stay in the correct place. It uses Torque's built in text routine, which supports very nice anti-aliased display and proportional fonts.
NOTE: I am very new at working with T2D and the Torque platform/OpenGL. I'm sure there's some stupidities in this code, and a bug or two. It works for me but YMMV. If you find anything I did wrong, please let me know so I can learn. :)
Installing the new class is pretty simple. Just stick the .cc and .h file in your T2D project. There's also one small change you need to make to fxSceneWindow2D.h. Add this method to the public methods section.
Point2F getCurrentCameraScale(void){ return mCameraCurrent.mSceneWindowScale; };Compile your new T2D and you should be good to go.
Here's a usage sample:
$test = new fxTextObject2D() { scenegraph = t2dSceneGraph; };
$test.setText("12345678901234567890");
// default font is Lucida Console, 14pt
$test.setFont("Tahoma");
$test.setSize(24);
// default font color is 0 0 0 255 (full black)
$test.setFontColor("255 255 255 255");
$test.setPosition("10 10");
// set the alpha of these to 0 to disable them (they won't draw for a slight speedup)
// they are disabled by default
$test.setBorderColor("0 0 0 255"); // R/G/B/A
$test.setBannerColor("255 0 0 255"); // R/G/B/A
#2
03/25/2005 (1:57 pm)
Awesome! Definitely could use this.
#3
This is officially the first T2D C++ custom object.
This is history in the making! :)
- Melv.
03/26/2005 (12:09 pm)
Nice one Greg!This is officially the first T2D C++ custom object.
This is history in the making! :)
- Melv.
#5
03/30/2005 (12:30 am)
Once I draw text on the screen how to I clear it so that I can update the text?
#6
Remember this isn't printing to the screen like you'd normally think of it. This is a T2D "sprite" that just happens to have text on it.
03/30/2005 (4:30 am)
Just use setText again to update the text. If you want to get rid of the object you can delete it or hide the layer you've assigned it to, just like any other T2D object.Remember this isn't printing to the screen like you'd normally think of it. This is a T2D "sprite" that just happens to have text on it.
#7
Nice work.
- Melv.
03/30/2005 (4:51 am)
@Greg: Well, I was going to send lots of cash through the post but if you want a cream scone instead, I sent that right away. ;)Nice work.
- Melv.
#8
Point2F cameraScale=window->getCurrentCamera().mSceneWindowScale;
... in other words, you can get a const reference to the current camera to save you adding that function. :)
Just thought I'd mention it.
- Melv.
04/01/2005 (12:47 am)
@Greg: Just thought I'd add that in the new SDK, you can do this...Point2F cameraScale=window->getCurrentCamera().mSceneWindowScale;
... in other words, you can get a const reference to the current camera to save you adding that function. :)
Just thought I'd mention it.
- Melv.
#9
08/23/2005 (6:59 am)
I collect the class , thanks!
#10
For example, the following code does not work properly because the function setAlignLeft() relies on %score having a valid X and Y size. However, at this point in the code, the X and Y are equal to the default "10 10".
Note: The C++ code has the correct values immediately after calling setText(). Is there any particular reason as to why my script code does not have access to the correct values until after the object has been rendered once? Any ideas?
09/10/2005 (9:57 pm)
I have a question on this code. For some reason, the size of the object is not updated until after the object has been rendered once (checked this by printing debug statements at the top and bottom of fxTextObject2D::renderObject()).For example, the following code does not work properly because the function setAlignLeft() relies on %score having a valid X and Y size. However, at this point in the code, the X and Y are equal to the default "10 10".
%score.setSize( 24 );
%score.setText( "SCORE" SPC 120000 );
%score.setFontColor("255 255 64 255");
%score.setPosition("-50 -36");
%score.setAlignLeft();Note: The C++ code has the correct values immediately after calling setText(). Is there any particular reason as to why my script code does not have access to the correct values until after the object has been rendered once? Any ideas?
#11
Thanks in advance.
10/29/2005 (8:55 am)
I want to know how to use in the T2D exe project , in C++ mode.Thanks in advance.
#12
file diff:
01/17/2006 (12:59 pm)
excellent, just upgraded to alpha2 and works as a champ!file diff:
Index: E:/jtech/T2D/engine/T2D/t2dTextObject.cc
===================================================================
--- E:/jtech/T2D/engine/T2D/t2dTextObject.cc (revision 30)
+++ E:/jtech/T2D/engine/T2D/t2dTextObject.cc (revision 31)
@@ -7,156 +7,145 @@
#include "math/mMathFn.h"
#include "console/consoleTypes.h"
#include "core/bitStream.h"
-#include "./fxTextObject2D.h"
+#include "./t2dTextObject.h"
#include "dgl/gFont.h"
#include "core/frameAllocator.h"
-IMPLEMENT_CONOBJECT(fxTextObject2D);
+IMPLEMENT_CONOBJECT(t2dTextObject);
//-----------------------------------------------------------------------------
// Constructor.
//-----------------------------------------------------------------------------
-fxTextObject2D::fxTextObject2D() : mTextString(NULL),
+t2dTextObject::t2dTextObject() : mTextString(NULL),
mSizeUpdateNeeded(false),
mStringFontSize(0,0),
- mFontColor(0,0,0,255),
+ mFontColor(255,255,0,255),
mBannerColor(0,0,0,0),
mBorderColor(0,0,0,0)
{
- mpFont = GFont::create("Lucida Console", 14, GuiControlProfile::sFontCacheDirectory);
+ mpFont = GFont::create("Lucida Console", 10, GuiControlProfile::sFontCacheDirectory);
mFontSize=14;
mFontName=StringTable->insert("Lucida Console");
}
-fxTextObject2D::~fxTextObject2D(void)
+t2dTextObject::~t2dTextObject(void)
{
//mProfile=NULL;
}
-ConsoleMethod(fxTextObject2D, setFontColor, void, 3, 3, "(R/G/B/A) - Sets the font color.")
+ConsoleMethod(t2dTextObject, setFontColor, void, 3, 3, "(R/G/B/A) - Sets the font color.")
{
// Check Parameters.
- if ( fxSceneObject2D::getStringElementCount(argv[2])<4 )
+ if ( t2dSceneObject::getStringElementCount ( argv[2]) < 4 )
{
- Con::warnf("fxTextObject2D::setFontColor - Color should be in this format: \"R G B A\".", argv[2]);
+ Con::warnf("t2dTextObject::setFontColor - Color should be in this format: \"R G B A\".", argv[2]);
return;
}
// Set Color, RGBA
- object->setFontColor( ColorI(dAtoi(fxSceneObject2D::getStringElement(argv[2],0)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],1)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],2)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],3)))
+ object->setFontColor (ColorI (dAtoi (t2dSceneObject::getStringElement (argv[2],0)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],1)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],2)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],3)))
);
}
-void fxTextObject2D::setFontColor(const ColorI& fontColor)
+void t2dTextObject::setFontColor(const ColorI& fontColor)
{
-
mFontColor=fontColor;
-
}
-ConsoleMethod(fxTextObject2D, setBorderColor, void, 3, 3, "(R/G/B/A) - Sets the border color (set alpha to 0 to disable).")
+ConsoleMethod(t2dTextObject, setBorderColor, void, 3, 3, "(R/G/B/A) - Sets the border color (set alpha to 0 to disable).")
{
// Check Parameters.
- if ( fxSceneObject2D::getStringElementCount(argv[2])<4 )
+ if ( t2dSceneObject::getStringElementCount ( argv[2]) < 4 )
{
- Con::warnf("fxTextObject2D::setBorderColor - Color should be in this format: \"R G B A\".", argv[2]);
+ Con::warnf("t2dTextObject::setBorderColor - Color should be in this format: \"R G B A\".", argv[2]);
return;
}
// Set Color, RGBA
- object->setBorderColor( ColorI(dAtoi(fxSceneObject2D::getStringElement(argv[2],0)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],1)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],2)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],3)))
+ object->setBorderColor (ColorI (dAtoi (t2dSceneObject::getStringElement (argv[2],0)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],1)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],2)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],3)))
);
}
-void fxTextObject2D::setBorderColor(const ColorI& borderColor)
+void t2dTextObject::setBorderColor(const ColorI& borderColor)
{
-
mBorderColor=borderColor;
-
}
-ConsoleMethod(fxTextObject2D, setBannerColor, void, 3, 3, "(R/G/B/A) - Sets the banner color (set alpha to 0 to disable).")
+ConsoleMethod(t2dTextObject, setBannerColor, void, 3, 3, "(R/G/B/A) - Sets the banner color (set alpha to 0 to disable).")
{
// Check Parameters.
- if ( fxSceneObject2D::getStringElementCount(argv[2])<4 )
+ if ( t2dSceneObject::getStringElementCount ( argv[2]) < 4 )
{
- Con::warnf("fxTextObject2D::setBannerColor - Color should be in this format: \"R G B A\".", argv[2]);
+ Con::warnf("t2dTextObject::setBannerColor - Color should be in this format: \"R G B A\".", argv[2]);
return;
}
// Set Color, RGBA
- object->setBannerColor( ColorI(dAtoi(fxSceneObject2D::getStringElement(argv[2],0)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],1)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],2)),
- dAtoi(fxSceneObject2D::getStringElement(argv[2],3)))
+ object->setBannerColor (ColorI (dAtoi (t2dSceneObject::getStringElement (argv[2],0)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],1)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],2)),
+ dAtoi(t2dSceneObject::getStringElement(argv[2],3)))
);
}
-void fxTextObject2D::setBannerColor(const ColorI& bannerColor)
+void t2dTextObject::setBannerColor(const ColorI& bannerColor)
{
-
mBannerColor=bannerColor;
-
}
-ConsoleMethod(fxTextObject2D, setFont, void, 3, 3, "(name) - Sets the font.")
+ConsoleMethod(t2dTextObject, setFont, void, 3, 3, "(name) - Sets the font.")
{
// Set Text
object->setFont( argv[2]);
}
-void fxTextObject2D::setFont(const char *fontName)
+void t2dTextObject::setFont(const char *fontName)
{
mFontName=StringTable->insert(fontName);
mpFont = GFont::create(mFontName, mFontSize, GuiControlProfile::sFontCacheDirectory);
updateSize();
-
}
-ConsoleMethod(fxTextObject2D, setSize, void, 3, 3, "(name) - Sets the font size.")
+ConsoleMethod(t2dTextObject, setSize, void, 3, 3, "(name) - Sets the font size.")
{
// Set Text
object->setSize( dAtoi( argv[2]));
-
}
-void fxTextObject2D::setSize(U32 fontSize)
+void t2dTextObject::setSize(U32 fontSize)
{
mFontSize = fontSize;
mpFont = GFont::create(mFontName, mFontSize, GuiControlProfile::sFontCacheDirectory);
updateSize();
-
}
-
-ConsoleMethod(fxTextObject2D, setText, void, 3, 3, "(text) - Sets the string to display.")
+ConsoleMethod(t2dTextObject, setText, void, 3, 3, "(text) - Sets the string to display.")
{
// Set Text
object->setText( argv[2]);
}
-void fxTextObject2D::setText( const char *text)
+void t2dTextObject::setText( const char *text)
{
mTextString = StringTable->insert( text );
updateSize();
-
}
-void fxTextObject2D::updateSize()
+void t2dTextObject::updateSize()
{
mSizeUpdateNeeded=true;
- fxSceneWindow2D *window = Parent::getSceneGraph()->getCurrentRenderWindow();
+ t2dSceneWindow *window = Parent::getSceneGraph()->getCurrentRenderWindow();
// we don't have a render window yet, this requres it so wait until we do
if(!window || !mTextString)
@@ -166,19 +155,18 @@
mStringFontSize.y=mpFont->getHeight();
// we need to convert the size from window to scene
- Point2F cameraScale=window->getCurrentCameraScale();
- Parent::setSize(fxVector2D(cameraScale.x*mStringFontSize.x,cameraScale.y*mStringFontSize.y ));
+ //Point2F cameraScale=window->getCurrentCameraScale();
+ Point2F cameraScale=window->getCurrentCamera().mSceneWindowScale;
+ Parent::setSize (t2dVector (cameraScale.x * mStringFontSize.x, cameraScale.y * mStringFontSize.y));
mSizeUpdateNeeded=false;
-
}
//-----------------------------------------------------------------------------
// Render Object.
//-----------------------------------------------------------------------------
-void fxTextObject2D::renderObject( const RectF& viewPort, const RectF& viewIntersection )
+void t2dTextObject::renderObject( const RectF& viewPort, const RectF& viewIntersection )
{
-
// Cannot render without valid text.
if ( !mTextString )
return;
@@ -186,13 +174,14 @@
//if(mSizeUpdateNeeded)
updateSize();
- fxSceneWindow2D *window = Parent::getSceneGraph()->getCurrentRenderWindow();
+ t2dSceneWindow *window = Parent::getSceneGraph()->getCurrentRenderWindow();
- Point2F cameraScale=window->getCurrentCameraScale();
+ //Point2F cameraScale=window->getCurrentCameraScale();
+ Point2F cameraScale=window->getCurrentCamera().mSceneWindowScale;
RectI updateRect=window->mScreenWindow.updateRect;
- fxVector2D objectLoc=Parent::getPosition();
- objectLoc=fxVector2D(objectLoc.mX/cameraScale.x,objectLoc.mY/cameraScale.y);
+ t2dVector objectLoc=Parent::getPosition();
+ objectLoc = t2dVector (objectLoc.mX/cameraScale.x, objectLoc.mY/cameraScale.y);
// save old viewport
RectI oldViewport;
@@ -253,26 +242,21 @@
// Call Parent.
Parent::renderObject( viewPort, viewIntersection ); // Always use for Debug Support!
-
}
//-----------------------------------------------------------------------------
// Initialise Persistent Fields.
//-----------------------------------------------------------------------------
-void fxTextObject2D::initPersistFields()
+void t2dTextObject::initPersistFields()
{
// Call Parent.
Parent::initPersistFields();
-
- // Datablock.
- //addField("config", TypeSimObjectPtr, Offset(mConfigDataBlock, fxScroller2D));
}
-
//-----------------------------------------------------------------------------
// OnRemove.
//-----------------------------------------------------------------------------
-void fxTextObject2D::onRemove()
+void t2dTextObject::onRemove()
{
// Call Parent.
Parent::onRemove();
@@ -281,7 +265,7 @@
//-----------------------------------------------------------------------------
// OnAdd.
//-----------------------------------------------------------------------------
-bool fxTextObject2D::onAdd()
+bool t2dTextObject::onAdd()
{
// Call Parent.
if(!Parent::onAdd())
@@ -293,12 +277,9 @@
//-----------------------------------------------------------------------------
// Integrate Object.
//-----------------------------------------------------------------------------
-void fxTextObject2D::integrateObject( F32 sceneTime, F32 elapsedTime, CDebugStats* pDebugStats )
+void t2dTextObject::integrateObject( F32 sceneTime, F32 elapsedTime, CDebugStats* pDebugStats )
{
- // no idea what this does. ;)
-
// Call Parent.
Parent::integrateObject( sceneTime, elapsedTime, pDebugStats );
-
}
Index: E:/jtech/T2D/engine/T2D/t2dTextObject.h
===================================================================
--- E:/jtech/T2D/engine/T2D/t2dTextObject.h (revision 30)
+++ E:/jtech/T2D/engine/T2D/t2dTextObject.h (revision 31)
@@ -3,19 +3,18 @@
// Copyright (C)2005 Greg Lincoln
//-----------------------------------------------------------------------------
-#ifndef _FXTEXTOBJECT2D_H_
-#define _FXTEXTOBJECT2D_H_
+#ifndef _T2DTEXTOBJECT_H_
+#define _T2DTEXTOBJECT_H_
-#ifndef _FXSCENEOBJECT2D_H_
-#include "../T2D/fxSceneObject2D.h"
+#ifndef _T2DSCENEOBJECT_H_
+#include "./t2dSceneObject.h"
#endif
-class fxTextObject2D :
- public fxSceneObject2D
+class t2dTextObject : public t2dSceneObject
{
private:
- typedef fxSceneObject2D Parent;
+ typedef t2dSceneObject Parent;
StringTableEntry mTextString;
Point2I mStringFontSize;
@@ -35,10 +34,10 @@
static void initPersistFields();
- fxTextObject2D(void);
- virtual ~fxTextObject2D(void);
+ t2dTextObject(void);
+ virtual ~t2dTextObject(void);
- virtual void integrateObject( F32 sceneTime, F32 elapsedTime, CDebugStats* pDebugStats );
+ virtual void integrateObject( const F32 sceneTime, const F32 elapsedTime, CDebugStats* pDebugStats );
virtual void renderObject( const RectF& viewPort, const RectF& viewIntersection );
void setText( const char *text);
@@ -51,7 +50,7 @@
void setBorderColor(const ColorI& boarderColor);
// Declare Console Object.
- DECLARE_CONOBJECT(fxTextObject2D);
+ DECLARE_CONOBJECT(t2dTextObject);
};
#endif // _FXTEXTOBJECT2D_H_
\ No newline at end of file
#13
I am using it w/ TGB Beta 2. I loaded a string onto the Fish demo, and noticed that the text is jumping around- there seems to be a bug with the routine "apply a location offset to stay in the correct place". Sorry I don't have a fix - just wondering if anyone else has run into this?
04/15/2006 (11:03 am)
Nice resource! The antialiased text is really nice .I am using it w/ TGB Beta 2. I loaded a string onto the Fish demo, and noticed that the text is jumping around- there seems to be a bug with the routine "apply a location offset to stay in the correct place". Sorry I don't have a fix - just wondering if anyone else has run into this?

Torque Owner Teck Lee Tan