Torque3D Documentation / _generateds / guiHealthBarHud.cpp

guiHealthBarHud.cpp

Engine/source/T3D/fps/guiHealthBarHud.cpp

More...

Classes:

class

A basic health bar control.

Public Functions

ConsoleDocClass(GuiHealthBarHud , "@brief A basic health bar. Shows the damage <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 displays the damage <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> object. " "The gui can 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 <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> 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/classguihealthbarhud/">GuiHealthBarHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " fillColor=\"0.0 1.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid green <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>" "	damageFillColor = \"1.0 0.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid red <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "	pulseRate = \"500\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "	pulseThreshold = \"0.25\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "	showFill = \"true\";\n" "	showFrame = \"true\";\n" "	displayEnergy = \"false\";\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(GuiHealthBarHud , "@brief A basic health bar. Shows the damage <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 displays the damage <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> object. " "The gui can 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 <a href="/coding/file/pointer_8h/#pointer_8h_1a32aff7c6c4cd253fdf6563677afab5ce">value</a> 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/classguihealthbarhud/">GuiHealthBarHud</a>()" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " fillColor=\"0.0 1.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid green <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>" "	damageFillColor = \"1.0 0.0 0.0 1.0\"; // Fills with <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> solid red <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">color\n</a>" "	pulseRate = \"500\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "	pulseThreshold = \"0.25\";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "	showFill = \"true\";\n" "	showFrame = \"true\";\n" "	displayEnergy = \"false\";\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(GuiHealthBarHud )

  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#include "platform/platform.h"
 25
 26#include "gui/core/guiControl.h"
 27#include "console/consoleTypes.h"
 28#include "T3D/gameBase/gameConnection.h"
 29#include "T3D/shapeBase.h"
 30#include "gfx/gfxDrawUtil.h"
 31
 32
 33//-----------------------------------------------------------------------------
 34/// A basic health bar control.
 35/// This gui displays the damage value of the current PlayerObjectType
 36/// control object.  The gui can be set to pulse if the health value
 37/// drops below a set value. This control only works if a server
 38/// connection exists and it's control object is a PlayerObjectType. If
 39/// either of these requirements is false, the control is not rendered.
 40class GuiHealthBarHud : public GuiControl
 41{
 42   typedef GuiControl Parent;
 43
 44   bool     mShowFrame;
 45   bool     mShowFill;
 46   bool     mDisplayEnergy;
 47   bool     mFlip;
 48
 49   LinearColorF   mFillColor;
 50   LinearColorF   mFrameColor;
 51   LinearColorF   mDamageFillColor;
 52
 53   S32      mPulseRate;
 54   F32      mPulseThreshold;
 55
 56   F32      mValue;
 57
 58public:
 59   GuiHealthBarHud();
 60
 61   void onRender( Point2I, const RectI &);
 62   static void initPersistFields();
 63   DECLARE_CONOBJECT( GuiHealthBarHud );
 64   DECLARE_CATEGORY( "Gui Game" );
 65   DECLARE_DESCRIPTION( "A basic health bar. Shows the damage value of the current\n"
 66      "PlayerObjectType control object." );
 67};
 68
 69
 70//-----------------------------------------------------------------------------
 71
 72IMPLEMENT_CONOBJECT( GuiHealthBarHud );
 73
 74ConsoleDocClass( GuiHealthBarHud,
 75   "@brief A basic health bar. Shows the damage value of the current PlayerObjectType control object.\n\n"
 76   "This gui displays the damage value of the current PlayerObjectType control object. "
 77   "The gui can be set to pulse if the health value drops below a set value. "
 78   "This control only works if a server connection exists and it's control object "
 79   "is a PlayerObjectType. If either of these requirements is false, the control is not rendered.\n\n"
 80   
 81   "@tsexample\n"
 82      "\n new GuiHealthBarHud()"
 83      "{\n"
 84      "  fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
 85      "  frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
 86      "  damageFillColor = \"1.0 0.0 0.0 1.0\"; // Fills with a solid red color\n"
 87      "  pulseRate = \"500\";\n"
 88      "  pulseThreshold = \"0.25\";\n"
 89      "  showFill = \"true\";\n"
 90      "  showFrame = \"true\";\n"
 91      "  displayEnergy = \"false\";\n"
 92      "};\n"
 93   "@endtsexample\n\n"
 94   
 95   "@ingroup GuiGame\n"
 96);
 97
 98
 99GuiHealthBarHud::GuiHealthBarHud()
100{
101   mShowFrame = mShowFill = true;
102   mDisplayEnergy = false;
103   mFillColor.set(0, 0, 0, 0.5);
104   mFrameColor.set(0, 1, 0, 1);
105   mDamageFillColor.set(0, 1, 0, 1);
106
107   mPulseRate = 0;
108   mPulseThreshold = 0.3f;
109   mValue = 0.2f;
110
111   mFlip = false;
112}
113
114void GuiHealthBarHud::initPersistFields()
115{
116   addGroup("Colors");     
117   addField( "fillColor", TypeColorF, Offset( mFillColor, GuiHealthBarHud ), "Standard color for the background of the control." );
118   addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiHealthBarHud ), "Color for the control's frame." );
119   addField( "damageFillColor", TypeColorF, Offset( mDamageFillColor, GuiHealthBarHud ), "As the health bar depletes, this color will represent the health loss amount." );
120   endGroup("Colors");     
121
122   addGroup("Pulse");      
123   addField( "pulseRate", TypeS32, Offset( mPulseRate, GuiHealthBarHud ), "Speed at which the control will pulse." );
124   addField( "pulseThreshold", TypeF32, Offset( mPulseThreshold, GuiHealthBarHud ), "Health level the control must be under before the control will pulse." );
125   endGroup("Pulse");      
126
127   addGroup("Misc");    
128   addField( "showFill", TypeBool, Offset( mShowFill, GuiHealthBarHud ), "If true, we draw the background color of the control." );
129   addField( "showFrame", TypeBool, Offset( mShowFrame, GuiHealthBarHud ), "If true, we draw the frame of the control." );
130   addField( "displayEnergy", TypeBool, Offset( mDisplayEnergy, GuiHealthBarHud ), "If true, display the energy value rather than the damage value." );
131   addField(  "flip", TypeBool, Offset( mFlip, GuiHealthBarHud), "If true, will fill bar in opposite direction.");
132   endGroup("Misc");
133
134   Parent::initPersistFields();
135}
136
137
138//-----------------------------------------------------------------------------
139/**
140   Gui onRender method.
141   Renders a health bar with filled background and border.
142*/
143void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
144{
145   // Must have a connection and player control object
146   GameConnection* conn = GameConnection::getConnectionToServer();
147   if (!conn)
148      return;
149   ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());
150   if (!control || !(control->getTypeMask() & PlayerObjectType))
151      return;
152
153   if(mDisplayEnergy)
154   {
155      mValue = control->getEnergyValue();
156   }
157   else
158   {
159      // We'll just grab the damage right off the control object.
160      // Damage value 0 = no damage.
161      mValue = 1 - control->getDamageValue();
162   }
163
164
165   // Background first
166   if (mShowFill)
167      GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor.toColorI());
168
169   // Pulse the damage fill if it's below the threshold
170   if (mPulseRate != 0)
171   {
172      if (mValue < mPulseThreshold) 
173      {
174         U32 time = Platform::getVirtualMilliseconds();
175         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
176         mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
177      }
178      else
179         mDamageFillColor.alpha = 1;
180   }
181   // Render damage fill %
182   RectI rect(updateRect);
183   if(getWidth() > getHeight())
184   {
185      rect.extent.x = (S32)(rect.extent.x * mValue);
186
187      if(mFlip)
188         rect.point.x = (S32)(updateRect.point.x + (updateRect.extent.x - rect.extent.x));
189   }
190   else
191   {
192      S32 bottomY = rect.point.y + rect.extent.y;
193      rect.extent.y = (S32)(rect.extent.y * mValue);
194
195      if(mFlip)
196         rect.extent.y = (S32)(updateRect.extent.y - (updateRect.extent.y - rect.extent.y));
197      else
198         rect.point.y = bottomY - rect.extent.y;
199   }
200   GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor.toColorI());
201
202   // Border last
203   if (mShowFrame)
204      GFX->getDrawUtil()->drawRect(updateRect, mFrameColor.toColorI());
205}
206