[Qt] Add capability to load scripts from files

This commit is contained in:
wheremyfoodat 2023-12-16 17:18:52 +02:00
parent c30fbd3801
commit 7c6b498918
4 changed files with 38 additions and 2 deletions

View file

@ -59,6 +59,7 @@ class MainWindow : public QMainWindow {
void swapEmuBuffer();
void emuThreadMainLoop();
void selectLuaFile();
void selectROM();
void dumpRomFS();
void openLuaEditor();

View file

@ -19,4 +19,5 @@ class TextEditorWindow : public QDialog {
public:
TextEditorWindow(QWidget* parent, const std::string& filename, const std::string& initialText);
void setText(const std::string& text) { zepWidget.GetEditor().GetMRUBuffer()->SetText(text); }
};

View file

@ -2,6 +2,7 @@
#include <QFileDialog>
#include <cstdio>
#include <fstream>
MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent), screen(this) {
setWindowTitle("Alber");
@ -23,8 +24,10 @@ MainWindow::MainWindow(QApplication* app, QWidget* parent) : QMainWindow(parent)
auto aboutMenu = menuBar->addMenu(tr("About"));
// Create and bind actions for them
auto pandaAction = fileMenu->addAction(tr("Load game"));
connect(pandaAction, &QAction::triggered, this, &MainWindow::selectROM);
auto loadGameAction = fileMenu->addAction(tr("Load game"));
auto loadLuaAction = fileMenu->addAction(tr("Load Lua script"));
connect(loadGameAction, &QAction::triggered, this, &MainWindow::selectROM);
connect(loadLuaAction, &QAction::triggered, this, &MainWindow::selectLuaFile);
auto pauseAction = emulationMenu->addAction(tr("Pause"));
auto resumeAction = emulationMenu->addAction(tr("Resume"));
@ -124,6 +127,34 @@ void MainWindow::selectROM() {
}
}
void MainWindow::selectLuaFile() {
auto path = QFileDialog::getOpenFileName(this, tr("Select Lua script to load"), "", tr("Lua scripts (*.lua *.txt)"));
if (!path.isEmpty()) {
std::ifstream file(std::filesystem::path(path.toStdU16String()), std::ios::in);
if (file.fail()) {
printf("Failed to load selected lua file\n");
return;
}
// Read whole file into an std::string string
// Get file size, preallocate std::string to avoid furthermemory allocations
std::string code;
file.seekg(0, std::ios::end);
code.resize(file.tellg());
// Rewind and read the whole file
file.seekg(0, std::ios::beg);
file.read(&code[0], code.size());
file.close();
loadLuaScript(code);
// Copy the Lua script to the Lua editor
luaEditor->setText(code);
}
}
// Cleanup when the main window closes
MainWindow::~MainWindow() {
appRunning = false; // Set our running atomic to false in order to make the emulator thread stop, and join it

View file

@ -15,15 +15,18 @@ TextEditorWindow::TextEditorWindow(QWidget* parent, const std::string& filename,
ZepRegressExCommand::Register(zepWidget.GetEditor());
ZepReplExCommand::Register(zepWidget.GetEditor(), &replProvider);
// Default to standard mode instead of vim mode, initialize text box
zepWidget.GetEditor().SetGlobalMode(Zep::ZepMode_Standard::StaticName());
zepWidget.GetEditor().InitWithText(filename, initialText);
// Layout for widgets
QVBoxLayout* mainLayout = new QVBoxLayout();
setLayout(mainLayout);
QPushButton* button = new QPushButton(tr("Load script"), this);
button->setFixedSize(100, 20);
// When the Load Script button is pressed, send the current text to the MainWindow, which will upload it to the emulator's lua object
connect(button, &QPushButton::pressed, this, [this]() {
if (parentWidget()) {
auto buffer = zepWidget.GetEditor().GetMRUBuffer();