mirror of https://github.com/mgba-emu/mgba.git
Qt: Start cheat input
This commit is contained in:
parent
c8d3488804
commit
297551a5be
|
@ -27,6 +27,7 @@ QVariant CheatsModel::data(const QModelIndex& index, int role) const {
|
||||||
const GBACheatSet* cheats = static_cast<const GBACheatSet*>(index.internalPointer());
|
const GBACheatSet* cheats = static_cast<const GBACheatSet*>(index.internalPointer());
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case Qt::DisplayRole:
|
case Qt::DisplayRole:
|
||||||
|
case Qt::EditRole:
|
||||||
return cheats->name ? cheats->name : tr("(untitled)");
|
return cheats->name ? cheats->name : tr("(untitled)");
|
||||||
case Qt::CheckStateRole:
|
case Qt::CheckStateRole:
|
||||||
return Qt::Checked;
|
return Qt::Checked;
|
||||||
|
@ -35,6 +36,29 @@ QVariant CheatsModel::data(const QModelIndex& index, int role) const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CheatsModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
||||||
|
if (!index.isValid()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int row = index.row();
|
||||||
|
GBACheatSet* cheats = static_cast<GBACheatSet*>(index.internalPointer());
|
||||||
|
switch (role) {
|
||||||
|
case Qt::DisplayRole:
|
||||||
|
case Qt::EditRole:
|
||||||
|
if (cheats->name) {
|
||||||
|
free(cheats->name);
|
||||||
|
cheats->name = nullptr;
|
||||||
|
}
|
||||||
|
cheats->name = strdup(value.toString().toLocal8Bit().constData());
|
||||||
|
return true;
|
||||||
|
case Qt::CheckStateRole:
|
||||||
|
return false;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex CheatsModel::index(int row, int column, const QModelIndex& parent) const {
|
QModelIndex CheatsModel::index(int row, int column, const QModelIndex& parent) const {
|
||||||
return createIndex(row, column, *GBACheatSetsGetPointer(&m_device->cheats, row));
|
return createIndex(row, column, *GBACheatSetsGetPointer(&m_device->cheats, row));
|
||||||
}
|
}
|
||||||
|
@ -43,6 +67,14 @@ QModelIndex CheatsModel::parent(const QModelIndex& index) const {
|
||||||
return QModelIndex();
|
return QModelIndex();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Qt::ItemFlags CheatsModel::flags(const QModelIndex &index) const {
|
||||||
|
if (!index.isValid()) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
|
||||||
|
}
|
||||||
|
|
||||||
int CheatsModel::columnCount(const QModelIndex& parent) const {
|
int CheatsModel::columnCount(const QModelIndex& parent) const {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
@ -54,6 +86,13 @@ int CheatsModel::rowCount(const QModelIndex& parent) const {
|
||||||
return GBACheatSetsSize(&m_device->cheats);
|
return GBACheatSetsSize(&m_device->cheats);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GBACheatSet* CheatsModel::itemAt(const QModelIndex& index) {
|
||||||
|
if (!index.isValid()) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
return static_cast<GBACheatSet*>(index.internalPointer());
|
||||||
|
}
|
||||||
|
|
||||||
void CheatsModel::loadFile(const QString& path) {
|
void CheatsModel::loadFile(const QString& path) {
|
||||||
VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
|
VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY);
|
||||||
if (!vf) {
|
if (!vf) {
|
||||||
|
@ -64,3 +103,9 @@ void CheatsModel::loadFile(const QString& path) {
|
||||||
endResetModel();
|
endResetModel();
|
||||||
vf->close(vf);
|
vf->close(vf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheatsModel::addSet(GBACheatSet* set) {
|
||||||
|
beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1);
|
||||||
|
*GBACheatSetsAppend(&m_device->cheats) = set;
|
||||||
|
endInsertRows();
|
||||||
|
}
|
|
@ -9,6 +9,7 @@
|
||||||
#include <QAbstractItemModel>
|
#include <QAbstractItemModel>
|
||||||
|
|
||||||
struct GBACheatDevice;
|
struct GBACheatDevice;
|
||||||
|
struct GBACheatSet;
|
||||||
|
|
||||||
namespace QGBA {
|
namespace QGBA {
|
||||||
|
|
||||||
|
@ -19,14 +20,19 @@ public:
|
||||||
CheatsModel(GBACheatDevice* m_device, QObject* parent = nullptr);
|
CheatsModel(GBACheatDevice* m_device, QObject* parent = nullptr);
|
||||||
|
|
||||||
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
|
||||||
|
virtual bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole) override;
|
||||||
|
|
||||||
virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
|
||||||
virtual QModelIndex parent(const QModelIndex& index) const override;
|
virtual QModelIndex parent(const QModelIndex& index) const override;
|
||||||
|
|
||||||
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
|
||||||
|
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||||
|
|
||||||
|
GBACheatSet* itemAt(const QModelIndex& index);
|
||||||
|
|
||||||
void loadFile(const QString& path);
|
void loadFile(const QString& path);
|
||||||
|
void addSet(GBACheatSet* set);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GBACheatDevice* m_device;
|
GBACheatDevice* m_device;
|
||||||
|
|
|
@ -7,6 +7,10 @@
|
||||||
|
|
||||||
#include <QFileDialog>
|
#include <QFileDialog>
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include "gba/cheats.h"
|
||||||
|
}
|
||||||
|
|
||||||
using namespace QGBA;
|
using namespace QGBA;
|
||||||
|
|
||||||
CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent)
|
CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent)
|
||||||
|
@ -18,6 +22,19 @@ CheatsView::CheatsView(GBACheatDevice* device, QWidget* parent)
|
||||||
m_ui.cheatList->setModel(&m_model);
|
m_ui.cheatList->setModel(&m_model);
|
||||||
|
|
||||||
connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
|
connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load()));
|
||||||
|
connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet()));
|
||||||
|
|
||||||
|
connect(m_ui.add, &QPushButton::clicked, [this]() {
|
||||||
|
enterCheat(GBACheatAddLine);
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(m_ui.addGSA, &QPushButton::clicked, [this]() {
|
||||||
|
enterCheat(GBACheatAddGameSharkLine);
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(m_ui.addCB, &QPushButton::clicked, [this]() {
|
||||||
|
enterCheat(GBACheatAddCodeBreakerLine);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void CheatsView::load() {
|
void CheatsView::load() {
|
||||||
|
@ -26,3 +43,23 @@ void CheatsView::load() {
|
||||||
m_model.loadFile(filename);
|
m_model.loadFile(filename);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CheatsView::addSet() {
|
||||||
|
GBACheatSet* set = new GBACheatSet;
|
||||||
|
GBACheatSetInit(set, nullptr);
|
||||||
|
m_model.addSet(set);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {
|
||||||
|
GBACheatSet* set;
|
||||||
|
QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes();
|
||||||
|
if (selection.count() != 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
set = m_model.itemAt(selection[0]);
|
||||||
|
QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts);
|
||||||
|
for (const QString& string : cheats) {
|
||||||
|
callback(set, string.toLocal8Bit().constData());
|
||||||
|
}
|
||||||
|
m_ui.codeEntry->clear();
|
||||||
|
}
|
|
@ -8,6 +8,8 @@
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "CheatsModel.h"
|
#include "CheatsModel.h"
|
||||||
|
|
||||||
#include "ui_CheatsView.h"
|
#include "ui_CheatsView.h"
|
||||||
|
@ -24,8 +26,11 @@ public:
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void load();
|
void load();
|
||||||
|
void addSet();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback);
|
||||||
|
|
||||||
Ui::CheatsView m_ui;
|
Ui::CheatsView m_ui;
|
||||||
CheatsModel m_model;
|
CheatsModel m_model;
|
||||||
};
|
};
|
||||||
|
|
|
@ -26,9 +26,6 @@
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1" colspan="2">
|
<item row="0" column="1" colspan="2">
|
||||||
<widget class="QPushButton" name="addSet">
|
<widget class="QPushButton" name="addSet">
|
||||||
<property name="enabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add New Set</string>
|
<string>Add New Set</string>
|
||||||
</property>
|
</property>
|
||||||
|
@ -36,9 +33,6 @@
|
||||||
</item>
|
</item>
|
||||||
<item row="7" column="1" colspan="2">
|
<item row="7" column="1" colspan="2">
|
||||||
<widget class="QPushButton" name="addGSA">
|
<widget class="QPushButton" name="addGSA">
|
||||||
<property name="enabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add GameShark</string>
|
<string>Add GameShark</string>
|
||||||
</property>
|
</property>
|
||||||
|
@ -56,9 +50,6 @@
|
||||||
</item>
|
</item>
|
||||||
<item row="9" column="1" colspan="2">
|
<item row="9" column="1" colspan="2">
|
||||||
<widget class="QPushButton" name="addCB">
|
<widget class="QPushButton" name="addCB">
|
||||||
<property name="enabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add CodeBreaker</string>
|
<string>Add CodeBreaker</string>
|
||||||
</property>
|
</property>
|
||||||
|
@ -73,9 +64,6 @@
|
||||||
</item>
|
</item>
|
||||||
<item row="6" column="1" colspan="2">
|
<item row="6" column="1" colspan="2">
|
||||||
<widget class="QPushButton" name="add">
|
<widget class="QPushButton" name="add">
|
||||||
<property name="enabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add</string>
|
<string>Add</string>
|
||||||
</property>
|
</property>
|
||||||
|
@ -115,10 +103,7 @@
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="4" column="1" rowspan="2" colspan="2">
|
<item row="4" column="1" rowspan="2" colspan="2">
|
||||||
<widget class="QTextEdit" name="codeEntry">
|
<widget class="QPlainTextEdit" name="codeEntry">
|
||||||
<property name="enabled">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="sizePolicy">
|
<property name="sizePolicy">
|
||||||
<sizepolicy hsizetype="Maximum" vsizetype="Expanding">
|
<sizepolicy hsizetype="Maximum" vsizetype="Expanding">
|
||||||
<horstretch>0</horstretch>
|
<horstretch>0</horstretch>
|
||||||
|
@ -131,6 +116,11 @@
|
||||||
<height>16777215</height>
|
<height>16777215</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="font">
|
||||||
|
<font>
|
||||||
|
<family>Courier New</family>
|
||||||
|
</font>
|
||||||
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
|
|
Loading…
Reference in New Issue