guiHealthBarHud.cpp 7.3 KB

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