all repos — mgba @ bc8175515bca1df94785300cd2de41a6f2495a29

mGBA Game Boy Advance Emulator

src/platform/qt/CheatsView.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 "CheatsView.h"
 7
 8#include "GameController.h"
 9
10#include <QFileDialog>
11
12extern "C" {
13#include "gba/cheats.h"
14}
15
16using namespace QGBA;
17
18CheatsView::CheatsView(GameController* controller, QWidget* parent)
19	: QWidget(parent)
20	, m_controller(controller)
21	, m_model(controller->cheatDevice())
22{
23	m_ui.setupUi(this);
24
25	m_ui.cheatList->setModel(&m_model);
26
27	connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
28	connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet()));
29	connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeSet()));
30	connect(controller, SIGNAL(gameStopped(GBAThread*)), &m_model, SLOT(invalidated()));
31
32	connect(m_ui.add, &QPushButton::clicked, [this]() {
33		enterCheat(GBACheatAddLine);
34	});
35
36	connect(m_ui.addGSA, &QPushButton::clicked, [this]() {
37		enterCheat(GBACheatAddGameSharkLine);
38	});
39
40	connect(m_ui.addCB, &QPushButton::clicked, [this]() {
41		enterCheat(GBACheatAddCodeBreakerLine);
42	});
43}
44
45void CheatsView::load() {
46	QString filename = QFileDialog::getOpenFileName(this, tr("Select cheats file"));
47	if (!filename.isEmpty()) {
48		m_model.loadFile(filename);
49	}
50}
51
52void CheatsView::addSet() {
53	GBACheatSet* set = new GBACheatSet;
54	GBACheatSetInit(set, nullptr);
55	m_controller->threadInterrupt();
56	m_model.addSet(set);
57	m_controller->threadContinue();
58}
59
60void CheatsView::removeSet() {
61	GBACheatSet* set;
62	QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
63	if (selection.count() < 1) {
64		return;
65	}
66	m_controller->threadInterrupt();
67	for (const QModelIndex& index : selection) {
68		m_model.removeAt(selection[0]);
69	}
70	m_controller->threadContinue();
71}
72
73void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {
74	GBACheatSet* set;
75	QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
76	if (selection.count() != 1) {
77		return;
78	}
79	set = m_model.itemAt(selection[0]);
80	if (!set) {
81		return;
82	}
83	m_controller->threadInterrupt();
84	QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts);
85	for (const QString& string : cheats) {
86		m_model.beginAppendRow(selection[0]);
87		callback(set, string.toLocal8Bit().constData());
88		m_model.endAppendRow();
89	}
90	m_controller->threadContinue();
91	m_ui.codeEntry->clear();
92}