This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
180 lines (153 loc) · 5.49 KB
/
mainwindow.cpp
File metadata and controls
180 lines (153 loc) · 5.49 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "mainwindow.h"
MainWindow::MainWindow(bool newGame, QString filename, QWidget *parent)
: QMainWindow(parent), filename(filename) {
problemLabel = new QLabel("ProblemSet 0", this);
taskLabel = new QLabel("Task 0 / 0", this);
itemLabel = new QLabel("0 / 0", this);
enhanceLabel = new QLabel("Local Enhance: 0", this);
moneyLabel = new QLabel("Money: 0", this);
if (newGame) {
game = new GameState(32, 24, scene, goal, this);
} else {
QFile saveslot(filename);
saveslot.open(QIODevice::ReadOnly);
QDataStream savestream(&saveslot);
game = new GameState(savestream, scene, goal, this);
saveslot.close();
}
connect(game, &GameState::saveEvent, this, &MainWindow::saveEvent);
connect(goal, &GoalManager::updateGoal, this, &MainWindow::updateGoal);
view = new QGraphicsView(scene, this);
QVBoxLayout *left = new QVBoxLayout, *right = new QVBoxLayout;
QHBoxLayout *dButtons = new QHBoxLayout(this);
QWidget *buttonGroupWidget = new QWidget(this);
buttonGroupWidget->setLayout(dButtons);
addDeviceButtons(dButtons);
left->addWidget(view);
left->addWidget(buttonGroupWidget);
right->addWidget(problemLabel);
right->addWidget(taskLabel);
right->addWidget(itemLabel);
right->addWidget(enhanceLabel);
addDeviceRatios(right);
QWidget *rightShopWidget = new QWidget(this);
QHBoxLayout *shopLayout = new QHBoxLayout(rightShopWidget);
shopLayout->addWidget(moneyLabel);
right->addWidget(rightShopWidget);
QHBoxLayout *layout = new QHBoxLayout;
layout->setSpacing(0);
layout->setContentsMargins(0, 0, 0, 0);
layout->addLayout(left);
layout->addLayout(right);
QWidget *central = new QWidget(this);
central->setLayout(layout);
setCentralWidget(central);
connect(game, &GameState::deviceChangeEvent, this,
&MainWindow::deviceChangeEvent);
connect(game, &GameState::deviceRatioChangeEvent, this, &MainWindow::deviceRatioUpdateEvent);
connect(game, &GameState::enhanceChangeEvent, this, &MainWindow::enhanceChangeEvent);
connect(game, &GameState::moneyChangeEvent, this, &MainWindow::moneyChangeEvent);
connect(this, &MainWindow::keyPressEvent, game, &GameState::keyPressEvent);
connect(this, &MainWindow::keyReleaseEvent, game,
&GameState::keyReleaseEvent);
connect(game, &GameState::zoomIn, this, &MainWindow::zoomIn);
connect(game, &GameState::zoomOut, this, &MainWindow::zoomOut);
connect(game, &GameState::zoomReset, this, &MainWindow::zoomReset);
game->init();
}
MainWindow::~MainWindow() {}
void MainWindow::updateGoal(int problemSet, int task, int received, int required, const QPicture *icon)
{
using std::to_string;
problemLabel->setText(("ProblemSet " + to_string(problemSet)).c_str());
taskLabel->setText(("Task " + to_string(task)).c_str());
itemLabel->setPicture(*icon);
itemLabel->setText((to_string(received) + " / " + to_string(required)).c_str());
}
void MainWindow::deviceChangeEvent(device_id_t id) {
if (id == DEV_NONE) {
for (auto x: deviceButtons) {
x->setChecked(false);
}
} else {
for (int i = 0; i < 6; i ++) {
if (i == id) {
deviceButtons[i]->setChecked(true);
} else {
deviceButtons[i]->setChecked(false);
}
}
}
}
void MainWindow::deviceRatioUpdateEvent(device_id_t id, qreal ratio)
{
using std::to_string;
assert((int)id < (int)DEV_NONE);
deviceRatioLabels[id]->setText(QString::number(ratio));
}
void MainWindow::saveEvent() {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
QMessageBox::critical(this, "Invalid saveslot",
"The passed in saveslot name is invalid, please "
"choose or create a new one and then save again.");
filename = QFileDialog::getSaveFileName(this, "New Save File");
}
QDataStream out(&file);
game->save(out);
}
void MainWindow::enhanceChangeEvent(int enhance)
{
this->enhanceLabel->setText(("Local Enhancement: " + std::to_string(enhance)).c_str());
}
void MainWindow::moneyChangeEvent(int money)
{
using std::to_string;
this->moneyLabel->setText(("Money: " + to_string(money)).c_str());
}
void MainWindow::zoomIn()
{
view->scale(1.2, 1.2);
}
void MainWindow::zoomOut()
{
view->scale(1/1.2, 1/1.2);
}
void MainWindow::zoomReset()
{
view->resetTransform();
}
void MainWindow::addDeviceButtons(QHBoxLayout *dButtons)
{
QIcon minerIcon(":/device_icon/miner");
QIcon beltIcon(":/device_icon/belt");
QIcon cutterIcon(":/device_icon/cutter");
QIcon mixerIcon(":/device_icon/mixer");
QIcon rotatorIcon(":/device_icon/rotater");
QIcon trashIcon(":/device_icon/trash");
QPushButton *miner = new QPushButton(minerIcon, "Miner");
QPushButton *belt = new QPushButton(beltIcon, "belt");
QPushButton *cutter = new QPushButton(cutterIcon, "Cutter");
QPushButton *mixer = new QPushButton(mixerIcon, "Mixer");
QPushButton *rotator = new QPushButton(rotatorIcon, "Rotator");
QPushButton *trash = new QPushButton(trashIcon, "Trash");
auto list = {miner, belt, cutter, mixer, rotator, trash};
for (auto x : list) {
x->setCheckable(true);
dButtons->addWidget(x, 0, Qt::AlignLeft);
deviceButtons.push_back(x);
}
}
void MainWindow::addDeviceRatios(QVBoxLayout *r)
{
for (int i = 0; i < DEV_NONE; i ++) {
QWidget *widget = new QWidget();
QLayout *layout = new QHBoxLayout(widget);
QLabel *name = new QLabel(), *ratio = new QLabel();
name->setText(getDeviceName(device_id_t(i)));
layout->addWidget(name);
layout->addWidget(ratio);
deviceRatioLabels.push_back(ratio);
r->addWidget(widget);
}
}