Torque3D Documentation / _generateds / guiHealthTextHud.cpp

guiHealthTextHud.cpp

Engine/source/T3D/fps/guiHealthTextHud.cpp

More...

Classes:

Public Functions

ConsoleDocClass(GuiHealthTextHud , "@brief Shows the health or energy <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of the current <a href="/coding/file/objecttypes_8h/#objecttypes_8h_1ac18d4a11e9446825e48fd474d7529084aabc4c763464f732b143d331ec4c15dce">PlayerObjectType</a> <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n\n</a>" "This gui can be configured <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display either the health or energy <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of the current <a href="/coding/class/classplayer/">Player</a> Object. " "It can use an alternate display color <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the health or drops below <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> set value. " "It can also be set <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> pulse <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the health or energy drops below <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> set value. " "This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> only works <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> server connection exists and it's <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object " "is <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> PlayerObjectType. If either of these requirements is false, the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is not <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendered.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a> <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguihealthtexthud/">GuiHealthTextHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " fillColor=\"0.0 0.0 0.0 0.5\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> transparent black <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   textColor = \"0.0 1.0 0.0 1.0\" // Solid green text <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   warningColor = \"1.0 0.0 0.0 1.0\"; // Solid red color, used when <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">damaged\n</a>" "   showFill = \"true\";\n" "   showFrame = \"true\";\n" "   showTrueValue = \"false\";\n" "   showEnergy = \"false\";\n" "   warnThreshold = \"50\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   pulseThreshold = \"25\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   pulseRate = \"500\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   profile = \"GuiBigTextProfile\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiGame\n</a>" )

Detailed Description

Public Functions

ConsoleDocClass(GuiHealthTextHud , "@brief Shows the health or energy <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of the current <a href="/coding/file/objecttypes_8h/#objecttypes_8h_1ac18d4a11e9446825e48fd474d7529084aabc4c763464f732b143d331ec4c15dce">PlayerObjectType</a> <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">object.\n\n</a>" "This gui can be configured <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> display either the health or energy <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> of the current <a href="/coding/class/classplayer/">Player</a> Object. " "It can use an alternate display color <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the health or drops below <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> set value. " "It can also be set <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> pulse <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> the health or energy drops below <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> set value. " "This <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> only works <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> server connection exists and it's <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> object " "is <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> PlayerObjectType. If either of these requirements is false, the <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> is not <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">rendered.\n\n</a>" " @<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a> <a href="/coding/file/tmm__on_8h/#tmm__on_8h_1a1ac41480eb2e4aadd52252ee550b630a">new</a> <a href="/coding/class/classguihealthtexthud/">GuiHealthTextHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " fillColor=\"0.0 0.0 0.0 0.5\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> transparent black <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   textColor = \"0.0 1.0 0.0 1.0\" // Solid green text <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "   warningColor = \"1.0 0.0 0.0 1.0\"; // Solid red color, used when <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">damaged\n</a>" "   showFill = \"true\";\n" "   showFrame = \"true\";\n" "   showTrueValue = \"false\";\n" "   showEnergy = \"false\";\n" "   warnThreshold = \"50\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   pulseThreshold = \"25\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   pulseRate = \"500\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "   profile = \"GuiBigTextProfile\";\n" "};\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">GuiGame\n</a>" )

IMPLEMENT_CONOBJECT(GuiHealthTextHud )

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24// ----------------------------------------------------------------------------  
 25// A gui control that displays health or energy information.  Based upon the   
 26// stock healthBar control but rewritten by M.Hall to display a numerical value.  
 27// ----------------------------------------------------------------------------  
 28  
 29#include "platform/platform.h"  
 30#include "gui/core/guiControl.h"  
 31#include "console/consoleTypes.h"  
 32#include "T3D/gameBase/gameConnection.h"  
 33#include "T3D/shapeBase.h"  
 34#include "gfx/gfxDrawUtil.h"  
 35  
 36class GuiHealthTextHud : public GuiControl  
 37{  
 38   typedef GuiControl Parent;  
 39  
 40   bool mShowFrame;  
 41   bool mShowFill;  
 42   bool mShowEnergy;  
 43   bool mShowTrueHealth;  
 44  
 45   LinearColorF mFillColor;  
 46   LinearColorF mFrameColor;  
 47   LinearColorF mTextColor;  
 48   LinearColorF mWarnColor;  
 49  
 50   F32 mWarnLevel;  
 51   F32 mPulseThreshold;  
 52   S32 mPulseRate;  
 53  
 54   F32 mValue;  
 55  
 56public:  
 57   GuiHealthTextHud();  
 58  
 59   void onRender(Point2I, const RectI &);  
 60   static void initPersistFields();  
 61   DECLARE_CONOBJECT(GuiHealthTextHud);  
 62   DECLARE_CATEGORY("Gui Game");  
 63   DECLARE_DESCRIPTION("Shows the damage or energy level of the current\n"  
 64      "PlayerObjectType control object as a numerical text display.");  
 65};  
 66  
 67// ----------------------------------------------------------------------------  
 68  
 69IMPLEMENT_CONOBJECT(GuiHealthTextHud);  
 70  
 71ConsoleDocClass(GuiHealthTextHud,  
 72   "@brief Shows the health or energy value of the current PlayerObjectType control object.\n\n"  
 73   "This gui can be configured to display either the health or energy value of the current Player Object. "  
 74   "It can use an alternate display color if the health or drops below a set value. "  
 75   "It can also be set to pulse if the health or energy drops below a set value. "  
 76   "This control only works if a server connection exists and it's control object "  
 77   "is a PlayerObjectType. If either of these requirements is false, the control is not rendered.\n\n"  
 78  
 79   "@tsexample\n"  
 80      "\n new GuiHealthTextHud()"  
 81      "{\n"  
 82      "   fillColor = \"0.0 0.0 0.0 0.5\"; // Fills with a transparent black color\n"  
 83      "   frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"  
 84      "   textColor = \"0.0 1.0 0.0 1.0\" // Solid green text color\n"  
 85      "   warningColor = \"1.0 0.0 0.0 1.0\"; // Solid red color, used when damaged\n"  
 86      "   showFill = \"true\";\n"  
 87      "   showFrame = \"true\";\n"  
 88      "   showTrueValue = \"false\";\n"  
 89      "   showEnergy = \"false\";\n"  
 90      "   warnThreshold = \"50\";\n"  
 91      "   pulseThreshold = \"25\";\n"  
 92      "   pulseRate = \"500\";\n"  
 93      "   profile = \"GuiBigTextProfile\";\n"  
 94      "};\n"  
 95   "@endtsexample\n\n"  
 96  
 97   "@ingroup GuiGame\n"  
 98);  
 99  
100GuiHealthTextHud::GuiHealthTextHud()  
101{  
102   mShowFrame = mShowFill = true;  
103   mShowEnergy = false;  
104   mShowTrueHealth = false;  
105  
106   mFillColor.set(0, 0, 0, 0.5);  
107   mFrameColor.set(1, 1, 1, 1);  
108   mTextColor.set(0, 1, 0, 1);  
109   mWarnColor.set(1, 0, 0, 1);  
110  
111   mWarnLevel = 50.0f;  
112   mPulseThreshold = 25.0f;  
113   mPulseRate = 0;  
114     
115   mValue = 0.2f;  
116}  
117  
118void GuiHealthTextHud::initPersistFields()  
119{  
120   addGroup("Colors");       
121   addField("fillColor", TypeColorF, Offset(mFillColor, GuiHealthTextHud), "Color for the background of the control.");  
122   addField("frameColor", TypeColorF, Offset(mFrameColor, GuiHealthTextHud), "Color for the control's frame.");  
123   addField("textColor", TypeColorF, Offset(mTextColor, GuiHealthTextHud), "Color for the text on this control.");  
124   addField("warningColor", TypeColorF, Offset(mWarnColor, GuiHealthTextHud), "Color for the text when health is low.");    
125   endGroup("Colors");          
126  
127   addGroup("View");      
128   addField("showFill", TypeBool, Offset(mShowFill, GuiHealthTextHud), "If true, draw the background.");  
129   addField("showFrame", TypeBool, Offset(mShowFrame, GuiHealthTextHud), "If true, draw the frame.");  
130   addField("showTrueValue", TypeBool, Offset(mShowTrueHealth, GuiHealthTextHud), "If true, we don't hardcode maxHealth to 100.");  
131   addField("showEnergy", TypeBool, Offset(mShowEnergy, GuiHealthTextHud), "If true, display the energy value rather than the damage value.");  
132   endGroup("View");    
133  
134   addGroup("Alert");  
135   addField("warnThreshold", TypeF32, Offset(mWarnLevel, GuiHealthTextHud), "The health level at which to use the warningColor.");    
136   addField("pulseThreshold", TypeF32, Offset(mPulseThreshold, GuiHealthTextHud), "Health level at which to begin pulsing.");  
137   addField("pulseRate", TypeS32, Offset(mPulseRate, GuiHealthTextHud), "Speed at which the control will pulse.");  
138   endGroup("Alert");  
139  
140   Parent::initPersistFields();  
141}  
142  
143// ----------------------------------------------------------------------------  
144  
145void GuiHealthTextHud::onRender(Point2I offset, const RectI &updateRect)  
146{  
147   // Must have a connection and player control object  
148   GameConnection* conn = GameConnection::getConnectionToServer();  
149   if (!conn)  
150      return;  
151   ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());  
152   if (!control || !(control->getTypeMask() & PlayerObjectType))  
153      return;  
154  
155   // Just grab the damage/energy right off the control object.    
156   // Damage value 0 = no damage (full health).    
157   if(mShowEnergy)    
158      mValue = control->getEnergyLevel();  
159   else    
160   {  
161      if (mShowTrueHealth)  
162         mValue = control->getMaxDamage() - control->getDamageLevel();  
163      else  
164         mValue = 100 - (100 * control->getDamageValue());    
165   }  
166
167   GFXDrawUtil* drawUtil = GFX->getDrawUtil();
168  
169   // If enabled draw background first  
170   if (mShowFill)  
171      drawUtil->drawRectFill(updateRect, mFillColor.toColorI());
172  
173   // Prepare text and center it  
174   S32 val = (S32)mValue;    
175   char buf[256];    
176   dSprintf(buf, sizeof(buf), "%d", val);    
177   offset.x += (getBounds().extent.x - mProfile->mFont->getStrWidth((const UTF8 *)buf)) / 2;    
178   offset.y += (getBounds().extent.y - mProfile->mFont->getHeight()) / 2;    
179  
180   LinearColorF tColor = mTextColor;   
181  
182   // If warning level is exceeded switch to warning color  
183   if(mValue < mWarnLevel)   
184   {  
185      tColor = mWarnColor;    
186  
187      // If the pulseRate is set then pulse the text if health is below the threshold  
188      if (mPulseRate != 0 && mValue < mPulseThreshold)   
189      {  
190         U32 time = Platform::getVirtualMilliseconds();  
191         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);  
192         tColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;  
193      }  
194   }  
195  
196   drawUtil->setBitmapModulation(tColor.toColorI());
197   drawUtil->drawText(mProfile->mFont, offset, buf);    
198   drawUtil->clearBitmapModulation();    
199  
200   // If enabled draw the border last  
201   if (mShowFrame)  
202      drawUtil->drawRect(updateRect, mFrameColor.toColorI());
203}  
204