Super Simple Text Speedometer for T3D
by Dan Keller · 06/06/2011 (2:52 pm) · 5 comments
Exactly what it sounds like. A text speedometer control.
To use:
To use:
- Create the file "guiTextSpeedometer.cpp" in T3D/vehicles.
- Insert all of the following code.
- Salt to taste, compile and enjoy.
#include "gui/core/guiControl.h"
#include "gui/controls/guiTextCtrl.h"
#include "T3D/gameBase/gameConnection.h"
#include "T3D/vehicles/vehicle.h"
#include "console/consoleTypes.h"
class GuiTextSpeedometerCtrl : public GuiTextCtrl
{
private:
typedef GuiTextCtrl Parent;
char txtBuf[1024];
F32 scaleFactor; ///< Number to multiply speed by
public:
GuiTextSpeedometerCtrl();
static void initPersistFields();
DECLARE_CONOBJECT(GuiTextSpeedometerCtrl);
DECLARE_CATEGORY( "Gui Text" );
DECLARE_DESCRIPTION( "A control that displays control object speed in text format." );
void onPreRender();
};
IMPLEMENT_CONOBJECT(GuiTextSpeedometerCtrl);
GuiTextSpeedometerCtrl::GuiTextSpeedometerCtrl()
{
scaleFactor = 2.23693629f; //MPH. would be 3.6 for kph
}
void GuiTextSpeedometerCtrl::initPersistFields()
{
addField("scaleFactor", TypeF32, Offset( scaleFactor, GuiTextSpeedometerCtrl ),
"Conversion factor to multiply meter / second speed by. Defaults to 2.23"
"(mph). For kph it would be 3.6" );
Parent::initPersistFields();
}
void GuiTextSpeedometerCtrl::onPreRender()
{
Parent::onPreRender();
setText();
// Must have a connection and player control object
GameConnection* conn = GameConnection::getConnectionToServer();
if (!conn)
return;
Vehicle* control = dynamic_cast<Vehicle*>(conn->getControlObject());
if (!control)
return;
// That number before the f controls how many decimal places are displayed
// It could be put into a variable using .* ie
// dSprintf(txtBuf, 1023, "%.*f", precision, control->getVelocity().len()*scaleFactor);
// (precision would have to be an int)
dSprintf(txtBuf, 1023, "%.1f", control->getVelocity().len()*scaleFactor);
setText(txtBuf);
}About the author
#2
06/08/2011 (8:03 pm)
You might not have a guiProfile set on the speedometer control. That's just a guess though.
#3
06/09/2011 (10:32 am)
Sry, but i really don't find out. I did set the speedometer control in the player gui. But it don't works. Could you may give a example for coding noobs like me :) Also the stock speedometer don't work. When i set a speedometer picture in the stock, i don't see the picture. Im sure, i do some thing wrong.
#4
02/13/2012 (6:56 am)
worked fine, thanks Dan
#5
02/11/2013 (11:46 am)
Anyone care to share how to implement this? Do i need to schedule a speed calculation or does it automatically read/update as the control object moves??? 
Torque Owner D Fasel
Game 3D
Maybe i miss some thing?, and how to set this in game?