前段时间在研究VGUI,所以也就找了点资料写了一个简单的HUD,提供给大家参考:
客户端项目中添加:hud_hello.cpp
1
//============Copyright ?2007-2008, RootCat, All rights reserved========================//
2
//
3
// Purpose: 通过一个Hud显示当前玩家是飞行模式还是行走模式
4
//
5
// : $
6
//
7
//=============================================================================//
8
//
9
//
10
// implementation of CHudHello class
11
//
12
#include "cbase.h"
13
#include "hud.h"
14
#include "hud_macros.h"
15
#include "view.h"
16
#include "iclientmode.h"
17
18
#include <vgui/IScheme.h>
19
#include <vgui/ISurface.h>
20
#include <vgui/ISystem.h>
21
#include <vgui_controls/Panel.h>
22
#include <vgui/ILocalize.h>
23
24
using namespace vgui;
25
26
#include "hudelement.h"
27
28
#include "ConVar.h"
29
30
// memdbgon must be the last include file in a .cpp file!!!
31
#include "tier0/memdbgon.h"
32
33
//-----------------------------------------------------------------------------
34
// Purpose: Hello panel
35
//-----------------------------------------------------------------------------
36
class CHudHello : public CHudElement, public vgui::Panel
37

{
38
DECLARE_CLASS_SIMPLE( CHudHello,vgui::Panel);
39
40
public:
41
CHudHello( const char *pElementName );
42
virtual void Init( void );
43
virtual void VidInit( void );
44
virtual void Reset( void );
45
virtual void Paint( void );
46
protected:
47
virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
48
private:
49
// old variables
50
vgui::HScheme scheme; //The Scheme object to hold our scheme info.
51
};
52
53
DECLARE_HUDELEMENT(CHudHello);
54
55
//-----------------------------------------------------------------------------
56
// Purpose: Constructor
57
//-----------------------------------------------------------------------------
58
CHudHello::CHudHello( const char *pElementName ) : CHudElement( pElementName ), vgui::Panel( NULL, "HudHello" )
59

{
60
61
scheme = vgui::scheme()->LoadSchemeFromFile("resource/ClientScheme.res", "ClientScheme");
62
SetScheme(scheme); // Here we load up our scheme and set this element to use it. Using a different scheme than ClientScheme doesn\\\\\\\\\'t work right off the bat anyways, so
:)
63
vgui::Panel *pParent = g_pClientMode->GetViewport();
64
SetParent( pParent ); // Our parent is the screen itself.
65
SetPos(158,432);
66
}
67
68
//-----------------------------------------------------------------------------
69
// Purpose:
70
//-----------------------------------------------------------------------------
71
void CHudHello::Init()
72

{
73
Reset();
74
}
75
76
//-----------------------------------------------------------------------------
77
// Purpose:
78
//-----------------------------------------------------------------------------
79
void CHudHello::Reset()
80

{
81
}
82
83
//-----------------------------------------------------------------------------
84
// Purpose:
85
//-----------------------------------------------------------------------------
86
void CHudHello::VidInit()
87

{
88
Reset();
89
}
90
91
//-----------------------------------------------------------------------------
92
// Purpose:
93
//-----------------------------------------------------------------------------
94
void CHudHello::ApplySchemeSettings( vgui::IScheme *pScheme )
95

{
96
BaseClass::ApplySchemeSettings( pScheme );
97
}
98
99
//-----------------------------------------------------------------------------
100
// Purpose:
101
//-----------------------------------------------------------------------------
102
void CHudHello::Paint( void )
103

{
104
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();//获取当前用户
105
if ( !pPlayer )
106
return;
107
wchar_t *pText = L"行走模式";
108
surface()->DrawSetColor( 247, 148, 28, 255 );
109
surface()->DrawSetTextColor( 247, 148, 28, 255 );
110
if ( pPlayer->GetMoveType() == MOVETYPE_NOCLIP )
111
{
112
pText = L"飞行模式";
113
surface()->DrawSetColor( 255, 0, 0, 255 );
114
surface()->DrawSetTextColor( 255, 0, 0, 255 );
115
}
116
117
// get the right font handle for this scheme
118
vgui::IScheme *pScheme = vgui::scheme()->GetIScheme(GetScheme());
119
vgui::HFont hFont = pScheme->GetFont( "Default" );
120
surface()->DrawSetTextFont( hFont ); // set the font
121
surface()->DrawPrintText( pText, wcslen(pText) ); // print text这里是打印文字
122
surface()->DrawFilledRect( 10, 0,100 ,10 ); //x0,y0,x1,y1这里是绘制长方型
123
BaseClass::Paint();
124
}
并修改scripts/HudLayout.res文件,添加以下代码:
1
HudHello
2
{
3
"fieldName" "HudHello"
4
"visible" "1"
5
"enabled" "1"
6
7
"PaintBackgroundType" "2"
8
9
"text_xpos" "8"
10
"text_ypos" "20"
11
"digit_xpos" "50"
12
"digit_ypos" "2"
13
}