// CommonObject.cpp : implementation file
//

#include "stdafx.h"
#include "INKS.h"
#include "CommonObject.h"

CCommonObject::CCommonObject(int nIn, int nOut, CString strName, CStringA strOrigin) 
	: m_strUniqueName(strName), m_strOrigin(strOrigin), m_nIn(nIn), m_nOut(nOut), m_gPath(NULL), m_gpText(NULL)
{
	m_Connection.m_pbInvIn = new bool[nIn];
	m_Connection.m_pnInvOut = new bool[nOut];
	m_Connection.m_pObjectIn = new CCommonObject*[nIn];
	m_Connection.m_pObjectOut = new CCommonObject*[nOut];
	m_Connection.m_rectIns = new CRect[nIn];
	m_Connection.m_rectOuts = new CRect[nOut];
	m_Connection.m_slObjectIn.RemoveAll();
	m_Connection.m_slObjectOut.RemoveAll();
	for(int i=0; i<nIn; i++){
		m_Connection.m_pbInvIn[i] = false;
		m_Connection.m_pObjectIn[i] = NULL;
		m_Connection.m_rectIns[i].SetRectEmpty();
	}
	for(int i=0; i<nOut; i++){
		m_Connection.m_pnInvOut[i] = false;
		m_Connection.m_pObjectOut[i] = NULL;
		m_Connection.m_rectOuts[i].SetRectEmpty();
	}
}

CCommonObject::~CCommonObject()
{
	//SAFE_DELETE_ARRAY(m_Connection.m_pbInvIn);
	//SAFE_DELETE_ARRAY(m_Connection.m_pnInvOut);
	//SAFE_DELETE_ARRAY(*m_Connection.m_pObjectIn);
	//SAFE_DELETE_ARRAY(*m_Connection.m_pObjectOut);
	//SAFE_DELETE(m_Connection.m_pObjectIn);
	//SAFE_DELETE(m_Connection.m_pObjectOut);
	//SAFE_DELETE_ARRAY(m_Connection.m_rectIns);
	//SAFE_DELETE_ARRAY(m_Connection.m_rectOuts);
	//SAFE_DELETE(m_gPath);
	//SAFE_DELETE(m_pPen);
}
// CCommonObject message handlers


CCommonObject* CCommonObject::ConnectHitTest(CPoint point, int &nNumber)
{
	for(int i=1; i<=m_nIn; i++){
		if(this->m_Connection.m_rectIns[i-1].PtInRect(point)){
			nNumber = i;
			return this;
		}
	}
	for(int i=1; i<=m_nOut; i++){
		if(this->m_Connection.m_rectOuts[i-1].PtInRect(point)){
			nNumber = -i;
			return this;
		}
	}
	nNumber = 0;
	return NULL;
}

Rect* CCommonObject::GetBounds()
{
	int nPntCount = (int)m_gPath->GetPointCount();
	Point *pnts = new Point[nPntCount];
	m_gPath->GetPathPoints(pnts, nPntCount);
	int nXmin, nXmax, nYmin, nYmax;
	nXmax = nXmin = pnts[0].X;
	nYmax = nYmin = pnts[0].Y;
	for(int i=1; i<nPntCount; i++){
		if(pnts[i].X < nXmin)
			nXmin = pnts[i].X;
		if(pnts[i].Y < nYmin)
			nYmin = pnts[i].Y;
		if(pnts[i].X > nXmax)
			nXmax = pnts[i].X;
		if(pnts[i].Y > nYmax)
			nYmax = pnts[i].Y;
	}
	SAFE_DELETE_ARRAY(pnts);
	m_rectBounding.X = nXmin+10;
	m_rectBounding.Y = nYmin;
	m_rectBounding.Width = nXmax-nXmin-10;
	m_rectBounding.Height = nYmax-nYmin;
	return &m_rectBounding;
}

void CCommonObject::SetRotation(int nRotation)
{
	m_nRotation = nRotation;
	if(m_gPath){
		Matrix *pMat = new Matrix();
		pMat->Translate((REAL)-m_pointTopLeft.x, (REAL)-m_pointTopLeft.y);
		m_gPath->Transform(pMat);
		pMat->Reset();
		pMat->Rotate((REAL)nRotation);
		m_gPath->Transform(pMat);
		pMat->Reset();
		pMat->Translate((REAL)m_pointTopLeft.x, (REAL)m_pointTopLeft.y);
		m_gPath->Transform(pMat);
		SAFE_DELETE(pMat);
	}
}


void CCommonObject::SetConnection(bool bIn, int nPos, CCommonObject* pConnectTo)
{
	if(bIn){
		ASSERT(nPos < m_nIn);
		m_Connection.m_pObjectIn[nPos] = pConnectTo;
	}
	else{
		ASSERT(nPos < m_nOut);
		m_Connection.m_pObjectOut[nPos] = pConnectTo;
	}
	
}

void CCommonObject::DisconnectCObject(CCommonObject *pObject)
{
	for(int i=0; i<m_nIn; i++){
		if(m_Connection.m_pObjectIn[i] == pObject){
			m_Connection.m_pObjectIn[i] = NULL;
		}
	}
	for(int i=0; i<m_nOut; i++){
		if(m_Connection.m_pObjectOut[i] == pObject){
			m_Connection.m_pObjectOut[i] = NULL;
		}
	}
}

void CCommonObject::Disconnect()
{
	for(int i=0; i<m_nIn; i++){
		if(m_Connection.m_pObjectIn[i]){
			m_Connection.m_pObjectIn[i]->DisconnectCObject(this);
			m_Connection.m_pObjectIn[i] = NULL;
		}
	}
	for(int i=0; i<m_nOut; i++){
		if(m_Connection.m_pObjectOut[i]){
			m_Connection.m_pObjectOut[i]->DisconnectCObject(this);
			m_Connection.m_pObjectOut[i] = NULL;
		}
	}
}