From d050a1acf298d9101ea76e4947459eb27e2cd8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gallet?= Date: Wed, 1 Feb 2023 14:02:18 +0100 Subject: [PATCH] Add a window for lua output --- CHANGELOG.md | 1 + src/program/Makefile.am | 4 ++ src/program/lua/Main.cpp | 4 ++ src/program/lua/Print.cpp | 47 ++++++++++++++++++++++ src/program/lua/Print.h | 57 ++++++++++++++++++++++++++ src/program/ui/LuaConsoleWindow.cpp | 62 +++++++++++++++++++++++++++++ src/program/ui/LuaConsoleWindow.h | 40 +++++++++++++++++++ src/program/ui/MainWindow.cpp | 3 ++ src/program/ui/MainWindow.h | 3 +- 9 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/program/lua/Print.cpp create mode 100644 src/program/lua/Print.h create mode 100644 src/program/ui/LuaConsoleWindow.cpp create mode 100644 src/program/ui/LuaConsoleWindow.h diff --git a/CHANGELOG.md b/CHANGELOG.md index d5f4a44c..5a801805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ * Hook mono sleep function * Implement XIQueryDevice() * Implement SDL_JoystickGetDeviceInstanceID() +* Add a window for lua output ### Changed diff --git a/src/program/Makefile.am b/src/program/Makefile.am index f27e440d..2344cc1b 100644 --- a/src/program/Makefile.am +++ b/src/program/Makefile.am @@ -3,6 +3,7 @@ bin_PROGRAMS = libTAS libTAS_QTSOURCES = \ GameEvents.h \ GameLoop.h \ + lua/Print.h \ ramsearch/MemScanner.h \ ui/AnnotationsWindow.h \ ui/ControllerAxisWidget.h \ @@ -16,6 +17,7 @@ libTAS_QTSOURCES = \ ui/InputEditorWindow.h \ ui/InputWindow.h \ ui/KeyPressedDialog.h \ + ui/LuaConsoleWindow.h \ ui/MainWindow.h \ ui/PointerScanModel.h \ ui/PointerScanWindow.h \ @@ -56,6 +58,7 @@ libTAS_SOURCES = \ lua/Main.cpp \ lua/Memory.cpp \ lua/Movie.cpp \ + lua/Print.cpp \ movie/MovieFile.cpp \ movie/MovieFileAnnotations.cpp \ movie/MovieFileEditor.cpp \ @@ -74,6 +77,7 @@ libTAS_SOURCES = \ ui/InputEditorWindow.cpp \ ui/InputWindow.cpp \ ui/KeyPressedDialog.cpp \ + ui/LuaConsoleWindow.cpp \ ui/MainWindow.cpp \ ui/PointerScanModel.cpp \ ui/PointerScanWindow.cpp \ diff --git a/src/program/lua/Main.cpp b/src/program/lua/Main.cpp index 9d8051f0..c3a206d2 100644 --- a/src/program/lua/Main.cpp +++ b/src/program/lua/Main.cpp @@ -22,6 +22,8 @@ #include "Input.h" #include "Movie.h" #include "Memory.h" +#include "Print.h" + #include extern "C" { #include @@ -45,6 +47,8 @@ void Lua::Main::init(Context* context) Lua::Input::registerFunctions(lua_state); Lua::Memory::registerFunctions(lua_state); Lua::Movie::registerFunctions(lua_state, context); + + Lua::Print::init(lua_state); } void Lua::Main::exit() diff --git a/src/program/lua/Print.cpp b/src/program/lua/Print.cpp new file mode 100644 index 00000000..7abbfb0b --- /dev/null +++ b/src/program/lua/Print.cpp @@ -0,0 +1,47 @@ +/* + Copyright 2015-2020 Clément Gallet + + This file is part of libTAS. + + libTAS is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libTAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libTAS. If not, see . + */ + +#include "Print.h" + +#include +extern "C" { +#include +// #include +#include +} + +void Lua::Print::init(lua_State *L) { + lua_pushcfunction(L, print); + lua_setglobal(L, "print"); +} + +/* Redefine lua `print` function to output on a Qt window */ +int Lua::Print::print(lua_State *L) { + std::ostringstream oss; + int nargs = lua_gettop(L); + for (int i = 1; i <= nargs; ++i) { + oss << luaL_tolstring(L, i, nullptr); + lua_pop(L, 1); // remove the string + } + + QString qstr(oss.str().c_str()); + + emit get().signalPrint(qstr); + return 0; +} diff --git a/src/program/lua/Print.h b/src/program/lua/Print.h new file mode 100644 index 00000000..eab199e8 --- /dev/null +++ b/src/program/lua/Print.h @@ -0,0 +1,57 @@ +/* + Copyright 2015-2020 Clément Gallet + + This file is part of libTAS. + + libTAS is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libTAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libTAS. If not, see . + */ + +#ifndef LIBTAS_LUAPRINT_H_INCLUDED +#define LIBTAS_LUAPRINT_H_INCLUDED + +#include +#include + +extern "C" { +#include +} + +namespace Lua { + +class Print : public QObject { + Q_OBJECT + +protected: + Print() = default; + +public: + Print(const Print&) = delete; + Print& operator=(const Print&) = delete; + static void init(lua_State *L); + + static Print &get() { + static Print p; + return p; + } + +private: + static int print(lua_State *L); + +signals: + /* Send the print string to console */ + void signalPrint(QString str); +}; +} + +#endif diff --git a/src/program/ui/LuaConsoleWindow.cpp b/src/program/ui/LuaConsoleWindow.cpp new file mode 100644 index 00000000..00ca965a --- /dev/null +++ b/src/program/ui/LuaConsoleWindow.cpp @@ -0,0 +1,62 @@ +/* + Copyright 2015-2020 Clément Gallet + + This file is part of libTAS. + + libTAS is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libTAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libTAS. If not, see . + */ + +#include +#include +#include +#include + +#include "LuaConsoleWindow.h" +#include "../lua/Print.h" + +LuaConsoleWindow::LuaConsoleWindow(QWidget *parent) : QDialog(parent) +{ + setWindowTitle("Lua Console"); + + /* Text Edit */ + consoleText = new QPlainTextEdit(); + consoleText->setReadOnly(true); + consoleText->setMaximumBlockCount(10000); + connect(&Lua::Print::get(), &Lua::Print::signalPrint, this, &LuaConsoleWindow::slotAppend); + + /* Buttons */ + QPushButton *clearButton = new QPushButton(tr("Clear")); + connect(clearButton, &QAbstractButton::clicked, this, &LuaConsoleWindow::slotClear); + + QDialogButtonBox *buttonBox = new QDialogButtonBox(); + buttonBox->addButton(clearButton, QDialogButtonBox::ActionRole); + + /* Layout */ + QVBoxLayout *mainLayout = new QVBoxLayout; + + mainLayout->addWidget(consoleText); + mainLayout->addWidget(buttonBox); + + setLayout(mainLayout); +} + +void LuaConsoleWindow::slotAppend(QString qstr) +{ + consoleText->appendPlainText(qstr); +} + +void LuaConsoleWindow::slotClear() +{ + consoleText->clear(); +} diff --git a/src/program/ui/LuaConsoleWindow.h b/src/program/ui/LuaConsoleWindow.h new file mode 100644 index 00000000..a03187ab --- /dev/null +++ b/src/program/ui/LuaConsoleWindow.h @@ -0,0 +1,40 @@ +/* + Copyright 2015-2020 Clément Gallet + + This file is part of libTAS. + + libTAS is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libTAS is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libTAS. If not, see . + */ + +#ifndef LIBTAS_LUACONSOLEWINDOW_H_INCLUDED +#define LIBTAS_LUACONSOLEWINDOW_H_INCLUDED + +#include +#include + +class LuaConsoleWindow : public QDialog { + Q_OBJECT + +public: + LuaConsoleWindow(QWidget *parent = Q_NULLPTR); + +private: + QPlainTextEdit *consoleText; + +private slots: + void slotAppend(QString qstr); + void slotClear(); +}; + +#endif diff --git a/src/program/ui/MainWindow.cpp b/src/program/ui/MainWindow.cpp index 9b2cf765..de78bb91 100644 --- a/src/program/ui/MainWindow.cpp +++ b/src/program/ui/MainWindow.cpp @@ -45,6 +45,7 @@ #include "AnnotationsWindow.h" #include "TimeTraceWindow.h" #include "TimeTraceModel.h" +#include "LuaConsoleWindow.h" #include "../movie/MovieFile.h" #include "ErrorChecking.h" #include "../../shared/version.h" @@ -167,6 +168,7 @@ MainWindow::MainWindow(Context* c) : QMainWindow(), context(c) inputEditorWindow = new InputEditorWindow(c, this); annotationsWindow = new AnnotationsWindow(c, this); timeTraceWindow = new TimeTraceWindow(c, this); + luaConsoleWindow = new LuaConsoleWindow(this); connect(gameLoop, &GameLoop::inputsToBeChanged, inputEditorWindow->inputEditorView->inputEditorModel, &InputEditorModel::beginModifyInputs); connect(gameLoop->gameEvents, &GameEvents::inputsToBeChanged, inputEditorWindow->inputEditorView->inputEditorModel, &InputEditorModel::beginModifyInputs); @@ -656,6 +658,7 @@ void MainWindow::createMenus() luaMenu->addAction(tr("Execute Lua script..."), this, &MainWindow::slotLuaExecute); luaMenu->addAction(tr("Reset Lua VM"), this, [=](){Lua::Main::reset(context);}); + luaMenu->addAction(tr("Lua Console..."), luaConsoleWindow, &LuaConsoleWindow::show); toolsMenu->addSeparator(); diff --git a/src/program/ui/MainWindow.h b/src/program/ui/MainWindow.h index ede6e7f1..4d454f0c 100644 --- a/src/program/ui/MainWindow.h +++ b/src/program/ui/MainWindow.h @@ -61,7 +61,7 @@ class OsdWindow; class AnnotationsWindow; class AutoSaveWindow; class TimeTraceWindow; -class CustomResolutionDialog; +class LuaConsoleWindow; class MainWindow : public QMainWindow { @@ -92,6 +92,7 @@ class MainWindow : public QMainWindow AnnotationsWindow* annotationsWindow; AutoSaveWindow* autoSaveWindow; TimeTraceWindow* timeTraceWindow; + LuaConsoleWindow* luaConsoleWindow; QList disabledWidgetsOnStart; QList disabledActionsOnStart;