Advertisement
Guest User

Untitled

a guest
Feb 27th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  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.  
  23. #include "platform/platform.h"
  24.  
  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"
  30.  
  31.  
  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
  40. {
  41. typedef GuiControl Parent;
  42.  
  43. S32 mTargetId;
  44.  
  45. bool mShowFrame;
  46. bool mShowFill;
  47. bool mDisplayEnergy;
  48.  
  49. ColorF mFillColor;
  50. ColorF mFrameColor;
  51. ColorF mDamageFillColor;
  52.  
  53. S32 mPulseRate;
  54. F32 mPulseThreshold;
  55.  
  56. F32 mValue;
  57.  
  58. public:
  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.  
  72. IMPLEMENT_CONOBJECT( GuiHealthBarHud );
  73.  
  74. ConsoleDocClass( 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.  
  99. GuiHealthBarHud::GuiHealthBarHud()
  100. {
  101. mTargetId = -1;
  102. mShowFrame = mShowFill = true;
  103. mDisplayEnergy = false;
  104. mFillColor.set(0, 0, 0, 0.5);
  105. mFrameColor.set(0, 1, 0, 1);
  106. mDamageFillColor.set(0, 1, 0, 1);
  107.  
  108. mPulseRate = 0;
  109. mPulseThreshold = 0.3f;
  110. mValue = 0.2f;
  111. }
  112.  
  113. void GuiHealthBarHud::initPersistFields()
  114. {
  115.  
  116. addField("target", TypeS32, Offset(mTargetId, GuiHealthBarHud));
  117. addGroup("Colors");
  118. addField( "fillColor", TypeColorF, Offset( mFillColor, GuiHealthBarHud ), "Standard color for the background of the control." );
  119. addField( "frameColor", TypeColorF, Offset( mFrameColor, GuiHealthBarHud ), "Color for the control's frame." );
  120. addField( "damageFillColor", TypeColorF, Offset( mDamageFillColor, GuiHealthBarHud ), "As the health bar depletes, this color will represent the health loss amount." );
  121. endGroup("Colors");
  122.  
  123. addGroup("Pulse");
  124. addField( "pulseRate", TypeS32, Offset( mPulseRate, GuiHealthBarHud ), "Speed at which the control will pulse." );
  125. addField( "pulseThreshold", TypeF32, Offset( mPulseThreshold, GuiHealthBarHud ), "Health level the control must be under before the control will pulse." );
  126. endGroup("Pulse");
  127.  
  128. addGroup("Misc");
  129. addField( "showFill", TypeBool, Offset( mShowFill, GuiHealthBarHud ), "If true, we draw the background color of the control." );
  130. addField( "showFrame", TypeBool, Offset( mShowFrame, GuiHealthBarHud ), "If true, we draw the frame of the control." );
  131. addField( "displayEnergy", TypeBool, Offset( mDisplayEnergy, GuiHealthBarHud ), "If true, display the energy value rather than the damage value." );
  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. */
  143. void 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)||(mTargetId == 0))
  148. return;
  149. ShapeBase* control = NULL;
  150.  
  151. if(mTargetId != -1)
  152. {
  153. control = dynamic_cast<ShapeBase*>(conn->resolveGhost(mTargetId));
  154. }
  155. else
  156. control = dynamic_cast<ShapeBase*>(conn->getControlObject());
  157.  
  158. if (!control || !(control->getTypeMask() & ShapeBaseObjectType))
  159. return;
  160.  
  161. if(mDisplayEnergy)
  162. {
  163. mValue = control->getEnergyValue();
  164. }
  165. else
  166. {
  167. // We'll just grab the damage right off the control object.
  168. // Damage value 0 = no damage.
  169. mValue = 1 - control->getDamageValue();
  170. }
  171.  
  172.  
  173. // Background first
  174. if (mShowFill)
  175. GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
  176.  
  177. // Pulse the damage fill if it's below the threshold
  178. if (mPulseRate != 0)
  179. if (mValue < mPulseThreshold)
  180. {
  181. U32 time = Platform::getVirtualMilliseconds();
  182. F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
  183. mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
  184. }
  185. else
  186. mDamageFillColor.alpha = 1;
  187.  
  188. // Render damage fill %
  189. RectI rect(updateRect);
  190. if(getWidth() > getHeight())
  191. rect.extent.x = (S32)(rect.extent.x * mValue);
  192. else
  193. {
  194. S32 bottomY = rect.point.y + rect.extent.y;
  195. rect.extent.y = (S32)(rect.extent.y * mValue);
  196. rect.point.y = bottomY - rect.extent.y;
  197. }
  198. GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor);
  199.  
  200. // Border last
  201. if (mShowFrame)
  202. GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement