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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
//========= 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/ILocalize.h>
#include <vgui/ISurface.h>
#include "iclientmode.h"
#include "vgui_controls/AnimationController.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
using namespace vgui;
extern ConVar hud_drawhistory_time;
DECLARE_HUDELEMENT( CHudHistoryResource );
DECLARE_HUD_MESSAGE( CHudHistoryResource, ItemPickup );
DECLARE_HUD_MESSAGE( CHudHistoryResource, AmmoDenied );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudHistoryResource::CHudHistoryResource( const char *pElementName ) :
CHudElement( pElementName ), BaseClass( NULL, "HudHistoryResource" )
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
m_bDoNotDraw = true;
m_wcsAmmoFullMsg[0] = 0;
m_bNeedsDraw = false;
SetHiddenBits( HIDEHUD_MISCSTATUS );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudHistoryResource::ApplySchemeSettings( IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
SetPaintBackgroundEnabled( false );
// lookup text to display for ammo full message
wchar_t *wcs = g_pVGuiLocalize->Find("#hl2_AmmoFull");
if (wcs)
{
wcsncpy(m_wcsAmmoFullMsg, wcs, sizeof(m_wcsAmmoFullMsg) / sizeof(wchar_t));
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudHistoryResource::Init( void )
{
HOOK_HUD_MESSAGE( CHudHistoryResource, ItemPickup );
HOOK_HUD_MESSAGE( CHudHistoryResource, AmmoDenied );
Reset();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudHistoryResource::Reset( void )
{
m_PickupHistory.RemoveAll();
m_iCurrentHistorySlot = 0;
m_bDoNotDraw = true;
}
//-----------------------------------------------------------------------------
// Purpose: these kept only for hl1-port compatibility
//-----------------------------------------------------------------------------
void CHudHistoryResource::SetHistoryGap( int iNewHistoryGap )
{
}
//-----------------------------------------------------------------------------
// Purpose: adds an element to the history
//-----------------------------------------------------------------------------
void CHudHistoryResource::AddToHistory( C_BaseCombatWeapon *weapon )
{
// don't draw exhaustable weapons (grenades) since they'll have an ammo pickup icon as well
if ( weapon->GetWpnData().iFlags & ITEM_FLAG_EXHAUSTIBLE )
return;
int iId = weapon->entindex();
// don't show the same weapon twice
for ( int i = 0; i < m_PickupHistory.Count(); i++ )
{
if ( m_PickupHistory[i].iId == iId )
{
// it's already in list
return;
}
}
AddIconToHistory( HISTSLOT_WEAP, iId, weapon, 0, NULL );
}
//-----------------------------------------------------------------------------
// 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 )
{
if ( !iCount )
return;
#if defined( CSTRIKE_DLL )
// don't leave blank gaps for ammo we're not going to display
const FileWeaponInfo_t *pWpnInfo = gWR.GetWeaponFromAmmo( iId );
if ( pWpnInfo && ( pWpnInfo->iMaxClip1 >= 0 || pWpnInfo->iMaxClip2 >= 0 ) )
{
if ( !pWpnInfo->iconSmall )
return;
}
#endif
// clear out any ammo pickup denied icons, since we can obviously pickup again
for ( int i = 0; i < m_PickupHistory.Count(); i++ )
{
if ( m_PickupHistory[i].type == HISTSLOT_AMMODENIED && m_PickupHistory[i].iId == iId )
{
// kill the old entry
m_PickupHistory[i].DisplayTime = 0.0f;
// change the pickup to be in this entry
m_iCurrentHistorySlot = i;
break;
}
}
}
AddIconToHistory( iType, iId, NULL, iCount, NULL );
}
//-----------------------------------------------------------------------------
// Purpose: Add a new entry to the pickup history
//-----------------------------------------------------------------------------
void CHudHistoryResource::AddToHistory( int iType, const char *szName, int iCount )
{
if ( iType != HISTSLOT_ITEM )
return;
// Get the item's icon
CHudTexture *i = gHUD.GetIcon( szName );
if ( i == NULL )
return;
AddIconToHistory( iType, 1, NULL, iCount, i );
}
//-----------------------------------------------------------------------------
// Purpose: adds a history icon
//-----------------------------------------------------------------------------
void CHudHistoryResource::AddIconToHistory( int iType, int iId, C_BaseCombatWeapon *weapon, int iCount, CHudTexture *icon )
{
m_bNeedsDraw = true;
// Check to see if the pic would have to be drawn too high. If so, start again from the bottom
if ( (m_flHistoryGap * (m_iCurrentHistorySlot+1)) > GetTall() )
{
m_iCurrentHistorySlot = 0;
}
// If the history resource is appearing, slide the hint message element down
if ( m_iCurrentHistorySlot == 0 )
{
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HintMessageLower" );
}
// ensure the size
m_PickupHistory.EnsureCount(m_iCurrentHistorySlot + 1);
// default to just writing to the first slot
HIST_ITEM *freeslot = &m_PickupHistory[m_iCurrentHistorySlot];
if ( iType == HISTSLOT_AMMODENIED && freeslot->DisplayTime )
{
// don't override existing pickup icons with denied icons
return;
}
freeslot->iId = iId;
freeslot->icon = icon;
freeslot->type = iType;
freeslot->m_hWeapon = weapon;
freeslot->iCount = iCount;
if (iType == HISTSLOT_AMMODENIED)
{
freeslot->DisplayTime = gpGlobals->curtime + (hud_drawhistory_time.GetFloat() / 2.0f);
}
else
{
freeslot->DisplayTime = gpGlobals->curtime + hud_drawhistory_time.GetFloat();
}
++m_iCurrentHistorySlot;
}
//-----------------------------------------------------------------------------
// Purpose: Handle an item pickup event from the server
//-----------------------------------------------------------------------------
void CHudHistoryResource::MsgFunc_ItemPickup( bf_read &msg )
{
char szName[1024];
msg.ReadString( szName, sizeof(szName) );
// Add the item to the history
AddToHistory( HISTSLOT_ITEM, szName );
}
//-----------------------------------------------------------------------------
// Purpose: ammo denied message
//-----------------------------------------------------------------------------
void CHudHistoryResource::MsgFunc_AmmoDenied( bf_read &msg )
{
int iAmmo = msg.ReadShort();
// see if there are any existing ammo items of that type
for ( int i = 0; i < m_PickupHistory.Count(); i++ )
{
if ( m_PickupHistory[i].type == HISTSLOT_AMMO && m_PickupHistory[i].iId == iAmmo )
{
// it's already in the list as a pickup, ignore
return;
}
}
// see if there are any denied ammo icons, if so refresh their timer
for ( int i = 0; i < m_PickupHistory.Count(); i++ )
{
if ( m_PickupHistory[i].type == HISTSLOT_AMMODENIED && m_PickupHistory[i].iId == iAmmo )
{
// it's already in the list, refresh
m_PickupHistory[i].DisplayTime = gpGlobals->curtime + (hud_drawhistory_time.GetFloat() / 2.0f);
m_bNeedsDraw = true;
return;
}
}
// add into the list
AddToHistory( HISTSLOT_AMMODENIED, iAmmo, 0 );
}
//-----------------------------------------------------------------------------
// 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;
// Slide the hint message element back up
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "HintMessageRaise" );
}
//-----------------------------------------------------------------------------
// Purpose: Save CPU cycles by letting the HUD system early cull
// costly traversal. Called per frame, return true if thinking and
// painting need to occur.
//-----------------------------------------------------------------------------
bool CHudHistoryResource::ShouldDraw( void )
{
#ifdef TF_CLIENT_DLL
return false;
#else
return ( ( m_iCurrentHistorySlot > 0 || m_bNeedsDraw ) && CHudElement::ShouldDraw() );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Draw the pickup history
//-----------------------------------------------------------------------------
void CHudHistoryResource::Paint( void )
{
if ( m_bDoNotDraw )
{
// this is to not draw things until the first rendered
m_bDoNotDraw = false;
return;
}
// set when drawing should occur
// will be set if valid drawing does occur
m_bNeedsDraw = false;
int wide, tall;
GetSize( wide, tall );
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;
Color clr = gHUD.m_clrNormal;
clr[3] = MIN( scale, 255 );
bool bUseAmmoFullMsg = false;
// get the icon and number to draw
const CHudTexture *itemIcon = NULL;
const CHudTexture *itemAmmoIcon = NULL;
int iAmount = 0;
bool bHalfHeight = true;
switch ( m_PickupHistory[i].type )
{
case HISTSLOT_AMMO:
{
// Get the weapon we belong to
#ifndef HL2MP
const FileWeaponInfo_t *pWpnInfo = gWR.GetWeaponFromAmmo( m_PickupHistory[i].iId );
if ( pWpnInfo && ( pWpnInfo->iMaxClip1 >= 0 || pWpnInfo->iMaxClip2 >= 0 ) )
{
// The weapon will be the main icon, and the ammo the smaller
itemIcon = pWpnInfo->iconSmall;
itemAmmoIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
}
else
#endif // HL2MP
{
itemIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
itemAmmoIcon = NULL;
}
#ifdef CSTRIKE_DLL
// show grenades as the weapon icon
if ( pWpnInfo && pWpnInfo->iFlags & ITEM_FLAG_EXHAUSTIBLE )
{
itemIcon = pWpnInfo->iconActive;
itemAmmoIcon = NULL;
bHalfHeight = false;
}
#endif
iAmount = m_PickupHistory[i].iCount;
}
break;
case HISTSLOT_AMMODENIED:
{
itemIcon = gWR.GetAmmoIconFromWeapon( m_PickupHistory[i].iId );
iAmount = 0;
bUseAmmoFullMsg = true;
// display as red
clr = gHUD.m_clrCaution;
clr[3] = MIN( scale, 255 );
}
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
clr = gHUD.m_clrCaution;
clr[3] = MIN( scale, 255 );
}
itemIcon = pWeapon->GetSpriteInactive();
bHalfHeight = false;
}
break;
case HISTSLOT_ITEM:
{
if ( !m_PickupHistory[i].iId )
continue;
itemIcon = m_PickupHistory[i].icon;
bHalfHeight = false;
}
break;
default:
// unknown history type
Assert( 0 );
break;
}
if ( !itemIcon )
continue;
if ( clr[3] )
{
// valid drawing will occur
m_bNeedsDraw = true;
}
int ypos = tall - (m_flHistoryGap * (i + 1));
int xpos = wide - itemIcon->Width() - m_flIconInset;
#ifndef HL2MP
// Adjust for a half-height icon
if ( bHalfHeight )
{
ypos += itemIcon->Height() / 2;
}
#endif // HL2MP
itemIcon->DrawSelf( xpos, ypos, clr );
if ( itemAmmoIcon )
{
itemAmmoIcon->DrawSelf( xpos - ( itemAmmoIcon->Width() * 1.25f ), ypos, clr );
}
if ( iAmount )
{
wchar_t text[16];
_snwprintf( text, sizeof( text ) / sizeof(wchar_t), L"%i", m_PickupHistory[i].iCount );
// offset the number to sit properly next to the icon
ypos -= ( surface()->GetFontTall( m_hNumberFont ) - itemIcon->Height() ) / 2;
vgui::surface()->DrawSetTextFont( m_hNumberFont );
vgui::surface()->DrawSetTextColor( clr );
vgui::surface()->DrawSetTextPos( wide - m_flTextInset, ypos );
vgui::surface()->DrawUnicodeString( text );
}
else if ( bUseAmmoFullMsg )
{
// offset the number to sit properly next to the icon
ypos -= ( surface()->GetFontTall( m_hTextFont ) - itemIcon->Height() ) / 2;
vgui::surface()->DrawSetTextFont( m_hTextFont );
vgui::surface()->DrawSetTextColor( clr );
vgui::surface()->DrawSetTextPos( wide - m_flTextInset, ypos );
vgui::surface()->DrawUnicodeString( m_wcsAmmoFullMsg );
}
}
}
}
|