// GUINode.cpp: implementation of the GUINode class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "mw4gameed2.h"
#include "GUINode.h"
#include "DrawInfo.h"
#include "GUIObjPanel.h"
#include "ObjPanelDlg.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


GUINode::GUINode(CString name,Point3D pnt):EdGUIObject(name,pnt)
{
	ScreenDiv=75;
	RadiusSqu=0;
	Visual=false;
}

EdGUIObject *GUINode::Clone()
{
	GUINode *obj=new GUINode;
	obj->Copy(*this);
	return obj;
}

void GUINode::Draw(DrawInfo &dinf)
{
	SetPen(dinf);
	if(IsHidden()) return;

	int rad=GetScreenRadius(dinf.vinf);
	
	CPoint spnt,pnt=dinf.vinf.DataToScreen(GetPos());
	Point3D pnt2,npt,tpnt=dinf.vinf.ScreenToData(CPoint(0,-rad)),org=dinf.vinf.ScreenToData(CPoint(0,0));
	tpnt.x-=org.x;
	tpnt.y=0.0f;
	tpnt.z-=org.z;
	YawPitchRoll rot(GetRot());
	rot.pitch=0.0f;
	rot.roll=0.0f;

	LinearMatrix4D mat(rot);
	npt.Multiply(tpnt,mat);

	npt+=GetPos();
	spnt=dinf.vinf.DataToScreen(npt);

	dinf.GetDC()->Ellipse(pnt.x-rad,pnt.y-rad,pnt.x+rad,pnt.y+rad);
	
	dinf.GetDC()->MoveTo(pnt);
	dinf.GetDC()->LineTo(spnt);
	
}

void GUINode::AddPanel(CObjPanelDlg *pnl,UndoCommand **cmd)
{
	((EdGUIObject *)GetParent())->AddPanel(pnl,cmd);
	pnl->AddPanel(new GUIObjPanel(this,cmd,pnl));
}



GUINode::~GUINode()
{
}

Scalar DistToPoint(Point3D &a,Point3D &b,Point3D &c)
{
	Scalar lsqu,r,s;
	lsqu=(b.x-a.x)*(b.x-a.x)+(b.z-a.z)*(b.z-a.z);
	
	r=((a.z-c.z)*(a.z-b.z)-(a.x-c.x)*(b.x-a.x))/lsqu;
	s=((a.z-c.z)*(b.x-a.x)-(a.x-c.x)*(b.z-a.z))/lsqu;
	
	
	if(r<=0.0f)
	{
		return (Scalar)sqrt((c.x-a.x)*(c.x-a.x)+(c.z-a.z)*(c.z-a.z));
	}
	else
		if(r>=1.0f)
		{
			return (Scalar)sqrt((c.x-b.x)*(c.x-b.x)+(c.z-b.z)*(c.z-b.z));
		}
		else
		{
			return (Scalar)fabs(s*sqrt(lsqu));
		}
}
