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
This commit is contained in:
RickyRister 2024-11-28 11:59:31 -08:00 committed by GitHub
parent 24b5dab456
commit 37bb1367db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 42 deletions

View file

@ -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)

View file

@ -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,