-
Notifications
You must be signed in to change notification settings - Fork 2
/
offboard.cpp
55 lines (48 loc) · 1.5 KB
/
offboard.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <QPainter>
#include "offboard.hpp"
#include "board.hpp"
#include "globals.hpp"
OffBoard::OffBoard(Board& board_,const Side side_) :
QWidget(&board_),
board(board_),
side(side_)
{
connect(&board,&Board::boardChanged,this,static_cast<void (OffBoard::*)()>(&OffBoard::update));
}
std::vector<PieceTypeAndSide> OffBoard::missingPieces() const
{
std::array<unsigned int,NUM_PIECE_TYPES> missingPiecesPerType=numStartingPiecesPerType;
for (const auto& placement:board.gameState().placements(side))
--missingPiecesPerType[toPieceType(placement.piece)];
std::vector<PieceTypeAndSide> result;
for (int pieceType=NUM_PIECE_TYPES-1;pieceType>=FIRST_PIECE_TYPE;--pieceType)
result.insert(result.end(),missingPiecesPerType[pieceType],toPieceTypeAndSide(static_cast<PieceType>(pieceType),side));
return result;
}
void OffBoard::paintEvent(QPaintEvent*)
{
QPainter qPainter(this);
const bool tall=height()>width();
int squareWidth,squareHeight;
if (tall) {
squareWidth=width();
squareHeight=height()/numStartingPieces;
}
else {
squareWidth=width()/numStartingPieces;
squareHeight=height();
}
QRect qRect(0,0,squareWidth,squareHeight);
for (const auto missingPiece:missingPieces()) {
board.globals.pieceIcons.drawPiece(qPainter,board.iconSet,missingPiece,qRect);
if (tall)
qRect.translate(0,squareHeight);
else
qRect.translate(squareWidth,0);
}
qPainter.end();
}
QSize OffBoard::sizeHint() const
{
return {board.squareWidth(),board.squareHeight()};
}