-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainmenuscene.cpp
100 lines (90 loc) · 3.21 KB
/
mainmenuscene.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "mainmenuscene.h"
#include "button.h"
#include <QSignalMapper>
MainMenuScene::MainMenuScene()
: TransformScene(),
m_gameNames({"MAIN MENU", "POKER", "TRAFEL", "SPAFEL", "FLAFEL", "RAFEL"})
{
}
void MainMenuScene::slotGameButtonPressed(int button)
{
emit signalStartGame( Games(button) );
}
void MainMenuScene::slotChangeNumPlayers(int numPlayers)
{
emit signalChangeNumPlayers(numPlayers);
updateText();
}
void MainMenuScene::createGameButtons()
{
const double buttonSpacing = 1.5;
QSignalMapper* signalMapper = new QSignalMapper(this);
QRectF tableRect = getTableRect();
double totalHeight = 0;
std::array<Button*, static_cast<unsigned int>(Games::MAX)> buttons;
for ( unsigned int i = 1; i != m_gameNames.size(); ++i )
{
buttons[i] = new Button(m_gameNames[i]);
totalHeight += buttonSpacing * buttons[i]->boundingRect().height();
}
double currentOffsetY = -0.5 * totalHeight;
for ( unsigned int i = 1; i != m_gameNames.size(); ++i )
{
QRectF buttonRect = buttons[i]->boundingRect();
QPointF buttonOffset = buttonRect.bottomRight() - buttonRect.topLeft();
buttons[i]->setPos(tableRect.center() + QPointF(0, currentOffsetY) - 0.5*buttonOffset );
currentOffsetY += buttonSpacing * buttonRect.height();
connect(buttons[i], SIGNAL(pressed()), signalMapper, SLOT(map()));
signalMapper->setMapping(buttons[i], i );
addItem( buttons[i] );
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(slotGameButtonPressed(int)));
}
void MainMenuScene::createNumPlayerButtons()
{
const double buttonSpacing = 1.5;
const unsigned int maxPlayers = 8;
QSignalMapper* signalMapper = new QSignalMapper(this);
QRectF tableRect = getTableRect();
double totalWidth = 0;
std::array<Button*, maxPlayers+1> buttons;
for ( unsigned int i = 1; i < buttons.size(); ++i )
{
buttons[i] = new Button(std::to_string(i));
totalWidth += buttonSpacing * buttons[i]->boundingRect().width();
}
double currentOffsetY = -0.5 * totalWidth;
for ( unsigned int i = 1; i < buttons.size(); ++i )
{
QRectF buttonRect = buttons[i]->boundingRect();
QPointF buttonOffset = buttonRect.bottomRight() - buttonRect.topLeft();
buttons[i]->setPos(0.5 * tableRect.center() +
0.25 * tableRect.bottomLeft() +
0.25 * tableRect.bottomRight() +
QPointF(currentOffsetY, 0) - 0.5*buttonOffset );
currentOffsetY += buttonSpacing * buttonRect.width();
connect(buttons[i], SIGNAL(pressed()), signalMapper, SLOT(map()));
signalMapper->setMapping(buttons[i], i );
addItem( buttons[i] );
}
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(slotChangeNumPlayers(int)));
}
void MainMenuScene::updateText()
{
m_numPlayerText->setPlainText("Number of players: " + QString::number(getNumPlayers()));
}
void MainMenuScene::init()
{
QRectF tableRect = getTableRect();
m_numPlayerText = addText("");
QFont font;
font.setPixelSize(40);
m_numPlayerText->setFont(font);
m_numPlayerText->setDefaultTextColor(Qt::white);
m_numPlayerText->setPos(0.3 * tableRect.center() +
0.7 * tableRect.bottomLeft() );
m_numPlayerText->show();
createGameButtons();
createNumPlayerButtons();
updateText();
}