/****************************************************************************************

		ModelerView.cpp

		Copyright (c)1990 - 1999 MetaCreations. All rights reserved.

		Author:	Jerome Bignon
		Date:	05/18/99

		Revision History

		when		who			what & why
		==========	==========	======================================================
		05/18/99	Jerome		Creation

*****************************************************************************************/

#include <math.h>

#include "ModelerView.h"
#include "IMCGraphicContext.h"
#include "IShUtilities.h"
#include "TreePrim.h"
#include "MCCountedPtrHelper.h"
#include "IModelerView.h"
#include "I3DShModule.h"


const MCGUID IID_IModelerView	(R_IID_IModelerView);
const MCGUID CLSID_ModelerView	(R_CLSID_ModelerView);


TMCRGBColor PenColor;

MCCOMErr ModelerView::QueryInterface(const MCIID& riid, void** ppvObj)
{
	if (MCIsEqualIID(riid, IID_IModelerView))
	{
		TMCCountedGetHelper<IModelerView>	result(ppvObj);
		result = this;
		return MC_S_OK;
	}
	else
		return TBasicPart::QueryInterface(riid, ppvObj);
}

ModelerView::ModelerView()
{
	fTreeModeler = NULL;	
	ViewSide = 4;
	fZoom = 25.0f;
	fbackgroundColor.rr = 0;
	fbackgroundColor.gg = 0;
	fbackgroundColor.bb = 0;
}

uint32 ModelerView::AddRef()
{
	return TBasicPart::AddRef();
}



TMCPoint TreePoint3Dto2D( TVector3 *v , int numView , real zoom )
{
	TMCPoint pt;
	int v1,v2;
	short z,xsign;
	
	switch ( numView )
	{
		case  1 :
			v1 = 0; v2 = 2; z=0;xsign = 1;
			break;
		case  3 :
			v1 = 0; v2 = 2; z=0;xsign = -1;
			break;		
		case  6 :
			v1 = 1; v2 = 2; z=0;xsign = -1;
			break;
		case  4 :
			v1 = 1; v2 = 2; z=0;xsign = 1;
			break;
		case  8 :
			v1 = 0; v2 = 1; z=-100;xsign = 1;
			break;
		case  2 :
			v1 = 0; v2 = 1; z=-100;xsign = -1;
			break;		
		default :
			v1 = 1; v2 = 2; z=0;xsign = -1;
			break;		
	}
	
	pt.x = xsign*(short)((*v)[v1] * zoom  ) + 300   ;
	pt.y =      -(short)((*v)[v2] * zoom  ) + 300 +z;
	
	return pt;
}

void DrawTree( TFacet3D *facets, DrawTreePrivData *privData)
{
	IMCGraphicContext* fGC = (IMCGraphicContext*) (privData)->fGC;
	int numView = (privData)->fNumView;
	real zoom  = (privData)->fZoom;
	TMCPoint pt0,pt1,pt2;
	
	pt0 = TreePoint3Dto2D( &(facets->fVertices[0].fVertex) , numView , zoom );
	pt1 = TreePoint3Dto2D( &(facets->fVertices[1].fVertex) , numView , zoom );
	pt2 = TreePoint3Dto2D( &(facets->fVertices[2].fVertex) , numView , zoom );

	if ( fGC )
	{
		fGC->DrawLine( pt0.x, pt0.y , pt1.x, pt1.y  );
		fGC->DrawLine( pt0.x, pt0.y , pt2.x, pt2.y  );
		fGC->DrawLine( pt2.x, pt2.y , pt1.x, pt1.y  );
	}
}

void ModelerView::SelfDraw(IMCGraphicContext* graphicContext, const TMCRect& inZone)
{
	PenColor.rr = 0;
	PenColor.gg = 0;
	PenColor.bb = 65535;

	graphicContext->FillRect(*(TMCRect*)&inZone , *(TMCRGBColor*)&fbackgroundColor);
	graphicContext->SetPen( kCopyPen , kSolidPen , 1 , PenColor);

	if ( GetModelerNoAddRef() )
	{
		DrawTreePrivData PrivData;		
		
		// Nbr branches total of the tree : ----------------------------
		TreeData treedata = GetModelerNoAddRef()->GetTreePrimNoAddRef()->GetData();		
		uint32 totalBranches = 1;
		uint16 i;

		for(i=1;i<=treedata.fRecursiv;i++) 
			totalBranches += RealPow(treedata.fNbBranches,i);
		
		PrivData.fGC = graphicContext;
		PrivData.fNumView = ViewSide;		
		PrivData.fZoom    = fZoom;

		TMCCountedPtr<FacetMesh> fMesh;
		GetModelerNoAddRef()->GetI3DExExPrimNoAddRef()->GetFacetMesh(uint32(0), &fMesh);

		FacetMeshFacetIterator facetIterator;
		facetIterator.Initialize(fMesh);
		TFacet3D facet;
		
		for (facetIterator.First(); facetIterator.More(); facetIterator.Next())
		{
			facet = facetIterator.GetFacet();
			DrawTree(&facet,&PrivData);
		}
	}
}


void ModelerView::SetBackgroundColor( const TMCRGBColor &rgbcolor )
{
	if ( (fbackgroundColor.rr != rgbcolor.rr) || (fbackgroundColor.gg != rgbcolor.gg) || (fbackgroundColor.bb != rgbcolor.bb))
	{
		fbackgroundColor = rgbcolor;
	}
}	


Modeler*	ModelerView::GetModelerNoAddRef()
{ 	
	return fTreeModeler; 
}
	
void	ModelerView::SetModeler( Modeler* TheModeler )
{ 
	fTreeModeler = TheModeler; 
}