From 3c1549cc93be4db1eeb36ea727d7cc8652ce9b9e Mon Sep 17 00:00:00 2001 From: Marcus Huderle Date: Fri, 8 May 2020 09:46:26 -0500 Subject: [PATCH] Add setTimeout to script api, and properly refresh scene when overlay is changed --- include/mainwindow.h | 2 ++ src/mainwindow.cpp | 22 ++++++++++++++++++++ src/ui/overlay.cpp | 1 - test_script.js | 48 +++++++++++++++++++++++++++++++++----------- 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/include/mainwindow.h b/include/mainwindow.h index b2b651a8..eed46769 100644 --- a/include/mainwindow.h +++ b/include/mainwindow.h @@ -96,6 +96,8 @@ public: Q_INVOKABLE void setSmartPathsEnabled(bool visible); Q_INVOKABLE bool getSmartPathsEnabled(); Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = ""); + Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds); + void invokeCallback(QJSValue callback); public slots: diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index e562ae64..8fc9e8d9 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -2728,30 +2728,35 @@ void MainWindow::clearOverlay() { if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->overlay.clearItems(); + this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addText(QString text, int x, int y, QString color, int fontSize) { if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->overlay.addText(text, x, y, color, fontSize); + this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addRect(int x, int y, int width, int height, QString color) { if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, false); + this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addFilledRect(int x, int y, int width, int height, QString color) { if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); + this->ui->graphicsView_Map->scene()->update(); } void MainWindow::addImage(int x, int y, QString filepath) { if (!this->ui || !this->ui->graphicsView_Map) return; this->ui->graphicsView_Map->overlay.addImage(x, y, filepath); + this->ui->graphicsView_Map->scene()->update(); } void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { @@ -3000,3 +3005,20 @@ void MainWindow::registerAction(QString functionName, QString actionName, QStrin action->setShortcut(QKeySequence(shortcut)); } } + +void MainWindow::setTimeout(QJSValue callback, int milliseconds) { + if (!callback.isCallable() || milliseconds < 0) + return; + + QTimer *timer = new QTimer(0); + connect(timer, &QTimer::timeout, [=](){ + this->invokeCallback(callback); + }); + connect(timer, SIGNAL(timeout()), timer, SLOT(deleteLater())); + timer->setSingleShot(true); + timer->start(milliseconds); +} + +void MainWindow::invokeCallback(QJSValue callback) { + callback.call(); +} diff --git a/src/ui/overlay.cpp b/src/ui/overlay.cpp index f1be1a94..004a1fed 100644 --- a/src/ui/overlay.cpp +++ b/src/ui/overlay.cpp @@ -43,4 +43,3 @@ void Overlay::addRect(int x, int y, int width, int height, QString color, bool f void Overlay::addImage(int x, int y, QString filepath) { this->items.append(new OverlayImage(x, y, QImage(filepath))); } - diff --git a/test_script.js b/test_script.js index c10f9b39..1e7913b8 100644 --- a/test_script.js +++ b/test_script.js @@ -28,18 +28,6 @@ function applyTintToPalettes(tint) { } } -// Porymap callback when project is opened. -export function on_project_opened(projectPath) { - try { - console.log(`opened ${projectPath}`) - map.registerAction("resetTint", "View Day Tint", "K") - map.registerAction("applyMorningTint", "View Morning Tint", "N") - map.registerAction("applyNightTint", "View Night Tint", "Ctrl+J") - } catch(err) { - console.log(err) - } -} - export function resetTint() { applyTintToPalettes(normalTint) } @@ -51,3 +39,39 @@ export function applyMorningTint() { export function applyNightTint() { applyTintToPalettes(nightTint) } + +let curTint = 0 + +function nextTint() { + map.clearOverlay() + map.addRect(curTint * 40, 40, 40, 40, "#FFFF0000") + curTint = (curTint + 1) % 4 + map.setTimeout(nextTint, 100) +} + +export function animated() { + try { + map.setTimeout(nextTint, 1000) + } catch(err) { + console.log(err) + } +} + +let registered = false + +// Porymap callback when project is opened. +export function on_project_opened(projectPath) { + if (registered) { + return; + } + + try { + // map.registerAction("resetTint", "View Day Tint", "K") + // map.registerAction("applyMorningTint", "View Morning Tint", "N") + // map.registerAction("applyNightTint", "View Night Tint", "Ctrl+J") + map.registerAction("animated", "Animate") + registered = true; + } catch(err) { + console.log(err) + } +}