bool ColorGrid::init() { m_nNodes = m_nRows*m_nColumns; m_pNodes = new ColorNode[m_nNodes]; assert(m_pNodes && "New failed to create the sequence array for the grid."); m_cbRandomSequence = new PaletteIndex[m_nRows*m_nColumns]; assert(m_cbRandomSequence && "New failed to create the random sequence for the grid."); fHorizontalSpacing = getHorizontalSpacing(); fVerticalSpacing = getVerticalSpacing(); fInitialX = fHorizontalSpacing * 0.5f + m_cbBounds.getMinX(); fInitialY = fVerticalSpacing * 0.5f + m_cbBounds.getMinY(); // Note that the origin for the points start in the lower left. cocos2d::CCPoint cbCurrentPoint(fInitialX, fInitialY); for(int i = 0; i < m_nNodes; i++) { bool bIsSuccessful = m_pNodes[i].init(); if(bIsSuccessful) { if(i / m_nRows % 2 == 0) cbCurrentPoint.y = fInitialY + ((i % m_nRows) * fVerticalSpacing); else cbCurrentPoint.y = fInitialY + (fVerticalSpacing / 2) + ((i % m_nRows) * fVerticalSpacing); cbCurrentPoint.x = fInitialX + ((i / m_nRows) * fHorizontalSpacing); m_pNodes[i].setPosition(cbCurrentPoint); m_pNodes[i].setParent(this, 1); m_pNodes[i].setColor(my_utility::random(m_nColors)); m_uncaptured_count.tally(m_pNodes[i].getPaletteIndex()); } } RandomCapture(m_nInitCapturePercent); for(int i = 0; i < m_nComboSize; i++) { m_pSelectedNodes[i] = 0; } for(int i = 0; i < m_nComboSize; i++) { m_pSelectedNodeNumbers[i] = 0; } return true; }
void FlowLayout::layoutChildren() { int curX = 0; int curY = 0; int highestWidget = 0; int numWidgets = 0; int rlOffset = 0; int btOffset = 0; int numRows = 1; int numOnRow = 0; std::vector<Widget*> curRow; Widget* firstWidget = NULL; int lowestPoint = 0; for(WidgetArray::iterator it = getChildBegin(); it != getChildEnd(); ++it) { if(!(*it)->isVisible() && isFilteringVisibility()) { continue; } if(!firstWidget) { firstWidget = (*it); } if(isResizingRowToWidth()) { (*it)->setSize(getInnerWidth(),(*it)->getHeight()); } if((maxOnRow > 0 && numOnRow >= maxOnRow) || isResizingRowToWidth() || (curX + (*it)->getWidth() > getInnerWidth() && numWidgets > 0 && !singleRow)) { numRows++; numOnRow = 0; curX = 0; curY += highestWidget + getVerticalSpacing(); highestWidget = 0; if(center && !curRow.empty()) { int x1 = curRow[0]->getLocation().getX(); int x2 = curRow.back()->getLocation().getX() + curRow.back()->getWidth(); int w = x2 - x1; int centerOffset = (getInnerWidth() - w) / 2; for(size_t i = 0; i < curRow.size(); ++i) { curRow[i]->setLocation( curRow[i]->getLocation().getX() + centerOffset, curRow[i]->getLocation().getY()); } } curRow.clear(); } numOnRow++; if(!topToBottom) { btOffset = getInnerHeight() - (*it)->getHeight() - (curY + curY); } if(!leftToRight && !center) { rlOffset = getInnerWidth() - (*it)->getWidth() - (curX + curX); } (*it)->setLocation(curX + rlOffset,curY + btOffset); curX += (*it)->getWidth() + getHorizontalSpacing(); numWidgets++; if((*it)->getHeight() > highestWidget) { highestWidget = (*it)->getHeight(); } curRow.push_back((*it)); //find the content height int l = (*it)->getLocation().getY() + (*it)->getHeight(); if(l > lowestPoint) { lowestPoint = l; } } //code duplication, I know :( if(center && !curRow.empty()) { if(alignLastRow && numRows > 1 && firstWidget) { int x1 = curRow[0]->getLocation().getX(); int x2 = firstWidget->getLocation().getX(); int diff = x2 - x1; for(size_t i = 0; i < curRow.size(); ++i) { curRow[i]->setLocation( curRow[i]->getLocation().getX() + diff, curRow[i]->getLocation().getY()); } } else { int x1 = curRow[0]->getLocation().getX(); int x2 = curRow.back()->getLocation().getX() + curRow.back()->getWidth(); int w = x2 - x1; int centerOffset = (getInnerWidth() - w) / 2; for(size_t i = 0; i < curRow.size(); ++i) { curRow[i]->setLocation( curRow[i]->getLocation().getX() + centerOffset, curRow[i]->getLocation().getY()); } } } //set content height contentHSz = lowestPoint + getMargin(SIDE_TOP) + getMargin(SIDE_BOTTOM); }