From 41c30cdf376730a68fcda69b57b1139f143a5543 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Wed, 26 Dec 2018 19:53:20 +0100 Subject: Fix indentation and code-style. --- src/codeeditor.cc | 47 ++-- src/codeeditor.h | 28 +-- src/kaiman.cc | 26 +- src/luascript.cc | 673 ++++++++++++++++++++++++++++------------------------ src/luascript.h | 66 +++--- src/mainwindow.cc | 72 +++--- src/mainwindow.h | 25 +- src/outputwindow.cc | 317 +++++++++++++------------ src/outputwindow.h | 72 +++--- src/soundplayer.cc | 158 ++++++------ src/soundplayer.h | 36 +-- 11 files changed, 798 insertions(+), 722 deletions(-) (limited to 'src') diff --git a/src/codeeditor.cc b/src/codeeditor.cc index 44b0e9b..d639953 100644 --- a/src/codeeditor.cc +++ b/src/codeeditor.cc @@ -60,13 +60,12 @@ CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) highlightCurrentLine(); } - - int CodeEditor::lineNumberAreaWidth() { int digits = 1; int max = qMax(1, blockCount()); - while (max >= 10) { + while(max >= 10) + { max /= 10; ++digits; } @@ -76,44 +75,43 @@ int CodeEditor::lineNumberAreaWidth() return space; } - - void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); } - - void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) { - if (dy) + if(dy) + { lineNumberArea->scroll(0, dy); + } else + { lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); + } - if (rect.contains(viewport()->rect())) + if(rect.contains(viewport()->rect())) + { updateLineNumberAreaWidth(0); + } } - - void CodeEditor::resizeEvent(QResizeEvent *e) { QPlainTextEdit::resizeEvent(e); QRect cr = contentsRect(); lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), - cr.height())); + cr.height())); } - - void CodeEditor::highlightCurrentLine() { QList extraSelections; - if (!isReadOnly()) { + if(!isReadOnly()) + { QTextEdit::ExtraSelection selection; QColor lineColor = QColor(Qt::yellow).lighter(160); @@ -139,20 +137,27 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) QPainter painter(lineNumberArea); painter.fillRect(event->rect(), Qt::lightGray); - QTextBlock block = firstVisibleBlock(); int blockNumber = block.blockNumber(); int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); int bottom = top + (int) blockBoundingRect(block).height(); - while (block.isValid() && top <= event->rect().bottom()) { - if (block.isVisible() && bottom >= event->rect().top()) { + while(block.isValid() && top <= event->rect().bottom()) + { + if(block.isVisible() && bottom >= event->rect().top()) + { QString number = QString::number(blockNumber + 1); - if(blockNumber + 1 == lineNumber) painter.setPen(Qt::green); - else painter.setPen(Qt::black); + if(blockNumber + 1 == lineNumber) + { + painter.setPen(Qt::green); + } + else + { + painter.setPen(Qt::black); + } painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), - Qt::AlignRight, number); + Qt::AlignRight, number); } block = block.next(); diff --git a/src/codeeditor.h b/src/codeeditor.h index 1f0274a..a470a52 100644 --- a/src/codeeditor.h +++ b/src/codeeditor.h @@ -39,9 +39,7 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - -#ifndef __CODEEDITOR_H__ -#define __CODEEDITOR_H__ +#pragma once #include #include @@ -53,11 +51,12 @@ class QWidget; class LineNumberArea; -class CodeEditor : public QPlainTextEdit +class CodeEditor + : public QPlainTextEdit { Q_OBJECT - public: +public: CodeEditor(QWidget *parent = 0); void lineNumberAreaPaintEvent(QPaintEvent *event); @@ -67,7 +66,7 @@ protected: void resizeEvent(QResizeEvent *event); public slots: - void runningLine(int lineno); + void runningLine(int lineno); private slots: void updateLineNumberAreaWidth(int newBlockCount); @@ -76,28 +75,29 @@ private slots: private: QWidget *lineNumberArea; - int lineNumber; + int lineNumber; }; - -class LineNumberArea : public QWidget +class LineNumberArea + : public QWidget { public: - LineNumberArea(CodeEditor *editor) : QWidget(editor) { + LineNumberArea(CodeEditor *editor) : QWidget(editor) + { codeEditor = editor; } - QSize sizeHint() const { + QSize sizeHint() const + { return QSize(codeEditor->lineNumberAreaWidth(), 0); } protected: - void paintEvent(QPaintEvent *event) { + void paintEvent(QPaintEvent *event) + { codeEditor->lineNumberAreaPaintEvent(event); } private: CodeEditor *codeEditor; }; - -#endif/*__CODEEDITOR_H__*/ diff --git a/src/kaiman.cc b/src/kaiman.cc index 75d3035..8a605e1 100644 --- a/src/kaiman.cc +++ b/src/kaiman.cc @@ -34,24 +34,24 @@ void __attribute__ ((constructor)) premain() { - printf("premain()\n"); - printf("locale: %s\n", setlocale(LC_ALL, "da_DK")); + printf("premain()\n"); + printf("locale: %s\n", setlocale(LC_ALL, "da_DK")); } int main(int argc, char *argv[]) { - if(argc != 2) { - printf("Usage: %s script\n", argv[0]); - return 1; - } + if(argc != 2) + { + printf("Usage: %s script\n", argv[0]); + return 1; + } - // QLocale::setDefault(QLocale(QLocale::Danish, QLocale::Denmark)); - - QApplication app(argc, argv); + // QLocale::setDefault(QLocale(QLocale::Danish, QLocale::Denmark)); - MainWindow wnd(argv[1]); - wnd.showMaximized(); + QApplication app(argc, argv); - return app.exec(); -} + MainWindow wnd(argv[1]); + wnd.showMaximized(); + return app.exec(); +} diff --git a/src/luascript.cc b/src/luascript.cc index ecbed53..e023a1d 100644 --- a/src/luascript.cc +++ b/src/luascript.cc @@ -31,6 +31,7 @@ // For atoi #include +#include //#include "luautil.h" @@ -40,420 +41,474 @@ #define ERR(x, fmt...) printf(fmt) static int _debug(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - std::string msg = lua_tostring(L, lua_gettop(L)); +{ + + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + std::string msg = lua_tostring(L, lua_gettop(L)); - printf("%s\n", msg.c_str()); + printf("%s\n", msg.c_str()); - return 0; + return 0; } static int _forward(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - double x = lua_tonumber(L, lua_gettop(L)); +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + double x = lua_tonumber(L, lua_gettop(L)); - printf("forward %f\n", x); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + printf("forward %f\n", x); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - lua->out->forward(x * 5); + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } - return 0; + lua->out->forward(x * 5); + + return 0; } static int _coord(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } + + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } + + lua_pushnumber(L, lua->out->coordX()); + lua_pushnumber(L, lua->out->coordY()); + + return 2; +} + +static int _loadpen(lua_State *L) +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + const char *pen = lua_tostring(L, lua_gettop(L)); + + printf("load pen %s\n", pen); + + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } - lua_pushnumber(L, lua->out->coordX()); - lua_pushnumber(L, lua->out->coordY()); + lua->out->loadPen(pen); - return 2; + return 0; } -static int _loadpen(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - const char *pen = lua_tostring(L, lua_gettop(L)); +static int _speed(lua_State *L) +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + int x = lua_tonumber(L, lua_gettop(L)); + + printf("speed %d\n", x); - printf("load pen %s\n", pen); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } - lua->out->loadPen(pen); + lua->out->setSpeed(x); - return 0; + return 0; } -static int _speed(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - int x = lua_tonumber(L, lua_gettop(L)); +static int _sleep(lua_State *L) +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); - printf("speed %d\n", x); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + int s = lua_tonumber(L, lua_gettop(L)); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + printf("sleep %d\n", s); - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - lua->out->setSpeed(x); + lua->sleep(s); - return 0; + return 0; } static int _scale(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - double x = lua_tonumber(L, lua_gettop(L)); +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + double x = lua_tonumber(L, lua_gettop(L)); + + printf("scale %f\n", x); - printf("scale %f\n", x); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } - lua->out->setScale(x); + lua->out->setScale(x); - return 0; + return 0; } static int _colour(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - int r = luaL_checknumber(L, 1); - int g = luaL_checknumber(L, 2); - int b = luaL_checknumber(L, 3); - int a = luaL_checknumber(L, 4); - - printf("colour %d %d %d %d\n", r,b,g,a); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } - - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } - - lua->out->setColour(r,g,b,a); - - return 0; +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + int r = luaL_checknumber(L, 1); + int g = luaL_checknumber(L, 2); + int b = luaL_checknumber(L, 3); + int a = luaL_checknumber(L, 4); + + printf("colour %d %d %d %d\n", r,b,g,a); + + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } + + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } + + lua->out->setColour(r,g,b,a); + + return 0; } static int _turn(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - double x = lua_tonumber(L, lua_gettop(L)); +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); + + double x = lua_tonumber(L, lua_gettop(L)); - printf("turn %f\n", x); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + printf("turn %f\n", x); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(lua->lua_stop) { - printf("stopping...\n"); - lua_pushstring(L, "stop"); - lua_error(L); - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - lua->out->turn(-x * 10); + if(lua->lua_stop) + { + printf("stopping...\n"); + lua_pushstring(L, "stop"); + lua_error(L); + } - return 0; + lua->out->turn(-x * 10); + + return 0; } static int _playsound(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ - QString file = lua_tostring(L, lua_gettop(L)); +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + QString file = lua_tostring(L, lua_gettop(L)); - lua->playSound(file); + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - return 0; + lua->playSound(file); + + return 0; } static int _reset(lua_State *L) -{/* - Pracro::checkParameters(L, - Pracro::T_STRING, - Pracro::T_END); - */ +{ + //Pracro::checkParameters(L, + // Pracro::T_STRING, + // Pracro::T_END); - printf("reset\n"); + printf("reset\n"); - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return 1; - } + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return 1; + } - if(lua->lua_stop) { - lua_pushstring(L, "stop"); - lua_error(L); - } + if(lua->lua_stop) + { + lua_pushstring(L, "stop"); + lua_error(L); + } - lua->out->reset(); + lua->out->reset(); - return 0; + return 0; } void hook(lua_State *L, lua_Debug *ar) { - lua_getinfo(L, "Snl", ar); - - lua_getglobal(L, GLOBAL_POINTER); - LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); - - if(!lua) { - lua_pushstring(L, "No LUA pointer!"); - lua_error(L); - return; - } - - // printf("kaiman file: %s\n", lua->file.toStdString().c_str()); - - if(lua->file == QString(ar->short_src)) { - // printf(" ---- currentline: %d\n", ar->currentline); - lua->lineChange(ar->currentline); - qApp->processEvents(); - } - /* - printf("HOOK:\n"); - printf(" what: %s\n", ar->what); - printf(" name: %s\n", ar->name); - printf(" source: %s\n", ar->source); - printf(" short_src: %s\n", ar->short_src); - printf(" currentline: %d\n", ar->currentline); - */ + lua_getinfo(L, "Snl", ar); + + lua_getglobal(L, GLOBAL_POINTER); + LUAScript *lua = (LUAScript*)lua_touserdata(L, lua_gettop(L)); + + if(!lua) + { + lua_pushstring(L, "No LUA pointer!"); + lua_error(L); + return; + } + + //printf("kaiman file: %s\n", lua->file.toStdString().c_str()); + + if(lua->file == QString(ar->short_src)) + { + // printf(" ---- currentline: %d\n", ar->currentline); + lua->lineChange(ar->currentline); + qApp->processEvents(); + } + + //printf("HOOK:\n"); + //printf(" what: %s\n", ar->what); + //printf(" name: %s\n", ar->name); + //printf(" source: %s\n", ar->source); + //printf(" short_src: %s\n", ar->short_src); + //printf(" currentline: %d\n", ar->currentline); } LUAScript::LUAScript(OutputWindow *o, QString f) { - DEBUG(luascript, "LUAScript()\n"); - file = f; - L = NULL; - out = o; + DEBUG(luascript, "LUAScript()\n"); + file = f; + L = NULL; + out = o; - // Don't call init from here, it need to be done in the thread. + // Don't call init from here, it need to be done in the thread. - lua_stop = false; - lua_stopped = true; + lua_stop = true; + lua_stopped = true; } LUAScript::~LUAScript() { - DEBUG(luascript, "~LUAScript()\n"); + DEBUG(luascript, "~LUAScript()\n"); } void LUAScript::lineChange(int lineno) { - emit lineChanged(lineno); + emit lineChanged(lineno); } void LUAScript::init() - throw(Exception) { - if(L) return; - - L = luaL_newstate(); - if(L == NULL) { - ERR(luascript, "Could not create LUA state.\n"); - throw Exception("Could not create LUA state."); - } - - luaL_openlibs(L); - - lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance - lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. - - lua_register(L, "debug", _debug); - lua_register(L, "forward", _forward); - lua_register(L, "turn", _turn); - lua_register(L, "speed", _speed); - lua_register(L, "scale", _scale); - lua_register(L, "reset", _reset); - lua_register(L, "colour", _colour); - lua_register(L, "loadpen", _loadpen); - lua_register(L, "coord", _coord); - lua_register(L, "playsound", _playsound); - - lua_sethook(L, hook, LUA_MASKLINE, 0); + if(L) + { + return; + } + + L = luaL_newstate(); + if(L == NULL) + { + ERR(luascript, "Could not create LUA state.\n"); + throw Exception("Could not create LUA state."); + } + + luaL_openlibs(L); + + lua_pushlightuserdata(L, this); // Push the pointer to 'this' instance + lua_setglobal(L, GLOBAL_POINTER); // Assign it to a global lua var. + + lua_register(L, "debug", _debug); + lua_register(L, "forward", _forward); + lua_register(L, "turn", _turn); + lua_register(L, "speed", _speed); + lua_register(L, "scale", _scale); + lua_register(L, "reset", _reset); + lua_register(L, "colour", _colour); + lua_register(L, "loadpen", _loadpen); + lua_register(L, "coord", _coord); + lua_register(L, "playsound", _playsound); + lua_register(L, "sleep", _sleep); + + lua_sethook(L, hook, LUA_MASKLINE, 0); } void LUAScript::cleanup() { - if(L) { - lua_close(L); - L = NULL; - } + if(L) + { + lua_close(L); + L = NULL; + } } void LUAScript::run() { - lua_stopped = false; - try { - init(); - runScript(); - } catch(Exception &e) { - printf("LUA Error: %s\n", e.msg.c_str()); - } - cleanup(); - lua_stopped = true; - printf("LUA Thread Stopped!\n"); + lua_stopped = false; + try + { + init(); + runScript(); + } + catch(Exception &e) + { + printf("LUA Error: %s\n", e.msg.c_str()); + } + cleanup(); + lua_stopped = true; + printf("LUA Thread Stopped!\n"); } void LUAScript::playSound(QString file) { - player.playFile(file); + player.playFile(file); } void LUAScript::stopScript() { - lua_stop = true; - while(!lua_stopped) { - wait(25); - qApp->processEvents(); - printf("!\n"); - } + lua_stop = true; + while(!lua_stopped) + { + wait(25); + qApp->processEvents(); + printf("!\n"); + } } void LUAScript::runScript() - throw(Exception) { - lua_stop = false; - - try { - init(); - } catch(Exception &e) { - throw Exception(e.msg); - } - - if(L == NULL) { - ERR(luascript, "LUA state not initialized!"); - return; - } - - top = lua_gettop(L); - - // DEBUG(luascript, "Running %s\n", file); - - if(luaL_loadfile(L, file.toStdString().c_str())) { - ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L))); - throw Exception(lua_tostring(L, lua_gettop(L))); - } - - // Run the loaded code - if(lua_pcall(L, 0, LUA_MULTRET, 0)) { - ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); - throw Exception(lua_tostring(L, lua_gettop(L))); - } + lua_stop = false; + + try + { + init(); + } + catch(Exception &e) + { + throw Exception(e.msg); + } + + if(L == NULL) + { + ERR(luascript, "LUA state not initialized!"); + return; + } + + top = lua_gettop(L); + + // DEBUG(luascript, "Running %s\n", file); + + if(luaL_loadfile(L, file.toStdString().c_str())) + { + ERR(luascript, "loadbuffer: %s\n", lua_tostring(L, lua_gettop(L))); + throw Exception(lua_tostring(L, lua_gettop(L))); + } + + // Run the loaded code + if(lua_pcall(L, 0, LUA_MULTRET, 0)) + { + ERR(luascript, "pcall: %s\n" , lua_tostring(L, lua_gettop(L))); + throw Exception(lua_tostring(L, lua_gettop(L))); + } +} + +void LUAScript::sleep(int n) +{ + ::sleep(n); } diff --git a/src/luascript.h b/src/luascript.h index 93478df..c7ea9b7 100644 --- a/src/luascript.h +++ b/src/luascript.h @@ -25,8 +25,7 @@ * along with Pracro; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __PRACRO_LUASCRIPT_H__ -#define __PRACRO_LUASCRIPT_H__ +#pragma once #include #include @@ -40,54 +39,55 @@ #include -class LUAScript : public QThread { -Q_OBJECT +class LUAScript + : public QThread +{ + Q_OBJECT public: - class Exception { - public: - Exception(std::string m) : msg(m) {} - std::string msg; - }; + class Exception + { + public: + Exception(std::string m) : msg(m) {} + std::string msg; + }; - LUAScript(OutputWindow *out, QString file); - ~LUAScript(); + LUAScript(OutputWindow *out, QString file); + ~LUAScript(); - void init() throw(Exception); - void cleanup(); + void init(); + void cleanup(); - void run(); - void runScript() throw(Exception); + void run(); + void runScript(); - OutputWindow *out; + OutputWindow *out; - void lineChange(int lineno); + void lineChange(int lineno); - void playSound(QString file); + void playSound(QString file); + void sleep(int n); - volatile bool lua_stop; - volatile bool lua_stopped; - QString file; + volatile bool lua_stop; + volatile bool lua_stopped; + QString file; public slots: - void stopScript(); + void stopScript(); signals: - void reset(); - void lineChanged(int lineno); + void reset(); + void lineChanged(int lineno); protected: - lua_State *L; + lua_State *L; private: - std::vector > scripts; - std::map values; + std::vector > scripts; + std::map values; - int top; + int top; - bool running; + bool running; - SoundPlayer player; + SoundPlayer player; }; - - -#endif/*__PRACRO_LUASCRIPT_H__*/ diff --git a/src/mainwindow.cc b/src/mainwindow.cc index 8271eb6..bd52c0c 100644 --- a/src/mainwindow.cc +++ b/src/mainwindow.cc @@ -37,53 +37,51 @@ MainWindow::MainWindow(QString p) { - program = p; + program = p; - /* // Watch file on disk? - connect(&watcher, SIGNAL(fileChanged(const QString &)), - this, SLOT(reset())); + // Watch file on disk? + //connect(&watcher, SIGNAL(fileChanged(const QString &)), + // this, SLOT(reset())); + //watcher.addPath(program); - watcher.addPath(program); - */ + QSplitter *splitter = new QSplitter(); + setCentralWidget(splitter); - QSplitter *splitter = new QSplitter(); - setCentralWidget(splitter); + editor = new CodeEditor(); + splitter->addWidget(editor); + QFile file(program); + file.open(QIODevice::ReadOnly); + editor->setPlainText(file.readAll()); + file.close(); - editor = new CodeEditor(); - splitter->addWidget(editor); - QFile file(program); - file.open(QIODevice::ReadOnly); - editor->setPlainText(file.readAll()); - file.close(); + out = new OutputWindow(); + splitter->addWidget(out); - out = new OutputWindow(); - splitter->addWidget(out); + l = new LUAScript(out, program); + connect(l, SIGNAL(lineChanged(int)), editor, SLOT(runningLine(int))); - l = new LUAScript(out, program); - connect(l, SIGNAL(lineChanged(int)), editor, SLOT(runningLine(int))); + QToolBar *toolbar = new QToolBar(); + addToolBar(Qt::TopToolBarArea, toolbar); + QAction *act_run = toolbar->addAction("Run"); + connect(act_run, SIGNAL(triggered()), this, SLOT(reset())); - QToolBar *toolbar = new QToolBar(); - addToolBar(Qt::TopToolBarArea, toolbar); - QAction *act_run = toolbar->addAction("Run"); - connect(act_run, SIGNAL(triggered()), this, SLOT(reset())); + QAction *act_stop = toolbar->addAction("Stop"); + connect(act_stop, SIGNAL(triggered()), l, SLOT(stopScript())); - QAction *act_stop = toolbar->addAction("Stop"); - connect(act_stop, SIGNAL(triggered()), l, SLOT(stopScript())); - - reset(); + reset(); } void MainWindow::reset() { - printf("Resetting...\n"); - QFile file(program); - file.open(QIODevice::WriteOnly); - QString code = editor->toPlainText(); - file.write(code.toLocal8Bit()); - file.close(); - out->stopScript(); - l->stopScript(); - out->reset(); - l->start(); - printf("Reset done\n"); + printf("Resetting...\n"); + QFile file(program); + file.open(QIODevice::WriteOnly); + QString code = editor->toPlainText(); + file.write(code.toLocal8Bit()); + file.close(); + out->stopScript(); + l->stopScript(); + out->reset(); + l->start(); + printf("Reset done\n"); } diff --git a/src/mainwindow.h b/src/mainwindow.h index c9a1844..1138dfb 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -25,8 +25,7 @@ * along with Kaiman; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __KAIMAN_MAINWINDOW_H__ -#define __KAIMAN_MAINWINDOW_H__ +#pragma once #include @@ -36,21 +35,21 @@ #include "outputwindow.h" #include "luascript.h" -class MainWindow : public QMainWindow { -Q_OBJECT +class MainWindow + : public QMainWindow +{ + Q_OBJECT public: - MainWindow(QString program); + MainWindow(QString program); private slots: - void reset(); + void reset(); private: - CodeEditor *editor; + CodeEditor *editor; - QFileSystemWatcher watcher; - QString program; - OutputWindow *out; - LUAScript *l; + QFileSystemWatcher watcher; + QString program; + OutputWindow *out; + LUAScript *l; }; - -#endif/*__KAIMAN_MAINWINDOW_H__*/ diff --git a/src/outputwindow.cc b/src/outputwindow.cc index 498af23..815da8c 100644 --- a/src/outputwindow.cc +++ b/src/outputwindow.cc @@ -34,216 +34,233 @@ OutputWindow::OutputWindow() { - resize(1000, 1000); - - /* - QTransform t; - t.scale(0.2, 0.2); - kaiman = kaiman.transformed(t, Qt::SmoothTransformation); - */ - connect(&timer, SIGNAL(timeout()), this, SLOT(timeout())); - timer.setSingleShot(true); - timer.start(25); - - reset(); + resize(1000, 1000); + + //QTransform t; + //t.scale(0.2, 0.2); + //kaiman = kaiman.transformed(t, Qt::SmoothTransformation); + + connect(&timer, SIGNAL(timeout()), this, SLOT(timeout())); + timer.setSingleShot(true); + timer.start(25); + + reset(); } double OutputWindow::coordX() { - return x; + return x; } double OutputWindow::coordY() { - return y; + return y; } void OutputWindow::stopScript() { - stop = true; + stop = true; } void OutputWindow::loadPen(QString file) { - sem.acquire(); - penfile = file; - loadpen = true; + sem.acquire(); + penfile = file; + loadpen = true; } void OutputWindow::setScale(double s) { - scale = s; - sem.acquire(); + scale = s; + sem.acquire(); } void OutputWindow::setSpeed(int s) { - if(s < 1) speed = 100; - else if(s > 100) speed = 1; - else speed = 101 - s; - sem.acquire(); + if(s < 1) + { + speed = 100; + } + else if(s > 100) + { + speed = 1; + } + else + { + speed = 101 - s; + } + sem.acquire(); } void OutputWindow::setColour(int r, int g, int b, int a) { - colour = QColor(r, g, b, a); - sem.acquire(); + colour = QColor(r, g, b, a); + sem.acquire(); } void OutputWindow::timeout() { - /* - x++; - y++; - r++; - */ - repaint(); - timer.start(25); + //x++; + //y++; + //r++; + repaint(); + timer.start(25); } void OutputWindow::paintEvent(QPaintEvent *) { - // sem.acquire(); - QPainter p(this); - // QTransform tp; tp.scale(3, 3); p.setTransform(tp); - - if(loadpen) { - QPixmap img(penfile); - kaiman = img.toImage(); - loadpen = false; - } - - QTransform t; - t.rotate(-r + 90); - t.scale((100.0 / kaiman.width() * scale), - (100.0 / kaiman.width() * scale) ); - QImage img = kaiman.transformed(t, Qt::SmoothTransformation); - p.drawImage((x * scale) - img.width()/2 + width() / 2, - (y * scale) - img.height()/2 + height() / 2, img); - - QTransform gt; - gt.translate(width() / 2, height() / 2); - gt.scale(scale, scale); - p.setTransform(gt); - - QPen pen; - pen.setWidth(4); - /* - p.setPen(pen); - p.drawLines(points); - */ - for(int i = 0; i < lines.size(); i++) { - ColLine l = lines[i]; - pen.setColor(l.colour); - p.setPen(pen); - QLineF line = l.line; - line.setP1(line.p1()); - line.setP2(line.p2()); - p.drawLine(line); - } - - pen.setWidth(2); - pen.setStyle(Qt::DotLine); - // pen.setCapStyle(Qt::RoundCap); - pen.setColor(Qt::black); - - // pen.setColor(colour); - p.setPen(pen); - for(int i = 0; i < current_points.size(); i+=2) { - QPointF p1 = current_points[i]; - QPointF p2 = current_points[i + 1]; - p.drawLine(QLineF(p1, p2)); - } - - QColor c = colour; - c.setAlpha(c.alpha() / 4); - pen.setStyle(Qt::SolidLine); - pen.setWidth(4); - pen.setColor(c); - p.setPen(pen); - for(int i = 0; i < current_points.size(); i+=2) { - QPointF p1 = current_points[i]; - QPointF p2 = current_points[i + 1]; - p.drawLine(QLineF(p1, p2)); - } - - sem.release(); + //sem.acquire(); + QPainter p(this); + //QTransform tp; tp.scale(3, 3); p.setTransform(tp); + + if(loadpen) + { + QPixmap img(penfile); + kaiman = img.toImage(); + loadpen = false; + } + + QTransform t; + t.rotate(-r + 90); + t.scale((100.0 / kaiman.width() * scale), + (100.0 / kaiman.width() * scale) ); + QImage img = kaiman.transformed(t, Qt::SmoothTransformation); + p.drawImage((x * scale) - img.width()/2 + width() / 2, + (y * scale) - img.height()/2 + height() / 2, img); + + QTransform gt; + gt.translate(width() / 2, height() / 2); + gt.scale(scale, scale); + p.setTransform(gt); + + QPen pen; + pen.setWidth(4); + //p.setPen(pen); + //p.drawLines(points); + for(int i = 0; i < lines.size(); i++) + { + ColLine l = lines[i]; + pen.setColor(l.colour); + p.setPen(pen); + QLineF line = l.line; + line.setP1(line.p1()); + line.setP2(line.p2()); + p.drawLine(line); + } + + pen.setWidth(2); + pen.setStyle(Qt::DotLine); + // pen.setCapStyle(Qt::RoundCap); + pen.setColor(Qt::black); + + // pen.setColor(colour); + p.setPen(pen); + for(int i = 0; i < current_points.size(); i+=2) + { + QPointF p1 = current_points[i]; + QPointF p2 = current_points[i + 1]; + p.drawLine(QLineF(p1, p2)); + } + + QColor c = colour; + c.setAlpha(c.alpha() / 4); + pen.setStyle(Qt::SolidLine); + pen.setWidth(4); + pen.setColor(c); + p.setPen(pen); + for(int i = 0; i < current_points.size(); i+=2) + { + QPointF p1 = current_points[i]; + QPointF p2 = current_points[i + 1]; + p.drawLine(QLineF(p1, p2)); + } + + sem.release(); } void OutputWindow::reset() { - scale = 2.0; - speed = 50; - penfile = "gfx/kaiman.png"; - loadpen = true; - - lines.clear(); - current_points.clear(); - x = 0;//(width() / 2) / scale; - y = 0;//(height() / 2) / scale; - r = 0; - colour = QColor(150, 0, 0, 150); - stop = false; - while(sem.tryAcquire()) {} + scale = 2.0; + speed = 50; + penfile = "gfx/kaiman.png"; + loadpen = true; + + lines.clear(); + current_points.clear(); + x = 0;//(width() / 2) / scale; + y = 0;//(height() / 2) / scale; + r = 0; + colour = QColor(150, 0, 0, 150); + stop = false; + while(sem.tryAcquire()) + { + } } static inline int sign(int x) { - if(x > 0) return 1; - return -1; + if(x > 0) + { + return 1; + } + return -1; } void OutputWindow::forward(double dist) { - sem.acquire(); + sem.acquire(); - float target_x = sin(r * (M_PI / 180.0)) * dist; - float target_y = cos(r * (M_PI / 180.0)) * dist; + float target_x = sin(r * (M_PI / 180.0)) * dist; + float target_y = cos(r * (M_PI / 180.0)) * dist; - float source_x = x; - float source_y = y; - - int spd = abs(dist * speed) / 60;//28; - for(int i = 0; i < spd; i++) { - if(stop) return; - float d = (float)i / (float)spd; + float source_x = x; + float source_y = y; - current_points.clear(); - current_points.append(QPointF(source_x, source_y)); + int spd = abs(dist * speed) / 60;//28; + for(int i = 0; i < spd; i++) + { + if(stop) return; + float d = (float)i / (float)spd; - x = source_x * (1 - d) + (source_x + target_x) * d; - y = source_y * (1 - d) + (source_y + target_y) * d; + current_points.clear(); + current_points.append(QPointF(source_x, source_y)); - current_points.append(QPointF(x, y)); + x = source_x * (1 - d) + (source_x + target_x) * d; + y = source_y * (1 - d) + (source_y + target_y) * d; - sem.acquire(); - } - x = source_x + target_x; - y = source_y + target_y; + current_points.append(QPointF(x, y)); - ColLine l; - l.colour = colour; - l.line = QLine(QPoint(source_x, source_y), - QPoint(x, y)); - lines.append(l); + sem.acquire(); + } + x = source_x + target_x; + y = source_y + target_y; + ColLine l; + l.colour = colour; + l.line = QLine(QPoint(source_x, source_y), + QPoint(x, y)); + lines.append(l); - current_points.clear(); - sem.acquire(); + current_points.clear(); + sem.acquire(); } // Turn x degrees void OutputWindow::turn(double x) { - double spd = (speed * abs(x) / 80.0); - double offset = this->r; - sem.acquire(); - for(int i = 0; i < spd; i++) { - if(stop) return; - double p = (double)i / spd; - this->r = offset + x * p; - sem.acquire(); - } - this->r = offset + x; - sem.acquire(); + double spd = (speed * abs(x) / 80.0); + double offset = this->r; + sem.acquire(); + for(int i = 0; i < spd; i++) + { + if(stop) + { + return; + } + double p = (double)i / spd; + this->r = offset + x * p; + sem.acquire(); + } + this->r = offset + x; + sem.acquire(); } diff --git a/src/outputwindow.h b/src/outputwindow.h index bd8bf27..5e91f71 100644 --- a/src/outputwindow.h +++ b/src/outputwindow.h @@ -25,8 +25,7 @@ * along with Kaiman; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __KAIMAN_OUTPUTWINDOW_H__ -#define __KAIMAN_OUTPUTWINDOW_H__ +#pragma once #include #include @@ -35,53 +34,54 @@ #include #include -class OutputWindow : public QGLWidget { -Q_OBJECT +class OutputWindow + : public QGLWidget +{ + Q_OBJECT public: - OutputWindow(); + OutputWindow(); - void forward(double x); - void turn(double x); + void forward(double x); + void turn(double x); - void stopScript(); + void stopScript(); - void setSpeed(int s); - void setScale(double s); - void setColour(int r, int g, int b, int a); + void setSpeed(int s); + void setScale(double s); + void setColour(int r, int g, int b, int a); - void loadPen(QString file); + void loadPen(QString file); - double coordX(); - double coordY(); + double coordX(); + double coordY(); public slots: - void timeout(); - void reset(); + void timeout(); + void reset(); protected: - void paintEvent(QPaintEvent * event); + void paintEvent(QPaintEvent * event); private: - class ColLine { - public: - QColor colour; - QLine line; - }; - QImage kaiman; - double x, y, r; - QTimer timer; - QSemaphore sem; - QVector lines; - QVector current_points; - QColor colour; - volatile bool stop; + class ColLine + { + public: + QColor colour; + QLine line; + }; + QImage kaiman; + double x, y, r; + QTimer timer; + QSemaphore sem; + QVector lines; + QVector current_points; + QColor colour; + volatile bool stop; - volatile int speed; + volatile int speed; - volatile bool loadpen; - QString penfile; + volatile bool loadpen; + QString penfile; - volatile float scale; + volatile float scale; }; - -#endif/*__KAIMAN_OUTPUTWINDOW_H__*/ diff --git a/src/soundplayer.cc b/src/soundplayer.cc index c59e84e..e5feebc 100644 --- a/src/soundplayer.cc +++ b/src/soundplayer.cc @@ -34,101 +34,103 @@ SoundPlayer::SoundPlayer() { - start(); + start(); } SoundPlayer::~SoundPlayer() { - running = false; + running = false; wait(); } void SoundPlayer::run() { - - printf("SoundPlayer running\n"); - - ao_initialize(); - - ao_sample_format sf; - memset(&sf, 0, sizeof(sf)); - sf.bits = 16; - sf.rate = 44100; - sf.channels = 2; - sf.byte_format = AO_FMT_NATIVE; - - ao_device *dev = ao_open_live(ao_default_driver_id(), &sf, 0); - - running = true; - - short s[BUFSZ]; - while(running) { - - { // Check for new Selection. - QMutexLocker lock(&mutex); - while(queue.size()) { - printf(" - New sound\n"); - active.append(queue.front()); - queue.pop_front(); - } - } - - memset(s, 0, BUFSZ * sizeof(short)); - - QList::iterator it = active.begin(); - while(it != active.end()) { - QueueItem &item = *it; - - for(size_t i = 0; i < BUFSZ; i++) { - if(item.pos >= item.size) { - it = active.erase(it); - goto nextitem; - } - s[i] += item.samples[item.pos] * 32536; - item.pos++; - } - - it++; - - nextitem: - int a = 1;(void)a; - } - - ao_play(dev, (char*)s, BUFSZ * sizeof(short)); - } - - ao_close(dev); - ao_shutdown(); + printf("SoundPlayer running\n"); + + ao_initialize(); + + ao_sample_format sf; + memset(&sf, 0, sizeof(sf)); + sf.bits = 16; + sf.rate = 44100; + sf.channels = 2; + sf.byte_format = AO_FMT_NATIVE; + + ao_device *dev = ao_open_live(ao_default_driver_id(), &sf, 0); + + running = true; + + short s[BUFSZ]; + while(running) + { + { // Check for new Selection. + QMutexLocker lock(&mutex); + while(queue.size()) + { + printf(" - New sound\n"); + active.append(queue.front()); + queue.pop_front(); + } + } + + memset(s, 0, BUFSZ * sizeof(short)); + + QList::iterator it = active.begin(); + while(it != active.end()) + { + QueueItem &item = *it; + + for(size_t i = 0; i < BUFSZ; i++) + { + if(item.pos >= item.size) + { + it = active.erase(it); + goto nextitem; + } + s[i] += item.samples[item.pos] * 32536 * 0.5; + item.pos++; + } + + it++; + + nextitem: + int a = 1;(void)a; + } + + ao_play(dev, (char*)s, BUFSZ * sizeof(short)); + } + + ao_close(dev); + ao_shutdown(); } void SoundPlayer::playFile(QString file) { + printf(" - playFile(%s)\n", file.toStdString().c_str()); - printf(" - playFile(%s)\n", file.toStdString().c_str()); - - SNDFILE *fh; - SF_INFO sf_info; + SNDFILE *fh; + SF_INFO sf_info; - fh = sf_open(file.toStdString().c_str(), SFM_READ, &sf_info); - if(!fh) { - printf("Load error '%s'\n", file.toStdString().c_str()); - return; - } - - size_t size = sf_info.frames * sf_info.channels; - float *data = new float[size]; - sf_read_float(fh, data, size); + fh = sf_open(file.toStdString().c_str(), SFM_READ, &sf_info); + if(!fh) + { + printf("Load error '%s'\n", file.toStdString().c_str()); + return; + } - sf_close(fh); + size_t size = sf_info.frames * sf_info.channels; + float *data = new float[size]; + sf_read_float(fh, data, size); - QueueItem qi; - qi.samples = data; - qi.pos = 0; - qi.size = size; + sf_close(fh); - { - QMutexLocker lock(&mutex); - queue.append(qi); - } + QueueItem qi; + qi.samples = data; + qi.pos = 0; + qi.size = size; + { + QMutexLocker lock(&mutex); + queue.append(qi); + } } diff --git a/src/soundplayer.h b/src/soundplayer.h index c5b36d4..8502e6e 100644 --- a/src/soundplayer.h +++ b/src/soundplayer.h @@ -25,36 +25,36 @@ * along with Kaiman; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __KAIMAN_SOUNDPLAYER_H__ -#define __KAIMAN_SOUNDPLAYER_H__ +#pragma once #include #include #include #include -class QueueItem { +class QueueItem +{ public: - float *samples; - size_t pos; - size_t size; + float *samples; + size_t pos; + size_t size; }; -class SoundPlayer : public QThread { +class SoundPlayer + : public QThread +{ public: - SoundPlayer(); - ~SoundPlayer(); - - void playFile(QString file); + SoundPlayer(); + ~SoundPlayer(); - void run(); + void playFile(QString file); + + void run(); private: - volatile bool running; - QMutex mutex; + volatile bool running; + QMutex mutex; - QList queue; - QList active; + QList queue; + QList active; }; - -#endif/*__KAIMAN_SOUNDPLAYER_H__*/ -- cgit v1.2.3