Skip to content

Commit

Permalink
Add a window for lua output
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed Feb 1, 2023
1 parent 3b05bd7 commit d050a1a
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Hook mono sleep function
* Implement XIQueryDevice()
* Implement SDL_JoystickGetDeviceInstanceID()
* Add a window for lua output

### Changed

Expand Down
4 changes: 4 additions & 0 deletions src/program/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
4 changes: 4 additions & 0 deletions src/program/lua/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "Input.h"
#include "Movie.h"
#include "Memory.h"
#include "Print.h"

#include <iostream>
extern "C" {
#include <lua.h>
Expand All @@ -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()
Expand Down
47 changes: 47 additions & 0 deletions src/program/lua/Print.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
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 <http://www.gnu.org/licenses/>.
*/

#include "Print.h"

#include <sstream>
extern "C" {
#include <lua.h>
// #include <lualib.h>
#include <lauxlib.h>
}

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;
}
57 changes: 57 additions & 0 deletions src/program/lua/Print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
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 <http://www.gnu.org/licenses/>.
*/

#ifndef LIBTAS_LUAPRINT_H_INCLUDED
#define LIBTAS_LUAPRINT_H_INCLUDED

#include <QtCore/QObject>
#include <QtCore/QString>

extern "C" {
#include <lua.h>
}

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
62 changes: 62 additions & 0 deletions src/program/ui/LuaConsoleWindow.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
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 <http://www.gnu.org/licenses/>.
*/

#include <QtWidgets/QTableView>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QDialogButtonBox>
#include <QtWidgets/QVBoxLayout>

#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();
}
40 changes: 40 additions & 0 deletions src/program/ui/LuaConsoleWindow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
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 <http://www.gnu.org/licenses/>.
*/

#ifndef LIBTAS_LUACONSOLEWINDOW_H_INCLUDED
#define LIBTAS_LUACONSOLEWINDOW_H_INCLUDED

#include <QtWidgets/QDialog>
#include <QtWidgets/QPlainTextEdit>

class LuaConsoleWindow : public QDialog {
Q_OBJECT

public:
LuaConsoleWindow(QWidget *parent = Q_NULLPTR);

private:
QPlainTextEdit *consoleText;

private slots:
void slotAppend(QString qstr);
void slotClear();
};

#endif
3 changes: 3 additions & 0 deletions src/program/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion src/program/ui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class OsdWindow;
class AnnotationsWindow;
class AutoSaveWindow;
class TimeTraceWindow;
class CustomResolutionDialog;
class LuaConsoleWindow;

class MainWindow : public QMainWindow
{
Expand Down Expand Up @@ -92,6 +92,7 @@ class MainWindow : public QMainWindow
AnnotationsWindow* annotationsWindow;
AutoSaveWindow* autoSaveWindow;
TimeTraceWindow* timeTraceWindow;
LuaConsoleWindow* luaConsoleWindow;

QList<QWidget*> disabledWidgetsOnStart;
QList<QAction*> disabledActionsOnStart;
Expand Down

0 comments on commit d050a1a

Please sign in to comment.