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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
//
// hud.cpp
//
// implementation of CHud class
//
#include "cbase.h"
#include "hud_macros.h"
#include "history_resource.h"
#include "iinput.h"
#include "clientmode.h"
#include "in_buttons.h"
#include <vgui_controls/Controls.h>
#include <vgui/ISurface.h>
#include <KeyValues.h>
#include "itextmessage.h"
#include "mempool.h"
#include <KeyValues.h>
#include "filesystem.h"
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include "hud_lcd.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static CClassMemoryPool< CHudTexture > g_HudTextureMemoryPool( 128 );
//-----------------------------------------------------------------------------
// Purpose: Parses the weapon txt files to get the sprites needed.
//-----------------------------------------------------------------------------
struct HudTextureFileRef
{
HudTextureFileRef ( const char *cszFileKey, const char *cszHudTexturePrefix )
{
Q_strncpy( m_cszFileKey, cszFileKey, kcszFileKeyLength );
Q_strncpy( m_cszHudTexturePrefix, cszHudTexturePrefix, kcszHudTexturePrefix );
m_uiPrefixLength = Q_strlen( cszHudTexturePrefix );
m_fileKeySymbol = KeyValuesSystem()->GetSymbolForString( m_cszFileKey );
Assert( m_fileKeySymbol != INVALID_KEY_SYMBOL );
}
enum { kcszFileKeyLength = 64, };
enum { kcszHudTexturePrefix = 16, };
char m_cszFileKey[kcszFileKeyLength];
char m_cszHudTexturePrefix[kcszHudTexturePrefix];
unsigned int m_uiPrefixLength;
HKeySymbol m_fileKeySymbol;
};
void LoadHudTextures( CUtlDict< CHudTexture *, int >& list, const char *szFilenameWithoutExtension, const unsigned char *pICEKey )
{
KeyValues *pTemp, *pTextureSection;
KeyValues *pKeyValuesData = ReadEncryptedKVFile( filesystem, szFilenameWithoutExtension, pICEKey );
if ( pKeyValuesData )
{
CUtlVector<HudTextureFileRef> hudTextureFileRefs;
// By default, add a default entry mapping "file" to no prefix. This will allow earlier-version files
// to work with no modification.
hudTextureFileRefs.AddToTail( HudTextureFileRef( "file", "" ) );
// Read "*file"-to-prefix mapping.
KeyValues *pTextureFileRefs = pKeyValuesData->FindKey( "TextureFileRefs" );
if ( pTextureFileRefs )
{
pTemp = pTextureFileRefs->GetFirstSubKey();
while ( pTemp )
{
hudTextureFileRefs.AddToTail( HudTextureFileRef( pTemp->GetName(), pTemp->GetString( "prefix", "" ) ) );
pTemp = pTemp->GetNextKey();
}
}
// Read our individual HUD texture data blocks.
pTextureSection = pKeyValuesData->FindKey( "TextureData" );
if ( pTextureSection )
{
// Read the sprite data
pTemp = pTextureSection->GetFirstSubKey();
while ( pTemp )
{
if ( pTemp->GetString( "font", NULL ) )
{
CHudTexture *tex = new CHudTexture();
// Key Name is the sprite name
Q_strncpy( tex->szShortName, pTemp->GetName(), sizeof( tex->szShortName ) );
// it's a font-based icon
tex->bRenderUsingFont = true;
tex->cCharacterInFont = *(pTemp->GetString("character", ""));
Q_strncpy( tex->szTextureFile, pTemp->GetString( "font" ), sizeof( tex->szTextureFile ) );
list.Insert( tex->szShortName, tex );
}
else
{
int iTexLeft = pTemp->GetInt( "x", 0 ),
iTexTop = pTemp->GetInt( "y", 0 ),
iTexRight = pTemp->GetInt( "width", 0 ) + iTexLeft,
iTexBottom = pTemp->GetInt( "height", 0 ) + iTexTop;
for ( int i = 0; i < hudTextureFileRefs.Size(); i++ )
{
const char *cszFilename = pTemp->GetString( hudTextureFileRefs[i].m_fileKeySymbol, NULL );
if ( cszFilename )
{
CHudTexture *tex = new CHudTexture();
tex->bRenderUsingFont = false;
tex->rc.left = iTexLeft;
tex->rc.top = iTexTop;
tex->rc.right = iTexRight;
tex->rc.bottom = iTexBottom;
Q_strncpy( tex->szShortName, hudTextureFileRefs[i].m_cszHudTexturePrefix, sizeof( tex->szShortName ) );
Q_strncpy( tex->szShortName + hudTextureFileRefs[i].m_uiPrefixLength, pTemp->GetName(), sizeof( tex->szShortName ) - hudTextureFileRefs[i].m_uiPrefixLength );
Q_strncpy( tex->szTextureFile, cszFilename, sizeof( tex->szTextureFile ) );
list.Insert( tex->szShortName, tex );
}
}
}
pTemp = pTemp->GetNextKey();
}
}
}
// Failed for some reason. Delete the Key data and abort.
pKeyValuesData->deleteThis();
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : * -
// list -
//-----------------------------------------------------------------------------
void FreeHudTextureList( CUtlDict< CHudTexture *, int >& list )
{
int c = list.Count();
for ( int i = 0; i < c; i++ )
{
CHudTexture *tex = list[ i ];
delete tex;
}
list.RemoveAll();
}
// Globally-used fonts
vgui::HFont g_hFontTrebuchet24 = vgui::INVALID_FONT;
//=======================================================================================================================
// Hud Element Visibility handling
//=======================================================================================================================
typedef struct hudelement_hidden_s
{
char *sElementName;
int iHiddenBits; // Bits in which this hud element is hidden
} hudelement_hidden_t;
ConVar hidehud( "hidehud", "0", FCVAR_CHEAT );
CHudTexture::CHudTexture()
{
Q_memset( szShortName, 0, sizeof( szShortName ) );
Q_memset( szTextureFile, 0, sizeof( szTextureFile ) );
Q_memset( texCoords, 0, sizeof( texCoords ) );
Q_memset( &rc, 0, sizeof( rc ) );
textureId = -1;
bRenderUsingFont = false;
bPrecached = false;
cCharacterInFont = 0;
hFont = ( vgui::HFont )NULL;
}
CHudTexture& CHudTexture::operator =( const CHudTexture& src )
{
if ( this == &src )
return *this;
Q_strncpy( szShortName, src.szShortName, sizeof( szShortName ) );
Q_strncpy( szTextureFile, src.szTextureFile, sizeof( szTextureFile ) );
Q_memcpy( texCoords, src.texCoords, sizeof( texCoords ) );
if ( src.textureId == -1 )
{
// Didn't have a texture ID set
textureId = -1;
}
else
{
// Make a new texture ID that uses the same texture
textureId = vgui::surface()->CreateNewTextureID();
vgui::surface()->DrawSetTextureFile( textureId, src.szTextureFile, false, false );
}
rc = src.rc;
bRenderUsingFont = src.bRenderUsingFont;
cCharacterInFont = src.cCharacterInFont;
hFont = src.hFont;
return *this;
}
CHudTexture::~CHudTexture()
{
if ( vgui::surface() && textureId != -1 )
{
vgui::surface()->DestroyTextureID( textureId );
textureId = -1;
}
}
//=======================================================================================================================
// CHudElement
// All hud elements are derived from this class.
//=======================================================================================================================
//-----------------------------------------------------------------------------
// Purpose: Registers the hud element in a global list, in CHud
//-----------------------------------------------------------------------------
CHudElement::CHudElement( const char *pElementName )
{
m_bActive = false;
m_iHiddenBits = 0;
m_pElementName = pElementName;
SetNeedsRemove( false );
m_bIsParentedToClientDLLRootPanel = false;
// Make this for all hud elements, but when its a bit safer
#if defined( TF_CLIENT_DLL ) || defined( DOD_DLL )
RegisterForRenderGroup( "global" );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Remove this hud element from the global list in CHUD
//-----------------------------------------------------------------------------
CHudElement::~CHudElement()
{
if ( m_bNeedsRemove )
{
gHUD.RemoveHudElement( this );
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudElement::SetActive( bool bActive )
{
m_bActive = bActive;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : needsremove -
//-----------------------------------------------------------------------------
void CHudElement::SetNeedsRemove( bool needsremove )
{
m_bNeedsRemove = needsremove;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudElement::SetHiddenBits( int iBits )
{
m_iHiddenBits = iBits;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CHudElement::ShouldDraw( void )
{
bool bShouldDraw = ( !gHUD.IsHidden( m_iHiddenBits ) );
if ( bShouldDraw )
{
// for each render group
int iNumGroups = m_HudRenderGroups.Count();
for ( int iGroupIndex = 0; iGroupIndex < iNumGroups; iGroupIndex++ )
{
if ( gHUD.IsRenderGroupLockedFor( this, m_HudRenderGroups.Element(iGroupIndex ) ) )
return false;
}
}
return bShouldDraw;
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CHudElement::IsParentedToClientDLLRootPanel() const
{
return m_bIsParentedToClientDLLRootPanel;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : parented -
//-----------------------------------------------------------------------------
void CHudElement::SetParentedToClientDLLRootPanel( bool parented )
{
m_bIsParentedToClientDLLRootPanel = parented;
}
//-----------------------------------------------------------------------------
// Purpose: We can register to be affected by multiple hud render groups
//-----------------------------------------------------------------------------
void CHudElement::RegisterForRenderGroup( const char *pszGroupName )
{
int iGroupIndex = gHUD.RegisterForRenderGroup( pszGroupName );
// add group index to our list of registered groups
if ( m_HudRenderGroups.Find( iGroupIndex ) == m_HudRenderGroups.InvalidIndex() )
{
m_HudRenderGroups.AddToTail( iGroupIndex );
}
}
void CHudElement::UnregisterForRenderGroup( const char *pszGroupName )
{
int iGroupIndex = gHUD.RegisterForRenderGroup( pszGroupName );
m_HudRenderGroups.FindAndRemove( iGroupIndex );
}
//-----------------------------------------------------------------------------
// Purpose: We want to obscure other elements in this group
//-----------------------------------------------------------------------------
void CHudElement::HideLowerPriorityHudElementsInGroup( const char *pszGroupName )
{
// look up the render group
int iGroupIndex = gHUD.LookupRenderGroupIndexByName( pszGroupName );
// lock the group
gHUD.LockRenderGroup( iGroupIndex, this );
}
//-----------------------------------------------------------------------------
// Purpose: Stop obscuring other elements in this group
//-----------------------------------------------------------------------------
void CHudElement::UnhideLowerPriorityHudElementsInGroup( const char *pszGroupName )
{
// look up the render group
int iGroupIndex = gHUD.LookupRenderGroupIndexByName( pszGroupName );
// unlock the group
gHUD.UnlockRenderGroup( iGroupIndex, this );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CHudElement::GetRenderGroupPriority( void )
{
return 0;
}
CHud gHUD; // global HUD object
DECLARE_MESSAGE(gHUD, ResetHUD);
#ifdef CSTRIKE_DLL
DECLARE_MESSAGE(gHUD, SendAudio);
#endif
CHud::CHud()
{
SetDefLessFunc( m_RenderGroups );
m_flScreenShotTime = -1;
}
//-----------------------------------------------------------------------------
// Purpose: This is called every time the DLL is loaded
//-----------------------------------------------------------------------------
void CHud::Init( void )
{
HOOK_HUD_MESSAGE( gHUD, ResetHUD );
#ifdef CSTRIKE_DLL
HOOK_HUD_MESSAGE( gHUD, SendAudio );
#endif
InitFonts();
// Create all the Hud elements
CHudElementHelper::CreateAllElements();
gLCD.Init();
// Initialize all created elements
for ( int i = 0; i < m_HudList.Size(); i++ )
{
m_HudList[i]->Init();
}
m_bHudTexturesLoaded = false;
KeyValues *kv = new KeyValues( "layout" );
if ( kv )
{
if ( kv->LoadFromFile( filesystem, "scripts/HudLayout.res" ) )
{
int numelements = m_HudList.Size();
for ( int i = 0; i < numelements; i++ )
{
CHudElement *element = m_HudList[i];
vgui::Panel *pPanel = dynamic_cast<vgui::Panel*>(element);
if ( !pPanel )
{
Msg( "Non-vgui hud element %s\n", m_HudList[i]->GetName() );
continue;
}
KeyValues *key = kv->FindKey( pPanel->GetName(), false );
if ( !key )
{
Msg( "Hud element '%s' doesn't have an entry '%s' in scripts/HudLayout.res\n", m_HudList[i]->GetName(), pPanel->GetName() );
}
// Note: When a panel is parented to the module root, it's "parent" is returned as NULL.
if ( !element->IsParentedToClientDLLRootPanel() &&
!pPanel->GetParent() )
{
DevMsg( "Hud element '%s'/'%s' doesn't have a parent\n", m_HudList[i]->GetName(), pPanel->GetName() );
}
}
}
kv->deleteThis();
}
if ( m_bHudTexturesLoaded )
return;
m_bHudTexturesLoaded = true;
CUtlDict< CHudTexture *, int > textureList;
// check to see if we have sprites for this res; if not, step down
LoadHudTextures( textureList, "scripts/hud_textures", NULL );
LoadHudTextures( textureList, "scripts/mod_textures", NULL );
int c = textureList.Count();
for ( int index = 0; index < c; index++ )
{
CHudTexture* tex = textureList[ index ];
AddSearchableHudIconToList( *tex );
}
FreeHudTextureList( textureList );
}
//-----------------------------------------------------------------------------
// Purpose: Init Hud global colors
// Input : *scheme -
//-----------------------------------------------------------------------------
void CHud::InitColors( vgui::IScheme *scheme )
{
m_clrNormal = scheme->GetColor( "Normal", Color( 255, 208, 64 ,255 ) );
m_clrCaution = scheme->GetColor( "Caution", Color( 255, 48, 0, 255 ) );
m_clrYellowish = scheme->GetColor( "Yellowish", Color( 255, 160, 0, 255 ) );
}
//-----------------------------------------------------------------------------
// Initializes fonts
//-----------------------------------------------------------------------------
void CHud::InitFonts()
{
vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
vgui::IScheme *pScheme = vgui::scheme()->GetIScheme( scheme );
g_hFontTrebuchet24 = pScheme->GetFont("CenterPrintText", true);
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHud::Shutdown( void )
{
gLCD.Shutdown();
// Deleting hudlist items can result in them being removed from the same hudlist (m_bNeedsRemove).
// So go through and kill the last item until the array is empty.
while ( m_HudList.Size() > 0 )
{
delete m_HudList.Tail();
}
m_HudList.Purge();
m_bHudTexturesLoaded = false;
}
//-----------------------------------------------------------------------------
// Purpose: LevelInit's called whenever a new level is starting
//-----------------------------------------------------------------------------
void CHud::LevelInit( void )
{
// Tell all the registered hud elements to LevelInit
for ( int i = 0; i < m_HudList.Size(); i++ )
{
m_HudList[i]->LevelInit();
}
// Unhide all render groups
int iCount = m_RenderGroups.Count();
for ( int i = 0; i < iCount; i++ )
{
CHudRenderGroup *group = m_RenderGroups[ i ];
group->bHidden = false;
group->m_pLockingElements.Purge();
}
}
//-----------------------------------------------------------------------------
// Purpose: LevelShutdown's called whenever a level is finishing
//-----------------------------------------------------------------------------
void CHud::LevelShutdown( void )
{
// Tell all the registered hud elements to LevelShutdown
for ( int i = 0; i < m_HudList.Size(); i++ )
{
m_HudList[i]->LevelShutdown();
}
}
//-----------------------------------------------------------------------------
// Purpose: cleans up memory allocated for m_rg* arrays
//-----------------------------------------------------------------------------
CHud::~CHud()
{
int c = m_Icons.Count();
for ( int i = c - 1; i >= 0; i-- )
{
CHudTexture *tex = m_Icons[ i ];
g_HudTextureMemoryPool.Free( tex );
}
m_Icons.Purge();
c = m_RenderGroups.Count();
for ( int i = c - 1; i >= 0; i-- )
{
CHudRenderGroup *group = m_RenderGroups[ i ];
m_RenderGroups.RemoveAt(i);
delete group;
}
}
void CHudTexture::Precache( void )
{
// costly function, used selectively on specific hud elements to get font pages built out at load time
if ( IsX360() && bRenderUsingFont && !bPrecached && hFont != vgui::INVALID_FONT )
{
wchar_t wideChars[2];
wideChars[0] = (wchar_t)cCharacterInFont;
wideChars[1] = 0;
vgui::surface()->PrecacheFontCharacters( hFont, wideChars );
bPrecached = true;
}
}
void CHudTexture::DrawSelf( int x, int y, const Color& clr ) const
{
DrawSelf( x, y, Width(), Height(), clr );
}
void CHudTexture::DrawSelf( int x, int y, int w, int h, const Color& clr ) const
{
if ( bRenderUsingFont )
{
vgui::surface()->DrawSetTextFont( hFont );
vgui::surface()->DrawSetTextColor( clr );
vgui::surface()->DrawSetTextPos( x, y );
vgui::surface()->DrawUnicodeChar( cCharacterInFont );
}
else
{
if ( textureId == -1 )
return;
vgui::surface()->DrawSetTexture( textureId );
vgui::surface()->DrawSetColor( clr );
vgui::surface()->DrawTexturedSubRect( x, y, x + w, y + h,
texCoords[ 0 ], texCoords[ 1 ], texCoords[ 2 ], texCoords[ 3 ] );
}
}
void CHudTexture::DrawSelfCropped( int x, int y, int cropx, int cropy, int cropw, int croph, int finalWidth, int finalHeight, Color clr ) const
{
if ( bRenderUsingFont )
{
// work out how much we've been cropped
int height = vgui::surface()->GetFontTall( hFont );
float frac = (height - croph) / (float)height;
y -= cropy;
vgui::surface()->DrawSetTextFont( hFont );
vgui::surface()->DrawSetTextColor( clr );
vgui::surface()->DrawSetTextPos( x, y );
vgui::CharRenderInfo info;
if ( vgui::surface()->DrawGetUnicodeCharRenderInfo( cCharacterInFont, info ) )
{
if ( cropy )
{
info.verts[0].m_Position.y = Lerp( frac, info.verts[0].m_Position.y, info.verts[1].m_Position.y );
info.verts[0].m_TexCoord.y = Lerp( frac, info.verts[0].m_TexCoord.y, info.verts[1].m_TexCoord.y );
}
else if ( croph != height )
{
info.verts[1].m_Position.y = Lerp( 1.0f - frac, info.verts[0].m_Position.y, info.verts[1].m_Position.y );
info.verts[1].m_TexCoord.y = Lerp( 1.0f - frac, info.verts[0].m_TexCoord.y, info.verts[1].m_TexCoord.y );
}
vgui::surface()->DrawRenderCharFromInfo(info);
}
}
else
{
if ( textureId == -1 )
return;
float fw = (float)Width();
float fh = (float)Height();
float twidth = texCoords[ 2 ] - texCoords[ 0 ];
float theight = texCoords[ 3 ] - texCoords[ 1 ];
// Interpolate coords
float tCoords[ 4 ];
tCoords[ 0 ] = texCoords[ 0 ] + ( (float)cropx / fw ) * twidth;
tCoords[ 1 ] = texCoords[ 1 ] + ( (float)cropy / fh ) * theight;
tCoords[ 2 ] = texCoords[ 0 ] + ( (float)(cropx + cropw ) / fw ) * twidth;
tCoords[ 3 ] = texCoords[ 1 ] + ( (float)(cropy + croph ) / fh ) * theight;
vgui::surface()->DrawSetTexture( textureId );
vgui::surface()->DrawSetColor( clr );
vgui::surface()->DrawTexturedSubRect(
x, y,
x + finalWidth, y + finalHeight,
tCoords[ 0 ], tCoords[ 1 ],
tCoords[ 2 ], tCoords[ 3 ] );
}
}
void CHudTexture::DrawSelfCropped( int x, int y, int cropx, int cropy, int cropw, int croph, Color clr ) const
{
DrawSelfCropped( x, y, cropx, cropy, cropw, croph, cropw, croph, clr );
}
//-----------------------------------------------------------------------------
// Purpose: returns width of texture with scale factor applied. (If rendered
// using font, scale factor is ignored.)
//-----------------------------------------------------------------------------
int CHudTexture::EffectiveWidth( float flScale ) const
{
if ( !bRenderUsingFont )
{
return (int) ( Width() * flScale );
}
else
{
return vgui::surface()->GetCharacterWidth( hFont, cCharacterInFont );
}
}
//-----------------------------------------------------------------------------
// Purpose: returns height of texture with scale factor applied. (If rendered
// using font, scale factor is ignored.)
//-----------------------------------------------------------------------------
int CHudTexture::EffectiveHeight( float flScale ) const
{
if ( !bRenderUsingFont )
{
return (int) ( Height() * flScale );
}
else
{
return vgui::surface()->GetFontAscent( hFont, cCharacterInFont );
}
}
//-----------------------------------------------------------------------------
// Purpose: Gets texture handles for the hud icon
//-----------------------------------------------------------------------------
void CHud::SetupNewHudTexture( CHudTexture *t )
{
if ( t->bRenderUsingFont )
{
vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
t->hFont = vgui::scheme()->GetIScheme(scheme)->GetFont( t->szTextureFile, true );
t->rc.top = 0;
t->rc.left = 0;
t->rc.right = vgui::surface()->GetCharacterWidth( t->hFont, t->cCharacterInFont );
t->rc.bottom = vgui::surface()->GetFontTall( t->hFont );
}
else
{
// Set up texture id and texture coordinates
t->textureId = vgui::surface()->CreateNewTextureID();
vgui::surface()->DrawSetTextureFile( t->textureId, t->szTextureFile, false, false );
int wide, tall;
vgui::surface()->DrawGetTextureSize( t->textureId, wide, tall );
t->texCoords[ 0 ] = (float)(t->rc.left + 0.5f) / (float)wide;
t->texCoords[ 1 ] = (float)(t->rc.top + 0.5f) / (float)tall;
t->texCoords[ 2 ] = (float)(t->rc.right - 0.5f) / (float)wide;
t->texCoords[ 3 ] = (float)(t->rc.bottom - 0.5f) / (float)tall;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture *CHud::AddUnsearchableHudIconToList( CHudTexture& texture )
{
// These names are composed based on the texture file name
char composedName[ 512 ];
if ( texture.bRenderUsingFont )
{
Q_snprintf( composedName, sizeof( composedName ), "%s_c%i",
texture.szTextureFile, texture.cCharacterInFont );
}
else
{
Q_snprintf( composedName, sizeof( composedName ), "%s_%i_%i_%i_%i",
texture.szTextureFile, texture.rc.left, texture.rc.top, texture.rc.right, texture.rc.bottom );
}
CHudTexture *icon = GetIcon( composedName );
if ( icon )
{
return icon;
}
CHudTexture *newTexture = ( CHudTexture * )g_HudTextureMemoryPool.Alloc();
*newTexture = texture;
SetupNewHudTexture( newTexture );
int idx = m_Icons.Insert( composedName, newTexture );
return m_Icons[ idx ];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudTexture *CHud::AddSearchableHudIconToList( CHudTexture& texture )
{
CHudTexture *icon = GetIcon( texture.szShortName );
if ( icon )
{
return icon;
}
CHudTexture *newTexture = ( CHudTexture * )g_HudTextureMemoryPool.Alloc();
*newTexture = texture;
SetupNewHudTexture( newTexture );
int idx = m_Icons.Insert( texture.szShortName, newTexture );
return m_Icons[ idx ];
}
//-----------------------------------------------------------------------------
// Purpose: returns a pointer to an icon in the list
//-----------------------------------------------------------------------------
CHudTexture *CHud::GetIcon( const char *szIcon )
{
int i = m_Icons.Find( szIcon );
if ( i == m_Icons.InvalidIndex() )
return NULL;
return m_Icons[ i ];
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHud::RefreshHudTextures()
{
if ( !m_bHudTexturesLoaded )
{
Assert( 0 );
return;
}
CUtlDict< CHudTexture *, int > textureList;
// check to see if we have sprites for this res; if not, step down
LoadHudTextures( textureList, "scripts/hud_textures", NULL );
LoadHudTextures( textureList, "scripts/mod_textures", NULL );
// fix up all the texture icons first
int c = textureList.Count();
for ( int index = 0; index < c; index++ )
{
CHudTexture *tex = textureList[ index ];
Assert( tex );
CHudTexture *icon = GetIcon( tex->szShortName );
if ( !icon )
continue;
// Update file
Q_strncpy( icon->szTextureFile, tex->szTextureFile, sizeof( icon->szTextureFile ) );
if ( !icon->bRenderUsingFont )
{
// Update subrect
icon->rc = tex->rc;
// Keep existing texture id, but now update texture file and texture coordinates
vgui::surface()->DrawSetTextureFile( icon->textureId, icon->szTextureFile, false, false );
// Get new texture dimensions in case it changed
int wide, tall;
vgui::surface()->DrawGetTextureSize( icon->textureId, wide, tall );
// Assign coords
icon->texCoords[ 0 ] = (float)(icon->rc.left + 0.5f) / (float)wide;
icon->texCoords[ 1 ] = (float)(icon->rc.top + 0.5f) / (float)tall;
icon->texCoords[ 2 ] = (float)(icon->rc.right - 0.5f) / (float)wide;
icon->texCoords[ 3 ] = (float)(icon->rc.bottom - 0.5f) / (float)tall;
}
}
FreeHudTextureList( textureList );
// fixup all the font icons
vgui::HScheme scheme = vgui::scheme()->GetScheme( "ClientScheme" );
for (int i = m_Icons.First(); m_Icons.IsValidIndex(i); i = m_Icons.Next(i))
{
CHudTexture *icon = m_Icons[i];
if ( !icon )
continue;
// Update file
if ( icon->bRenderUsingFont )
{
icon->hFont = vgui::scheme()->GetIScheme(scheme)->GetFont( icon->szTextureFile, true );
icon->rc.top = 0;
icon->rc.left = 0;
icon->rc.right = vgui::surface()->GetCharacterWidth( icon->hFont, icon->cCharacterInFont );
icon->rc.bottom = vgui::surface()->GetFontTall( icon->hFont );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHud::OnRestore()
{
ResetHUD();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHud::VidInit( void )
{
for ( int i = 0; i < m_HudList.Size(); i++ )
{
m_HudList[i]->VidInit();
}
ResetHUD();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudElement *CHud::FindElement( const char *pName )
{
for ( int i = 0; i < m_HudList.Size(); i++ )
{
if ( V_stricmp( m_HudList[i]->GetName(), pName ) == 0 )
return m_HudList[i];
}
DevWarning(1, "Could not find Hud Element: %s\n", pName );
Assert(0);
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Adds a member to the HUD
//-----------------------------------------------------------------------------
void CHud::AddHudElement( CHudElement *pHudElement )
{
// Add the hud element to the end of the array
m_HudList.AddToTail( pHudElement );
pHudElement->SetNeedsRemove( true );
}
//-----------------------------------------------------------------------------
// Purpose: Remove an element from the HUD
//-----------------------------------------------------------------------------
void CHud::RemoveHudElement( CHudElement *pHudElement )
{
m_HudList.FindAndRemove( pHudElement );
}
//-----------------------------------------------------------------------------
// Purpose: Returns current mouse sensitivity setting
// Output : float - the return value
//-----------------------------------------------------------------------------
float CHud::GetSensitivity( void )
{
#ifndef _X360
return m_flMouseSensitivity;
#else
return 1.0f;
#endif
}
float CHud::GetFOVSensitivityAdjust()
{
return m_flFOVSensitivityAdjust;
}
//-----------------------------------------------------------------------------
// Purpose: Return true if the passed in sections of the HUD shouldn't be drawn
//-----------------------------------------------------------------------------
bool CHud::IsHidden( int iHudFlags )
{
// Not in game?
if ( !engine->IsInGame() )
return true;
// No local player yet?
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return true;
// Get current hidden flags
int iHideHud = pPlayer->m_Local.m_iHideHUD;
if ( hidehud.GetInt() )
{
iHideHud = hidehud.GetInt();
}
// Everything hidden?
if ( iHideHud & HIDEHUD_ALL )
return true;
// Local player dead?
if ( ( iHudFlags & HIDEHUD_PLAYERDEAD ) && ( pPlayer->GetHealth() <= 0 && !pPlayer->IsAlive() ) )
return true;
// Need the HEV suit ( HL2 )
if ( ( iHudFlags & HIDEHUD_NEEDSUIT ) && ( !pPlayer->IsSuitEquipped() ) )
return true;
// Hide all HUD elements during screenshot if the user's set hud_freezecamhide ( TF2 )
#if defined( TF_CLIENT_DLL )
extern bool IsTakingAFreezecamScreenshot();
extern ConVar hud_freezecamhide;
if ( IsTakingAFreezecamScreenshot() && hud_freezecamhide.GetBool() )
return true;
#endif
return ( ( iHudFlags & iHideHud ) != 0);
}
//-----------------------------------------------------------------------------
// Purpose: Allows HUD to modify input data
//-----------------------------------------------------------------------------
void CHud::ProcessInput( bool bActive )
{
if ( bActive )
{
m_iKeyBits = input->GetButtonBits( 0 );
// Weaponbits need to be sent down as a UserMsg now.
gHUD.Think();
}
}
int CHud::LookupRenderGroupIndexByName( const char *pszGroupName )
{
return m_RenderGroupNames.Find( pszGroupName );
}
//-----------------------------------------------------------------------------
// Purpose: A hud element wants to lock this render group so other panels in the
// group do not draw
//-----------------------------------------------------------------------------
bool CHud::LockRenderGroup( int iGroupIndex, CHudElement *pLocker /* = NULL */ )
{
// does this index exist?
if ( !DoesRenderGroupExist(iGroupIndex) )
return false;
int i = m_RenderGroups.Find( iGroupIndex );
Assert( m_RenderGroups.IsValidIndex(i) );
CHudRenderGroup *group = m_RenderGroups.Element(i);
Assert( group );
if ( group )
{
// NULL pLocker means some higher power is globally hiding this group
if ( pLocker == NULL )
{
group->bHidden = true;
}
else
{
bool bFound = false;
// See if we have it locked already
int iNumLockers = group->m_pLockingElements.Count();
for ( int i=0;i<iNumLockers;i++ )
{
if ( pLocker == group->m_pLockingElements.Element(i) )
{
bFound = true;
break;
}
}
// otherwise lock us
if ( !bFound )
group->m_pLockingElements.Insert( pLocker );
}
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: A hud element wants to release the lock on this render group
//-----------------------------------------------------------------------------
bool CHud::UnlockRenderGroup( int iGroupIndex, CHudElement *pLocker /* = NULL */ )
{
// does this index exist?
if ( !DoesRenderGroupExist(iGroupIndex) )
return false;
int i = m_RenderGroups.Find( iGroupIndex );
Assert( m_RenderGroups.IsValidIndex(i) );
CHudRenderGroup *group = m_RenderGroups.Element(i);
if ( group )
{
// NULL pLocker means some higher power is globally hiding this group
if ( group->bHidden && pLocker == NULL )
{
group->bHidden = false;
return true;
}
int iNumLockers = group->m_pLockingElements.Count();
for ( int i=0;i<iNumLockers;i++ )
{
if ( pLocker == group->m_pLockingElements.Element(i) )
{
group->m_pLockingElements.RemoveAt( i );
return true;
}
}
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: See if we should draw based on a hud render group
// Return true if this group is locked, hud elem will be hidden
//-----------------------------------------------------------------------------
bool CHud::IsRenderGroupLockedFor( CHudElement *pHudElement, int iGroupIndex )
{
// does this index exist?
if ( !DoesRenderGroupExist(iGroupIndex) )
return false;
int i = m_RenderGroups.Find( iGroupIndex );
Assert( m_RenderGroups.IsValidIndex(i) );
CHudRenderGroup *group = m_RenderGroups.Element(i);
if ( !group )
return false;
// hidden for everyone!
if ( group->bHidden )
return true;
if ( group->m_pLockingElements.Count() == 0 )
return false;
if ( !pHudElement )
return true;
CHudElement *pLocker = group->m_pLockingElements.ElementAtHead();
return ( pLocker != pHudElement && pLocker->GetRenderGroupPriority() > pHudElement->GetRenderGroupPriority() );
}
//-----------------------------------------------------------------------------
// Purpose: CHudElements can ask for the index of hud element render groups
// returns a group index
//-----------------------------------------------------------------------------
int CHud::RegisterForRenderGroup( const char *pszGroupName )
{
int iGroupNameIndex = m_RenderGroupNames.Find( pszGroupName );
if ( iGroupNameIndex != m_RenderGroupNames.InvalidIndex() )
{
return iGroupNameIndex;
}
// otherwise add the group
return AddHudRenderGroup( pszGroupName );
}
//-----------------------------------------------------------------------------
// Purpose: Create a new hud render group
// returns a group index
//-----------------------------------------------------------------------------
int CHud::AddHudRenderGroup( const char *pszGroupName )
{
// we tried to register for a group but didn't find it, add a new one
int iGroupNameIndex = m_RenderGroupNames.AddToTail( pszGroupName );
CHudRenderGroup *group = new CHudRenderGroup();
return m_RenderGroups.Insert( iGroupNameIndex, group );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
bool CHud::DoesRenderGroupExist( int iGroupIndex )
{
return ( m_RenderGroups.Find( iGroupIndex ) != m_RenderGroups.InvalidIndex() );
}
//-----------------------------------------------------------------------------
// Purpose: Allows HUD to Think and modify input data
// Input : *cdata -
// time -
// Output : int - 1 if there were changes, 0 otherwise
//-----------------------------------------------------------------------------
void CHud::UpdateHud( bool bActive )
{
// clear the weapon bits.
gHUD.m_iKeyBits &= (~(IN_WEAPON1|IN_WEAPON2));
g_pClientMode->Update();
gLCD.Update();
}
//-----------------------------------------------------------------------------
// Purpose: Force a Hud UI anim to play
//-----------------------------------------------------------------------------
CON_COMMAND_F( testhudanim, "Test a hud element animation.\n\tArguments: <anim name>\n", FCVAR_CHEAT )
{
if ( args.ArgC() != 2 )
{
Msg("Usage:\n testhudanim <anim name>\n");
return;
}
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( args[1] );
}
|