Qt: Removing cheats
Jeffrey Pfau jeffrey@endrift.com
Sat, 14 Feb 2015 18:39:18 -0800
6 files changed,
56 insertions(+),
5 deletions(-)
M
src/gba/cheats.c
→
src/gba/cheats.c
@@ -443,8 +443,23 @@ }
void GBACheatAddSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) { *GBACheatSetsAppend(&device->cheats) = cheats; + _addBreakpoint(device, cheats); _patchROM(device, cheats); - _addBreakpoint(device, cheats); +} + +void GBACheatRemoveSet(struct GBACheatDevice* device, struct GBACheatSet* cheats) { + size_t i; + for (i = 0; i < GBACheatSetsSize(&device->cheats); ++i) { + if (*GBACheatSetsGetPointer(&device->cheats, i) == cheats) { + break; + } + } + if (i == GBACheatSetsSize(&device->cheats)) { + return; + } + GBACheatSetsShift(&device->cheats, i, 1); + _unpatchROM(device, cheats); + _removeBreakpoint(device, cheats); } bool GBACheatAddCodeBreaker(struct GBACheatSet* cheats, uint32_t op1, uint16_t op2) {
M
src/platform/qt/CheatsModel.cpp
→
src/platform/qt/CheatsModel.cpp
@@ -93,6 +93,20 @@ }
return static_cast<GBACheatSet*>(index.internalPointer()); } +void CheatsModel::removeAt(const QModelIndex& index) { + if (!index.isValid()) { + return; + } + int row = index.row(); + GBACheatSet* set = static_cast<GBACheatSet*>(index.internalPointer()); + beginRemoveRows(QModelIndex(), row, row); + GBACheatRemoveSet(m_device, set); + GBACheatSetDeinit(set); + delete set; + endInsertRows(); + +} + void CheatsModel::loadFile(const QString& path) { VFile* vf = VFileOpen(path.toLocal8Bit().constData(), O_RDONLY); if (!vf) {@@ -106,7 +120,7 @@ }
void CheatsModel::addSet(GBACheatSet* set) { beginInsertRows(QModelIndex(), GBACheatSetsSize(&m_device->cheats), GBACheatSetsSize(&m_device->cheats) + 1); - *GBACheatSetsAppend(&m_device->cheats) = set; + GBACheatAddSet(m_device, set); endInsertRows(); }
M
src/platform/qt/CheatsModel.h
→
src/platform/qt/CheatsModel.h
@@ -30,6 +30,7 @@ virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
virtual Qt::ItemFlags flags(const QModelIndex &index) const override; GBACheatSet* itemAt(const QModelIndex& index); + void removeAt(const QModelIndex& index); void loadFile(const QString& path); void addSet(GBACheatSet* set);
M
src/platform/qt/CheatsView.cpp
→
src/platform/qt/CheatsView.cpp
@@ -17,6 +17,7 @@ using namespace QGBA;
CheatsView::CheatsView(GameController* controller, QWidget* parent) : QWidget(parent) + , m_controller(controller) , m_model(controller->cheatDevice()) { m_ui.setupUi(this);@@ -25,6 +26,7 @@ m_ui.cheatList->setModel(&m_model);
connect(m_ui.load, SIGNAL(clicked()), this, SLOT(load())); connect(m_ui.addSet, SIGNAL(clicked()), this, SLOT(addSet())); + connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeSet())); connect(controller, SIGNAL(gameStopped(GBAThread*)), &m_model, SLOT(invalidated())); connect(m_ui.add, &QPushButton::clicked, [this]() {@@ -50,7 +52,20 @@
void CheatsView::addSet() { GBACheatSet* set = new GBACheatSet; GBACheatSetInit(set, nullptr); + m_controller->threadInterrupt(); m_model.addSet(set); + m_controller->threadContinue(); +} + +void CheatsView::removeSet() { + GBACheatSet* set; + QModelIndexList selection = m_ui.cheatList->selectionModel()->selectedIndexes(); + if (selection.count() != 1) { + return; + } + m_controller->threadInterrupt(); + m_model.removeAt(selection[0]); + m_controller->threadContinue(); } void CheatsView::enterCheat(std::function<bool(GBACheatSet*, const char*)> callback) {@@ -60,9 +75,14 @@ if (selection.count() != 1) {
return; } set = m_model.itemAt(selection[0]); + if (!set) { + return; + } + m_controller->threadInterrupt(); QStringList cheats = m_ui.codeEntry->toPlainText().split('\n', QString::SkipEmptyParts); for (const QString& string : cheats) { callback(set, string.toLocal8Bit().constData()); } + m_controller->threadContinue(); m_ui.codeEntry->clear(); }
M
src/platform/qt/CheatsView.h
→
src/platform/qt/CheatsView.h
@@ -29,11 +29,13 @@
private slots: void load(); void addSet(); + void removeSet(); private: void enterCheat(std::function<bool(GBACheatSet*, const char*)> callback); Ui::CheatsView m_ui; + GameController* m_controller; CheatsModel m_model; };
M
src/platform/qt/CheatsView.ui
→
src/platform/qt/CheatsView.ui
@@ -16,9 +16,6 @@ </property>
<layout class="QGridLayout" name="gridLayout"> <item row="2" column="1" colspan="2"> <widget class="QPushButton" name="remove"> - <property name="enabled"> - <bool>false</bool> - </property> <property name="text"> <string>Remove</string> </property>@@ -129,6 +126,8 @@ <tabstops>
<tabstop>cheatList</tabstop> <tabstop>addSet</tabstop> <tabstop>load</tabstop> + <tabstop>save</tabstop> + <tabstop>remove</tabstop> <tabstop>codeEntry</tabstop> <tabstop>add</tabstop> <tabstop>addGSA</tabstop>