char const *IFaceposerModels::GetModelFileName( int index )
{
	CFacePoserModel *entry = GetEntry( index );
	if ( !entry )
		return "";

	return entry->GetModelFileName();
}
bool IFaceposerModels::IsModelShownIn3DView( int modelindex )
{
	CFacePoserModel *m = GetEntry( modelindex );
	if ( !m )
		return false;

	return m->GetVisibleIn3DView();
}
void IFaceposerModels::ShowModelIn3DView( int modelindex, bool show )
{
	CFacePoserModel *m = GetEntry( modelindex );
	if ( !m )
		return;

	m->SetVisibleIn3DView( show );
}
void IFaceposerModels::SetActorNameForModel( int modelindex, char const *actorname )
{
	CFacePoserModel *m = GetEntry( modelindex );
	if ( !m )
		return;

	m->SetActorName( actorname );
}
void IFaceposerModels::RecreateAllAnimationBitmaps( int modelindex )
{
	CFacePoserModel *m = GetEntry( modelindex );
	if ( !m )
		return;

	m->RecreateAllAnimationBitmaps();

}
mxbitmapdata_t *IFaceposerModels::GetBitmapForSequence( int modelindex, int sequence )
{
	static mxbitmapdata_t nullbitmap;
	CFacePoserModel *m = GetEntry( modelindex );
	if ( !m )
		return &nullbitmap;

	return m->GetBitmapForSequence( sequence );
}
StudioModel *IFaceposerModels::GetStudioModel( int index )
{
	CFacePoserModel *m = GetEntry( index );
	if ( !m )
		return NULL;

	if ( !m->GetModel() )
		return NULL;

	return m->GetModel();
}
void IFaceposerModels::RestoreModels( void )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		m->Restore();
	}
}
int IFaceposerModels::GetIndexForStudioModel( StudioModel *model )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		if ( m->GetModel() == model )
			return i;
	}
	return -1;
}
void IFaceposerModels::FreeModel( int index  )
{
	CFacePoserModel *entry = GetEntry( index );
	if ( !entry )
		return;

	StudioModel *m = entry->GetModel();
	m->FreeModel();
	delete m;

	m_Models.Remove( index );

	g_MDLViewer->InitModelTab();
}
void IFaceposerModels::SaveModelList( void )
{
	workspacefiles->StartStoringFiles( IWorkspaceFiles::MODELDATA );
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		workspacefiles->StoreFile( IWorkspaceFiles::MODELDATA, m->GetModelFileName() );
	}
	workspacefiles->FinishStoringFiles( IWorkspaceFiles::MODELDATA );
}
StudioModel *IFaceposerModels::GetModelForActor( char const *actorname )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		if ( !stricmp( m->GetActorName(), actorname ) )
			return m->GetModel();
	}

	return NULL;
}
int IFaceposerModels::FindModelByFilename( char const *filename )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		if ( !stricmp( m->GetModelFileName(), filename ) )
			return i;
	}

	return -1;
}
void IFaceposerModels::ClearModelTargets( bool force /*=false*/ )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		StudioModel *mdl = m->GetModel();
		if ( !mdl )
			continue;

		mdl->ClearLookTargets();
	}
}
void IFaceposerModels::ClearOverlaysSequences( void )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		StudioModel *model = m->GetModel();
		if ( !model )
			continue;

		model->ClearOverlaysSequences();
	}
}
void IFaceposerModels::SetSolveHeadTurn( int solve )
{
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		StudioModel *mdl = m->GetModel();
		if ( !mdl )
			continue;

		mdl->SetSolveHeadTurn( solve );
	}
}
int IFaceposerModels::CountVisibleModels( void )
{
	int num = 0;
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		if ( m->GetVisibleIn3DView() )
		{
			num++;
		}
	}

	return num;
}
void IFaceposerModels::FreeModel( int index  )
{
	CFacePoserModel *entry = GetEntry( index );
	if ( !entry )
		return;

	StudioModel *m = entry->GetModel();

	SaveViewerSettings( m->GetFileName(), m );

	m->FreeModel( false );
	delete m;

	delete entry;
	m_Models.Remove( index );

	g_MDLViewer->InitModelTab();
}
int IFaceposerModels::CountActiveSources()
{
	int count = 0;
	
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

		StudioModel *model = m->GetModel();
		if ( !model )
			continue;

		count += model->m_mouth.GetNumVoiceSources();
	}

	return count;
}