-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
146 lines (122 loc) · 5.83 KB
/
mainwindow.cpp
File metadata and controls
146 lines (122 loc) · 5.83 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <inventory.h>
#include <QPushButton>
#include <QTcpSocket>
#include <QTcpServer>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
/// Создает основное окно программы с размерами 360x320
/// В качестве основного компановщика используется QStackedLayout
/// В компановшик добавляются виджеты
/// Основного меню
/// Меню выбора подключения
/// Виджет игрового поля
ui->setupUi(this);
setFixedSize(360,320);
setWindowTitle("Инвентарь");
stacked = new QStackedLayout();
stacked->addWidget(getMainMenu());
stacked->addWidget(getPlayField());
centralWidget()->setLayout(stacked);
}
MainWindow::~MainWindow()
{
delete ui;
}
QWidget *MainWindow::getMainMenu()
{
/// Возвращает виджет с кнопками "Новая игра" и "Выход"
/// При клике по кнопке "Новая игра" отображается Окно сообщения
/// с выбором роли - клиент или сервер.
/// При клике по кнопке "Выход" компановщик stacked
/// отображает следующий виджет.
QWidget *w = new QWidget();
QVBoxLayout *vBox = new QVBoxLayout;
QPushButton *pbNewGame = new QPushButton(this);
QPushButton *pbExit = new QPushButton(this);
connect(pbNewGame, &QPushButton::clicked, [=](){
setWindowTitle("Инвентарь");
QMessageBox msgBox(QMessageBox::Information,
"Новая игра",
"Для начала игры и создания пустого инвентаря выберите \"Сервер\".\n"
"Для подключения к уже существующему инвентарю выберите \"Клиент\".",
QMessageBox::NoButton);
msgBox.addButton("Клиент", QMessageBox::AcceptRole);
msgBox.addButton("Сервер", QMessageBox::RejectRole);
if (msgBox.exec() == QMessageBox::AcceptRole){
connect(network, &Network::newMessage, inventory, &Inventory::setInventory);
connect(network, &Network::connected, [=](){
stacked->setCurrentIndex(1);
});
connect(network, &Network::noHostAvailable, [=](){
QMessageBox msgBox(QMessageBox::Warning,
"Новая игра",
"Отсутствует игра для подключения.\n"
"Выберите другой режим игры или попробуйтe подключиться еще раз.",
QMessageBox::Ok);
msgBox.exec();
});
connect(inventory, &Inventory::created, [=](){
disconnect(network, &Network::newMessage, inventory, &Inventory::setInventory);
connect(network, SIGNAL(newMessage(QByteArray)), inventory, SLOT(updateItem(QByteArray)));
});
network->serachServer();
} else {
setWindowTitle("Инвентарь - Сервер");
inventory->createInventory(3,3);
connect(network, &Network::connected, [=](){
network->sendMessage(inventory->getInventory());
connect(network, SIGNAL(newMessage(QByteArray)), inventory, SLOT(updateItem(QByteArray)));
});
network->createServer();
stacked->setCurrentIndex(1);
}
});
connect(pbExit, &QPushButton::clicked, [=](){QApplication::exit();});
pbNewGame->setMinimumHeight(50);
pbExit->setMinimumHeight(50);
pbNewGame->setText("Новая игра");
pbExit->setText("Выход");
vBox->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Expanding));
vBox->addWidget(pbNewGame);
vBox->addWidget(pbExit);
vBox->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Expanding));
w->setLayout(vBox);
return w;
}
QWidget *MainWindow::getPlayField()
{
/// Возвращает виджет с игровым полем, на котором расположены инвентарь,
/// источкин объектов и кнопка, возвращающая игрока в главное меню
QWidget *w = new QWidget();
QPushButton *pb = new QPushButton("Главое меню",this);
pb->setMinimumHeight(50);
connect(pb, &QPushButton::clicked, [=](){
stacked->setCurrentIndex(0);
setWindowTitle("Инвентарь");
network->disconnect();
});
/// Раскомментировать, есть необходима очистка инвентаря при "новой игре"
connect(pb, SIGNAL(clicked()), inventory, SLOT(createInventory()));
inventory = new Inventory(this);
connect(inventory, &Inventory::itemUpdated, [=](int position, ItemType type, int count){
network->sendMessage(inventory->getItem(position, type, count));
});
connect(inventory, &Inventory::itemUpdated, db, &DataBase::updateItem);
connect(inventory, &Inventory::itemCreated, db, &DataBase::insertItem);
Item *apple = new Item();
apple->setType(Apple);
apple->setInfinity(true);
apple->setBorder();
apple->setAcceptDrops(false);
QGridLayout *gBox = new QGridLayout();
gBox->addWidget(pb,0,0,1,2);
gBox->addWidget(inventory,1,0, Qt::AlignCenter);
gBox->addWidget(apple,1,1, Qt::AlignCenter);
w->setLayout(gBox);
return w;
}