Add setTimeout to script api, and properly refresh scene when overlay is changed

This commit is contained in:
Marcus Huderle 2020-05-08 09:46:26 -05:00
parent 71242e5714
commit 3c1549cc93
4 changed files with 60 additions and 13 deletions

View file

@ -96,6 +96,8 @@ public:
Q_INVOKABLE void setSmartPathsEnabled(bool visible); Q_INVOKABLE void setSmartPathsEnabled(bool visible);
Q_INVOKABLE bool getSmartPathsEnabled(); Q_INVOKABLE bool getSmartPathsEnabled();
Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = ""); Q_INVOKABLE void registerAction(QString functionName, QString actionName, QString shortcut = "");
Q_INVOKABLE void setTimeout(QJSValue callback, int milliseconds);
void invokeCallback(QJSValue callback);
public slots: public slots:

View file

@ -2728,30 +2728,35 @@ void MainWindow::clearOverlay() {
if (!this->ui || !this->ui->graphicsView_Map) if (!this->ui || !this->ui->graphicsView_Map)
return; return;
this->ui->graphicsView_Map->overlay.clearItems(); 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) { void MainWindow::addText(QString text, int x, int y, QString color, int fontSize) {
if (!this->ui || !this->ui->graphicsView_Map) if (!this->ui || !this->ui->graphicsView_Map)
return; return;
this->ui->graphicsView_Map->overlay.addText(text, x, y, color, fontSize); 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) { void MainWindow::addRect(int x, int y, int width, int height, QString color) {
if (!this->ui || !this->ui->graphicsView_Map) if (!this->ui || !this->ui->graphicsView_Map)
return; return;
this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, false); 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) { void MainWindow::addFilledRect(int x, int y, int width, int height, QString color) {
if (!this->ui || !this->ui->graphicsView_Map) if (!this->ui || !this->ui->graphicsView_Map)
return; return;
this->ui->graphicsView_Map->overlay.addRect(x, y, width, height, color, true); 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) { void MainWindow::addImage(int x, int y, QString filepath) {
if (!this->ui || !this->ui->graphicsView_Map) if (!this->ui || !this->ui->graphicsView_Map)
return; return;
this->ui->graphicsView_Map->overlay.addImage(x, y, filepath); this->ui->graphicsView_Map->overlay.addImage(x, y, filepath);
this->ui->graphicsView_Map->scene()->update();
} }
void MainWindow::refreshAfterPaletteChange(Tileset *tileset) { void MainWindow::refreshAfterPaletteChange(Tileset *tileset) {
@ -3000,3 +3005,20 @@ void MainWindow::registerAction(QString functionName, QString actionName, QStrin
action->setShortcut(QKeySequence(shortcut)); 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();
}

View file

@ -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) { void Overlay::addImage(int x, int y, QString filepath) {
this->items.append(new OverlayImage(x, y, QImage(filepath))); this->items.append(new OverlayImage(x, y, QImage(filepath)));
} }

View file

@ -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() { export function resetTint() {
applyTintToPalettes(normalTint) applyTintToPalettes(normalTint)
} }
@ -51,3 +39,39 @@ export function applyMorningTint() {
export function applyNightTint() { export function applyNightTint() {
applyTintToPalettes(nightTint) 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)
}
}