workspacemanager.cpp

来自「hl2 source code. Do not use it illegal.」· C++ 代码 · 共 1,903 行 · 第 1/3 页

CPP
1,903
字号
//-----------------------------------------------------------------------------
bool CWorkspaceManager::CloseWorkspace()
{
	CWorkspace *ws = GetBrowser()->GetWorkspace();
	if ( !ws )
		return true;

	if ( !ws->CanClose() )
		return false;

	delete ws;

	SetWorkspace( NULL );

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnUpdateTitle( void )
{
	CWorkspace *ws = GetBrowser()->GetWorkspace();

	char szTitle[ 256 ];
	if ( ws )
	{
		char const *fmt = g_appTitleFmt;
		if ( ws->IsDirty() )
		{
			fmt = g_appTitleFmtModified;
		}
		Q_snprintf( szTitle, sizeof( szTitle ), fmt, ws->GetName() );
	}
	else
	{
		Q_snprintf( szTitle, sizeof( szTitle ), g_appTitle );
	}

	setLabel( szTitle );

	UpdateMenus();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *ws - 
//-----------------------------------------------------------------------------
void CWorkspaceManager::SetWorkspace( CWorkspace *ws )
{
	GetBrowser()->SetWorkspace( ws );

	OnUpdateTitle();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnNewWorkspace()
{
	// Show file io
	const char *fullpath = mxGetSaveFileName( 
		0, 
		".", 
		"*.vsw" );

	if ( !fullpath || !fullpath[ 0 ] )
		return;

	// Strip game directory and slash
	char workspace_name[ 512 ];
	filesystem->FullPathToRelativePath( fullpath, workspace_name );

	StripExtension( workspace_name );
	DefaultExtension( workspace_name, ".vsw" );

	if ( filesystem->FileExists( workspace_name ) )
	{
		Con_Printf( "%s exists already!\n", workspace_name );
		Assert( 0 );
		return;
	}

	LoadWorkspace( workspace_name );
}

void CWorkspaceManager::AddFileToRecentWorkspaceList( char const *filename )
{
	int i;
	int c = m_RecentFiles.Count();

	for ( i = 0; i < c; i++ )
	{
		if (!Q_stricmp( m_RecentFiles[i].filename, filename ))
			break;
	}

	// swap existing recent file
	if ( i < c )
	{
		RecentFile rf = m_RecentFiles[0];
		m_RecentFiles[ 0 ] = m_RecentFiles[ i ];
		m_RecentFiles[ i ] = rf;
	}
	// insert recent file
	else
	{
		RecentFile rf;
		Q_strcpy( rf.filename, filename );

		m_RecentFiles.AddToHead( rf );
	}

	while( m_RecentFiles.Count() > GetMaxRecentFiles() )
	{
		m_RecentFiles.Remove( m_RecentFiles.Count() - 1 );
	}

	UpdateRecentFilesMenu();
}

void CWorkspaceManager::LoadRecentFilesMenuFromDisk()
{
	KeyValues *kv = new KeyValues( "recentfiles" );
	if ( kv->LoadFromFile( filesystem, RECENT_FILES_FILE ) )
	{
		for ( KeyValues *sub = kv->GetFirstSubKey(); sub; sub = sub->GetNextKey() )
		{
			RecentFile rf;
			Q_strncpy( rf.filename, sub->GetString(), sizeof( rf ) );
			m_RecentFiles.AddToTail( rf );
		}
	}
	kv->deleteThis();

	UpdateRecentFilesMenu();
}

void CWorkspaceManager::AutoLoad( char const *workspace )
{
	if ( workspace )
	{
		LoadWorkspace( workspace );
	}
	else
	{
		if ( m_RecentFiles.Count() > 0 )
		{
			LoadWorkspace( m_RecentFiles[ 0 ].filename );
		}
	}
}

void CWorkspaceManager::SaveRecentFilesMenuToDisk()
{
	int i;
	int c = m_RecentFiles.Count();

	CUtlBuffer buf( 0, 0, true );

	buf.Printf( "recentfiles\n{\n" );

	for ( i = 0; i < c; i++ )
	{
		buf.Printf( "\t\"%i\"\t\"%s\"\n", i + 1, m_RecentFiles[ i ].filename );
	}

	buf.Printf( "}\n" );

	char const *recentfiles = RECENT_FILES_FILE;

	// Write it out baby
	FileHandle_t fh = filesystem->Open( recentfiles, "wt" );
	if (fh)
	{
		filesystem->Write( buf.Base(), buf.TellPut(), fh );
		filesystem->Close(fh);
	}
	else
	{
		Con_Printf( "CWorkspace::SaveRecentFilesMenuToDisk:  Unable to write file %s!!!\n", recentfiles );
	}
}

void CWorkspaceManager::UpdateRecentFilesMenu()
{
	int c = m_RecentFiles.Count();

	while ( c > m_nRecentMenuItems )
	{
		m_pRecentFileMenu->add( "(empty)", IDC_WSM_FILE_RECENT_WORKSPACE_START + m_nRecentMenuItems++ );
	}

	for (int i = 0; i < c; i++)
	{
		m_pMenuBar->modify (IDC_WSM_FILE_RECENT_WORKSPACE_START + i, IDC_WSM_FILE_RECENT_WORKSPACE_START + i, m_RecentFiles[i].filename );
	}
}

void CWorkspaceManager::LoadWorkspace( char const *filename )
{
	if ( !CloseWorkspace() )
		return;

	Con_Printf( "Loading workspace %s\n", filename );
	
	CWorkspace *wks = new CWorkspace( filename );
	SetWorkspace( wks );

	AddFileToRecentWorkspaceList( filename );

	OnUpdateTitle();

	UpdateMenus();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnOpenWorkspace()
{
	// Show file io
	const char *fullpath = mxGetOpenFileName( 
		0, 
		".", 
		"*.vsw" );

	if ( !fullpath || !fullpath[ 0 ] )
		return;

	// Strip game directory and slash
	char workspace_name[ 512 ];
	filesystem->FullPathToRelativePath( fullpath, workspace_name );

	LoadWorkspace( workspace_name );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnCloseWorkspace()
{
	if ( !CloseWorkspace() )
		return;

	Con_Printf( "Closed workspace\n" );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnSaveWorkspace()
{
	CWorkspace *ws = GetBrowser()->GetWorkspace();
	if ( ws )
	{
		ws->SaveChanges();

		OnUpdateTitle();
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnNewProject()
{
	Con_Printf( "OnNewProject()\n" );

	// Show file io
	const char *fullpath = mxGetSaveFileName( 
		0, 
		".", 
		"*.vsp" );

	if ( !fullpath || !fullpath[ 0 ] )
		return;

	// Strip game directory and slash
	char project_name[ 512 ];
	filesystem->FullPathToRelativePath( fullpath, project_name );

	StripExtension( project_name );
	DefaultExtension( project_name, ".vsp" );

	if ( filesystem->FileExists( project_name ) )
	{
		Con_Printf( "%s exists already!\n", project_name );
		Assert( 0 );
		return;
	}

	CWorkspace *ws = GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		// Create one on the fly
		char workspace_name[ 512 ];
		Q_strcpy( workspace_name, project_name );

		StripExtension( workspace_name );
		DefaultExtension( workspace_name, ".vsw" );

		if ( !filesystem->FileExists( workspace_name ) )
		{
			int retval = mxMessageBox( NULL, va( "Automatically create workspace %s?", workspace_name ), g_appTitle, MX_MB_YESNOCANCEL );
			if ( retval != 0 )
			{
				Con_Printf( "Canceling project creation\n" );
				return;
			}
		}
		else
		{
			Con_Printf( "Found workspace '%s', automatically loading...\n", workspace_name );
		}

		LoadWorkspace( workspace_name );

		ws = GetBrowser()->GetWorkspace();
	}

	if ( ws && ws->FindProjectFile( project_name ) )
	{
		Con_Printf( "Project %s already exists in workspace\n" );
		return;
	}

	// Create a new project and add it into current workspace
	CProject *proj = new CProject( ws, project_name );
	Assert( proj );
	Assert( ws );

	if ( ws )
	{
		GetBrowser()->AddProject( proj );
	}

	OnUpdateTitle();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnInsertProject()
{
	Con_Printf( "OnInsertProject()\n" );

	// Show file io
	const char *fullpath = mxGetOpenFileName( 
		0, 
		".", 
		"*.vsp" );

	if ( !fullpath || !fullpath[ 0 ] )
		return;

	// Strip game directory and slash
	char project_name[ 512 ];
	filesystem->FullPathToRelativePath( fullpath, project_name );

	StripExtension( project_name );
	DefaultExtension( project_name, ".vsp" );

	CWorkspace *ws = GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		// Create one on the fly
		char workspace_name[ 512 ];
		Q_strcpy( workspace_name, project_name );

		StripExtension( workspace_name );
		DefaultExtension( workspace_name, ".vsw" );

		if ( !filesystem->FileExists( workspace_name ) )
		{
			int retval = mxMessageBox( NULL, va( "Automatically create workspace %s?", workspace_name ), g_appTitle, MX_MB_YESNOCANCEL );
			if ( retval != 0 )
			{
				Con_Printf( "Canceling project creation\n" );
				return;
			}
		}
		else
		{
			Con_Printf( "Found workspace '%s', automatically loading...\n", workspace_name );
		}

		LoadWorkspace( workspace_name );

		ws = GetBrowser()->GetWorkspace();
	}

	if ( ws && ws->FindProjectFile( project_name ) )
	{
		Con_Printf( "Project %s already exists in workspace\n", project_name );
		return;
	}

	// Create a new project and add it into current workspace
	CProject *proj = new CProject( ws, project_name );
	Assert( proj );
	Assert( ws );

	if ( ws )
	{
		GetBrowser()->AddProject( proj );
	}

	OnUpdateTitle();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnRemoveProject()
{
	Con_Printf( "OnRemoveProject()\n" );

	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( !item )
		return;

	CProject *project = item->GetProject();
	if ( !project )
	{
		Con_Printf( "Can't remove project, item is not a project\n" );
		return;
	}

	CWorkspace *ws = GetBrowser()->GetWorkspace();
	if ( !ws )
	{
		Con_Printf( "Can't remove project '%s', no current workspace?!\n",
			project->GetName() );
		return;
	}

	ws->RemoveProject( project );
	Con_Printf( "Removed project '%s'\n", project->GetName() );
	delete project;

	GetBrowser()->PopulateTree();

	OnUpdateTitle();
}

void CWorkspaceManager::OnRemoveScene()
{
	Con_Printf( "OnRemoveScene()\n" );

	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( !item )
		return;

	CScene *scene = item->GetScene();
	if ( !scene )
	{
		Con_Printf( "Can't remove scene, item is not a scene\n" );
		return;
	}

	CProject *project = scene->GetOwnerProject();
	if ( !project )
	{
		Con_Printf( "Can't remove scene '%s', no current owner project?!\n",
			scene->GetName() );
		return;
	}

	project->RemoveScene( scene );
	Con_Printf( "Removed scene '%s'\n", scene->GetName() );
	delete scene;

	GetBrowser()->PopulateTree();

	OnUpdateTitle();
}

void CWorkspaceManager::OnNewScene()
{
	Con_Printf( "OnNewScene()\n" );

	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( !item )
		return;

	CProject *project = item->GetProject();
	if ( !project )
	{
		Con_Printf( "Can't add new scene, selected item is not a project\n" );
		return;
	}

	CInputParams params;
	memset( &params, 0, sizeof( params ) );
	Q_snprintf( params.m_szDialogTitle, sizeof( params.m_szDialogTitle ), "Create scene in '%s'", project->GetName() );
	Q_strcpy( params.m_szPrompt, "Scene Name:" );
	Q_strcpy( params.m_szInputText, "" );

	if ( !InputProperties( &params ) )
		return;

	if ( !params.m_szInputText[ 0 ] )
	{
		Con_Printf( "You must name the scene!\n" );
		return;
	}

	CScene *scene = new CScene( project, params.m_szInputText );
	Assert( scene );

	project->AddScene( scene );

	Con_Printf( "Added scene '%s' to project '%s'\n", scene->GetName(),
		project->GetName() );

	GetBrowser()->PopulateTree();

	OnUpdateTitle();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnModifyProjectComments()
{
	Con_Printf( "OnModifyProjectComments()\n" );

	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( !item )
		return;

	CProject *project = item->GetProject();
	if ( !project )
	{
		Con_Printf( "Can't modify comments, item is not a project\n" );
		return;
	}

	CInputParams params;
	memset( &params, 0, sizeof( params ) );
	Q_snprintf( params.m_szDialogTitle, sizeof( params.m_szDialogTitle ), "Edit comments for '%s'", project->GetName() );
	Q_strcpy( params.m_szPrompt, "Comments:" );
	Q_strncpy( params.m_szInputText, project->GetComments(), sizeof( params.m_szInputText ) );

	if ( !InputProperties( &params ) )
		return;

	if ( !Q_strcmp( params.m_szInputText, project->GetComments() ) )
		return;

	project->SetComments( params.m_szInputText );

	GetBrowser()->PopulateTree();

	OnUpdateTitle();
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : index - 
//-----------------------------------------------------------------------------
void CWorkspaceManager::OnRecentWorkspace( int index )
{
	if ( index < 0 || index >= m_RecentFiles.Count() )
		return;

	LoadWorkspace( m_RecentFiles[ index ].filename );
}

void CWorkspaceManager::OnDoubleClicked( ITreeItem *item )
{
	CSoundEntry *s = item->GetSoundEntry();
	if ( s )
	{
		s->Play();
		return;
	}
}

void CWorkspaceManager::OnSoundPlay()
{
	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( item->GetSoundEntry() )
	{
		item->GetSoundEntry()->Play();
	}
	else if ( item->GetWaveFile() )
	{
		item->GetWaveFile()->Play();
	}
}

void CWorkspaceManager::OnSoundToggleVoiceDuck()
{
	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( item->GetWaveFile() )
	{
		item->GetWaveFile()->ToggleVoiceDucking();
	}
}

void CWorkspaceManager::OnSoundEditText()
{
	ITreeItem *item = GetBrowser()->GetSelectedItem();
	if ( !item )
		return;

	CWaveFile *s = item->GetWaveFile();
	if ( !s )
		return;

	CInputParams params;
	memset( &params, 0, sizeof( params ) );
	Q_snprintf( params.m_szDialogTitle, sizeof( params.m_szDialogTitle ), "Edit text of '%s'", s->GetName() );
	Q_strcpy( params.m_szPrompt, "Sentence text:" );
	Q_strcpy( params.m_szInputText, s->GetSentenceText() );

	if ( !InputProperties( &params ) )
		return;

	if ( !Q_stricmp( params.m_szInputText, s->GetSentenceText() ) )
	{
		return;
	}

	s->SetSentenceText( params.m_szInputText );
	
	GetBrowser()->PopulateTree();

	OnUpdateTitle();
}

// Scene entries
void CWorkspaceManager::OnSceneAddVCD()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?