From 122473b575ea791d7ec57d5d01c600cfd2db0749 Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Sun, 19 Jul 2020 21:15:56 -0400 Subject: [PATCH] Added Lua control window. --- src/CMakeLists.txt | 1 + src/drivers/Qt/ConsoleWindow.cpp | 63 ++------ src/drivers/Qt/LuaControl.cpp | 246 +++++++++++++++++++++++++++++++ src/drivers/Qt/LuaControl.h | 47 ++++++ src/lua-engine.cpp | 11 +- 5 files changed, 310 insertions(+), 58 deletions(-) create mode 100644 src/drivers/Qt/LuaControl.cpp create mode 100644 src/drivers/Qt/LuaControl.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5b4900ff..9d2dd703 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -390,6 +390,7 @@ set(SRC_DRIVERS_SDL ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/HotKeyConf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/PaletteConf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/GuiConf.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/LuaControl.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/ConsoleVideoConf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/ConsoleSoundConf.cpp ${CMAKE_CURRENT_SOURCE_DIR}/drivers/Qt/AboutWindow.cpp diff --git a/src/drivers/Qt/ConsoleWindow.cpp b/src/drivers/Qt/ConsoleWindow.cpp index f52b2caf..5502c9bb 100644 --- a/src/drivers/Qt/ConsoleWindow.cpp +++ b/src/drivers/Qt/ConsoleWindow.cpp @@ -21,6 +21,7 @@ #include "Qt/HotKeyConf.h" #include "Qt/PaletteConf.h" #include "Qt/GuiConf.h" +#include "Qt/LuaControl.h" #include "Qt/ConsoleSoundConf.h" #include "Qt/ConsoleVideoConf.h" #include "Qt/AboutWindow.h" @@ -829,62 +830,18 @@ void consoleWin_t::takeScreenShot(void) void consoleWin_t::loadLua(void) { #ifdef _S9XLUA_H - int ret, useNativeFileDialogVal; - QString filename; - std::string last; - char dir[512]; - QFileDialog dialog(this, tr("Open LUA Script") ); + LuaControlDialog_t *luaCtrlWin; - dialog.setFileMode(QFileDialog::ExistingFile); + //printf("Open Lua Control Window\n"); + + luaCtrlWin = new LuaControlDialog_t(this); + + luaCtrlWin->show(); + luaCtrlWin->exec(); - dialog.setNameFilter(tr("LUA Scripts (*.lua *.LUA) ;; All files (*)")); + delete luaCtrlWin; - dialog.setViewMode(QFileDialog::List); - - g_config->getOption ("SDL.LastLoadLua", &last ); - - if ( last.size() == 0 ) - { - last.assign( "/usr/share/fceux/luaScripts" ); - } - - getDirFromFile( last.c_str(), dir ); - - dialog.setDirectory( tr(dir) ); - - // Check config option to use native file dialog or not - g_config->getOption ("SDL.UseNativeFileDialog", &useNativeFileDialogVal); - - dialog.setOption(QFileDialog::DontUseNativeDialog, !useNativeFileDialogVal); - - dialog.show(); - ret = dialog.exec(); - - if ( ret ) - { - QStringList fileList; - fileList = dialog.selectedFiles(); - - if ( fileList.size() > 0 ) - { - filename = fileList[0]; - } - } - - if ( filename.isNull() ) - { - return; - } - qDebug() << "selected file path : " << filename.toUtf8(); - - g_config->setOption ("SDL.LastLoadLua", filename.toStdString().c_str() ); - - fceuWrapperLock(); - if ( 0 == FCEU_LoadLuaCode( filename.toStdString().c_str() ) ) - { - printf("Error: Could not open the selected lua script: '%s'\n", filename.toStdString().c_str() ); - } - fceuWrapperUnLock(); + //printf("Lua Control Window Destroyed\n"); #endif } diff --git a/src/drivers/Qt/LuaControl.cpp b/src/drivers/Qt/LuaControl.cpp new file mode 100644 index 00000000..277d1077 --- /dev/null +++ b/src/drivers/Qt/LuaControl.cpp @@ -0,0 +1,246 @@ +// LuaControl.cpp +// +#include + +#include +#include + +#include "../../fceu.h" + +#ifdef _S9XLUA_H +#include "../../fceulua.h" +#endif + +#include "Qt/LuaControl.h" +#include "Qt/main.h" +#include "Qt/input.h" +#include "Qt/config.h" +#include "Qt/keyscan.h" +#include "Qt/fceuWrapper.h" + +static bool luaScriptRunning = false; + +static std::list winList; +//---------------------------------------------------- +LuaControlDialog_t::LuaControlDialog_t(QWidget *parent) + : QDialog( parent ) +{ + QVBoxLayout *mainLayout; + QHBoxLayout *hbox; + QLabel *lbl; + std::string filename; + + resize( 512, 512 ); + + setWindowTitle( tr("Lua Script Control") ); + + mainLayout = new QVBoxLayout(); + + lbl = new QLabel( tr("Script File:") ); + + scriptPath = new QLineEdit(); + scriptArgs = new QLineEdit(); + + g_config->getOption ("SDL.LastLoadLua", &filename ); + + scriptPath->setText( filename.c_str() ); + + luaOutput = new QTextEdit(); + luaOutput->setReadOnly(true); + + hbox = new QHBoxLayout(); + + browseButton = new QPushButton( tr("Browse") ); + stopButton = new QPushButton( tr("Stop") ); + + if ( luaScriptRunning ) + { + startButton = new QPushButton( tr("Restart") ); + } + else + { + startButton = new QPushButton( tr("Start") ); + } + + stopButton->setEnabled( luaScriptRunning ); + + connect(browseButton , SIGNAL(clicked()), this, SLOT(openLuaScriptFile(void)) ); + connect(stopButton , SIGNAL(clicked()), this, SLOT(stopLuaScript(void)) ); + connect(startButton , SIGNAL(clicked()), this, SLOT(startLuaScript(void)) ); + + hbox->addWidget( browseButton ); + hbox->addWidget( stopButton ); + hbox->addWidget( startButton ); + + mainLayout->addWidget( lbl ); + mainLayout->addWidget( scriptPath ); + mainLayout->addLayout( hbox ); + + hbox = new QHBoxLayout(); + lbl = new QLabel( tr("Arguments:") ); + + hbox->addWidget( lbl ); + hbox->addWidget( scriptArgs ); + + mainLayout->addLayout( hbox ); + + lbl = new QLabel( tr("Output Console:") ); + mainLayout->addWidget( lbl ); + mainLayout->addWidget( luaOutput ); + + //connect(useNativeFileDialog , SIGNAL(stateChanged(int)), this, SLOT(useNativeFileDialogChanged(int)) ); + + setLayout( mainLayout ); + + winList.push_back( this ); +} + +//---------------------------------------------------- +LuaControlDialog_t::~LuaControlDialog_t(void) +{ + std::list ::iterator it; + + for (it = winList.begin(); it != winList.end(); it++) + { + if ( (*it) == this ) + { + winList.erase(it); + //printf("Removing Lua Window\n"); + break; + } + } +} +//---------------------------------------------------- +void LuaControlDialog_t::closeWindow(void) +{ + //printf("Close Window\n"); + done(0); +} +//---------------------------------------------------- +void LuaControlDialog_t::openLuaScriptFile(void) +{ +#ifdef _S9XLUA_H + int ret, useNativeFileDialogVal; + QString filename; + std::string last; + //char dir[512]; + QFileDialog dialog(this, tr("Open LUA Script") ); + + dialog.setFileMode(QFileDialog::ExistingFile); + + dialog.setNameFilter(tr("LUA Scripts (*.lua *.LUA) ;; All files (*)")); + + dialog.setViewMode(QFileDialog::List); + + g_config->getOption ("SDL.LastLoadLua", &last ); + + if ( last.size() == 0 ) + { + last.assign( "/usr/share/fceux/luaScripts" ); + } + + //getDirFromFile( last.c_str(), dir ); + + //dialog.setDirectory( tr(dir) ); + + // Check config option to use native file dialog or not + g_config->getOption ("SDL.UseNativeFileDialog", &useNativeFileDialogVal); + + dialog.setOption(QFileDialog::DontUseNativeDialog, !useNativeFileDialogVal); + + dialog.show(); + ret = dialog.exec(); + + if ( ret ) + { + QStringList fileList; + fileList = dialog.selectedFiles(); + + if ( fileList.size() > 0 ) + { + filename = fileList[0]; + } + } + + if ( filename.isNull() ) + { + return; + } + qDebug() << "selected file path : " << filename.toUtf8(); + + g_config->setOption ("SDL.LastLoadLua", filename.toStdString().c_str() ); + + scriptPath->setText( filename.toStdString().c_str() ); + +#endif +} +//---------------------------------------------------- +void LuaControlDialog_t::startLuaScript(void) +{ +#ifdef _S9XLUA_H + fceuWrapperLock(); + if ( 0 == FCEU_LoadLuaCode( scriptPath->text().toStdString().c_str(), scriptArgs->text().toStdString().c_str() ) ) + { + printf("Error: Could not open the selected lua script: '%s'\n", scriptPath->text().toStdString().c_str() ); + } + fceuWrapperUnLock(); +#endif +} +//---------------------------------------------------- +void LuaControlDialog_t::stopLuaScript(void) +{ +#ifdef _S9XLUA_H + fceuWrapperLock(); + FCEU_LuaStop(); + fceuWrapperUnLock(); +#endif +} +//---------------------------------------------------- +void LuaControlDialog_t::refreshState(void) +{ + if ( luaScriptRunning ) + { + stopButton->setEnabled( true ); + startButton->setText( tr("Restart") ); + } + else + { + stopButton->setEnabled( false ); + startButton->setText( tr("Start") ); + } +} +//---------------------------------------------------- +void updateLuaWindows( void ) +{ + std::list ::iterator it; + + for (it = winList.begin(); it != winList.end(); it++) + { + (*it)->refreshState(); + } +} +//---------------------------------------------------- +void WinLuaOnStart(intptr_t hDlgAsInt) +{ + luaScriptRunning = true; + + printf("Lua Script Running: %i \n", luaScriptRunning ); + + updateLuaWindows(); +} +//---------------------------------------------------- +void WinLuaOnStop(intptr_t hDlgAsInt) +{ + luaScriptRunning = false; + + printf("Lua Script Running: %i \n", luaScriptRunning ); + + updateLuaWindows(); +} +//---------------------------------------------------- +void PrintToWindowConsole(intptr_t hDlgAsInt, const char* str) +{ + printf("%s\n", str ); + +} +//---------------------------------------------------- diff --git a/src/drivers/Qt/LuaControl.h b/src/drivers/Qt/LuaControl.h new file mode 100644 index 00000000..824f52fa --- /dev/null +++ b/src/drivers/Qt/LuaControl.h @@ -0,0 +1,47 @@ +// LuaControl.h +// + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "Qt/main.h" + +class LuaControlDialog_t : public QDialog +{ + Q_OBJECT + + public: + LuaControlDialog_t(QWidget *parent = 0); + ~LuaControlDialog_t(void); + + void refreshState(void); + + protected: + QLineEdit *scriptPath; + QLineEdit *scriptArgs; + QPushButton *browseButton; + QPushButton *stopButton; + QPushButton *startButton; + QTextEdit *luaOutput; + private: + + public slots: + void closeWindow(void); + private slots: + void openLuaScriptFile(void); + void startLuaScript(void); + void stopLuaScript(void); + +}; diff --git a/src/lua-engine.cpp b/src/lua-engine.cpp index 645eaf31..42344162 100644 --- a/src/lua-engine.cpp +++ b/src/lua-engine.cpp @@ -170,11 +170,11 @@ static intptr_t info_uid; #ifdef WIN32 extern HWND LuaConsoleHWnd; extern INT_PTR CALLBACK DlgLuaScriptDialog(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam); +void TaseditorDisableManualFunctionIfNeeded(); +#endif extern void PrintToWindowConsole(intptr_t hDlgAsInt, const char* str); extern void WinLuaOnStart(intptr_t hDlgAsInt); extern void WinLuaOnStop(intptr_t hDlgAsInt); -void TaseditorDisableManualFunctionIfNeeded(); -#endif static lua_State *L; @@ -6249,9 +6249,10 @@ int FCEU_LoadLuaCode(const char *filename, const char *arg) { LuaConsoleHWnd = CreateDialog(fceu_hInstance, MAKEINTRESOURCE(IDD_LUA), hAppWnd, DlgLuaScriptDialog); info_uid = (intptr_t)LuaConsoleHWnd; #else - info_print = NULL; - info_onstart = NULL; - info_onstop = NULL; + info_print = PrintToWindowConsole; + info_onstart = WinLuaOnStart; + info_onstop = WinLuaOnStop; + info_uid = (intptr_t)0; #endif if (info_onstart) info_onstart(info_uid);