//-----------------------------------------------------------------------------
// Purpose: 
// Input  : Extents - 
//-----------------------------------------------------------------------------
void CFaceEditMaterialPage::GetAllFaceExtents( Extents_t Extents )
{
	BOOL		bFirst = TRUE;
	Extents_t	FaceExtents;

	CFaceEditSheet *pSheet = ( CFaceEditSheet* )GetParent();
	int faceCount = pSheet->GetFaceListCount();	

	for( int nFace = 0; nFace < faceCount; nFace++ )
	{
		CMapFace *pFace = pSheet->GetFaceListDataFace( nFace );
		pFace->GetFaceExtents(FaceExtents);

		if ((FaceExtents[EXTENTS_XMIN][0] < Extents[EXTENTS_XMIN][0]) || (bFirst))
		{
			Extents[EXTENTS_XMIN] = FaceExtents[EXTENTS_XMIN];
		}

		if ((FaceExtents[EXTENTS_XMAX][0] > Extents[EXTENTS_XMAX][0]) || (bFirst))
		{
			Extents[EXTENTS_XMAX] = FaceExtents[EXTENTS_XMAX];
		}

		if ((FaceExtents[EXTENTS_YMIN][1] < Extents[EXTENTS_YMIN][1]) || (bFirst))
		{
			Extents[EXTENTS_YMIN] = FaceExtents[EXTENTS_YMIN];
		}

		if ((FaceExtents[EXTENTS_YMAX][1] > Extents[EXTENTS_YMAX][1]) || (bFirst))
		{
			Extents[EXTENTS_YMAX] = FaceExtents[EXTENTS_YMAX];
		}

		if ((FaceExtents[EXTENTS_ZMIN][2] < Extents[EXTENTS_ZMIN][2]) || (bFirst))
		{
			Extents[EXTENTS_ZMIN] = FaceExtents[EXTENTS_ZMIN];
		}

		if ((FaceExtents[EXTENTS_ZMAX][2] > Extents[EXTENTS_ZMAX][2]) || (bFirst))
		{
			Extents[EXTENTS_ZMAX] = FaceExtents[EXTENTS_ZMAX];
		}

		bFirst = FALSE;
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : uCmd - 
// Output : Returns TRUE on success, FALSE on failure.
//-----------------------------------------------------------------------------
BOOL CFaceEditMaterialPage::OnJustify( UINT uCmd )
{
	// Set the material tool current.
	SetMaterialPageTool( MATERIALPAGETOOL_MATERIAL );

	BOOL		bTreatManyAsOneFace;
	Extents_t	Extents;

	// mark undo position
	GetHistory()->MarkUndoPosition( NULL, "Justify texture" );

	CFaceEditSheet *pSheet = ( CFaceEditSheet* )GetParent();
	int faceCount = pSheet->GetFaceListCount();	

	// If multiple faces are selected, use the m_bTreatManyAsOneFace variable to determine
	// how to perform the justification.
	if( faceCount > 1 )
	{
		bTreatManyAsOneFace = m_bTreatAsOneFace;
		if( bTreatManyAsOneFace )
		{
			GetAllFaceExtents( Extents );
		}
	}
	// If only one face is selected, treat it singly.
	else
	{
		bTreatManyAsOneFace = FALSE;
	}

	for( int i = 0; i < faceCount; i++ )
	{
		CMapFace *pFace = pSheet->GetFaceListDataFace( i );

		CMapSolid *pSolid = pSheet->GetFaceListDataSolid( i );
		GetHistory()->Keep( pSolid );

		if( !bTreatManyAsOneFace )
		{
			pFace->GetFaceExtents( Extents );
		}

		switch (uCmd)
		{
			case IDC_JUSTIFY_TOP:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_TOP, Extents);
				break;
			}

			case IDC_JUSTIFY_BOTTOM:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_BOTTOM, Extents);
				break;
			}

			case IDC_JUSTIFY_LEFT:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_LEFT, Extents);
				break;
			}

			case IDC_JUSTIFY_RIGHT:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_RIGHT, Extents);
				break;
			}

			case IDC_JUSTIFY_CENTER:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_CENTER, Extents);
				break;
			}

			case IDC_JUSTIFY_FITTOFACE:
			{
				pFace->JustifyTextureUsingExtents(TEXTURE_JUSTIFY_FIT, Extents);
				break;
			}
		}
	}

	CMapDoc::GetActiveMapDoc()->SetModifiedFlag();

	UpdateDialogData();

	return(TRUE);
}