/******************************************************************************
** Copyright (C) 2001, University of Alabama in Huntsville. All rights 
** reserved.
**
** File   : DrawableCOI.cpp
**
** Author : Satish Hota
**
** Description:
**    This file provides implementation for CDrawableCOI class.
**
** History:
** 28-Nov-01 Satish Hota
**     File Created. 
** 28-Nov-01 Satish Hota
**    Added Draw method.
** 01-Dec-01 Satish Hota
**    Implemented the Draw method.  Added and implemented the ObjectsAt method.
******************************************************************************/

#include "stdafx.h"
#include "ConGraT.h"
#include "DrawableCOI.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CDrawableCOI::CDrawableCOI()
{

}

CDrawableCOI::~CDrawableCOI()
{

}

long CDrawableCOI::Draw()
{
    IndividualMap::iterator iterCOI;
    CDrawable *pDrawable;
    bool bFirst = true;
	
    for(iterCOI = m_mapIndividual.begin();iterCOI != m_mapIndividual.end();iterCOI++)
    {
		pDrawable = dynamic_cast<CDrawable*>((*iterCOI).second);
        if(pDrawable)
        {
            pDrawable->Draw();
            if(bFirst)
            {
                pDrawable->GetFromLocation(&m_nFromX, &m_nFromY);
                pDrawable->GetToLocation(&m_nToX, &m_nToY);
                bFirst = false;
            }
            else
            {
                CRect curRect(m_nFromX, m_nFromY, m_nToX, m_nToY);
                int x1, y1, x2, y2;
                pDrawable->GetFromLocation(&x1, &y1);
                pDrawable->GetToLocation(&x2, &y2);
                CRect newRect(x1, y1, x2, y2);
                curRect.UnionRect(curRect, newRect);
                m_nFromX = curRect.left;
                m_nFromY = curRect.top;
                m_nToX = curRect.right;
                m_nToY = curRect.bottom;
            }
        }
        else
        {
            ASSERT(0);
        }
    }

    return CG_S_OK;
}

std::vector<CDrawableElement*> CDrawableCOI::ObjectsAt(int nXCoor, int nYCoor)
{
    IndividualMap::iterator iterCOI;
    CDrawableElement* pDrawableElement;
    std::vector<CDrawableElement*> vHitList;
    int nRC;

    for(iterCOI = m_mapIndividual.begin();iterCOI != m_mapIndividual.end();iterCOI++)
    {
		pDrawableElement = dynamic_cast<CDrawableElement*>((*iterCOI).second);
        if(pDrawableElement)
        {
            nRC = pDrawableElement->HitTest(nXCoor, nYCoor);
            if(nRC == CDrawableElement::RC_S_HitSucess)
            {
                vHitList.push_back(pDrawableElement);
            }
        }
        else
        {
            ASSERT(0);
        }
    }

	return vHitList;

}
