From 37bb1367db8c2a80e500a4c3663f362c2fb32c36 Mon Sep 17 00:00:00 2001 From: RickyRister <42636155+RickyRister@users.noreply.github.com> Date: Thu, 28 Nov 2024 11:59:31 -0800 Subject: [PATCH] refactor method for positioning cards in ZoneViewZone (#5203) * refactor out method for positioning cards in zone view * rename some variables * use max/min * some small formatting stuff --- cockatrice/src/game/zones/view_zone.cpp | 97 ++++++++++++++----------- cockatrice/src/game/zones/view_zone.h | 8 ++ 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/cockatrice/src/game/zones/view_zone.cpp b/cockatrice/src/game/zones/view_zone.cpp index 6c7defe66..6f238527f 100644 --- a/cockatrice/src/game/zones/view_zone.cpp +++ b/cockatrice/src/game/zones/view_zone.cpp @@ -112,73 +112,86 @@ void ZoneViewZone::reorganizeCards() for (int i = 0; i < cardCount; ++i) cards[i]->setId(i); - int cols = qFloor(qSqrt((double)cardCount / 2)); - if (cols > 7) - cols = 7; - int rows = qCeil((double)cardCount / cols); - if (rows < 1) - rows = 1; - if (minRows == 0) - minRows = rows; - else if (rows < minRows) { - rows = minRows; - cols = qCeil((double)cardCount / minRows); - } - if (cols < 2) - cols = 2; - - qDebug() << "reorganizeCards: rows=" << rows << "cols=" << cols; - CardList cardsToDisplay(cards); if (sortByName || sortByType) cardsToDisplay.sort((sortByName ? CardList::SortByName : 0) | (sortByType ? CardList::SortByType : 0)); - int typeColumn = 0; - int longestRow = 0; - if (pileView && sortByType) { // we need sort by type enabled for the feature to work - int typeRow = 0; + auto gridSize = positionCardsForDisplay(cardsToDisplay, pileView && sortByType); + + qreal aleft = 0; + qreal atop = 0; + qreal awidth = gridSize.cols * CARD_WIDTH + (CARD_WIDTH / 2) + HORIZONTAL_PADDING; + qreal aheight = (gridSize.rows * CARD_HEIGHT) / 3 + CARD_HEIGHT * 1.3; + optimumRect = QRectF(aleft, atop, awidth, aheight); + + updateGeometry(); + emit optimumRectChanged(); +} + +/** + * @brief Sets the position of each card to the proper position for the view + * + * @param cards The cards to reposition. Will modify the cards in the list. + * @param groupByType Whether to make piles by card type. If true, then expects `cards` to be sorted by type. + * + * @returns The number of rows and columns to display + */ +ZoneViewZone::GridSize ZoneViewZone::positionCardsForDisplay(CardList &cards, bool groupByType) +{ + int cardCount = cards.size(); + if (groupByType) { + int row = 0; + int col = 0; + int longestRow = 0; QString lastCardType; for (int i = 0; i < cardCount; i++) { - CardItem *c = cardsToDisplay.at(i); + CardItem *c = cards.at(i); QString cardType = c->getInfo() ? c->getInfo()->getMainCardType() : ""; if (i) { // if not the first card if (cardType == lastCardType) - typeRow++; // add below current card - else { // if no match then move card to next column - typeColumn++; - typeRow = 0; + row++; // add below current card + else { // if no match then move card to next column + col++; + row = 0; } } lastCardType = cardType; - qreal x = typeColumn * CARD_WIDTH; - qreal y = typeRow * CARD_HEIGHT / 3; + qreal x = col * CARD_WIDTH; + qreal y = row * CARD_HEIGHT / 3; c->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y); c->setRealZValue(i); - longestRow = qMax(typeRow, longestRow); + longestRow = qMax(row, longestRow); } + + return GridSize{longestRow, qMax(col + 1, 3)}; + } else { + int cols = qMin(qFloor(qSqrt((double)cardCount / 2)), 7); + int rows = qMax(qCeil((double)cardCount / cols), 1); + if (minRows == 0) { + minRows = rows; + } else if (rows < minRows) { + rows = minRows; + cols = qCeil((double)cardCount / minRows); + } + + if (cols < 2) + cols = 2; + + qDebug() << "reorganizeCards: rows=" << rows << "cols=" << cols; + for (int i = 0; i < cardCount; i++) { - CardItem *c = cardsToDisplay.at(i); + CardItem *c = cards.at(i); qreal x = (i / rows) * CARD_WIDTH; qreal y = (i % rows) * CARD_HEIGHT / 3; c->setPos(HORIZONTAL_PADDING + x, VERTICAL_PADDING + y); c->setRealZValue(i); } + + return GridSize{rows, qMax(cols, 1)}; } - - int totalRows = (pileView && sortByType) ? longestRow : rows; - int totalColumns = (pileView && sortByType) ? qMax(typeColumn + 1, 3) : qMax(cols, 1); - - qreal aleft = 0; - qreal atop = 0; - qreal awidth = totalColumns * CARD_WIDTH + (CARD_WIDTH / 2) + HORIZONTAL_PADDING; - qreal aheight = (totalRows * CARD_HEIGHT) / 3 + CARD_HEIGHT * 1.3; - optimumRect = QRectF(aleft, atop, awidth, aheight); - - updateGeometry(); - emit optimumRectChanged(); } void ZoneViewZone::setSortByName(int _sortByName) diff --git a/cockatrice/src/game/zones/view_zone.h b/cockatrice/src/game/zones/view_zone.h index 092a1c51c..9179c477b 100644 --- a/cockatrice/src/game/zones/view_zone.h +++ b/cockatrice/src/game/zones/view_zone.h @@ -35,6 +35,14 @@ private: bool sortByName, sortByType; bool pileView; + struct GridSize + { + int rows; + int cols; + }; + + GridSize positionCardsForDisplay(CardList &cards, bool groupByType = false); + public: ZoneViewZone(Player *_p, CardZone *_origZone,