summaryrefslogtreecommitdiff
path: root/game/client/hl1/hl1_hud_history_resource.cpp
blob: ec36282adf4da624aeb5c60a53d3bda1917f7a00 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Item pickup history displayed onscreen when items are picked up.
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "history_resource.h"
#include "hud_macros.h"
#include <vgui_controls/Controls.h>
#include <vgui/ISurface.h>
#include "iclientmode.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

using namespace vgui;

extern ConVar hud_drawhistory_time;

#define HISTORY_PICKUP_GAP				(m_iHistoryGap + 5)
#define HISTORY_PICKUP_PICK_HEIGHT		(32 + (m_iHistoryGap * 2))
#define HISTORY_PICKUP_HEIGHT_MAX		(GetTall() - 100)
#define	ITEM_GUTTER_SIZE				48


DECLARE_HUDELEMENT( CHudHistoryResource );
DECLARE_HUD_MESSAGE( CHudHistoryResource, ItemPickup );

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
CHudHistoryResource::CHudHistoryResource( const char *pElementName ) :
	CHudElement( pElementName ), BaseClass( NULL, "HudHistoryResource" )
{	
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );
	SetHiddenBits( HIDEHUD_MISCSTATUS );
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *pScheme - 
//-----------------------------------------------------------------------------
void CHudHistoryResource::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );
	SetPaintBackgroundEnabled( false );

	m_hNumberFont = pScheme->GetFont( "HudNumbersSmall" );
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHistoryResource::Init( void )
{
	HOOK_HUD_MESSAGE( CHudHistoryResource, ItemPickup );

	m_iHistoryGap = 0;
	Reset();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudHistoryResource::Reset( void )
{
	m_PickupHistory.RemoveAll();
	m_iCurrentHistorySlot = 0;
}

//-----------------------------------------------------------------------------
// Purpose: Set a new minimum size gap between history icons
//-----------------------------------------------------------------------------
void CHudHistoryResource::SetHistoryGap( int iNewHistoryGap )
{
	if ( iNewHistoryGap > m_iHistoryGap )
	{
		m_iHistoryGap = iNewHistoryGap;
	}
}

void CHudHistoryResource::AddToHistory( C_BaseCombatWeapon *weapon )
{
	// Check to see if the pic would have to be drawn too high. If so, start again from the bottom
	if ( ((HISTORY_PICKUP_GAP * m_iCurrentHistorySlot) + HISTORY_PICKUP_PICK_HEIGHT) > HISTORY_PICKUP_HEIGHT_MAX )
	{	
		m_iCurrentHistorySlot = 0;
	}

	// ensure the size 
	m_PickupHistory.EnsureCount(m_iCurrentHistorySlot + 1);
	
	// default to just writing to the first slot
	HIST_ITEM *freeslot = &m_PickupHistory[m_iCurrentHistorySlot++];
	freeslot->type = HISTSLOT_WEAP;
	freeslot->iId = weapon->entindex();
	freeslot->m_hWeapon = weapon;
	freeslot->iCount = 0;
	freeslot->DisplayTime = gpGlobals->curtime + hud_drawhistory_time.GetFloat();
}

//-----------------------------------------------------------------------------
// Purpose: Add a new entry to the pickup history
//-----------------------------------------------------------------------------
void CHudHistoryResource::AddToHistory( int iType, int iId, int iCount )
{
	// Ignore adds with no count
	if ( iType == HISTSLOT_AMMO && !iCount )
		return;  

	// Check to see if the pic would have to be drawn too high. If so, start again from the bottom
	if ( ((HISTORY_PICKUP_GAP * m_iCurrentHistorySlot) + HISTORY_PICKUP_PICK_HEIGHT) > HISTORY_PICKUP_HEIGHT_MAX )
	{	
		m_iCurrentHistorySlot = 0;
	}
	
	// ensure the size 
	m_PickupHistory.EnsureCount(m_iCurrentHistorySlot + 1);
	
	// default to just writing to the first slot
	HIST_ITEM *freeslot = &m_PickupHistory[m_iCurrentHistorySlot++];
	freeslot->type = iType;
	freeslot->iId = iId;
	freeslot->m_hWeapon = NULL;
	freeslot->iCount = iCount;
	freeslot->DisplayTime = gpGlobals->curtime + hud_drawhistory_time.GetFloat();
}

//-----------------------------------------------------------------------------
// Purpose: Add a new entry to the pickup history
//-----------------------------------------------------------------------------
void CHudHistoryResource::AddToHistory( int iType, const char *szName, int iCount )
{
	if ( iType != HISTSLOT_ITEM )
		return;

	// Check to see if the pic would have to be drawn too high. If so, start again from the bottom
	if ( ((HISTORY_PICKUP_GAP * m_iCurrentHistorySlot) + HISTORY_PICKUP_PICK_HEIGHT) > HISTORY_PICKUP_HEIGHT_MAX )
	{
		m_iCurrentHistorySlot = 0;
	}

	// ensure the size 
	m_PickupHistory.EnsureCount(m_iCurrentHistorySlot + 1);
	
	// default to just writing to the first slot
	HIST_ITEM *freeslot = &m_PickupHistory[m_iCurrentHistorySlot++];

	// Get the item's icon
	CHudTexture *i = gHUD.GetIcon( szName );
	if ( i == NULL )
		return;  

	freeslot->iId = 1;
	freeslot->icon = i;
	freeslot->type = iType;
	freeslot->m_hWeapon  = NULL;
	freeslot->iCount = iCount;
	freeslot->DisplayTime = gpGlobals->curtime + hud_drawhistory_time.GetFloat();
}

//-----------------------------------------------------------------------------
// Purpose: Handle an item pickup event from the server
//-----------------------------------------------------------------------------
void CHudHistoryResource::MsgFunc_ItemPickup(bf_read &msg)
{
	char szString[2048];
	
	msg.ReadString( szString, sizeof(szString) );
	
	// Add the item to the history
	AddToHistory( HISTSLOT_ITEM, szString );
}

//-----------------------------------------------------------------------------
// Purpose: If there aren't any items in the history, clear it out.
//-----------------------------------------------------------------------------
void CHudHistoryResource::CheckClearHistory( void )
{
	for ( int i = 0; i < m_PickupHistory.Count(); i++ )
	{
		if ( m_PickupHistory[i].type )
			return;
	}

	m_iCurrentHistorySlot = 0;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CHudHistoryResource::ShouldDraw( void )
{
	return ( CHudElement::ShouldDraw() && m_iCurrentHistorySlot );
}

//-----------------------------------------------------------------------------
// Purpose: Draw the pickup history
//-----------------------------------------------------------------------------
void CHudHistoryResource::Paint( void )
{
	for ( int i = 0; i < m_PickupHistory.Count(); i++ )
	{
		if ( m_PickupHistory[i].type )
		{
			m_PickupHistory[i].DisplayTime = MIN( m_PickupHistory[i].DisplayTime, gpGlobals->curtime + hud_drawhistory_time.GetFloat() );
			if ( m_PickupHistory[i].DisplayTime <= gpGlobals->curtime )
			{  // pic drawing time has expired
				memset( &m_PickupHistory[i], 0, sizeof(HIST_ITEM) );
				CheckClearHistory();
				continue;
			}

			float elapsed = m_PickupHistory[i].DisplayTime - gpGlobals->curtime;
			float scale = elapsed * 80;

			int r, g, b, nUnused;
			(gHUD.m_clrYellowish).GetColor( r, g, b, nUnused );

			Color clrAmmo( r, g, b, MIN( scale, 255 ) );

			int nHudElemWidth, nHudElemHeight;
			GetSize( nHudElemWidth, nHudElemHeight );

			switch ( m_PickupHistory[i].type )
			{
				case HISTSLOT_AMMO:
				{
					CHudTexture *icon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
					if ( icon  )   
					{ 
						// Draw the pic
						int ypos = nHudElemHeight - ( HISTORY_PICKUP_PICK_HEIGHT + ( HISTORY_PICKUP_GAP * i ) );
						int xpos = nHudElemWidth - 24;

						// the dll has to make sure it has sent info the weapons you need
						icon->DrawSelf( xpos, ypos, clrAmmo );

						//Offset the number to sit properly next to the icon
						ypos -= ( surface()->GetFontTall( m_hNumberFont ) - icon->Height() ) / 2;

						vgui::surface()->DrawSetTextFont( m_hNumberFont );
						vgui::surface()->DrawSetTextColor( clrAmmo );
						vgui::surface()->DrawSetTextPos( GetWide() - ( ITEM_GUTTER_SIZE * 0.85f ), ypos );

						if ( m_PickupHistory[i].iCount )
						{
							char sz[ 32 ];
							int len = Q_snprintf( sz, sizeof( sz ), "%i", m_PickupHistory[i].iCount );
						
							for ( int ch = 0; ch < len; ch++ )
							{
								char c = sz[ ch ];
								vgui::surface()->DrawUnicodeChar( c );
							}
						} 
					}
				}
				break;
			case HISTSLOT_WEAP:
				{
					C_BaseCombatWeapon *pWeapon = m_PickupHistory[i].m_hWeapon;
					if ( !pWeapon )
						return;

					if ( !pWeapon->HasAmmo() )
					{
						// if the weapon doesn't have ammo, display it as red
						Color clrReddish( 255, 16, 16, 255 );
						clrReddish.GetColor( r, g, b, nUnused );
						clrAmmo.SetColor( r, g, b, MIN( scale, 255 ) );
					}

					int ypos = nHudElemHeight - (HISTORY_PICKUP_PICK_HEIGHT + (HISTORY_PICKUP_GAP * i));
					int xpos = nHudElemWidth - pWeapon->GetSpriteInactive()->Width();

					pWeapon->GetSpriteInactive()->DrawSelf( xpos, ypos, clrAmmo );
				}
				break;
			case HISTSLOT_ITEM:
				{
					if ( !m_PickupHistory[i].iId )
						continue;

					CHudTexture *icon = m_PickupHistory[i].icon;
					if ( !icon )
						continue;

					int ypos = ScreenHeight() - ( HISTORY_PICKUP_PICK_HEIGHT + ( HISTORY_PICKUP_GAP * i ) );
					int xpos = ScreenWidth() - icon->Width() - 10;

					icon->DrawSelf( xpos, ypos, clrAmmo );
				}
				break;
			default:
				{
					// Unknown history type???!!!
					Assert( 0 );
				}
				break;
			}
		}
	}
}