Ejemplo n.º 1
0
bool IGFrame::ManageSelection (const IGLibrary::SELECTIONPARAMS& selParams, const std::list<POINT>& lPts)
{
	// Request thread access
	if (!RequestAccess ())
		return false;
	CxImage *pCurrentLayer = GetWorkingLayer();
	if (!pCurrentLayer)
		return false;
	int nCurLayerId = (int)pCurrentLayer->GetId();
	int nCurLayerWidth = (int)pCurrentLayer->GetWidth();
	int nCurLayerHeight = (int)pCurrentLayer->GetHeight();
	_ASSERTE ((nCurLayerId >= 0) && L"Current layer is not identified");
	if (nCurLayerId < 0)
		return false;
	bool bRes = false;
	POINT ptTopLeft = {0, 0};
	POINT ptBottomRight = {-1, -1};
	IGSELECTIONENUM eSelectionType = selParams.eSelectionType;
	if (((eSelectionType & IGSELECTION_SQUARE) == IGSELECTION_SQUARE) ||
		((eSelectionType & IGSELECTION_LASSO) == IGSELECTION_LASSO))
	{
		if (lPts.size() > 0)
		{
			bool bAllEqual = true;
			for (list <POINT>::const_iterator itPt = lPts.cbegin(); itPt != lPts.cend(); ++itPt)
			{
				if (((*itPt).x != lPts.front().x) || ((*itPt).y != lPts.front().y))
					bAllEqual = false;
			}
			if (bAllEqual)
				eSelectionType = IGSELECTION_CLEAR;
		}
	}
	if ((eSelectionType & IGSELECTION_CLEAR) == IGSELECTION_CLEAR)
	{
		if ((eSelectionType & IGSELECTION_INVERT) == IGSELECTION_INVERT)
			bRes = pCurrentLayer->SelectionInvert();
		else
			bRes = pCurrentLayer->SelectionDelete();
	}
	else
	{
		if ((eSelectionType & IGSELECTION_REPLACE) == IGSELECTION_REPLACE)
			pCurrentLayer->SelectionClear();	
		BYTE level = ((eSelectionType & IGSELECTION_REMOVE) == IGSELECTION_REMOVE) ? 0 : 255;

		std::list<POINT> lConvertedPts (lPts); 
		// convert IG coordinates to Cx coordinates
		IGConvertible::FromIGtoCxCoords(lConvertedPts, pCurrentLayer->GetHeight());
	
		// apply selection
		if ((eSelectionType & IGSELECTION_SQUARE) == IGSELECTION_SQUARE)
		{
			if (lPts.size() != 2)
				return false;	
			// read rectangle coordinates
			ptTopLeft = lConvertedPts.front();
			ptBottomRight = lConvertedPts.back();
			int nPosX = ptTopLeft.x;
			int nPosY = ptBottomRight.y;
			RECT rcSel;
			rcSel.left = nPosX; rcSel.top = nPosY;
			nPosX = ptBottomRight.x;
			nPosY = ptTopLeft.y;
			rcSel.right = nPosX; rcSel.bottom = nPosY;
			// adjust rectangle orientation
			int nWidth = rcSel.right - rcSel.left;
			int nHeight = rcSel.top - rcSel.bottom;
			nWidth = (nWidth < 0) ? -1 * nWidth : nWidth;
			nHeight = (nHeight < 0) ? -1 * nHeight : nHeight;
			rcSel.left = (rcSel.left < rcSel.right) ? rcSel.left : rcSel.right;
			rcSel.bottom = (rcSel.bottom < rcSel.top) ? rcSel.bottom : rcSel.top;
			rcSel.right = rcSel.left + nWidth;
			rcSel.top = rcSel.bottom + nHeight;
			// test if rectangle is inside the frame
			if ((rcSel.right < 0) || (rcSel.left >= nCurLayerWidth)
				|| (rcSel.top < 0) || (rcSel.bottom >= nCurLayerHeight))
				return false;	// selection out of bounds
			// adjust bounds
			rcSel.left = (rcSel.left < 0) ? 0 : rcSel.left;
			rcSel.right = (rcSel.right >= nCurLayerWidth) ? nCurLayerWidth - 1 : rcSel.right;
			rcSel.bottom = (rcSel.bottom < 0) ? 0 : rcSel.bottom;
			rcSel.top = (rcSel.top >= nCurLayerHeight) ? nCurLayerHeight - 1 : rcSel.top;
			// add the selection
			bRes = pCurrentLayer->SelectionAddRect (rcSel, level);
		}
		else if ((eSelectionType & IGSELECTION_LASSO) == IGSELECTION_LASSO)
		{
			bRes = pCurrentLayer->SelectionAddPolygon (lConvertedPts, level);
		}
		else if ((eSelectionType & IGSELECTION_MAGIC) == IGSELECTION_MAGIC)
		{
			bRes = pCurrentLayer->SelectionAddMagic (lConvertedPts.front(), level, selParams.nTolerance);
		}
		else if ((eSelectionType & IGSELECTION_FACES) == IGSELECTION_FACES)
		{
			bRes = pCurrentLayer->SelectionAddFaces (level);
		}
		else if ((eSelectionType & IGSELECTION_EYES) == IGSELECTION_EYES)
		{
			bRes = pCurrentLayer->SelectionAddEyes (level);
		}
		else if ((eSelectionType & IGSELECTION_MOUTH) == IGSELECTION_MOUTH)
		{
			bRes = pCurrentLayer->SelectionAddMouth (level);
		}
		else if ((eSelectionType & IGSELECTION_NOZE) == IGSELECTION_NOZE)
		{
			bRes = pCurrentLayer->SelectionAddNoze (level);
		}
		else if ((eSelectionType & IGSELECTION_LPE) == IGSELECTION_LPE)
		{
			bRes = pCurrentLayer->SelectionAddLPE (lConvertedPts, level);
		}
		if (ptBottomRight.x < ptTopLeft.x)
		{
			int nSwap = ptTopLeft.x;
			ptTopLeft.x = ptBottomRight.x;
			ptBottomRight.x = nSwap;
		}
		if (ptBottomRight.y < ptTopLeft.y)
		{
			int nSwap = ptTopLeft.y;
			ptTopLeft.y = ptBottomRight.y;
			ptBottomRight.y = nSwap;
		}
		if (ptTopLeft.x < 0)
			ptTopLeft.x = 0;
		if (ptTopLeft.y < 0)
			ptTopLeft.y = 0;
		if (ptBottomRight.x >= nCurLayerWidth)
			ptBottomRight.x = nCurLayerWidth - 1;
		if (ptBottomRight.y >= nCurLayerHeight)
			ptBottomRight.y = nCurLayerHeight - 1;
	}
	if (!pCurrentLayer->SelectionIsValid())
		pCurrentLayer->SelectionDelete();
	AddNewStep (IGFRAMEHISTORY_STEP_SELECTION, L"Change selection", (int)&selParams, (int)&lPts);
	Redraw (true);
	return bRes;
}