Hud AngleSplit

From Valve Developer Community
Revision as of 08:32, 14 June 2008 by Lodle (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This code is like the two image split for the ammo and health icons in ep1 (near the cross hair) but does it on an angle both on the left side and right side.

How it works

Angle split working.jpg

Result

Angle split.jpg

Code

This assumes you all ready have a hud panel set up and ready to use. First thing to do is we need to define the two images:

Header:

private:
	CHudTexture	*m_icon_rb;		// right bracket, full
	CHudTexture	*m_icon_lb;		// left bracket, full

Constructor:

	m_icon_lb = gHUD.GetIcon( "health_full" );
	m_icon_lbe = gHUD.GetIcon( "health_empty" );

The names here refer to the texture defined in mod_textures.txt

Once we have this we need to add some code to the paint function:

	int scaledW, scaledH, scaledDis, screenWide, screenTall, X1,Y1, X2, offset;

	offset = scheme()->GetProportionalScaledValue(100);
		
	//image width
	scaledW = scheme()->GetProportionalScaledValue(25);

	//image height
	scaledH = scheme()->GetProportionalScaledValue(100);

	//image indentation
	scaledDis = scheme()->GetProportionalScaledValue(200);

	GetHudSize(screenWide, screenTall);

	X = screenWide/2 - scaledDis - scaledW;
	Y = screenTall/2 - scaledH/2;

	m_icon_lbe->DrawScew_Left(X, Y, scaledW, scaledH, 1-HealthPer, true, offset);
	m_icon_lb->DrawScew_Left(X, Y, scaledW, scaledH, 1-HealthPer, false, offset);

This draws each element at pos X,Y with size scaledW, scaledH. Since these are health bars we have a health percent all ready defined. The true false refers to which one they are (true for empty image and false for full) and the offset is the between the edge and the point (see pict above).

We need to add the new functions into hud.cpp/hud.h
For the header add:

	void DrawScew_Left(int x, int y, int w, int h, float per, bool isTop, int offset) const;
	void DrawScew_Right(int x, int y, int w, int h, float per, bool isTop, int offset) const;

And the cpp file add:


void CHudTexture::DrawScew_Left(int tx, int ty, int tw, int th, float per, bool isTop, int offset) const
{
	per = max(0, min(1,per));

	vgui::surface()->DrawSetTexture( textureId );
	vgui::surface()->DrawSetColor( Color(125,125,125,125) );

	float x = (float)(tx);
	float y = (float)(ty);
	float w = (float)(tw);
	float h = (float)(th);

	if (!(per > 99 || per < 0))
	{
		float sy = y+h-(per*h);
		float sx = x+w;
		float qy = y+h/2;
		float ydis_sq=qy-sy;
		float ydis_ef = (offset+w)/offset*ydis_sq;
		float ex = x; 
		float ey = y+h/2-ydis_ef;
		float ratio_1 = (h/2.0-ydis_ef)/h;
		float ratio_2 = 1-per;

		if ( ey < y)
		{
			float q=x+w-(w*(sy-y))/(sy-ey);
			float ratio_3 = (q-x)/w;

			if (isTop)
			{
				vgui::Vertex_t points[3] =
				{
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y)), Vector2D(ratio_3,0)),
					vgui::Vertex_t( Vector2D((int)(x+w),	(int)(y)), Vector2D(1,0) ),
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(1,ratio_2) )
				};
				vgui::surface()->DrawTexturedPolygon( 3, points);
			}
			else
			{
				vgui::Vertex_t points[5] =
				{
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y)), Vector2D(0,0) ),		//top left
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y)), Vector2D(ratio_3,0) ),	//top right
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(1,ratio_2) ),	//bottom right
					vgui::Vertex_t( Vector2D((int)(w+x),		(int)(y+h)), Vector2D(1,1) ),		//bottom center
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y+h)), Vector2D(0,1) )		//bottom left
				};
				vgui::surface()->DrawTexturedPolygon( 5, points);
			}
		}
		else if ( ey > (y+h))
		{
			float o=ey-h-y;
			float q=x+(w*o/((y+h-sy)+o));
			float ratio_3=(q-x)/w;

			if (isTop)
			{
				vgui::Vertex_t points[5] =
				{
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y)), Vector2D(0,0) ),		//top left
					vgui::Vertex_t( Vector2D((int)(x+w),		(int)(y)), Vector2D(1,0) ),		//top right
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(1,ratio_2) ),	//bottom right
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y+h)), Vector2D(ratio_3,1) ),	//bottom center
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y+h)), Vector2D(0,1) )		//bottom left
				};
				vgui::surface()->DrawTexturedPolygon( 5, points);
			}
			else
			{
				vgui::Vertex_t points[3] =
				{
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(1,ratio_2) ),
					vgui::Vertex_t( Vector2D((int)(x+w),		(int)(y+h)), Vector2D(1,1)),
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y+h)), Vector2D(ratio_3,1) )
				};
				vgui::surface()->DrawTexturedPolygon( 3, points);
			}
		}
		else
		{
			if (isTop)
			{
				vgui::Vertex_t points[4] =
				{
					vgui::Vertex_t( Vector2D((int)(x),	(int)(y)), Vector2D(0,0) ),
					vgui::Vertex_t( Vector2D((int)(x+w),(int)(y)), Vector2D(1,0) ),
					vgui::Vertex_t( Vector2D((int)(sx), (int)(sy)), Vector2D(1,ratio_2) ),
					vgui::Vertex_t( Vector2D((int)(ex), (int)(ey)), Vector2D(0,ratio_1) )
				};
				vgui::surface()->DrawTexturedPolygon( 4, points);
			}
			else
			{
				vgui::Vertex_t points[4] =
				{
					vgui::Vertex_t( Vector2D((int)(ex), (int)(ey)), Vector2D(0,ratio_1)),
					vgui::Vertex_t( Vector2D((int)(sx), (int)(sy)), Vector2D(1,ratio_2)),
					vgui::Vertex_t( Vector2D((int)(x+w),(int)(y+h)), Vector2D(1,1) ),
					vgui::Vertex_t( Vector2D((int)(x), (int)(y+h)), Vector2D(0,1) )
				};
				vgui::surface()->DrawTexturedPolygon( 4, points);
			}
		}
	}
	else
	{
		vgui::Vertex_t points[4] =
		{
			vgui::Vertex_t( Vector2D(x,y), Vector2D(0,0) ),
			vgui::Vertex_t( Vector2D(x+w, y), Vector2D(1,0) ),
			vgui::Vertex_t( Vector2D(x+w, y+(h)), Vector2D(1,1) ),
			vgui::Vertex_t( Vector2D(x, y+h), Vector2D(0,1) )
		};
		vgui::surface()->DrawTexturedPolygon( 4, points);
	}
}

