//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBaseActionTextMessageStartDialog::Init( void )
{
	LoadControlSettings( "resource\\BaseActionTextMessageStartDialog.res" );

	BaseClass::Init();

	client_textmessage_t *tm = GetAction()->GetTextMessage();

	m_pX->SetText( va( "%f", tm->x ) );
	m_pY->SetText( va( "%f", tm->y ) );

	m_pFadeInTime->SetText( va( "%.3f", tm->fadein ) );
	m_pFadeOutTime->SetText( va( "%.3f", tm->fadeout ) );
	m_pHoldTime->SetText( va( "%.3f", tm->holdtime ) );
	m_pFXTime->SetText( va( "%.3f", tm->fxtime ) );

	m_pColor1->SetText( va( "%i %i %i %i", tm->r1, tm->g1, tm->b1, tm->a1 ) );
	m_pColor2->SetText( va( "%i %i %i %i", tm->r2, tm->g2, tm->b2, tm->a2 ) );

	m_pMessageText->SetText( GetAction()->GetMessageText() );

	FillInFonts();

	m_pFontName->SetText( GetAction()->GetFontName() );

	int c = NUM_EFFECT_TYPES;
	int i;
	for ( i = 0; i < c ; i++ )
	{
		m_pEffectType->AddItem( NameForEffectType( i ), NULL );
	}

	m_pEffectType->SetText( NameForEffectType( tm->effect ) );
}
void CAddPresetDialog::DoModal( CDmeAnimationSet *pAnimationSet, KeyValues *pContextKeyValues )
{
	int nTextLength = m_pInput->GetTextLength() + 1;
	char* pCurrentGroupName = (char*)_alloca( nTextLength * sizeof(char) );
	m_pInput->GetText( pCurrentGroupName, nTextLength );

	m_pPresetGroup->DeleteAllItems();

	// Populate the combo box with preset group names
	CDmrElementArray< CDmePresetGroup > presetGroupList = pAnimationSet->GetPresetGroups();
	int nCount = presetGroupList.Count();
	for ( int i = 0; i < nCount; ++i )
	{
		CDmePresetGroup *pPresetGroup = presetGroupList[i];
		if ( pPresetGroup->m_bIsReadOnly )
			continue;

		KeyValues *kv = new KeyValues( "entry" );
		SetElementKeyValue( kv, "presetGroup", pPresetGroup );
		int nItemID = m_pPresetGroup->AddItem( pPresetGroup->GetName(), kv );
		if ( pCurrentGroupName && !Q_stricmp( pPresetGroup->GetName(), pCurrentGroupName ) )
		{
			m_pPresetGroup->ActivateItem( nItemID );
		}
	}

	BaseClass::DoModal( pContextKeyValues );

	m_pInput->SetText( "" );
	m_pInput->RequestFocus();

	PlaceUnderCursor( );
}
//-----------------------------------------------------------------------------
// Sets up the shader listbox
//-----------------------------------------------------------------------------
void CMaterialEditorPanel::PopulateShaderList()
{
	int iShaderCount = g_pMaterialSystem->GetNumShaders();
	m_pShaderList = new vgui::ComboBox( this, "Shader List", 12, false );

	for (int i = 0; i < iShaderCount; ++i )
	{
		m_pShaderList->AddItem( g_pMaterialSystem->GetShaderName(i), NULL ); 
	}

	m_pShaderList->SortItems();
	m_nShaderIndex = 0;
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBaseActionWithStopTimeDialog::Init( void )
{
	BaseClass::Init();

	if ( GetAction()->m_bUsingStopFrame )
	{
		m_pStopType->SetText( "TimeUseFrame" );
		m_pStop->SetText( va( "%i", GetAction()->m_nStopFrame ) );
	}
	else
	{
		m_pStopType->SetText( "TimeUseClock" );
		m_pStop->SetText( va( "%.3f", GetAction()->m_flStopTime ) );
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
// Output : Returns true if changes were effected
//-----------------------------------------------------------------------------
bool CBaseActionSkipAheadDialog::OnSaveChanges( void )
{
	bool bret = BaseClass::OnSaveChanges();

	char skiptype[ 512 ];
	m_pSkipType->GetText( skiptype, sizeof( skiptype ) );

	char skipto[ 512 ];
	m_pSkip->GetText( skipto, sizeof( skipto ) );

	float fskip = (float)atof( skipto );
	int	 iskip = (int)atoi( skipto );

	if ( !Q_strcasecmp( skiptype, "TimeUseTick" ) )
	{
		if ( GetAction()->m_nSkipToTick != iskip )
		{
			bret = true;
			GetAction()->SetSkipToTick( iskip );
			GetAction()->SetSkipToTime( -1.0f );
		}
	}
	else
	{
		if ( GetAction()->m_flSkipToTime != fskip )
		{
			bret = true;
			GetAction()->SetSkipToTime( fskip );
			GetAction()->SetSkipToTick( -1 );
		}
	}

	return bret;
}
//-----------------------------------------------------------------------------
// Purpose: 
// Output : Returns true if changes were effected
//-----------------------------------------------------------------------------
bool CBaseActionWithStopTimeDialog::OnSaveChanges( void )
{
	bool bret = BaseClass::OnSaveChanges();

	char stoptype[ 512 ];
	m_pStopType->GetText( stoptype, sizeof( stoptype ) );

	char stop[ 512 ];
	m_pStop->GetText( stop, sizeof( stop ) );

	float fstop = (float)atof( stop );
	int	 istop = (int)atoi( stop );

	if ( !Q_strcasecmp( stoptype, "TimeUseFrame" ) )
	{
		if ( GetAction()->m_nStopFrame != istop )
		{
			bret = true;
			GetAction()->SetStopFrame( istop );
			GetAction()->SetStopTime( -1.0f );
		}
	}
	else
	{
		if ( GetAction()->m_flStopTime != fstop )
		{
			bret = true;
			GetAction()->SetStopTime( fstop );
			GetAction()->SetStopFrame( -1 );
		}
	}

	return bret;
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBaseActionSkipAheadDialog::Init( void )
{
	LoadControlSettings( "resource\\BaseActionSkipAheadDialog.res" );

	BaseClass::Init();

	if ( GetAction()->m_bUsingSkipTick )
	{
		m_pSkipType->SetText( "TimeUseTick" );
		m_pSkip->SetText( va( "%i", GetAction()->m_nSkipToTick ) );
	}
	else
	{
		m_pSkipType->SetText( "TimeUseClock" );
		m_pSkip->SetText( va( "%.3f", GetAction()->m_flSkipToTime ) );
	}
}
void CBaseActionTextMessageStartDialog::FillInFonts()
{
	m_pFontName->AddItem( "TextMessageDefault", NULL );

	KeyValues *schemeFile = new KeyValues( "Fonts" );
	if ( !schemeFile )
		return;

	if ( schemeFile->LoadFromFile( g_pFileSystem, "resource/SourceScheme.res" ) )
	{
		// Iterate fonts
		for (	KeyValues *kv = schemeFile->FindKey("Fonts", true)->GetFirstSubKey(); 
				kv != NULL; 
				kv = kv->GetNextKey() )
		{
			m_pFontName->AddItem( kv->GetName(), NULL );
		}
	}

	schemeFile->deleteThis();
}
//-----------------------------------------------------------------------------
// Purpose: 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CBaseActionTextMessageStartDialog::OnSaveChanges( void )
{
	bool bret = BaseClass::OnSaveChanges();

	client_textmessage_t *tm = GetAction()->GetTextMessage();
	bret |= SaveDifferingFloat( m_pFadeInTime, &tm->fadein );
	bret |= SaveDifferingFloat( m_pFadeOutTime, &tm->fadeout );
	bret |= SaveDifferingFloat( m_pHoldTime, &tm->holdtime );
	bret |= SaveDifferingFloat( m_pFXTime, &tm->fxtime );

	bret |= SaveDifferingFloat( m_pX, &tm->x );
	bret |= SaveDifferingFloat( m_pY, &tm->y );

	bret |= SaveDifferingColor( m_pColor1, &tm->r1, &tm->g1, &tm->b1, &tm->a1 );
	bret |= SaveDifferingColor( m_pColor2, &tm->r2, &tm->g2, &tm->b2, &tm->a2 );

	char sz[ 1024 ];
	m_pEffectType->GetText( sz, sizeof( sz ) );
	int iEffect = EffectTypeForName( sz );
	if ( iEffect != tm->effect )
	{
		tm->effect = iEffect;
		bret = true;
	}

	m_pMessageText->GetText( sz, sizeof( sz ) );
	if ( Q_strcasecmp( sz, GetAction()->GetMessageText() ) )
	{
		GetAction()->SetMessageText( sz );
		bret = true;
	}

	m_pFontName->GetText( sz, sizeof( sz ) );
	if ( Q_strcasecmp( sz, GetAction()->GetFontName() ) )
	{
		GetAction()->SetFontName( sz );
		bret = true;
	}
	return bret;
}
//-----------------------------------------------------------------------------
// command handler
//-----------------------------------------------------------------------------
void CAddPresetDialog::OnCommand( const char *command )
{
	if ( !Q_stricmp( command, "OK" ) )
	{
		int nTextLength = m_pInput->GetTextLength() + 1;
		char* txt = (char*)_alloca( nTextLength * sizeof(char) );
		m_pInput->GetText( txt, nTextLength );

		nTextLength = m_pPresetGroup->GetTextLength() + 1;
		char* pPresetGroupName = (char*)_alloca( nTextLength * sizeof(char) );
		m_pPresetGroup->GetText( pPresetGroupName, nTextLength );

		KeyValues *pCurrentGroup = m_pPresetGroup->GetActiveItemUserData();
		CDmePresetGroup *pPresetGroup = pCurrentGroup ? GetElementKeyValue<CDmePresetGroup>( pCurrentGroup, "presetGroup" ) : NULL;
		if ( pPresetGroup && Q_stricmp( pPresetGroup->GetName(), pPresetGroupName ) )
		{
			pPresetGroup = NULL;
		}
		KeyValues *kv = new KeyValues( "PresetNameSelected", "text", txt );
		kv->SetString( "presetGroupName", pPresetGroupName );
		SetElementKeyValue( kv, "presetGroup", pPresetGroup );

		if ( m_pContextKeyValues )
		{
			kv->AddSubKey( m_pContextKeyValues );
			m_pContextKeyValues = NULL;
		}
		PostActionSignal( kv );
		CloseModal();
		return;
	}

	if ( !Q_stricmp( command, "Cancel") )
	{
		CloseModal();
		return;
	}

	BaseClass::OnCommand( command );
}