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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "c_vguiscreen.h"
#include "vgui_controls/Label.h"
#include <vgui/IVGui.h>
#include "c_plantedc4.h"
#include "ienginevgui.h"
using namespace vgui;
//-----------------------------------------------------------------------------
// Control screen
//-----------------------------------------------------------------------------
class CC4Panel : public CVGuiScreenPanel
{
DECLARE_CLASS( CC4Panel, CVGuiScreenPanel );
public:
CC4Panel( vgui::Panel *parent, const char *panelName );
~CC4Panel();
virtual bool Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData );
virtual void OnTick();
virtual void ApplySchemeSettings( IScheme *pScheme );
private:
vgui::Label *m_pTimeLabel;
float m_flNextDigitRandomizeTime; //next time to grab a new digit while scrolling random digits
//in un-decoded digits
int m_iLastRandomInt; //store the random digit between new rand calls
bool m_bInitLabelColor;
Color m_cArmed;
Color m_cDefused;
Color m_cInvisible;
};
DECLARE_VGUI_SCREEN_FACTORY( CC4Panel, "c4_panel" );
//-----------------------------------------------------------------------------
// Constructor:
//-----------------------------------------------------------------------------
CC4Panel::CC4Panel( vgui::Panel *parent, const char *panelName )
: BaseClass( parent, "CC4Panel", vgui::scheme()->LoadSchemeFromFileEx( enginevgui->GetPanel( PANEL_CLIENTDLL ), "resource/C4Panel.res", "ClientScheme" ) )
{
SetSize( 10, 10 ); // Quiet "parent not sized yet" spew
m_pTimeLabel = new vgui::Label( this, "TimerLabel", "" );
m_flNextDigitRandomizeTime = 0;
m_iLastRandomInt = 0;
m_bInitLabelColor = true;
}
CC4Panel::~CC4Panel()
{
}
void CC4Panel::ApplySchemeSettings( IScheme *pScheme )
{
assert( pScheme );
m_cArmed = pScheme->GetColor( "C4Panel_Armed", GetFgColor() );
m_cDefused = pScheme->GetColor( "C4Panel_Defused", GetFgColor() );
m_cInvisible = Color( 0, 0, 0, 0 );
if( m_bInitLabelColor )
{
m_pTimeLabel->SetFgColor( m_cArmed );
m_bInitLabelColor = false;
}
}
//-----------------------------------------------------------------------------
// Initialization
//-----------------------------------------------------------------------------
bool CC4Panel::Init( KeyValues* pKeyValues, VGuiScreenInitData_t* pInitData )
{
// Make sure we get ticked...
vgui::ivgui()->AddTickSignal( GetVPanel() );
if (!BaseClass::Init(pKeyValues, pInitData))
return false;
return true;
}
//how long to spend decoding each digit
float flTransitionTimes[] = { 0.9, 0.8, 0.6, 0.45, 0.25, 0.15, 0.0 };
//the defuse code, taken from the view model animation, v_c4.mdl
char cDefuseCode[] = { '7', '3', '5', '5', '6', '0', '8', '\0' };
char cArmedDisplay[] = { '*', '*', '*', '*', '*', '*', '*', '\0' };
//convert an integer into the readable character version of that number
#define INT_TO_CHAR(i) ( '0' + (i) )
//-----------------------------------------------------------------------------
// Update the display string
//-----------------------------------------------------------------------------
void CC4Panel::OnTick()
{
BaseClass::OnTick();
SetVisible( true );
float flProgress = 1.0;
if ( g_PlantedC4s.Count() > 0 )
{
C_PlantedC4 *pC4 = g_PlantedC4s[0];
if( pC4 )
{
flProgress = pC4->GetDefuseProgress();
}
else
return;
}
m_pTimeLabel->SetFgColor( m_cArmed );
// If flProgress is less than 0, the bomb has been defused
if( flProgress < 0.0 )
{
//Flash when the bomb has been defused
if( flProgress > -0.2 ) //flash for 2 seconds
{
int x = (int)( flProgress * 100 );
if( x % 2 == 0 )
m_pTimeLabel->SetFgColor( m_cInvisible );
else
m_pTimeLabel->SetFgColor( m_cDefused );
}
else
m_pTimeLabel->SetFgColor( m_cDefused );
//Show the full, decoded defuse code
m_pTimeLabel->SetText( cDefuseCode );
}
else if( flProgress < 1.0 ) //defuse in progress
{
//Initial display
char buf[8];
Q_strncpy( buf, cArmedDisplay, MIN( sizeof(buf), sizeof(cArmedDisplay) ) );
int iDigitPos = 0;
while( flProgress < flTransitionTimes[iDigitPos] )
{
//Fill in the previously decoded digits
buf[iDigitPos] = cDefuseCode[iDigitPos];
iDigitPos++;
}
//Animate the character that we're decoding
//Value drawn will be based on how long we've been
//decoding this character
float flTimeInThisChar = 1.0 - flTransitionTimes[0];
if( iDigitPos > 0 )
flTimeInThisChar = flTransitionTimes[iDigitPos-1] - flTransitionTimes[iDigitPos];
assert( flTimeInThisChar > 0.0 );
float flPercentDecoding = ( flProgress - flTransitionTimes[iDigitPos] ) / flTimeInThisChar;
//Determine when to next change the digit that we're decoding
if( m_flNextDigitRandomizeTime < gpGlobals->curtime )
{
//Get a new random int to draw
m_iLastRandomInt = RandomInt( 0, 9 );
if( flPercentDecoding > 0.7 )
m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.05;
else if( flPercentDecoding > 0.5 )
m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.1;
else if( flPercentDecoding > 0.3 )
m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.15;
else
m_flNextDigitRandomizeTime = gpGlobals->curtime + 0.3;
}
//Settle on the real value if we're close
if( flPercentDecoding < 0.2 )
buf[iDigitPos] = cDefuseCode[iDigitPos];
else //else use a random digit
buf[iDigitPos] = INT_TO_CHAR( m_iLastRandomInt );
m_pTimeLabel->SetFgColor( m_cArmed );
m_pTimeLabel->SetText( buf );
}
else
{
//Not being defused - draw the armed string
m_pTimeLabel->SetFgColor( m_cArmed );
m_pTimeLabel->SetText( cArmedDisplay );
}
}
|