Steve_Yorkshire Posted September 21, 2020 Share Posted September 21, 2020 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 DECLARE_CONOBJECT(Vehicle); }; #endif 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; public: GuiSpeedTextHud(); void onRender(Point2I, const RectI &); static void initPersistFields(); DECLARE_CONOBJECT(GuiSpeedTextHud); DECLARE_CATEGORY("Gui Game"); DECLARE_DESCRIPTION("Shows the Speed or speed level of the current\n" "PlayerObjectType control object as a numerical text display."); }; // ---------------------------------------------------------------------------- IMPLEMENT_CONOBJECT(GuiSpeedTextHud); ConsoleDocClass(GuiSpeedTextHud, "@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" "@tsexample\n" "\n new GuiSpeedTextHud()" "{\n" " 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" "};\n" "@endtsexample\n\n" "@ingroup GuiGame\n" ); GuiSpeedTextHud::GuiSpeedTextHud() { 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() { addGroup("Colors"); 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."); endGroup("Colors"); addGroup("View"); 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."); endGroup("View"); addGroup("Alert"); 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."); endGroup("Alert"); Parent::initPersistFields(); } // ---------------------------------------------------------------------------- void GuiSpeedTextHud::onRender(Point2I offset, const RectI &updateRect) { // Must have a connection and player control object GameConnection* conn = GameConnection::getConnectionToServer(); if (!conn) return; ShapeBase* control = dynamic_cast(conn->getControlObject()); if (!control || !(control->getTypeMask() & PlayerObjectType)) return; //now get the vehicle we are mounted to if there is one, else quit Vehicle* cab = dynamic_cast(control->getObjectMount()); if (!cab) return; if (mShowThrust) { F32 thrust = cab->getThrottle(); //mThrottle is a float mValue = thrust * 100;//set it to 0-100% } else 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->setBitmapModulation(tColor.toColorI()); drawUtil->drawText(mProfile->mFont, offset, buf); drawUtil->clearBitmapModulation(); // 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.