void CHudTexture::DrawScew_Right(int tx, int ty, int tw, int th, float per, bool isTop, int offset) const
{
	per = max(0, min(1,per));

	vgui::surface()->DrawSetTexture( textureId );
	vgui::surface()->DrawSetColor( Color(125,125,125,125) );

	float x = (float)(tx);
	float y = (float)(ty);
	float w = (float)(tw);
	float h = (float)(th);

	if (!(per > 99 || per < 0))
	{
		float sy = y+h-(per*h);
		float sx = x;
		float qy = y+h/2;
		float ydis_sq=qy-sy;
		float ydis_ef = (ydis_sq*(offset+w)/offset);
		float ex = x+w; 
		float ey = y+h/2-ydis_ef;
		float ratio_1 = (h/2.0-ydis_ef)/h;
		float ratio_2 = 1-per;

		if ( ey < y)
		{
			//float q=x+((w*ey)/(h/2));
			float q=x+(w-((w+offset)*(y-ey))/((y+h/2)-ey));
			float ratio_3 = (q-x)/w;

			if (isTop)
			{
				vgui::Vertex_t points[3] =
				{
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y)), Vector2D(ratio_3,0)),
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(0,ratio_2) ),
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y)), Vector2D(0,0) )
				};
				vgui::surface()->DrawTexturedPolygon( 3, points);
			}
			else
			{
				vgui::Vertex_t points[5] =
				{
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y)), Vector2D(ratio_3,0) ),	//top center
					vgui::Vertex_t( Vector2D((int)(x+w),		(int)(y)), Vector2D(1,0) ),		//top right
					vgui::Vertex_t( Vector2D((int)(w+x),		(int)(y+h)), Vector2D(1,1) ),		//bottom right
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y+h)), Vector2D(0,1) ),		//bottom left
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(0,ratio_2) )	//top right
				};
				vgui::surface()->DrawTexturedPolygon( 5, points);
			}
		}
		else if ( ey > (y+h))
		{
			float q=x+w+((offset+w)*((y+h)-ey)/(ey-(h/2+y)));
			float ratio_3=(q-x)/w;

			if (isTop)
			{
				vgui::Vertex_t points[5] =
				{
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y)), Vector2D(0,0) ),		//top left
					vgui::Vertex_t( Vector2D((int)(x+w),		(int)(y)), Vector2D(1,0) ),		//top right
					vgui::Vertex_t( Vector2D((int)(x+w),		(int)(y+h)), Vector2D(1,1) ),		//bottom right
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y+h)), Vector2D(ratio_3,1) ),	//bottom center
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(0,ratio_2) )	//bottom left
					
					
				};
				vgui::surface()->DrawTexturedPolygon( 5, points);
			}
			else
			{
				vgui::Vertex_t points[3] =
				{
					vgui::Vertex_t( Vector2D((int)(sx),		(int)(sy)), Vector2D(0,ratio_2) ),
					vgui::Vertex_t( Vector2D((int)(q),		(int)(y+h)), Vector2D(ratio_3,1) ),
					vgui::Vertex_t( Vector2D((int)(x),		(int)(y+h)), Vector2D(0,1))
					
				};
				vgui::surface()->DrawTexturedPolygon( 3, points);
			}
		}
		else
		{
			if (isTop)
			{
				vgui::Vertex_t points[4] =
				{
					vgui::Vertex_t( Vector2D((int)(x),	(int)(y)), Vector2D(0,0) ),
					vgui::Vertex_t( Vector2D((int)(x+w),(int)(y)), Vector2D(1,0) ),
					vgui::Vertex_t( Vector2D((int)(ex), (int)(ey)), Vector2D(1,ratio_1) ),
					vgui::Vertex_t( Vector2D((int)(sx), (int)(sy)), Vector2D(0,ratio_2) )
				};
				vgui::surface()->DrawTexturedPolygon( 4, points);
			}
			else
			{
				vgui::Vertex_t points[4] =
				{
					vgui::Vertex_t( Vector2D((int)(sx), (int)(sy)), Vector2D(0,ratio_2)),
					vgui::Vertex_t( Vector2D((int)(ex), (int)(ey)), Vector2D(1,ratio_1)),	
					vgui::Vertex_t( Vector2D((int)(x+w),(int)(y+h)), Vector2D(1,1) ),
					vgui::Vertex_t( Vector2D((int)(x), (int)(y+h)), Vector2D(0,1) )
				};
				vgui::surface()->DrawTexturedPolygon( 4, points);
			}
		}
	}
	else
	{
		vgui::Vertex_t points[4] =
		{
			vgui::Vertex_t( Vector2D(x,y), Vector2D(0,0) ),
			vgui::Vertex_t( Vector2D(x+w, y), Vector2D(1,0) ),
			vgui::Vertex_t( Vector2D(x+w, y+(h)), Vector2D(1,1) ),
			vgui::Vertex_t( Vector2D(x, y+h), Vector2D(0,1) )
		};
		vgui::surface()->DrawTexturedPolygon( 4, points);
	}

}