void CACollectionView::ccTouchEnded(CATouch *pTouch, CAEvent *pEvent)
{
    //CC_RETURN_IF(m_vTouches.contains(pTouch) == false);
	CAScrollView::ccTouchEnded(pTouch, pEvent);

	if (m_pHighlightedCollectionCells)
	{
        CAViewAnimation::removeAnimations(m_s__StrID);

		CAIndexPath3E deselectedIndexPath = CAIndexPath3EZero;
		CAIndexPath3E selectedIndexPath = CAIndexPath3E(m_pHighlightedCollectionCells->getSection(), m_pHighlightedCollectionCells->getRow(), m_pHighlightedCollectionCells->getItem());

		if (m_pSelectedCollectionCells.count(selectedIndexPath) > 0 && m_bAllowsMultipleSelection)
		{
			deselectedIndexPath = selectedIndexPath;
			selectedIndexPath = CAIndexPath3EZero;
			m_pSelectedCollectionCells.erase(deselectedIndexPath);
		}
		else
		{
			if (m_pSelectedCollectionCells.size() > 0 && m_bAllowsMultipleSelection == false)
			{
				deselectedIndexPath = *m_pSelectedCollectionCells.begin();
				m_pSelectedCollectionCells.clear();
			}
			m_pSelectedCollectionCells.insert(selectedIndexPath);
		}

		if (deselectedIndexPath != CAIndexPath3EZero)
		{
			if (CACollectionViewCell* cell = m_mpUsedCollectionCells[deselectedIndexPath])
			{
				cell->setControlStateNormal();
			}
			if (m_pCollectionViewDelegate)
			{
				m_pCollectionViewDelegate->collectionViewDidDeselectCellAtIndexPath(this,
					deselectedIndexPath.section, deselectedIndexPath.row, deselectedIndexPath.item);
			}
		}

		if (selectedIndexPath != CAIndexPath3EZero)
		{
			if (CACollectionViewCell* cell = m_mpUsedCollectionCells[selectedIndexPath])
			{
				cell->setControlStateSelected();
			}
			if (m_pCollectionViewDelegate)
			{
				m_pCollectionViewDelegate->collectionViewDidSelectCellAtIndexPath(this,
					selectedIndexPath.section, selectedIndexPath.row, selectedIndexPath.item);
			}
		}
        
        m_pHighlightedCollectionCells = NULL;
	}
}
Beispiel #2
0
void CACollectionView::setUnSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item)
{
	CC_RETURN_IF(section >= m_rSectionRects.size());

	CAIndexPath3E indexPath = CAIndexPath3E(section, row, item);
	if (CACollectionViewCell* cell = m_pUsedCollectionCells.at(indexPath))
	{
		cell->setControlStateNormal();
	}
	m_pSelectedCollectionCells.erase(indexPath);
}
void CAAutoCollectionView::setUnSelectRowAtIndexPath(unsigned int section, unsigned int item)
{
	CC_RETURN_IF(section >= m_rCollectionViewSection.size());

	CAIndexPath3E indexPath = CAIndexPath3E(section, 0, item);
	CC_RETURN_IF(m_pSelectedCollectionCells.find(indexPath) == m_pSelectedCollectionCells.end());
	if (CACollectionViewCell* cell = m_mpUsedCollectionCells.at(indexPath))
	{
		cell->setControlStateNormal();
	}
	m_pSelectedCollectionCells.erase(indexPath);
}
void CACollectionView::mouseMovedOutSide(CATouch* pTouch, CAEvent* pEvent)
{
    if (m_pHighlightedCollectionCells)
    {
        CAIndexPath3E index = CAIndexPath3E(m_pHighlightedCollectionCells->getSection(),
                                            m_pHighlightedCollectionCells->getRow(),
                                            m_pHighlightedCollectionCells->getItem());
        if (m_pSelectedCollectionCells.count(index))
        {
            m_pHighlightedCollectionCells->setControlStateSelected();
        }
        else
        {
            m_pHighlightedCollectionCells->setControlStateNormal();
        }
        m_pHighlightedCollectionCells = NULL;
    }
}
void CACollectionView::mouseMoved(CATouch* pTouch, CAEvent* pEvent)
{
    if (m_bAllowsSelection)
    {
        DPoint point = m_pContainer->convertTouchToNodeSpace(pTouch);
        
        std::map<CAIndexPath3E, CACollectionViewCell*>::iterator itr;
        for (itr = m_mpUsedCollectionCells.begin(); itr != m_mpUsedCollectionCells.end(); ++itr)
        {
            CACollectionViewCell* cell = itr->second;
            CC_CONTINUE_IF(cell == NULL);
            if (cell->getFrame().containsPoint(point) && cell->isVisible())
            {
                CC_BREAK_IF(cell->getControlState() == CAControlStateDisabled);
                
                if (m_pHighlightedCollectionCells)
                {
                    CAIndexPath3E index = CAIndexPath3E(m_pHighlightedCollectionCells->getSection(),
                                                        m_pHighlightedCollectionCells->getRow(),
                                                        m_pHighlightedCollectionCells->getItem());
                    if (m_pSelectedCollectionCells.count(index))
                    {
                        m_pHighlightedCollectionCells->setControlStateHighlighted();
                    }
                    else
                    {
                        m_pHighlightedCollectionCells->setControlStateNormal();
                    }
                    
                }
                
                m_pHighlightedCollectionCells = cell;
                cell->setControlStateHighlighted();
                
                break;
            }
        }
    }
}
void CACollectionView::setSelectRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item)
{
	CC_RETURN_IF(section >= m_rSectionRects.size());

	if (!m_pSelectedCollectionCells.empty() && m_bAllowsMultipleSelection == false)
	{
		std::set<CAIndexPath3E>::iterator itr;
		for (itr = m_pSelectedCollectionCells.begin(); itr != m_pSelectedCollectionCells.end(); itr++)
		{
			if (CACollectionViewCell* cell = m_mpUsedCollectionCells[(*itr)])
			{
				cell->setControlState(CAControlStateNormal);
			}
		}
		m_pSelectedCollectionCells.clear();
	}

	CAIndexPath3E indexPath = CAIndexPath3E(section, row, item);
	if (CACollectionViewCell* cell = m_mpUsedCollectionCells.at(indexPath))
	{
		cell->setControlStateSelected();
	}
	m_pSelectedCollectionCells.insert(indexPath);
}
void CACollectionView::reloadData()
{
	CC_RETURN_IF(m_pCollectionViewDataSource == NULL);
    
	this->reloadViewSizeData();
    
	this->removeAllSubviews();
    
	DRect winRect = this->getBounds();
	winRect.origin = getContentOffset();
	float width = winRect.size.width;
	int y = 0;
    
	if (m_nCollectionHeaderHeight > 0 && m_pCollectionHeaderView)
	{
		m_pCollectionHeaderView->setDisplayRange(true);
		m_pCollectionHeaderView->setFrame(DRect(0, y, width, m_nCollectionHeaderHeight));
		addSubview(m_pCollectionHeaderView);
		y += m_nCollectionHeaderHeight;
	}
    
;
    int begin = (int)m_rSectionRects.size();
    m_rSectionRects.resize(m_rSectionRects.size() + m_nSections);
	for (int i = 0; i < m_nSections; i++)
	{
		unsigned int iSectionHeaderHeight = m_nSectionHeaderHeights.at(i);
		DRect sectionHeaderRect = DRect(0, y, width, iSectionHeaderHeight);
		if (iSectionHeaderHeight>0)
		{
			CAView* pSectionHeaderView = m_pCollectionViewDataSource->collectionViewSectionViewForHeaderInSection(this, sectionHeaderRect.size, i);
			if (pSectionHeaderView != NULL)
			{
				pSectionHeaderView->setDisplayRange(true);
				pSectionHeaderView->setFrame(sectionHeaderRect);
				insertSubview(pSectionHeaderView, 1);
				m_pSectionHeaderViews[i] = pSectionHeaderView;
				y += iSectionHeaderHeight;
			}
		}
        
		y += m_nVertInterval;
		unsigned int rowCount = m_nRowsInSections.at(i);
		for (int j = 0; j < rowCount; j++)
		{
			int iHeight = m_nRowHeightss.at(i).at(j);
            
			unsigned int itemCount = m_pCollectionViewDataSource->numberOfItemsInRowsInSection(this, i, j);
            
			unsigned int cellWidth = 0;
			if (itemCount>0)
			{
				cellWidth = (width - m_nHoriInterval) / itemCount - m_nHoriInterval;
			}
			for (int k = 0; k < itemCount; k++)
			{
				CAIndexPath3E indexPath = CAIndexPath3E(i, j, k);
				DRect cellRect = DRect(m_nHoriInterval + (cellWidth + m_nHoriInterval)*k, y, cellWidth, iHeight);
				m_rUsedCollectionCellRects[indexPath] = cellRect;
                
				std::pair<std::map<CAIndexPath3E, CACollectionViewCell*>::iterator, bool> itrResult =
                m_mpUsedCollectionCells.insert(std::make_pair(indexPath, (CACollectionViewCell*)NULL));
                
				CC_CONTINUE_IF(!winRect.intersectsRect(cellRect));
                
				CACollectionViewCell* cell = m_pCollectionViewDataSource->collectionCellAtIndex(this, cellRect.size, i, j, k);
				if (cell)
				{
					addSubview(cell);
					cell->setFrame(cellRect);
					cell->m_nSection = i;
					cell->m_nRow = j;
					cell->m_nItem = k;
					itrResult.first->second = cell;
                    m_vpUsedCollectionCells.pushBack(cell);
                    
                    if (m_pCollectionViewDataSource)
                    {
                        m_pCollectionViewDataSource->collectionViewWillDisplayCellAtIndex(this, cell, i, j, k);
                    }
				}
			}
			y += (iHeight + m_nVertInterval);
		}
        
		unsigned int iSectionFooterHeight = m_nSectionFooterHeights.at(i);
		DRect sectionFooterRect = DRect(0, y, width, iSectionFooterHeight);
		if (iSectionFooterHeight > 0)
		{
			CAView* pSectionFooterView = m_pCollectionViewDataSource->collectionViewSectionViewForFooterInSection(this, sectionFooterRect.size, i);
			if (pSectionFooterView != NULL)
			{
				pSectionFooterView->setDisplayRange(true);
				pSectionFooterView->setFrame(sectionFooterRect);
				insertSubview(pSectionFooterView, 1);
				m_pSectionFooterViews[i] = pSectionFooterView;
				y += iSectionFooterHeight;
			}
		}
        
		DRect sectionRect = sectionHeaderRect;
		sectionRect.size.height = sectionFooterRect.origin.y
        + sectionFooterRect.size.height
        - sectionHeaderRect.origin.y;
        m_rSectionRects[begin + i] = sectionRect;
	}
    
	if (m_nCollectionFooterHeight > 0 && m_pCollectionFooterView)
	{
		m_pCollectionFooterView->setFrame(DRect(0, y, width, m_nCollectionFooterHeight));
		addSubview(m_pCollectionFooterView);
		y += m_nCollectionFooterHeight;
	}
    
	this->updateSectionHeaderAndFooterRects();
	this->layoutPullToRefreshView();
	this->startDeaccelerateScroll();
}
CACollectionViewCell* CACollectionView::cellForRowAtIndexPath(unsigned int section, unsigned int row, unsigned int item)
{
    return m_mpUsedCollectionCells[CAIndexPath3E(section, row, item)];
}
int CAAutoCollectionView::calculateAllCells(CollectionViewSection& cvs, int index, int dd, int dv, int dw)
{
	int iMaxLengthValue = calculateAllCellsLength(cvs);

	for (int j = 0, l = 0; j < cvs.CollectionViewRows.size(); j++)
	{
		CollectionViewRow& r = cvs.CollectionViewRows[j];

		if (j>0)
		{
			dd += dv;
		}
		for (int k = 0; k < r.rItemRects.size(); k++, l++)
		{
			CAIndexPath3E indexPath = CAIndexPath3E(index, j, l);

			DRect& cellRect = r.rItemRects[k];

			if (m_pCollectionViewOrientation == CACollectionViewOrientationVertical)
			{
				cellRect.origin.y = dd;
				if (m_pCollectionViewCellVertAlign == eCollectionViewCellVertAlignCenter)
				{
					int d = (r.iMaxValue - r.rItemRects[k].size.height) / 2;
					cellRect.origin.y += d;
				}
				if (m_pCollectionViewCellVertAlign == eCollectionViewCellVertAlignBottom)
				{
					int d = r.iMaxValue - r.rItemRects[k].size.height;
					cellRect.origin.y += d;
				}
				if (m_pCollectionViewCellHoriAlign == eCollectionViewCellHoriAlignCenter)
				{
					int d = (dw - iMaxLengthValue) / 2;
					cellRect.origin.x += d;
				}
				if (m_pCollectionViewCellHoriAlign == eCollectionViewCellHoriAlignRight)
				{
					int d = dw - iMaxLengthValue;
					cellRect.origin.x += d;
				}
			}
			else
			{
				if (m_pCollectionViewCellVertAlign == eCollectionViewCellVertAlignCenter)
				{
					int d = (dw - iMaxLengthValue) / 2;
					cellRect.origin.y += d;
				}
				if (m_pCollectionViewCellVertAlign == eCollectionViewCellVertAlignBottom)
				{
					int d = dw - iMaxLengthValue;
					cellRect.origin.y += d;
				}

				cellRect.origin.x = dd;
				if (m_pCollectionViewCellHoriAlign == eCollectionViewCellHoriAlignCenter)
				{
					int d = (r.iMaxValue - r.rItemRects[k].size.width) / 2;
					cellRect.origin.x += d;
				}
				if (m_pCollectionViewCellHoriAlign == eCollectionViewCellHoriAlignRight)
				{
					int d = r.iMaxValue - r.rItemRects[k].size.width;
					cellRect.origin.x += d;
				}
			}
			m_rUsedCollectionCellRects[indexPath] = cellRect;

			m_mpUsedCollectionCells.insert(std::make_pair(indexPath, (CACollectionViewCell*)NULL));
		}
		
		dd += r.iMaxValue;
	}
	return dd;
}