Jump to content

GuiSpeedTextHud - Text Speedometer


Recommended Posts

I made a text speed-o-meter based on the health text hud gui.

1: First up alter source/T3D/vehicles/vehicle.h so we can get some data returned for the speed hud. This is actual speed (eg: length of velocity) and throttle control. Throttle is pretty much just full power using the keyboard but can be incremented using a gamepad stick.


   //for returning values to the guiSpeedTextHud
   F32 getThrottle() { return mThrottle; }//yorks
   F32 getSpeed() { return getVelocity().len(); }//yorks


Next up we create our new cpp file for the speed text to dsipaly which will reference getSpeed and getThrottle.

Call it guiSpeedTextHud.cpp and then save it to your game's source folder and add an fps folder for it to go into.

Then add it to your solution in Visual Studio.

It gets the player, then what they are mounted to, and bails it it's not a vehicle, or proceeds to get the speed and throttle values.

// ----------------------------------------------------------------------------  
// A gui control that displays Speed as a numerical value.  
// ----------------------------------------------------------------------------  
#include "platform/platform.h"  
#include "gui/core/guiControl.h"  
#include "console/consoleTypes.h"  
#include "T3D/gameBase/gameConnection.h"
#include "T3D/shapeBase.h"
#include "T3D/rigidShape.h"
#include "T3D/vehicles/vehicle.h"
#include "gfx/gfxDrawUtil.h"  
class GuiSpeedTextHud : public GuiControl  
   typedef GuiControl Parent;  
   bool mShowFrame;  
   bool mShowFill;  
   bool mShowThrust;  
   //bool mShowTrueSpeed;  
   LinearColorF mFillColor;  
   LinearColorF mFrameColor;  
   LinearColorF mTextColor;  
   LinearColorF mWarnColor;  
   F32 mWarnLevel;  
   F32 mPulseThreshold;  
   S32 mPulseRate;  
   F32 mValue;
   void onRender(Point2I, const RectI &);  
   static void initPersistFields();  
   DECLARE_CATEGORY("Gui Game");  
   DECLARE_DESCRIPTION("Shows the Speed or speed level of the current\n"  
      "PlayerObjectType control object as a numerical text display.");  
// ----------------------------------------------------------------------------  
   "@brief Shows the Speed or Speed value of the current PlayerObjectType control object.\n\n"  
   "This gui can be configured to display either the Speed or speed value of the current Player Object. "  
   "It can use an alternate display color if the Speed or drops below a set value. "  
   "It can also be set to pulse if the Speed or speed drops below a set value. "  
   "This control only works if a server connection exists and it's control object "  
   "is a PlayerObjectType. If either of these requirements is false, the control is not rendered.\n\n"  
      "\n new GuiSpeedTextHud()"  
      "   fillColor = \"0.0 0.0 0.0 0.5\"; // Fills with a transparent black color\n"  
      "   frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"  
      "   textColor = \"0.0 1.0 0.0 1.0\" // Solid green text color\n"  
      "   warningColor = \"1.0 0.0 0.0 1.0\"; // Solid red color, used when damaged\n"  
      "   showFill = \"true\";\n"  
      "   showFrame = \"true\";\n"  
      "   showTrueValue = \"false\";\n"  
      "   showThrust = \"false\";\n"  
      "   warnThreshold = \"20\";\n"  
      "   pulseThreshold = \"10\";\n"  
      "   pulseRate = \"500\";\n"  
      "   profile = \"GuiBigTextProfile\";\n"  
   "@ingroup GuiGame\n"  
   mShowFrame = mShowFill = true;  
   mShowThrust = false;  
   //mShowTrueSpeed = false;  
   mFillColor.set(0, 0, 0, 0.5);  
   mFrameColor.set(1, 1, 1, 1);  
   mTextColor.set(0, 1, 0, 1);  
   mWarnColor.set(1, 0, 0, 1);  
   mWarnLevel = 20.0f;  
   mPulseThreshold = 10.0f;  
   mPulseRate = 0;  
   mValue = 0.2f;
