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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Color correction entity.
//
// $NoKeywords: $
//===========================================================================//
#include <string.h>
#include "cbase.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define COLOR_CORRECTION_ENT_THINK_RATE TICK_INTERVAL
static const char *s_pFadeInContextThink = "ColorCorrectionFadeInThink";
static const char *s_pFadeOutContextThink = "ColorCorrectionFadeOutThink";
//------------------------------------------------------------------------------
// FIXME: This really should inherit from something more lightweight
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Purpose : Shadow control entity
//------------------------------------------------------------------------------
class CColorCorrection : public CBaseEntity
{
DECLARE_CLASS( CColorCorrection, CBaseEntity );
public:
DECLARE_SERVERCLASS();
DECLARE_DATADESC();
CColorCorrection();
void Spawn( void );
int UpdateTransmitState();
void Activate( void );
virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
// Inputs
void InputEnable( inputdata_t &inputdata );
void InputDisable( inputdata_t &inputdata );
void InputSetFadeInDuration ( inputdata_t &inputdata );
void InputSetFadeOutDuration ( inputdata_t &inputdata );
private:
void FadeIn ( void );
void FadeOut ( void );
void FadeInThink( void ); // Fades lookup weight from Cur->MaxWeight
void FadeOutThink( void ); // Fades lookup weight from CurWeight->0.0
float m_flFadeInDuration; // Duration for a full 0->MaxWeight transition
float m_flFadeOutDuration; // Duration for a full Max->0 transition
float m_flStartFadeInWeight;
float m_flStartFadeOutWeight;
float m_flTimeStartFadeIn;
float m_flTimeStartFadeOut;
float m_flMaxWeight;
bool m_bStartDisabled;
CNetworkVar( bool, m_bEnabled );
CNetworkVar( float, m_MinFalloff );
CNetworkVar( float, m_MaxFalloff );
CNetworkVar( float, m_flCurWeight );
CNetworkString( m_netlookupFilename, MAX_PATH );
string_t m_lookupFilename;
};
LINK_ENTITY_TO_CLASS(color_correction, CColorCorrection);
BEGIN_DATADESC( CColorCorrection )
DEFINE_THINKFUNC( FadeInThink ),
DEFINE_THINKFUNC( FadeOutThink ),
DEFINE_FIELD( m_flCurWeight, FIELD_FLOAT ),
DEFINE_FIELD( m_flTimeStartFadeIn, FIELD_FLOAT ),
DEFINE_FIELD( m_flTimeStartFadeOut, FIELD_FLOAT ),
DEFINE_FIELD( m_flStartFadeInWeight, FIELD_FLOAT ),
DEFINE_FIELD( m_flStartFadeOutWeight, FIELD_FLOAT ),
DEFINE_KEYFIELD( m_MinFalloff, FIELD_FLOAT, "minfalloff" ),
DEFINE_KEYFIELD( m_MaxFalloff, FIELD_FLOAT, "maxfalloff" ),
DEFINE_KEYFIELD( m_flMaxWeight, FIELD_FLOAT, "maxweight" ),
DEFINE_KEYFIELD( m_flFadeInDuration, FIELD_FLOAT, "fadeInDuration" ),
DEFINE_KEYFIELD( m_flFadeOutDuration, FIELD_FLOAT, "fadeOutDuration" ),
DEFINE_KEYFIELD( m_lookupFilename, FIELD_STRING, "filename" ),
DEFINE_KEYFIELD( m_bEnabled, FIELD_BOOLEAN, "enabled" ),
DEFINE_KEYFIELD( m_bStartDisabled, FIELD_BOOLEAN, "StartDisabled" ),
// DEFINE_ARRAY( m_netlookupFilename, FIELD_CHARACTER, MAX_PATH ),
DEFINE_INPUTFUNC( FIELD_VOID, "Enable", InputEnable ),
DEFINE_INPUTFUNC( FIELD_VOID, "Disable", InputDisable ),
DEFINE_INPUTFUNC( FIELD_FLOAT, "SetFadeInDuration", InputSetFadeInDuration ),
DEFINE_INPUTFUNC( FIELD_FLOAT, "SetFadeOutDuration", InputSetFadeOutDuration ),
END_DATADESC()
extern void SendProxy_Origin( const SendProp *pProp, const void *pStruct, const void *pData, DVariant *pOut, int iElement, int objectID );
IMPLEMENT_SERVERCLASS_ST_NOBASE(CColorCorrection, DT_ColorCorrection)
SendPropVector( SENDINFO(m_vecOrigin), -1, SPROP_NOSCALE, 0.0f, HIGH_DEFAULT, SendProxy_Origin ),
SendPropFloat( SENDINFO(m_MinFalloff) ),
SendPropFloat( SENDINFO(m_MaxFalloff) ),
SendPropFloat( SENDINFO(m_flCurWeight) ),
SendPropString( SENDINFO(m_netlookupFilename) ),
SendPropBool( SENDINFO(m_bEnabled) ),
END_SEND_TABLE()
CColorCorrection::CColorCorrection() : BaseClass()
{
m_bEnabled = true;
m_MinFalloff = 0.0f;
m_MaxFalloff = 1000.0f;
m_flMaxWeight = 1.0f;
m_flCurWeight.Set( 0.0f );
m_flFadeInDuration = 0.0f;
m_flFadeOutDuration = 0.0f;
m_flStartFadeInWeight = 0.0f;
m_flStartFadeOutWeight = 0.0f;
m_flTimeStartFadeIn = 0.0f;
m_flTimeStartFadeOut = 0.0f;
m_netlookupFilename.GetForModify()[0] = 0;
m_lookupFilename = NULL_STRING;
}
//------------------------------------------------------------------------------
// Purpose : Send even though we don't have a model
//------------------------------------------------------------------------------
int CColorCorrection::UpdateTransmitState()
{
// ALWAYS transmit to all clients.
return SetTransmitState( FL_EDICT_ALWAYS );
}
//------------------------------------------------------------------------------
// Purpose :
//------------------------------------------------------------------------------
void CColorCorrection::Spawn( void )
{
AddEFlags( EFL_FORCE_CHECK_TRANSMIT | EFL_DIRTY_ABSTRANSFORM );
Precache();
SetSolid( SOLID_NONE );
// To fade in/out the weight.
SetContextThink( &CColorCorrection::FadeInThink, TICK_NEVER_THINK, s_pFadeInContextThink );
SetContextThink( &CColorCorrection::FadeOutThink, TICK_NEVER_THINK, s_pFadeOutContextThink );
if( m_bStartDisabled )
{
m_bEnabled = false;
m_flCurWeight.Set ( 0.0f );
}
else
{
m_bEnabled = true;
m_flCurWeight.Set ( 1.0f );
}
BaseClass::Spawn();
}
void CColorCorrection::Activate( void )
{
BaseClass::Activate();
Q_strncpy( m_netlookupFilename.GetForModify(), STRING( m_lookupFilename ), MAX_PATH );
}
//-----------------------------------------------------------------------------
// Purpose: Sets up internal vars needed for fade in lerping
//-----------------------------------------------------------------------------
void CColorCorrection::FadeIn ( void )
{
m_bEnabled = true;
m_flTimeStartFadeIn = gpGlobals->curtime;
m_flStartFadeInWeight = m_flCurWeight;
SetNextThink ( gpGlobals->curtime + COLOR_CORRECTION_ENT_THINK_RATE, s_pFadeInContextThink );
}
//-----------------------------------------------------------------------------
// Purpose: Sets up internal vars needed for fade out lerping
//-----------------------------------------------------------------------------
void CColorCorrection::FadeOut ( void )
{
m_bEnabled = false;
m_flTimeStartFadeOut = gpGlobals->curtime;
m_flStartFadeOutWeight = m_flCurWeight;
SetNextThink ( gpGlobals->curtime + COLOR_CORRECTION_ENT_THINK_RATE, s_pFadeOutContextThink );
}
//-----------------------------------------------------------------------------
// Purpose: Fades lookup weight from CurWeight->MaxWeight
//-----------------------------------------------------------------------------
void CColorCorrection::FadeInThink( void )
{
// Check for conditions where we shouldnt fade in
if ( m_flFadeInDuration <= 0 || // not set to fade in
m_flCurWeight >= m_flMaxWeight || // already past max weight
!m_bEnabled || // fade in/out mutex
m_flMaxWeight == 0.0f || // min==max
m_flStartFadeInWeight >= m_flMaxWeight ) // already at max weight
{
SetNextThink ( TICK_NEVER_THINK, s_pFadeInContextThink );
return;
}
// If we started fading in without fully fading out, use a truncated duration
float flTimeToFade = m_flFadeInDuration;
if ( m_flStartFadeInWeight > 0.0f )
{
float flWeightRatio = m_flStartFadeInWeight / m_flMaxWeight;
flWeightRatio = clamp ( flWeightRatio, 0.0f, 0.99f );
flTimeToFade = m_flFadeInDuration * (1.0 - flWeightRatio);
}
Assert ( flTimeToFade > 0.0f );
float flFadeRatio = (gpGlobals->curtime - m_flTimeStartFadeIn) / flTimeToFade;
flFadeRatio = clamp ( flFadeRatio, 0.0f, 1.0f );
m_flStartFadeInWeight = clamp ( m_flStartFadeInWeight, 0.0f, 1.0f );
m_flCurWeight = Lerp( flFadeRatio, m_flStartFadeInWeight, m_flMaxWeight );
SetNextThink( gpGlobals->curtime + COLOR_CORRECTION_ENT_THINK_RATE, s_pFadeInContextThink );
}
//-----------------------------------------------------------------------------
// Purpose: Fades lookup weight from CurWeight->0.0
//-----------------------------------------------------------------------------
void CColorCorrection::FadeOutThink( void )
{
// Check for conditions where we shouldn't fade out
if ( m_flFadeOutDuration <= 0 || // not set to fade out
m_flCurWeight <= 0.0f || // already faded out
m_bEnabled || // fade in/out mutex
m_flMaxWeight == 0.0f || // min==max
m_flStartFadeOutWeight <= 0.0f )// already at min weight
{
SetNextThink ( TICK_NEVER_THINK, s_pFadeOutContextThink );
return;
}
// If we started fading out without fully fading in, use a truncated duration
float flTimeToFade = m_flFadeOutDuration;
if ( m_flStartFadeOutWeight < m_flMaxWeight )
{
float flWeightRatio = m_flStartFadeOutWeight / m_flMaxWeight;
flWeightRatio = clamp ( flWeightRatio, 0.01f, 1.0f );
flTimeToFade = m_flFadeOutDuration * flWeightRatio;
}
Assert ( flTimeToFade > 0.0f );
float flFadeRatio = (gpGlobals->curtime - m_flTimeStartFadeOut) / flTimeToFade;
flFadeRatio = clamp ( flFadeRatio, 0.0f, 1.0f );
m_flStartFadeOutWeight = clamp ( m_flStartFadeOutWeight, 0.0f, 1.0f );
m_flCurWeight = Lerp( 1.0f - flFadeRatio, 0.0f, m_flStartFadeOutWeight );
SetNextThink( gpGlobals->curtime + COLOR_CORRECTION_ENT_THINK_RATE, s_pFadeOutContextThink );
}
//------------------------------------------------------------------------------
// Purpose : Input handlers
//------------------------------------------------------------------------------
void CColorCorrection::InputEnable( inputdata_t &inputdata )
{
m_bEnabled = true;
if ( m_flFadeInDuration > 0.0f )
{
FadeIn();
}
else
{
m_flCurWeight = m_flMaxWeight;
}
}
void CColorCorrection::InputDisable( inputdata_t &inputdata )
{
m_bEnabled = false;
if ( m_flFadeOutDuration > 0.0f )
{
FadeOut();
}
else
{
m_flCurWeight = 0.0f;
}
}
void CColorCorrection::InputSetFadeInDuration( inputdata_t& inputdata )
{
m_flFadeInDuration = inputdata.value.Float();
}
void CColorCorrection::InputSetFadeOutDuration( inputdata_t& inputdata )
{
m_flFadeOutDuration = inputdata.value.Float();
}
|