int CAAutoCollectionView::calculateAllRects() { int dw = 0, dd = 0, dv = 0; if (m_pCollectionViewOrientation == CACollectionViewOrientationVertical) { dw = this->getBounds().size.width - 2 * m_iHoriMargins; dd = m_iVertMargins + m_nCollectionHeaderHeight; dv = m_nVertCellInterval; } else { dw = this->getBounds().size.height - 2 * m_iVertMargins; dd = m_iHoriMargins + m_nCollectionHeaderHeight; dv = m_nHoriCellInterval; } for (int i = 0; i < m_rCollectionViewSection.size(); i++) { CollectionViewSection& cvs = m_rCollectionViewSection[i]; unsigned int iSectionHeaderHeight = cvs.nSectionHeaderHeight; DRect sectionHeaderRect; if (m_pCollectionViewOrientation == CACollectionViewOrientationVertical) { sectionHeaderRect = DRect(m_iHoriMargins, dd, dw, iSectionHeaderHeight); } else { sectionHeaderRect = DRect(dd, m_iVertMargins, iSectionHeaderHeight, dw); } if (iSectionHeaderHeight > 0) { CAView* pSectionHeaderView = m_pCollectionViewDataSource->collectionViewSectionViewForHeaderInSection(this, sectionHeaderRect.size, i); if (pSectionHeaderView != NULL) { pSectionHeaderView->setDisplayRange(true); pSectionHeaderView->setFrame(sectionHeaderRect); insertSubview(pSectionHeaderView, 1); } dd += (iSectionHeaderHeight + dv); cvs.pSectionHeaderView = pSectionHeaderView; } dd = calculateAllCells(cvs, i, dd, dv, dw); unsigned int iSectionFooterHeight = cvs.nSectionFooterHeight; DRect sectionFooterRect; if (m_pCollectionViewOrientation == CACollectionViewOrientationVertical) { sectionFooterRect = DRect(m_iHoriMargins, dd, dw, iSectionFooterHeight); } else { sectionFooterRect = DRect(dd, m_iVertMargins, iSectionFooterHeight, dw); } if (iSectionFooterHeight > 0) { CAView* pSectionFooterView = m_pCollectionViewDataSource->collectionViewSectionViewForFooterInSection(this, sectionFooterRect.size, i); if (pSectionFooterView != NULL) { pSectionFooterView->setDisplayRange(true); pSectionFooterView->setFrame(sectionFooterRect); insertSubview(pSectionFooterView, 1); } dd += (iSectionFooterHeight + dv); cvs.pSectionFooterView = pSectionFooterView; } DRect sectionRect = sectionHeaderRect; if (m_pCollectionViewOrientation == CACollectionViewOrientationVertical) { sectionRect.size.height = sectionFooterRect.origin.y + sectionFooterRect.size.height - sectionHeaderRect.origin.y; } else { sectionRect.size.width = sectionFooterRect.origin.x + sectionFooterRect.size.width - sectionHeaderRect.origin.x; } cvs.rSectionRect = sectionRect; } return dd; }
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(); }
void CATabBar::showItems() { do { CC_BREAK_IF(m_pViews.empty() == false); unsigned int count = MIN(m_nMaxShowCount, m_pItems.size()); float width = m_obContentSize.width / count; float height = m_obContentSize.height; m_cItemSize = CCSize(width, height); for (unsigned int i=0; i<count; i++) { CAView* view = CAView::createWithFrame(CCRect(i * width, 0, width, height), ccc4(0, 0, 0, 0)); this->insertSubview(view, 3); view->setDisplayRange(false); m_pViews.push_back(view); CAImageView* imageView = NULL; CCLabelTTF* title = NULL; if (m_pItems.at(i)->getImage()) { imageView = CAImageView::createWithImage(m_pItems.at(i)->getImage()); imageView->setTag(0xffff); view->addSubview(imageView); } if (m_pItems.at(i)->getTitle().compare("") != 0) { int fontSize = this->getContentSize().height / 5.0f; title = CCLabelTTF::create(m_pItems.at(i)->getTitle().c_str(), "Arial", fontSize); title->setTag(0xfffe); view->addSubview(title); } if (imageView && title == NULL) { CCSize imageViewSize = imageView->getBounds().size; float scaleX = width / imageViewSize.width * 2/3.0f; float scaleY = height / imageViewSize.height * 2/3.0f; float scale = MIN(scaleX, scaleY); scale = MIN(scale, 1.0f); imageViewSize = ccpMult(imageViewSize, scale); CCRect rect; rect.origin = view->getBounds().size/2; rect.size = imageViewSize; imageView->setCenter(rect); } else if (title && imageView == NULL) { int fontSize = this->getContentSize().height / 2.0f; title->setFontSize(fontSize); CCSize titleSize = title->getBounds().size; float titleScale = height / titleSize.height * 1/2.0f; titleSize = ccpMult(titleSize, titleScale); CCRect rect; rect.origin = view->getBounds().size/2; rect.size = titleSize; title->setCenter(rect); } else if (title && imageView) { CCSize imageViewSize = imageView->getBounds().size; float scaleX = width / imageViewSize.width * 1/2.0f; float scaleY = height / imageViewSize.height * 1/2.0f; float scale = MIN(scaleX, scaleY); scale = MIN(scale, 1.0f); imageViewSize = ccpMult(imageViewSize, scale); CCRect rect; rect.size = imageViewSize; rect.origin = view->getBounds().size; rect.origin.x *= 1/2.0f; rect.origin.y *= 7/20.0f; imageView->setCenter(rect); CCSize titleSize = title->getBounds().size; float titleScale = height / titleSize.height * 3/10; titleSize = ccpMult(titleSize, titleScale); CCRect rect2; rect2.size = titleSize; rect2.origin = view->getBounds().size; rect2.origin.x *= 1/2.0f; rect2.origin.y *= 15/20.0f; title->setCenter(rect2); } } } while (0); }