src/platform/qt/LoadSaveState.cpp (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "LoadSaveState.h"
7
8#include "GameController.h"
9#include "GamepadAxisEvent.h"
10#include "GamepadButtonEvent.h"
11#include "VFileDevice.h"
12
13#include <QDateTime>
14#include <QKeyEvent>
15#include <QPainter>
16
17extern "C" {
18#include "core/serialize.h"
19#ifdef M_CORE_GBA
20#include "gba/serialize.h"
21#endif
22#include "util/memory.h"
23}
24
25using namespace QGBA;
26
27LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent)
28 : QWidget(parent)
29 , m_controller(controller)
30 , m_currentFocus(controller->stateSlot() - 1)
31 , m_mode(LoadSave::LOAD)
32{
33 setAttribute(Qt::WA_TranslucentBackground);
34 m_ui.setupUi(this);
35
36 m_slots[0] = m_ui.state1;
37 m_slots[1] = m_ui.state2;
38 m_slots[2] = m_ui.state3;
39 m_slots[3] = m_ui.state4;
40 m_slots[4] = m_ui.state5;
41 m_slots[5] = m_ui.state6;
42 m_slots[6] = m_ui.state7;
43 m_slots[7] = m_ui.state8;
44 m_slots[8] = m_ui.state9;
45
46 int i;
47 for (i = 0; i < NUM_SLOTS; ++i) {
48 loadState(i + 1);
49 m_slots[i]->installEventFilter(this);
50 connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i + 1); });
51 }
52
53 if (m_currentFocus >= 9) {
54 m_currentFocus = 0;
55 }
56 if (m_currentFocus < 0) {
57 m_currentFocus = 0;
58 }
59
60 QAction* escape = new QAction(this);
61 escape->connect(escape, SIGNAL(triggered()), this, SLOT(close()));
62 escape->setShortcut(QKeySequence("Esc"));
63 escape->setShortcutContext(Qt::WidgetWithChildrenShortcut);
64 addAction(escape);
65}
66
67void LoadSaveState::setMode(LoadSave mode) {
68 m_mode = mode;
69 QString text = mode == LoadSave::LOAD ? tr("Load State") : tr("Save State");
70 setWindowTitle(text);
71 m_ui.lsLabel->setText(text);
72}
73
74bool LoadSaveState::eventFilter(QObject* object, QEvent* event) {
75 if (event->type() == QEvent::KeyPress) {
76 int column = m_currentFocus % 3;
77 int row = m_currentFocus / 3;
78 switch (static_cast<QKeyEvent*>(event)->key()) {
79 case Qt::Key_Up:
80 row += 2;
81 break;
82 case Qt::Key_Down:
83 row += 1;
84 break;
85 case Qt::Key_Left:
86 column += 2;
87 break;
88 case Qt::Key_Right:
89 column += 1;
90 break;
91 case Qt::Key_1:
92 case Qt::Key_2:
93 case Qt::Key_3:
94 case Qt::Key_4:
95 case Qt::Key_5:
96 case Qt::Key_6:
97 case Qt::Key_7:
98 case Qt::Key_8:
99 case Qt::Key_9:
100 triggerState(static_cast<QKeyEvent*>(event)->key() - Qt::Key_1 + 1);
101 break;
102 case Qt::Key_Enter:
103 case Qt::Key_Return:
104 triggerState(m_currentFocus + 1);
105 break;
106 default:
107 return false;
108 }
109 column %= 3;
110 row %= 3;
111 m_currentFocus = column + row * 3;
112 m_slots[m_currentFocus]->setFocus();
113 return true;
114 }
115 if (event->type() == QEvent::Enter) {
116 int i;
117 for (i = 0; i < 9; ++i) {
118 if (m_slots[i] == object) {
119 m_currentFocus = i;
120 m_slots[m_currentFocus]->setFocus();
121 return true;
122 }
123 }
124 }
125 if (event->type() == GamepadButtonEvent::Down() || event->type() == GamepadAxisEvent::Type()) {
126 int column = m_currentFocus % 3;
127 int row = m_currentFocus - column;
128 GBAKey key = GBA_KEY_NONE;
129 if (event->type() == GamepadButtonEvent::Down()) {
130 key = static_cast<GamepadButtonEvent*>(event)->gbaKey();
131 } else if (event->type() == GamepadAxisEvent::Type()) {
132 GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
133 if (gae->isNew()) {
134 key = gae->gbaKey();
135 } else {
136 return false;
137 }
138 }
139 switch (key) {
140 case GBA_KEY_UP:
141 row += 6;
142 break;
143 case GBA_KEY_DOWN:
144 row += 3;
145 break;
146 case GBA_KEY_LEFT:
147 column += 2;
148 break;
149 case GBA_KEY_RIGHT:
150 column += 1;
151 break;
152 case GBA_KEY_B:
153 event->accept();
154 close();
155 return true;
156 case GBA_KEY_A:
157 case GBA_KEY_START:
158 event->accept();
159 triggerState(m_currentFocus + 1);
160 return true;
161 default:
162 return false;
163 }
164 column %= 3;
165 row %= 9;
166 m_currentFocus = column + row;
167 m_slots[m_currentFocus]->setFocus();
168 event->accept();
169 return true;
170 }
171 return false;
172}
173
174void LoadSaveState::loadState(int slot) {
175 mCoreThread* thread = m_controller->thread();
176 VFile* vf = mCoreGetState(thread->core, slot, 0);
177 if (!vf) {
178 m_slots[slot - 1]->setText(tr("Empty"));
179 return;
180 }
181
182 mStateExtdata extdata;
183 mStateExtdataInit(&extdata);
184 void* state = mCoreExtractState(thread->core, vf, &extdata);
185 vf->seek(vf, 0, SEEK_SET);
186 if (!state) {
187 m_slots[slot - 1]->setText(tr("Corrupted"));
188 mStateExtdataDeinit(&extdata);
189 return;
190 }
191
192 QDateTime creation/*(QDateTime::fromMSecsSinceEpoch(state->creationUsec / 1000LL))*/; // TODO
193 QImage stateImage;
194
195 mStateExtdataItem item;
196 if (mStateExtdataGet(&extdata, EXTDATA_SCREENSHOT, &item) && item.size >= VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4) {
197 stateImage = QImage((uchar*) item.data, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, QImage::Format_ARGB32).rgbSwapped();
198 }
199
200 if (!stateImage.isNull()) {
201 QPixmap statePixmap;
202 statePixmap.convertFromImage(stateImage);
203 m_slots[slot - 1]->setIcon(statePixmap);
204 }
205 if (creation.toMSecsSinceEpoch()) {
206 m_slots[slot - 1]->setText(creation.toString(Qt::DefaultLocaleShortDate));
207 } else if (stateImage.isNull()) {
208 m_slots[slot - 1]->setText(tr("Slot %1").arg(slot));
209 } else {
210 m_slots[slot - 1]->setText(QString());
211 }
212 vf->close(vf);
213 mappedMemoryFree(state, thread->core->stateSize(thread->core));
214}
215
216void LoadSaveState::triggerState(int slot) {
217 if (m_mode == LoadSave::SAVE) {
218 m_controller->saveState(slot);
219 } else {
220 m_controller->loadState(slot);
221 }
222 close();
223}
224
225void LoadSaveState::closeEvent(QCloseEvent* event) {
226 emit closed();
227 QWidget::closeEvent(event);
228}
229
230void LoadSaveState::showEvent(QShowEvent* event) {
231 m_slots[m_currentFocus]->setFocus();
232 QWidget::showEvent(event);
233}
234
235void LoadSaveState::paintEvent(QPaintEvent*) {
236 QPainter painter(this);
237 QRect full(QPoint(), size());
238 painter.fillRect(full, QColor(0, 0, 0, 128));
239}