Jump to content

Adding Warning Colour To HealthBarGUI


Steve_Yorkshire

Recommended Posts

A noticed that HealthTextGui had both pulse and a warning colour, whilst HealthBarGui only had pulse. So I fancied changing that. It uses a decimal to determine the value when it will change colour (0.0 - 1.0).


I am using 2 seperate energy systems in the video, one linked to weapon energy (new style) - first bar top right and main bar at bottom of screen, and the other one is linked to sprint energy (old style) , 2nd bar at the top right.


dQXwYy6uEoQ



Engine/T3D/fps/guiHealthBarHud.cpp

class GuiHealthBarHud : public GuiControl
{
//...
 
   ColorF   mFillColor;
   ColorF   mFrameColor;
   ColorF   mDamageFillColor;
   ColorF   mWarnColor;//yorks new
 
   F32      mWarnLevel;//yorks new
   S32      mPulseRate;
   F32      mPulseThreshold;
 
//...
}
 
//...
 
//In the middle of the console doc class add:
ConsoleDocClass( GuiHealthBarHud,
//...
		"   warnThreshold = \"0.5\"; // yorks Warning colour\n"
//...
   "@ingroup GuiGame\n"
);
 
GuiHealthBarHud::GuiHealthBarHud()
{
//...
   mDamageFillColor.set(0, 1, 0, 1);
   mWarnColor.set(1, 0, 0, 1);//yorks new
 
   mWarnLevel = 0.5f;//yorks new
   mPulseRate = 0;
//...
}
 
void GuiHealthBarHud::initPersistFields()
{
   addGroup("Colors");		
//...
   addField("warningColor", TypeColorF, Offset(mWarnColor, GuiHealthBarHud), "Color for the text when Health is low.");//yorks new
   endGroup("Colors");		
 
   addGroup("Pulse");	
   addField("warnThreshold", TypeF32, Offset(mWarnLevel, GuiHealthBarHud), "The Health level - as a decimal - at which to use the warningColor.");//yorks new
   addField( "pulseRate", TypeS32, Offset( mPulseRate, GuiHealthBarHud ), "Speed at which the control will pulse." );
   addField( "pulseThreshold", TypeF32, Offset( mPulseThreshold, GuiHealthBarHud ), "Health level the control must be under before the control will pulse." );
   endGroup("Pulse");	
//...
}

 

And now you've two choices of code, both of each work. I am including both as I am not sure which is really better.

The first works like the original healthBar but calls value twice, the second works more like HealthTextGui using an alias/local var.


edit: 18 Feb 2015: after much testing Choice TWO seems to be the better option.


Choice ONE:

void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
{
/...
 
   // Background first
   if (mShowFill)
      GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
 
   /* //yorks out start
   // Pulse the health fill if it's below the threshold
   if (mPulseRate != 0)
      if (mValue < mPulseThreshold) 
      {
         U32 time = Platform::getVirtualMilliseconds();
         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
         mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
      }
      else
         mDamageFillColor.alpha = 1;
   */ //yorks out end
 
   if (mValue < mWarnLevel)//yorks in start
   {
	   if (mPulseRate != 0)
	   {
		   U32 time = Platform::getVirtualMilliseconds();
		   F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
		   mWarnColor.alpha = (alpha > 1.0f) ? 2.0f - alpha : alpha;
	   }
	   else
		   mWarnColor.alpha = 1;
   }
   else
	   mDamageFillColor.alpha = 1;//yorks in end
 
   // Render damage fill %
   RectI rect(updateRect);
   if(getWidth() > getHeight())
      rect.extent.x = (S32)(rect.extent.x * mValue);
   else
   {
      S32 bottomY = rect.point.y + rect.extent.y;
      rect.extent.y = (S32)(rect.extent.y * mValue);
      rect.point.y = bottomY - rect.extent.y;
   }//yorks in end
 
   if (mValue < mWarnLevel)//yorks new - second call :/
	GFX->getDrawUtil()->drawRectFill(rect, mWarnColor);//yorks new
   else//yorks new
	GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor);
 
   // Border last
   if (mShowFrame)
      GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
}

 

Choice TWO:

void GuiHealthBarHud::onRender(Point2I offset, const RectI &updateRect)
{
//...
   // Background first
   if (mShowFill)
      GFX->getDrawUtil()->drawRectFill(updateRect, mFillColor);
 
   /*
   // Pulse the health fill if it's below the threshold
   if (mPulseRate != 0)
      if (mValue < mPulseThreshold) 
      {
         U32 time = Platform::getVirtualMilliseconds();
         F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
         mDamageFillColor.alpha = (alpha > 1.0f)? 2.0f - alpha: alpha;
      }
      else
         mDamageFillColor.alpha = 1;
   */
 
   ///yorks in start
   ColorF tColor = mDamageFillColor;
 
   // If warning level is exceeded switch to warning color  
   if (mValue < mWarnLevel)
   {
	   tColor = mWarnColor;
 
	   // If the pulseRate is set then pulse the text if Health is below the threshold  
	   if (mPulseRate != 0 && mValue < mPulseThreshold)
	   {
		   U32 time = Platform::getVirtualMilliseconds();
		   F32 alpha = 2.0f * F32(time % mPulseRate) / F32(mPulseRate);
		   tColor.alpha = (alpha > 1.0f) ? 2.0f - alpha : alpha;
	   }
   }//yorks in end
 
   // Render damage fill %
   RectI rect(updateRect);
   if(getWidth() > getHeight())
      rect.extent.x = (S32)(rect.extent.x * mValue);
   else
   {
      S32 bottomY = rect.point.y + rect.extent.y;
      rect.extent.y = (S32)(rect.extent.y * mValue);
      rect.point.y = bottomY - rect.extent.y;
   }
 
   //GFX->getDrawUtil()->drawRectFill(rect, mDamageFillColor);//yorks out
   GFX->getDrawUtil()->drawRectFill(rect, tColor);//yorks in
 
   // Border last
   if (mShowFrame)
      GFX->getDrawUtil()->drawRect(updateRect, mFrameColor);
}
Edited by Steve_Yorkshire
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...