summaryrefslogtreecommitdiff
path: root/utils/vconfig/ManageGamesDialog.cpp
blob: f3706e69616085d6eb1ca20b11161e24e0113be1 (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include <windows.h>
#include <vgui/IVGui.h>
#include <vgui_controls/DirectorySelectDialog.h>
#include <vgui/IInput.h>
#include <KeyValues.h>

#include "vconfig_main.h"
#include "ManageGamesDialog.h"

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

using namespace vgui;

CManageGamesDialog *g_pManageGamesDialog = NULL;

class CModalDirectorySelectDialog : public vgui::DirectorySelectDialog
{
public:
	CModalDirectorySelectDialog( vgui::Panel *parent, const char *title )
		: vgui::DirectorySelectDialog( parent, title )
	{
		m_PrevAppFocusPanel = vgui::input()->GetAppModalSurface();
	}

	~CModalDirectorySelectDialog( void )
	{
		vgui::input()->SetAppModalSurface( m_PrevAppFocusPanel );
	}


public:
	vgui::VPANEL	m_PrevAppFocusPanel;
};

//-----------------------------------------------------------------------------
// Constructor
//-----------------------------------------------------------------------------
CManageGamesDialog::CManageGamesDialog( Panel *parent, const char *name, int configID ) : BaseClass( parent, name ), m_nConfigID( configID )
{
	Assert( !g_pManageGamesDialog );
	g_pManageGamesDialog = this;

	SetSize(384, 420);
	SetMinimumSize(200, 50);

	SetMinimizeButtonVisible( false );
	
	m_pGameNameEntry= new vgui::TextEntry( this, "GameName" );
	m_pGameDirEntry = new vgui::TextEntry( this, "GamePath" );
	
	LoadControlSettings( "ManageGamesDialog.res" );

	SetDeleteSelfOnClose( true );

	SetSizeable( false );
	MoveToCenterOfScreen();
}

//-----------------------------------------------------------------------------
// Destructor 
//-----------------------------------------------------------------------------
CManageGamesDialog::~CManageGamesDialog( void )
{
	g_pManageGamesDialog = NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Sets the game directory
//-----------------------------------------------------------------------------
void CManageGamesDialog::SetGameDir( const char *szDir )
{
	// Strip any trailing slashes
	char szGameDir[MAX_PATH];
	Q_strncpy( szGameDir, szDir, MAX_PATH );
	Q_StripTrailingSlash( szGameDir );

	m_pGameDirEntry->SetText( szGameDir );
}

//-----------------------------------------------------------------------------
// Purpose: Set the game name
//-----------------------------------------------------------------------------
void CManageGamesDialog::SetGameName( const char *szDir )
{
	m_pGameNameEntry->SetText( szDir );
}

//-----------------------------------------------------------------------------
// Purpose: Ensures the parameter name is unique
// Input  : *name - name to test
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CManageGamesDialog::IsGameNameUnique( const char *name )
{
	// Check all names
	for ( int i = 0; i < g_Configs.Count(); i++ )
	{
		// Skip ourself
		if ( i == m_nConfigID )
			continue;

		// Test it
		if ( Q_stricmp( name, g_Configs[i]->m_Name.Base() ) == 0 )
			return false;
	}

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Handles dialog commands
//-----------------------------------------------------------------------------
void CManageGamesDialog::OnCommand( const char *command )
{
	// Handle "OK" button
	if ( Q_stricmp( command, "Select" ) == 0 )
	{
		char textBuffer[1024];

		// Save out the data
		m_pGameNameEntry->GetText( textBuffer, sizeof( textBuffer ) );
		
		// Make sure we're not setting this to a duplicate
		if ( IsGameNameUnique( textBuffer ) == false )
		{
			// Select the text
			m_pGameNameEntry->SelectAllText( true );

			// Pop a message box and refuse to close
			VGUIMessageBox( this, "Error", "Game name %s already exists!  Please enter a unique name.", textBuffer );

			BaseClass::OnCommand( command );
			return;
		}

		KeyValues *actionSignal = new KeyValues("ManageSelect");

		if ( actionSignal == NULL )
		{
			Assert( 0 );
			return;
		}

		// See if we need to add a new config
		if ( m_nConfigID == NEW_CONFIG_ID )
		{
			// Create a new data container and point to it
			m_nConfigID = g_Configs.AddToTail( new CGameConfig() );

			// Send an overidden action signal to notify that we've added, not edited a field
			actionSignal->SetName("AddSelect");
		}

		// Otherwise take the name
		UtlStrcpy( g_Configs[m_nConfigID]->m_Name, textBuffer );
		
		// Take the game directory
		m_pGameDirEntry->GetText( textBuffer, sizeof( textBuffer ) );

		// Strip off the trailing slash always
		Q_StripTrailingSlash( textBuffer );

		UtlStrcpy( g_Configs[m_nConfigID]->m_ModDir, textBuffer );
		
		// Tell the parent we altered its data so it can refresh
		PostActionSignal( actionSignal );

		Close();
	}
	// Modified to allow more than one browse button
	else if ( Q_stricmp( command, "BrowseDir" ) == 0 )
	{
		// Create a new dialog
		CModalDirectorySelectDialog *pDlg = vgui::SETUP_PANEL( new CModalDirectorySelectDialog( this, "Select Game Directory" ) );
		
		char textBuffer[1024];
		m_pGameDirEntry->GetText( textBuffer, sizeof( textBuffer ) );
		
		// Get the currently set dir and use that as the start
		pDlg->ExpandTreeToPath( textBuffer );
		pDlg->MoveToCenterOfScreen();
		pDlg->AddActionSignalTarget( this );
		pDlg->SetDeleteSelfOnClose( true );
		pDlg->DoModal();
	}

	BaseClass::OnCommand( command );
}

//-----------------------------------------------------------------------------
// Purpose: Notify us that the directory dialog has returned a new entry
//-----------------------------------------------------------------------------
void CManageGamesDialog::OnChooseDirectory( const char *dir )
{
	SetGameDir( dir );
}