StudioModel *IFaceposerModels::GetStudioModel( int index )
{
	CFacePoserModel *m = GetEntry( index );
	if ( !m )
		return NULL;

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

	return m->GetModel();
}
void IFaceposerModels::CheckResetFlexes( void )
{
	int current_render_frame = g_MDLViewer->GetCurrentFrame();
	if ( current_render_frame == m_nLastRenderFrame )
		return;

	m_nLastRenderFrame = current_render_frame;

	// the phoneme editor just adds to the face, so reset the controllers 
	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

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

		CStudioHdr *hdr = model->GetStudioHdr();
		if ( !hdr )
			continue;

		for ( LocalFlexController_t i = LocalFlexController_t(0); i < hdr->numflexcontrollers(); i++ )
		{
			model->SetFlexController( i, 0.0f );
		}
	}
}
void IFaceposerModels::CheckResetFlexes( void )
{
	int current_render_frame = g_MDLViewer->GetCurrentFrame();
	if ( current_render_frame == m_nLastRenderFrame )
		return;

	m_nLastRenderFrame = current_render_frame;

	int c = Count();
	for ( int i = 0; i < c; i++ )
	{
		CFacePoserModel *m = GetEntry( i );
		if ( !m )
			continue;

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

		studiohdr_t *hdr = model->getStudioHeader();
		if ( !hdr )
			continue;

		for ( int i = 0; i < hdr->numflexcontrollers; i++ )
		{
			model->SetFlexController( i, 0.0f );
		}
	}
}
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();
}
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;
}
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 );
	}
}
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::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;
}