Adding Warning Colour To HealthBarGUI

Scripts and codes that enhances or changes the GUI in the engine.
3 posts Page 1 of 1
Steve_Yorkshire
Posts: 296
Joined: Tue Feb 03, 2015 10:30 pm
 
  by Steve_Yorkshire » Sat Feb 14, 2015 5:28 pm
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.


https://www.youtube.com/watch?v=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);
}
Last edited by Steve_Yorkshire on Wed Feb 18, 2015 5:09 pm, edited 1 time in total.
Nils
Posts: 159
Joined: Thu Feb 05, 2015 3:32 am
 
  by Nils » Wed Feb 18, 2015 9:03 am
Nice @ Steve_Yorkshire Very Nice! :D :D :D
Steve_Yorkshire
Posts: 296
Joined: Tue Feb 03, 2015 10:30 pm
 
by Steve_Yorkshire » Wed Feb 18, 2015 5:08 pm
Incidentally, Choice Two seems the better option after much testing
3 posts Page 1 of 1

Who is online

Users browsing this forum: No registered users and 1 guest