-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenuscene.cpp
119 lines (106 loc) · 3.57 KB
/
menuscene.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "menuscene.h"
#include "game.h"
#include <QGraphicsSceneMouseEvent>
#include <QKeyEvent>
#include <QDir>
#include <QPainter>
MenuScene::MenuScene(QObject *parent)
: QGraphicsScene{parent}
{
setBackgroundBrush(QBrush(QColor(153, 153, 102, 200)));
setSceneRect(0, 0, Game::RESOLUTION.width(), Game::RESOLUTION.height());
loadPixmaps();
m_startItem = new QGraphicsPixmapItem(m_startTextPixmap);
addItem(m_startItem);
m_optionsItem = new QGraphicsPixmapItem(m_optionsTextPixmap);
addItem(m_optionsItem);
m_quitItem = new QGraphicsPixmapItem(m_quitTextPixmap);
addItem(m_quitItem);
m_tetrisTitleItem = new QGraphicsPixmapItem(m_tetrisTitlePixmap.scaled(320, m_tetrisTitlePixmap.height()));
addItem(m_tetrisTitleItem);
createItemPos();
}
void MenuScene::loadPixmaps()
{
if( !m_startTextPixmap.load(":/images/start_text.png") )
{
qDebug() << "Failed to load: " << ":/images/start_text.png";
}
if( !m_optionsTextPixmap.load(":/images/options_text.png") )
{
qDebug() << "Failed to load: " << ":/images/options_text.png";
}
if( !m_quitTextPixmap.load(":/images/quit_text.png") )
{
qDebug() << "Failed to load: " << ":/images/quit_text.png";
}
if( !m_tetrisTitlePixmap.load(":/images/tetris_title.png") )
{
qDebug() << "Failed to load: " << ":/images/tetris_title.png";
}
}
void MenuScene::createItemPos()
{
m_startItem->setPos(80, 200);
m_optionsItem->setPos(48, 300);
m_quitItem->setPos(96, 400);
m_tetrisTitleItem->setPos(8,64);
}
void MenuScene::renderScene()
{
QString fileName = QDir::currentPath() + QDir::separator() + "game_menu.png";
QRect rect = sceneRect().toAlignedRect();
QImage image(rect.size(), QImage::Format_ARGB32);
image.fill(Qt::transparent);
QPainter painter(&image);
render(&painter);
image.save(fileName);
qDebug() << "saved " << fileName;
}
void MenuScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(
event->scenePos().x() > m_startItem->x() && event->scenePos().x() < (m_startItem->x() + m_startItem->boundingRect().width()) &&
event->scenePos().y() > m_startItem->y() && event->scenePos().y() < (m_startItem->y() + m_startItem->boundingRect().height())
)
{
qDebug() << "Start clicked";
emit startClicked();
}
if(
event->scenePos().x() > m_optionsItem->x() && event->scenePos().x() < (m_optionsItem->x() + m_optionsItem->boundingRect().width()) &&
event->scenePos().y() > m_optionsItem->y() && event->scenePos().y() < (m_optionsItem->y() + m_optionsItem->boundingRect().height())
)
{
qDebug() << "Options clicked";
emit optionsClicked();
}
if(
event->scenePos().x() > m_quitItem->x() && event->scenePos().x() < (m_quitItem->x() + m_quitItem->boundingRect().width()) &&
event->scenePos().y() > m_quitItem->y() && event->scenePos().y() < (m_quitItem->y() + m_quitItem->boundingRect().height())
)
{
qDebug() << "Quit clicked";
emit quitClicked();
}
QGraphicsScene::mousePressEvent(event);
}
void MenuScene::keyPressEvent(QKeyEvent *event)
{
if( !event->isAutoRepeat() )
{
switch(event->key())
{
case Qt::Key_S:
emit startClicked();
break;
case Qt::Key_O:
emit optionsClicked();
break;
case Qt::Key_Y:
//renderScene(); //uncomment to make screnshot
break;
}
}
QGraphicsScene::keyPressEvent(event);
}