void GuiSpeedTextHud::initPersistFields()  
   addField("fillColor", TypeColorF, Offset(mFillColor, GuiSpeedTextHud), "Color for the background of the control.");  
   addField("frameColor", TypeColorF, Offset(mFrameColor, GuiSpeedTextHud), "Color for the control's frame.");  
   addField("textColor", TypeColorF, Offset(mTextColor, GuiSpeedTextHud), "Color for the text on this control.");  
   addField("warningColor", TypeColorF, Offset(mWarnColor, GuiSpeedTextHud), "Color for the text when Speed is low.");    
   addField("showFill", TypeBool, Offset(mShowFill, GuiSpeedTextHud), "If true, draw the background.");  
   addField("showFrame", TypeBool, Offset(mShowFrame, GuiSpeedTextHud), "If true, draw the frame.");  
   //addField("showTrueValue", TypeBool, Offset(mShowTrueSpeed, GuiSpeedTextHud), "If true, we don't hardcode maxSpeed to 100.");  
   addField("showThrust", TypeBool, Offset(mShowThrust, GuiSpeedTextHud), "If true, display the speed value rather than the Speed value.");  
   addField("warnThreshold", TypeF32, Offset(mWarnLevel, GuiSpeedTextHud), "The Speed level at which to use the warningColor.");    
   addField("pulseThreshold", TypeF32, Offset(mPulseThreshold, GuiSpeedTextHud), "Speed level at which to begin pulsing.");  
   addField("pulseRate", TypeS32, Offset(mPulseRate, GuiSpeedTextHud), "Speed at which the control will pulse.");  
// ----------------------------------------------------------------------------  
void GuiSpeedTextHud::onRender(Point2I offset, const RectI &updateRect)  
   // Must have a connection and player control object  
   GameConnection* conn = GameConnection::getConnectionToServer();  
   if (!conn)  
   ShapeBase* control = dynamic_cast(conn->getControlObject());  
   if (!control || !(control->getTypeMask() & PlayerObjectType))
   //now get the vehicle we are mounted to if there is one, else quit
   Vehicle* cab = dynamic_cast(control->getObjectMount());
   if (!cab)
   if (mShowThrust)
      F32 thrust = cab->getThrottle(); //mThrottle is a float
      mValue = thrust * 100;//set it to 0-100%
      mValue = cab->getSpeed();
   GFXDrawUtil* drawUtil = GFX->getDrawUtil();
   // If enabled draw background first  
   if (mShowFill)  
      drawUtil->drawRectFill(updateRect, mFillColor.toColorI());
   // Prepare text and center it  
   S32 val = (S32)mValue;    
   char buf[256];    
   dSprintf(buf, sizeof(buf), "%d", val);    
   offset.x += (getBounds().extent.x - mProfile->mFont->getStrWidth((const UTF8 *)buf)) / 2;    
   offset.y += (getBounds().extent.y - mProfile->mFont->getHeight()) / 2;    
   LinearColorF tColor = mTextColor;   
   // If warning level is exceeded switch to warning color  
   if(mValue < mWarnLevel)   
      tColor = mWarnColor;    
      // If the pulseRate is set then pulse the text if Speed is below the threshold  
      if (mPulseRate != 0 && mValue < mPulseThreshold)   
         U32 time = Platform::getVirtualMilliseconds();  
         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);  
         tColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;  
   drawUtil->drawText(mProfile->mFont, offset, buf);    
   // If enabled draw the border last  
   if (mShowFrame)  
      drawUtil->drawRect(updateRect, mFrameColor.toColorI());


And that's it. You have to add it via the GUI Editor to the PlayGui.gui obviously, and it remains hidden until you mount a vehicle. Change the warning level in the editor to get it display green at smaller numbers, default is 10.


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...