Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Add cheat dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFTKP committed Oct 30, 2023
1 parent eeccaf4 commit 65ade55
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 deletions.
126 changes: 124 additions & 2 deletions qt/cheatswindow.cxx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
#include "cheatswindow.hxx"

#include "core_loader.hxx"
#include "hydra/core.hxx"
#include "json.hpp"
#include "settings.hxx"

#include <QCheckBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QPushButton>
#include <QRegularExpression>
#include <QTextEdit>
#include <QVBoxLayout>

static std::vector<uint8_t> cheat_to_bytes(const std::string& cheat)
Expand All @@ -18,9 +28,107 @@ static std::vector<uint8_t> cheat_to_bytes(const std::string& cheat)
return bytes;
}

// TODO: does this close gracefully when mainwindow closes?
class CheatEditDialog : public QDialog
{
public:
CheatEditDialog(hydra::ICheat* cheat_interface, CheatMetadata& metadata)
: QDialog(), cheat_interface_(cheat_interface), metadata_(metadata)
{
setModal(true);
QVBoxLayout* layout = new QVBoxLayout;
QTextEdit* txt_code = new QTextEdit;
QFont font;
font.setFamily("Courier");
font.setFixedPitch(true);
font.setPointSize(10);
txt_code->setFont(font);

if (metadata.code.size() != 0)
{
printf("Setting code to %s\n", metadata.code.c_str());
txt_code->setText(metadata.code.c_str());
}
connect(txt_code, &QTextEdit::textChanged, this, [this, txt_code]() {
QString new_text = txt_code->toPlainText();
new_text.replace(QRegularExpression("[^0-9a-fA-F]"), "");
QStringList tokens;
for (int i = 0; i < new_text.length(); i += 8)
{
tokens << new_text.mid(i, 8);
}
txt_code->blockSignals(true);
new_text = tokens.join(" ");
for (int i = 17; i < new_text.length(); i += 18)
{
new_text[i] = '\n';
}
txt_code->setText(new_text);
txt_code->moveCursor(QTextCursor::End);
txt_code->blockSignals(false);
});
is_editing_ = metadata_.handle != hydra::BAD_CHEAT;
layout->addWidget(txt_code);
setLayout(layout);

QDialogButtonBox* button_box =
new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
layout->addWidget(button_box);
}

private:
CheatMetadata& metadata_;
hydra::ICheat* cheat_interface_;
bool is_editing_;
};

class CheatWidget : public QWidget
{
public:
CheatWidget(hydra::ICheat* cheat_interface, const CheatMetadata& metadata, QListWidget* parent)
: QWidget(), metadata_(metadata)
{
QHBoxLayout* layout = new QHBoxLayout;
QCheckBox* chk_enabled = new QCheckBox;

connect(chk_enabled, &QCheckBox::stateChanged, this, [this](int state) {
metadata_.enabled = state == Qt::Checked;
if (metadata_.enabled)
{
cheat_interface_->enableCheat(metadata_.handle);
}
else
{
cheat_interface_->disableCheat(metadata_.handle);
}
});

QLabel* lbl_name = new QLabel(metadata_.name.c_str());
QPushButton* btn_edit = new QPushButton("Edit");

connect(btn_edit, &QPushButton::clicked, this, [this]() {

});

layout->addWidget(chk_enabled);
layout->addWidget(lbl_name);
layout->addWidget(btn_edit);
setLayout(layout);

QListWidgetItem* list_item = new QListWidgetItem;
list_item->setSizeHint(sizeHint());
parent->addItem(list_item);
parent->setItemWidget(list_item, this);
}

private:
CheatMetadata metadata_;
hydra::ICheat* cheat_interface_;
};

CheatsWindow::CheatsWindow(std::shared_ptr<hydra::EmulatorWrapper> wrapper, bool& open,
const std::string& hash, QWidget* parent)
: QWidget(parent), open_(open), wrapper_(wrapper)
: QWidget(parent, Qt::Window), open_(open), wrapper_(wrapper)
{
bool just_created = false;
if (!std::filesystem::create_directories(Settings::GetSavePath() / "cheats"))
Expand Down Expand Up @@ -79,10 +187,24 @@ CheatsWindow::CheatsWindow(std::shared_ptr<hydra::EmulatorWrapper> wrapper, bool
layout->setContentsMargins(6, 6, 6, 6);
setLayout(layout);

cheats_.resize(1);
CheatMetadata& metadata = cheats_[0];
metadata.enabled = true;
metadata.name = "Infinite Health";
metadata.description = "Infinite health cheat";

QListWidget* list = new QListWidget;
list->addItem("Test cheat");
new CheatWidget(wrapper->shell->asICheat(), metadata, list);
layout->addWidget(list);

QPushButton* btn_add = new QPushButton("Add");
connect(btn_add, &QPushButton::clicked, this, [this, &metadata]() {
CheatEditDialog* dialog = new CheatEditDialog(wrapper_->shell->asICheat(), metadata);
dialog->show();
});
layout->addWidget(btn_add);

show();
open_ = true;
}

Expand Down
13 changes: 7 additions & 6 deletions qt/cheatswindow.hxx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#pragma once

#include "hydra/core.hxx"
#include <filesystem>
#include <memory>
#include <QWidget>

struct CheatMetadata
{
bool enabled;
std::string name;
std::string description;
std::string code;
uint32_t handle;
bool enabled = true;
std::string name{};
std::string description{};
std::string code{};
uint32_t handle = hydra::BAD_CHEAT;
};

namespace hydra
Expand All @@ -32,4 +33,4 @@ private:
std::vector<CheatMetadata> cheats_;
std::shared_ptr<hydra::EmulatorWrapper> wrapper_;
std::filesystem::path cheat_path_;
};
};
4 changes: 4 additions & 0 deletions qt/mainwindow.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,10 @@ void MainWindow::enable_emulation_actions(bool should)
{
stop_act_->setEnabled(should);
reset_act_->setEnabled(should);
scripts_act_->setEnabled(should);
terminal_act_->setEnabled(should);
cheats_act_->setEnabled(should);
shaders_act_->setEnabled(should);
if (should)
screen_->show();
else
Expand Down

0 comments on commit 65ade55

Please sign in to comment.