forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcl_pluginhelpers.cpp
More file actions
637 lines (534 loc) · 18.5 KB
/
cl_pluginhelpers.cpp
File metadata and controls
637 lines (534 loc) · 18.5 KB
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: baseclientstate.cpp: implementation of the CBaseClientState class.
//
//===========================================================================//
#include "client_pch.h"
#include "limits.h"
#include "cl_pluginhelpers.h"
#include <inetchannel.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include <vgui/ILocalize.h>
#include <vgui/IVGui.h>
#include <vgui/IPanel.h>
#include <vgui_controls/Controls.h>
#include <vgui_controls/Frame.h>
#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/RichText.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/TextEntry.h>
#include <vgui_controls/ImagePanel.h>
#include <vgui_controls/AnimationController.h>
#include "vgui_baseui_interface.h"
#include "vgui_askconnectpanel.h"
#include "cmd.h"
#include "tier1/convar.h"
#include "baseclientstate.h"
extern CClientState cl;
//-----------------------------------------------------------------------------
// Purpose: Displays the options menu
//-----------------------------------------------------------------------------
class CPluginMenu : public vgui::EditablePanel
{
private:
DECLARE_CLASS_SIMPLE( CPluginMenu, vgui::EditablePanel );
public:
CPluginMenu( vgui::Panel *parent );
virtual ~CPluginMenu();
void Show( KeyValues *kv );
void OnCommand(const char *command);
};
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CPluginMenu::CPluginMenu( vgui::Panel *parent ) : EditablePanel(parent, "PluginMenu" )
{
LoadControlSettings("Resource/UI/PluginMenu.res");
}
//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CPluginMenu::~CPluginMenu()
{
}
//-----------------------------------------------------------------------------
// Purpose: Show the options menu after using key values to configure it
//-----------------------------------------------------------------------------
void CPluginMenu::Show( KeyValues *kv )
{
vgui::Label *control = dynamic_cast<vgui::Label *>(FindChildByName("Text"));
if (control)
{
control->SetText( kv->GetWString( "msg" ) );
}
int i = 0;
// hide all the buttons
for ( i = 0; i < GetChildCount(); i++ )
{
vgui::Button *button = dynamic_cast<vgui::Button *>(GetChild(i));
if ( button )
{
button->SetVisible( false );
}
}
i = 1;
// now work out what buttons to display
for ( KeyValues *pCur=kv->GetFirstTrueSubKey(); pCur; pCur=pCur->GetNextTrueSubKey(), i++ )
{
char controlName[64];
Q_snprintf( controlName, sizeof(controlName), "option%i", i );
vgui::Button *button = dynamic_cast<vgui::Button *>(FindChildByName(controlName,true));
Assert( button );
if ( button )
{
button->SetText( pCur->GetWString( "msg" ));
button->SetCommand( pCur->GetString( "command" ));
button->SetVisible( true );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: when a button is pressed send that command back to the engine
//-----------------------------------------------------------------------------
void CPluginMenu::OnCommand( const char *command )
{
Cbuf_AddText( command );
Cbuf_AddText( "\n" );
CallParentFunction( new KeyValues( "Command", "command", "close" ) );
}
//-----------------------------------------------------------------------------
// Purpose: Displays the gameui portion of plugin menus
//-----------------------------------------------------------------------------
class CPluginGameUIDialog : public vgui::Frame
{
private:
DECLARE_CLASS_SIMPLE( CPluginGameUIDialog, vgui::Frame );
public:
CPluginGameUIDialog();
virtual ~CPluginGameUIDialog();
virtual void Show( DIALOG_TYPE type, KeyValues *kv );
protected:
void OnCommand( const char *cmd );
private:
CPluginMenu *m_Menu;
vgui::RichText *m_RichText;
vgui::Label *m_Message;
vgui::TextEntry *m_Entry;
vgui::Label *m_EntryLabel;
vgui::Button *m_CloseButton;
char m_szEntryCommand[ 255 ];
};
//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CPluginGameUIDialog::CPluginGameUIDialog() : vgui::Frame( NULL, "Plugins" )
{
// initialize dialog
SetTitle( "Plugins", true );
SetAlpha( 255 );
SetScheme( "Tracker" );
m_szEntryCommand[ 0 ] = 0;
m_Menu = new CPluginMenu( this );
m_RichText = new vgui::RichText( this, "Rich" );
m_Message = new vgui::Label( this, "Label", "" );
m_Entry = new vgui::TextEntry( this, "Entry" );
m_EntryLabel = new vgui::Label( this, "EntryLabel", "" );
m_CloseButton = new vgui::Button( this, "Close", "");
LoadControlSettings("Resource/UI/Plugin.res");
InvalidateLayout();
}
//-----------------------------------------------------------------------------
// Purpose: destructor
//-----------------------------------------------------------------------------
CPluginGameUIDialog::~CPluginGameUIDialog()
{
}
//-----------------------------------------------------------------------------
// Purpose: called when the close button is pressed
//-----------------------------------------------------------------------------
void CPluginGameUIDialog::OnCommand( const char *cmd )
{
if ( !Q_stricmp( cmd, "close" ) )
{
if ( Q_strlen(m_szEntryCommand) > 0 )
{
// Check that we can add the two execution markers
if ( !Cbuf_HasRoomForExecutionMarkers( 2 ) )
{
AssertMsg( false, "CPluginGameUIDialog::OnCommand called but there is no room for the execution markers. Ignoring command." );
return;
}
char userCMD[ 512 ];
char entryText[ 255 ];
m_Entry->GetText( entryText, sizeof(entryText) );
Q_snprintf( userCMD, sizeof(userCMD), "%s %s\n", m_szEntryCommand, entryText );
// Only let them run commands marked with FCVAR_CLIENTCMD_CAN_EXECUTE.
Cbuf_AddTextWithMarkers( eCmdExecutionMarker_Enable_FCVAR_CLIENTCMD_CAN_EXECUTE, userCMD, eCmdExecutionMarker_Disable_FCVAR_CLIENTCMD_CAN_EXECUTE );
}
Close();
g_PluginManager->OnPanelClosed();
}
else
{
BaseClass::OnCommand( cmd );
}
}
//-----------------------------------------------------------------------------
// Purpose: shows a precanned style of message in the GameUI
//-----------------------------------------------------------------------------
void CPluginGameUIDialog::Show( DIALOG_TYPE type, KeyValues *kv )
{
m_Menu->SetVisible(false);
m_RichText->SetVisible(false);
m_Message->SetVisible(false);
m_Entry->SetVisible(false);
m_EntryLabel->SetVisible(false);
m_szEntryCommand[ 0 ] = 0;
SetTitle( kv->GetWString( "title" ), true );
switch ( type )
{
case DIALOG_MENU: // a options menu
m_Menu->Show( kv );
m_Menu->SetVisible( true );
break;
case DIALOG_TEXT: // a richtext dialog
m_RichText->SetText( kv->GetWString( "msg" ) );
m_RichText->SetVisible( true );
break;
case DIALOG_MSG: // just a msg to the screen, don't display in the gameui
SetVisible( false );
return;
break;
case DIALOG_ENTRY:
m_Entry->SetVisible( true );
m_EntryLabel->SetVisible( true );
m_EntryLabel->SetText( kv->GetWString( "msg" ) );
Q_strncpy( m_szEntryCommand, kv->GetString( "command" ), sizeof(m_szEntryCommand) );
m_CloseButton->SetText( "#GameUI_OK" );
break;
default:
Msg( "Invalid menu type (%i)\n", type );
break;
}
Activate();
}
//-----------------------------------------------------------------------------
// Purpose: the individual message snippets
//-----------------------------------------------------------------------------
class CMessage : public vgui::Label
{
private:
DECLARE_CLASS_SIMPLE( CMessage, vgui::Label );
public:
CMessage(vgui::Panel *parent, const char *panelName, const char *text);
~CMessage();
bool HasExtraPanel() { return m_bHasExtraPanel; }
protected:
void ApplySchemeSettings( vgui::IScheme *pScheme );
private:
bool m_bHasExtraPanel;
};
CMessage::CMessage( vgui::Panel *parent, const char *panelName, const char *text ) : vgui::Label( parent, panelName, text )
{
m_bHasExtraPanel = false;
}
CMessage::~CMessage()
{
}
void CMessage::ApplySchemeSettings( vgui::IScheme *pScheme )
{
vgui::HFont font = pScheme->GetFont( "PluginText", false );
if ( font == vgui::INVALID_FONT )
{
font = pScheme->GetFont( "HudHintText", false );
}
SetFont(font);
BaseClass::ApplySchemeSettings( pScheme );
}
//-----------------------------------------------------------------------------
// Purpose: the hud plugin message panel
//-----------------------------------------------------------------------------
class CPluginHudMessage : public vgui::Frame
{
private:
DECLARE_CLASS_SIMPLE( CPluginHudMessage, vgui::Frame );
public:
CPluginHudMessage( vgui::VPANEL parent );
~CPluginHudMessage();
void ShowMessage( const wchar_t *message, int time, Color clr, bool bHasExtraPanel );
void StartHiding();
void Hide();
protected:
void ApplySchemeSettings( vgui::IScheme *pScheme );
void OnTick();
void OnSizeChanged( int newWide, int newTall );
private:
enum { MESSAGE_X_INSET = 40, MAX_TEXT_LEN_PIXELS = 400 };
CMessage *m_Message;
vgui::ImagePanel *m_pExtraPanelIcon;
bool m_bHidingControl;
int m_iTargetH, m_iTargetW;
Color m_fgColor;
vgui::AnimationController *m_pAnimationController;
};
//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CPluginHudMessage::CPluginHudMessage( vgui::VPANEL parent ) : vgui::Frame( NULL, "PluginHudMessage" )
{
SetParent( parent );
SetVisible( false );
SetAlpha( 255 );
SetMinimumSize( 10 , 10 );
SetScheme( "ClientScheme" );
SetMoveable(false);
SetSizeable(false);
SetKeyBoardInputEnabled( false );
SetMouseInputEnabled( false );
SetTitleBarVisible( false );
m_pExtraPanelIcon = new vgui::ImagePanel( this, "ExtraPanelIcon" );
m_pExtraPanelIcon->SetVisible( false );
m_Message = new CMessage( this, "Msg", "");
m_Message->SetVisible( false );
m_pAnimationController = new vgui::AnimationController( NULL );
m_pAnimationController->SetParent( parent );
m_pAnimationController->SetScriptFile( parent, "scripts/plugin_animations.txt" );
m_pAnimationController->SetProportional( false );
vgui::ivgui()->AddTickSignal(GetVPanel());
LoadControlSettings("Resource/UI/PluginHud.res");
InvalidateLayout();
GetSize( m_iTargetW, m_iTargetH );
}
//-----------------------------------------------------------------------------
// Purpose: destructor
//-----------------------------------------------------------------------------
CPluginHudMessage::~CPluginHudMessage()
{
}
//-----------------------------------------------------------------------------
// Purpose: set the see through color and rounded corners
//-----------------------------------------------------------------------------
void CPluginHudMessage::ApplySchemeSettings( vgui::IScheme *pScheme )
{
m_pExtraPanelIcon->SetImage( vgui::scheme()->GetImage( "plugin/message_waiting", true ) );
BaseClass::ApplySchemeSettings( pScheme );
SetBgColor( pScheme->GetColor( "Plugins.BgColor", pScheme->GetColor( "TransparentBlack", Color( 0, 0, 0, 192 ))) );
SetPaintBackgroundType( 2 );
m_Message->SetFgColor( m_fgColor );
m_pExtraPanelIcon->SetVisible( !m_bHidingControl );
}
//-----------------------------------------------------------------------------
// Purpose: run the anim controller and hide the message label if the anim var says to
//-----------------------------------------------------------------------------
void CPluginHudMessage::OnTick()
{
m_pAnimationController->UpdateAnimations( Sys_FloatTime() );
BaseClass::OnTick();
}
//-----------------------------------------------------------------------------
// Purpose: get the label size to track
//-----------------------------------------------------------------------------
void CPluginHudMessage::OnSizeChanged( int newWide, int newTall )
{
BaseClass::OnSizeChanged( newWide, newTall );
int w, h;
GetSize( w, h );
m_Message->SetBounds( MESSAGE_X_INSET, 5, w - MESSAGE_X_INSET - 10, h - 10 );
}
//-----------------------------------------------------------------------------
// Purpose: start the shrinking anim for the control if it should be showed, the hide anim otherwsise
//-----------------------------------------------------------------------------
void CPluginHudMessage::StartHiding()
{
if ( m_pExtraPanelIcon->IsVisible() )
{
m_pAnimationController->StartAnimationSequence( "PluginMessageSmall" );
}
else
{
Hide();
}
}
//-----------------------------------------------------------------------------
// Purpose: starts the hide anim for the control
//-----------------------------------------------------------------------------
void CPluginHudMessage::Hide()
{
m_pAnimationController->StartAnimationSequence( "PluginMessageHide" );
m_pExtraPanelIcon->SetVisible( false );
}
//-----------------------------------------------------------------------------
// Purpose: shows a text message on the hud
//-----------------------------------------------------------------------------
void CPluginHudMessage::ShowMessage( const wchar_t *text, int time, Color clr, bool bHasExtraPanel )
{
m_Message->SetVisible( true );
m_Message->SetBounds( MESSAGE_X_INSET, 5, m_iTargetW - MESSAGE_X_INSET - 10, m_iTargetH - 10 );
m_Message->SetText( text );
m_Message->SetFgColor( clr );
m_fgColor = clr;
m_bHidingControl = !bHasExtraPanel;
if ( bHasExtraPanel )
{
m_pExtraPanelIcon->SetVisible( true );
}
m_pAnimationController->StartAnimationSequence( "PluginMessageShow" );
SetVisible( true );
InvalidateLayout();
int textW, textH;
m_Message->GetContentSize( textW, textH );
textW = min( textW + MESSAGE_X_INSET + 10, (int)MAX_TEXT_LEN_PIXELS );
SetSize( textW, m_iTargetH ); // the "small" animation event changes our size
}
//-----------------------------------------------------------------------------
// Purpose: constructor
//-----------------------------------------------------------------------------
CPluginUIManager::CPluginUIManager() : vgui::Panel( NULL, "PluginManager" )
{
m_iCurPriority = INT_MAX;
m_iMessageDisplayUntil = 0;
m_iHudDisplayUntil = 0;
m_bShutdown = false;
m_pGameUIDialog = new CPluginGameUIDialog();
Assert( m_pGameUIDialog );
m_pGameUIDialog->SetParent( EngineVGui()->GetPanel( PANEL_GAMEUIDLL ) );
m_pHudMessage = new CPluginHudMessage(EngineVGui()->GetPanel( PANEL_CLIENTDLL ));
Assert( m_pHudMessage );
vgui::ivgui()->AddTickSignal(GetVPanel());
}
//-----------------------------------------------------------------------------
// Purpose: destructor
//-----------------------------------------------------------------------------
CPluginUIManager::~CPluginUIManager()
{
}
//-----------------------------------------------------------------------------
// Purpose: hides the two plugin dialogs at the appropriate times
//-----------------------------------------------------------------------------
void CPluginUIManager::OnTick()
{
if ( m_bShutdown )
{
return;
}
if ( m_iMessageDisplayUntil != 0 && !EngineVGui()->IsGameUIVisible() && m_iMessageDisplayUntil < Sys_FloatTime() ) // check the GameUI large message
{
m_pGameUIDialog->SetVisible( false );
m_pHudMessage->Hide();
m_iMessageDisplayUntil = 0;
m_iCurPriority = INT_MAX;
}
if ( m_iHudDisplayUntil != 0 && m_iHudDisplayUntil < Sys_FloatTime() ) // check the hud panel
{
m_pHudMessage->StartHiding();
m_iHudDisplayUntil = 0;
}
BaseClass::OnTick();
}
//-----------------------------------------------------------------------------
// Purpose: shuts down the plugin UI
//-----------------------------------------------------------------------------
void CPluginUIManager::Shutdown()
{
vgui::ivgui()->RemoveTickSignal(GetVPanel());
m_pHudMessage->Hide();
m_pGameUIDialog->SetVisible( false );
MarkForDeletion();
m_bShutdown = true;
}
//-----------------------------------------------------------------------------
// Purpose: shows a particular ui type and queues their lifetime
//-----------------------------------------------------------------------------
void CPluginUIManager::Show( DIALOG_TYPE type, KeyValues *kv )
{
// Check for the special DIALOG_ASKCONNECT command.
if ( type == DIALOG_ASKCONNECT )
{
// Don't allow this prompt on QuickPlay servers
if ( cl.IsClientConnectionViaMatchMaking() )
return;
// Do the askconnect dialog.
float flDuration = kv->GetFloat( "time", 4.0f );
const char *pIP = kv->GetString( "title", NULL );
if ( !pIP )
{
DevMsg( "Ignoring DIALOG_ASKCONNECT message. No IP specified." );
return;
}
ShowAskConnectPanel( pIP, flDuration );
return;
}
int level = kv->GetInt( "level", INT_MAX );
if ( level < m_iCurPriority )
{
m_iCurPriority = level;
}
else
{
DevMsg( "Ignoring message %s, %i < %i\n", kv->GetName(), level, m_iCurPriority );
return;
}
if ( type != DIALOG_MSG )
{
m_iMessageDisplayUntil = Sys_FloatTime() + min(max( kv->GetInt( "time", 10 ),10),200);
}
else
{
m_iMessageDisplayUntil = Sys_FloatTime() + 10;
}
m_iHudDisplayUntil = Sys_FloatTime() + 10; // hud messages only get 10 seconds
m_pGameUIDialog->Show( type, kv );
Color clr( 255, 255, 255, 255 );
if ( !kv->IsEmpty( "color" ) )
{
clr = kv->GetColor( "color" );
}
m_pHudMessage->ShowMessage( kv->GetWString( "title" ), 10, clr, type != DIALOG_MSG );
}
//-----------------------------------------------------------------------------
// Purpose: called when the gameui panel is closed
//-----------------------------------------------------------------------------
void CPluginUIManager::OnPanelClosed()
{
m_iCurPriority = INT_MAX;
m_iHudDisplayUntil = 0;
m_iMessageDisplayUntil = 0;
m_pGameUIDialog->SetVisible( false );
m_pHudMessage->Hide();
}
void CPluginUIManager::GetHudMessagePosition( int &x, int &y, int &wide, int &tall )
{
if ( m_pHudMessage )
{
m_pHudMessage->GetBounds( x, y, wide, tall );
}
else
{
x = y = wide = tall = 0;
}
}
CPluginUIManager *g_PluginManager = NULL;
//=============================================================================
//
// external interfaces
//
//=============================================================================
ConVar cl_showpluginmessages ( "cl_showpluginmessages", "1", FCVAR_ARCHIVE, "Allow plugins to display messages to you" );
void PluginHelpers_Menu( SVC_Menu *msg )
{
if ( !msg->m_MenuKeyValues )
{
return;
}
if ( !cl_showpluginmessages.GetBool() )
{
return;
}
if ( !g_PluginManager )
{
g_PluginManager = new CPluginUIManager();
}
g_PluginManager->Show( msg->m_Type, msg->m_MenuKeyValues );
}