clang: trailing else
[Torque-3d.git] / Engine / source / T3D / fps / guiHealthBarHud.cpp
bloba402e23d19abd35aa884513b2bcee5109cf4d14c
1 //-----------------------------------------------------------------------------
2 // Copyright (c) 2012 GarageGames, LLC
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
21 //-----------------------------------------------------------------------------
23 #include "platform/platform.h"
25 #include "gui/core/guiControl.h"
26 #include "console/consoleTypes.h"
27 #include "T3D/gameBase/gameConnection.h"
28 #include "T3D/shapeBase.h"
29 #include "gfx/gfxDrawUtil.h"
32 //-----------------------------------------------------------------------------
33 /// A basic health bar control.
34 /// This gui displays the damage value of the current PlayerObjectType
35 /// control object. The gui can be set to pulse if the health value
36 /// drops below a set value. This control only works if a server
37 /// connection exists and it's control object is a PlayerObjectType. If
38 /// either of these requirements is false, the control is not rendered.
39 class GuiHealthBarHud : public GuiControl
41 typedef GuiControl Parent;
43 bool mShowFrame;
44 bool mShowFill;
45 bool mDisplayEnergy;
47 ColorF mFillColor;
48 ColorF mFrameColor;
49 ColorF mDamageFillColor;
51 S32 mPulseRate;
52 F32 mPulseThreshold;
54 F32 mValue;
56 public:
57 GuiHealthBarHud();
59 void onRender( Point2I, const RectI &);
60 static void initPersistFields();
61 DECLARE_CONOBJECT( GuiHealthBarHud );
62 DECLARE_CATEGORY( "Gui Game" );
63 DECLARE_DESCRIPTION( "A basic health bar. Shows the damage value of the current\n"
64 "PlayerObjectType control object." );
68 //-----------------------------------------------------------------------------
70 IMPLEMENT_CONOBJECT( GuiHealthBarHud );
72 ConsoleDocClass( GuiHealthBarHud,
73 "@brief A basic health bar. Shows the damage value of the current PlayerObjectType control object.\n\n"
74 "This gui displays the damage value of the current PlayerObjectType control object. "
75 "The gui can be set to pulse if the health value 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"
79 "@tsexample\n"
80 "\n new GuiHealthBarHud()"
81 "{\n"
82 " fillColor = \"0.0 1.0 0.0 1.0\"; // Fills with a solid green color\n"
83 " frameColor = \"1.0 1.0 1.0 1.0\"; // Solid white frame color\n"
84 " damageFillColor = \"1.0 0.0 0.0 1.0\"; // Fills with a solid red color\n"
85 " pulseRate = \"500\";\n"
86 " pulseThreshold = \"0.25\";\n"
87 " showFill = \"true\";\n"
88 " showFrame = \"true\";\n"
89 " displayEnergy = \"false\";\n"
90 "};\n"
91 "@endtsexample\n\n"
93 "@ingroup GuiGame\n"
97 GuiHealthBarHud::GuiHealthBarHud()
99 mShowFrame = mShowFill = true;
100 mDisplayEnergy = false;
101 mFillColor.set(0, 0, 0, 0.5);
102 mFrameColor.set(0, 1, 0, 1);
103 mDamageFillColor.set(0, 1, 0, 1);
105 mPulseRate = 0;
106 mPulseThreshold = 0.3f;
107 mValue = 0.2f;
110 void GuiHealthBarHud::initPersistFields()
112 addGroup("Colors");
113 addField( "fillColor", TypeColorF, Offset( mFillColor, GuiHealthBarHud ), "Standard color for the background of the control." );
114 addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiHealthBarHud ), "Color for the control's frame." );
115 addField( "damageFillColor", TypeColorF, Offset( mDamageFillColor, GuiHealthBarHud ), "As the health bar depletes, this color will represent the health loss amount." );
116 endGroup("Colors");
118 addGroup("Pulse");
119 addField( "pulseRate", TypeS32, Offset( mPulseRate, GuiHealthBarHud ), "Speed at which the control will pulse." );
120 addField( "pulseThreshold", TypeF32, Offset( mPulseThreshold, GuiHealthBarHud ), "Health level the control must be under before the control will pulse." );
121 endGroup("Pulse");
123 addGroup("Misc");
124 addField( "showFill", TypeBool, Offset( mShowFill, GuiHealthBarHud ), "If true, we draw the background color of the control." );
125 addField( "showFrame", TypeBool, Offset( mShowFrame, GuiHealthBarHud ), "If true, we draw the frame of the control." );
126 addField( "displayEnergy", TypeBool, Offset( mDisplayEnergy, GuiHealthBarHud ), "If true, display the energy value rather than the damage value." );
127 endGroup("Misc");
129 Parent::initPersistFields();
133 //-----------------------------------------------------------------------------
135 Gui onRender method.
136 Renders a health bar with filled background and border.
138 void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
140 // Must have a connection and player control object
141 GameConnection* conn = GameConnection::getConnectionToServer();
142 if (!conn)
143 return;
144 ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());
145 if (!control || !(control->getTypeMask() & PlayerObjectType))
146 return;
148 if(mDisplayEnergy)
150 mValue = control->getEnergyValue();
152 else
154 // We'll just grab the damage right off the control object.
155 // Damage value 0 = no damage.
156 mValue = 1 - control->getDamageValue();
160 // Background first
161 if (mShowFill)
162 GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
164 // Pulse the damage fill if it's below the threshold
165 if (mPulseRate != 0)
167 if (mValue < mPulseThreshold)
169 U32 time = Platform::getVirtualMilliseconds();
170 F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
171 mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
173 else
174 mDamageFillColor.alpha = 1;
176 // Render damage fill %
177 RectI rect(updateRect);
178 if(getWidth() > getHeight())
179 rect.extent.x = (S32)(rect.extent.x * mValue);
180 else
182 S32 bottomY = rect.point.y + rect.extent.y;
183 rect.extent.y = (S32)(rect.extent.y * mValue);
184 rect.point.y = bottomY - rect.extent.y;
186 GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor);
188 // Border last
189 if (mShowFrame)
190 GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);