guiHealthBarHud.cpp 6.9 KB

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