src/platform/qt/LoadSaveState.cpp (view raw)
1/* Copyright (c) 2013-2014 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 "GamepadButtonEvent.h"
10#include "VFileDevice.h"
11
12#include <QKeyEvent>
13#include <QPainter>
14
15extern "C" {
16#include "gba-serialize.h"
17#include "gba-video.h"
18}
19
20using namespace QGBA;
21
22LoadSaveState::LoadSaveState(GameController* controller, QWidget* parent)
23 : QWidget(parent)
24 , m_controller(controller)
25 , m_currentFocus(0)
26{
27 m_ui.setupUi(this);
28
29 m_slots[0] = m_ui.state1;
30 m_slots[1] = m_ui.state2;
31 m_slots[2] = m_ui.state3;
32 m_slots[3] = m_ui.state4;
33 m_slots[4] = m_ui.state5;
34 m_slots[5] = m_ui.state6;
35 m_slots[6] = m_ui.state7;
36 m_slots[7] = m_ui.state8;
37 m_slots[8] = m_ui.state9;
38
39 int i;
40 for (i = 0; i < NUM_SLOTS; ++i) {
41 loadState(i + 1);
42 m_slots[i]->installEventFilter(this);
43 connect(m_slots[i], &QAbstractButton::clicked, this, [this, i]() { triggerState(i + 1); });
44 }
45}
46
47void LoadSaveState::setMode(LoadSave mode) {
48 m_mode = mode;
49 QString text = mode == LoadSave::LOAD ? tr("Load State") : tr("Save State");
50 setWindowTitle(text);
51 m_ui.lsLabel->setText(text);
52}
53
54bool LoadSaveState::eventFilter(QObject* object, QEvent* event) {
55 if (event->type() == QEvent::KeyPress) {
56 int column = m_currentFocus % 3;
57 int row = m_currentFocus - column;
58 switch (static_cast<QKeyEvent*>(event)->key()) {
59 case Qt::Key_Up:
60 row += 6;
61 break;
62 case Qt::Key_Down:
63 row += 3;
64 break;
65 case Qt::Key_Left:
66 column += 2;
67 break;
68 case Qt::Key_Right:
69 column += 1;
70 break;
71 case Qt::Key_1:
72 case Qt::Key_2:
73 case Qt::Key_3:
74 case Qt::Key_4:
75 case Qt::Key_5:
76 case Qt::Key_6:
77 case Qt::Key_7:
78 case Qt::Key_8:
79 case Qt::Key_9:
80 triggerState(static_cast<QKeyEvent*>(event)->key() - Qt::Key_1 + 1);
81 break;
82 case Qt::Key_Escape:
83 close();
84 break;
85 case Qt::Key_Enter:
86 case Qt::Key_Return:
87 triggerState(m_currentFocus + 1);
88 break;
89 default:
90 return false;
91 }
92 column %= 3;
93 row %= 9;
94 m_currentFocus = column + row;
95 m_slots[m_currentFocus]->setFocus();
96 return true;
97 }
98 if (event->type() == QEvent::Enter) {
99 int i;
100 for (i = 0; i < 9; ++i) {
101 if (m_slots[i] == object) {
102 m_currentFocus = i;
103 m_slots[m_currentFocus]->setFocus();
104 return true;
105 }
106 }
107 }
108 if (event->type() == GamepadButtonEvent::Down()) {
109 int column = m_currentFocus % 3;
110 int row = m_currentFocus - column;
111 switch (static_cast<GamepadButtonEvent*>(event)->gbaKey()) {
112 case GBA_KEY_UP:
113 row += 6;
114 break;
115 case GBA_KEY_DOWN:
116 row += 3;
117 break;
118 case GBA_KEY_LEFT:
119 column += 2;
120 break;
121 case GBA_KEY_RIGHT:
122 column += 1;
123 break;
124 case GBA_KEY_B:
125 close();
126 break;
127 case GBA_KEY_A:
128 case GBA_KEY_START:
129 event->accept();
130 triggerState(m_currentFocus + 1);
131 return true;
132 default:
133 return false;
134 }
135 column %= 3;
136 row %= 9;
137 m_currentFocus = column + row;
138 m_slots[m_currentFocus]->setFocus();
139 event->accept();
140 return true;
141 }
142 return false;
143}
144
145void LoadSaveState::loadState(int slot) {
146 GBAThread* thread = m_controller->thread();
147 VFile* vf = GBAGetState(thread->gba, thread->stateDir, slot, false);
148 if (!vf) {
149 m_slots[slot - 1]->setText(tr("Empty"));
150 return;
151 }
152 VFileDevice vdev(vf);
153 QImage stateImage;
154 stateImage.load(&vdev, "PNG");
155 if (!stateImage.isNull()) {
156 QPixmap statePixmap;
157 statePixmap.convertFromImage(stateImage);
158 m_slots[slot - 1]->setIcon(statePixmap);
159 m_slots[slot - 1]->setText(QString());
160 } else {
161 m_slots[slot - 1]->setText(tr("Slot %1").arg(slot));
162 }
163}
164
165void LoadSaveState::triggerState(int slot) {
166 if (m_mode == LoadSave::SAVE) {
167 m_controller->saveState(slot);
168 } else {
169 m_controller->loadState(slot);
170 }
171 close();
172}
173
174void LoadSaveState::closeEvent(QCloseEvent* event) {
175 emit closed();
176 QWidget::closeEvent(event);
177}
178
179void LoadSaveState::showEvent(QShowEvent* event) {
180 m_slots[m_currentFocus]->setFocus();
181 QWidget::showEvent(event);
182}
183
184void LoadSaveState::paintEvent(QPaintEvent*) {
185 QPainter painter(this);
186 QRect full(QPoint(), size());
187 painter.drawPixmap(full, m_currentImage);
188 painter.fillRect(full, QColor(0, 0, 0, 128));
189}