2022-09-05 01:24:11 +01:00
|
|
|
#include "mainwindow.h"
|
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include "scripting.h"
|
|
|
|
#include "config.h"
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
ScriptUtility::ScriptUtility(MainWindow *mainWindow) {
|
|
|
|
this->window = mainWindow;
|
|
|
|
}
|
|
|
|
|
2023-02-10 16:46:19 +00:00
|
|
|
bool ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) {
|
2022-09-05 16:53:30 +01:00
|
|
|
if (!window || !window->ui || !window->ui->menuTools)
|
2023-02-10 16:46:19 +00:00
|
|
|
return false;
|
2022-09-05 01:24:11 +01:00
|
|
|
|
2023-02-09 18:55:25 +00:00
|
|
|
if (functionName.isEmpty() || actionName.isEmpty()) {
|
|
|
|
logError("Failed to register script action. 'functionName' and 'actionName' must be non-empty.");
|
2023-02-10 16:46:19 +00:00
|
|
|
return false;
|
2023-02-09 18:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this->registeredActions.size() == 0) {
|
2022-09-05 16:53:30 +01:00
|
|
|
QAction *section = window->ui->menuTools->addSection("Custom Actions");
|
2022-09-05 01:24:11 +01:00
|
|
|
this->registeredActions.append(section);
|
|
|
|
}
|
2023-02-09 18:55:25 +00:00
|
|
|
|
|
|
|
const int actionIndex = this->registeredActions.size();
|
|
|
|
QAction *action = window->ui->menuTools->addAction(actionName, [actionIndex](){
|
|
|
|
Scripting::invokeAction(actionIndex);
|
2022-09-05 01:24:11 +01:00
|
|
|
});
|
2023-02-09 18:55:25 +00:00
|
|
|
|
2022-09-05 01:24:11 +01:00
|
|
|
if (!shortcut.isEmpty()) {
|
|
|
|
action->setShortcut(QKeySequence(shortcut));
|
|
|
|
}
|
2023-02-09 18:55:25 +00:00
|
|
|
|
|
|
|
this->actionMap.insert(actionIndex, functionName);
|
2022-09-05 01:24:11 +01:00
|
|
|
this->registeredActions.append(action);
|
2023-02-10 16:46:19 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptUtility::registerToggleAction(QString functionName, QString actionName, QString shortcut, bool checked) {
|
|
|
|
if (!registerAction(functionName, actionName, shortcut))
|
|
|
|
return false;
|
|
|
|
QAction *action = this->registeredActions.last();
|
|
|
|
action->setCheckable(true);
|
|
|
|
action->setChecked(checked);
|
|
|
|
return true;
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::clearActions() {
|
|
|
|
for (auto action : this->registeredActions) {
|
|
|
|
window->ui->menuTools->removeAction(action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 18:55:25 +00:00
|
|
|
QString ScriptUtility::getActionFunctionName(int actionIndex) {
|
|
|
|
return this->actionMap.value(actionIndex);
|
2022-09-05 16:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptUtility::setTimeout(QJSValue callback, int milliseconds) {
|
2022-09-05 01:24:11 +01:00
|
|
|
if (!callback.isCallable() || milliseconds < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QTimer *timer = new QTimer(0);
|
|
|
|
connect(timer, &QTimer::timeout, [=](){
|
2022-09-05 16:53:30 +01:00
|
|
|
this->callTimeoutFunction(callback);
|
2022-09-05 01:24:11 +01:00
|
|
|
});
|
|
|
|
connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
|
|
|
|
timer->setSingleShot(true);
|
|
|
|
timer->start(milliseconds);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::callTimeoutFunction(QJSValue callback) {
|
2022-09-05 01:24:11 +01:00
|
|
|
Scripting::tryErrorJS(callback.call());
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::log(QString message) {
|
2022-09-05 01:24:11 +01:00
|
|
|
logInfo(message);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::warn(QString message) {
|
2022-09-05 01:24:11 +01:00
|
|
|
logWarn(message);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::error(QString message) {
|
2022-09-05 01:24:11 +01:00
|
|
|
logError(message);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon) {
|
|
|
|
QMessageBox messageBox(window);
|
2022-09-05 01:24:11 +01:00
|
|
|
messageBox.setText(text);
|
|
|
|
messageBox.setInformativeText(informativeText);
|
|
|
|
messageBox.setDetailedText(detailedText);
|
|
|
|
messageBox.setIcon(icon);
|
|
|
|
messageBox.exec();
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::showMessage(QString text, QString informativeText, QString detailedText) {
|
2022-09-05 01:24:11 +01:00
|
|
|
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Information);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::showWarning(QString text, QString informativeText, QString detailedText) {
|
2022-09-05 01:24:11 +01:00
|
|
|
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Warning);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::showError(QString text, QString informativeText, QString detailedText) {
|
2022-09-05 01:24:11 +01:00
|
|
|
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Critical);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
bool ScriptUtility::showQuestion(QString text, QString informativeText, QString detailedText) {
|
|
|
|
QMessageBox messageBox(window);
|
2022-09-05 01:24:11 +01:00
|
|
|
messageBox.setText(text);
|
|
|
|
messageBox.setInformativeText(informativeText);
|
|
|
|
messageBox.setDetailedText(detailedText);
|
|
|
|
messageBox.setIcon(QMessageBox::Question);
|
|
|
|
messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
|
|
|
|
return messageBox.exec() == QMessageBox::Yes;
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
QJSValue ScriptUtility::getInputText(QString title, QString label, QString defaultValue) {
|
2022-09-05 01:24:11 +01:00
|
|
|
bool ok;
|
2022-09-05 16:53:30 +01:00
|
|
|
QString input = QInputDialog::getText(window, title, label, QLineEdit::Normal, defaultValue, &ok);
|
2022-09-05 01:24:11 +01:00
|
|
|
return Scripting::dialogInput(input, ok);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
QJSValue ScriptUtility::getInputNumber(QString title, QString label, double defaultValue, double min, double max, int decimals, double step) {
|
2022-09-05 01:24:11 +01:00
|
|
|
bool ok;
|
2022-09-05 16:53:30 +01:00
|
|
|
double input = QInputDialog::getDouble(window, title, label, defaultValue, min, max, decimals, &ok, Qt::WindowFlags(), step);
|
2022-09-05 01:24:11 +01:00
|
|
|
return Scripting::dialogInput(input, ok);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
QJSValue ScriptUtility::getInputItem(QString title, QString label, QStringList items, int defaultValue, bool editable) {
|
2022-09-05 01:24:11 +01:00
|
|
|
bool ok;
|
2022-09-05 16:53:30 +01:00
|
|
|
QString input = QInputDialog::getItem(window, title, label, items, defaultValue, editable, &ok);
|
2022-09-05 01:24:11 +01:00
|
|
|
return Scripting::dialogInput(input, ok);
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
int ScriptUtility::getMainTab() {
|
|
|
|
if (!window || !window->ui || !window->ui->mainTabBar)
|
2022-09-05 01:24:11 +01:00
|
|
|
return -1;
|
2022-09-05 16:53:30 +01:00
|
|
|
return window->ui->mainTabBar->currentIndex();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setMainTab(int index) {
|
|
|
|
if (!window || !window->ui || !window->ui->mainTabBar || index < 0 || index >= window->ui->mainTabBar->count())
|
2022-09-05 01:24:11 +01:00
|
|
|
return;
|
|
|
|
// Can't select Wild Encounters tab if it's disabled
|
2022-09-25 16:02:24 +01:00
|
|
|
if (index == 4 && !userConfig.getEncounterJsonActive())
|
2022-09-05 01:24:11 +01:00
|
|
|
return;
|
2022-09-05 16:53:30 +01:00
|
|
|
window->on_mainTabBar_tabBarClicked(index);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
int ScriptUtility::getMapViewTab() {
|
|
|
|
if (!window || !window->ui || !window->ui->mapViewTab)
|
2022-09-05 01:24:11 +01:00
|
|
|
return -1;
|
2022-09-05 16:53:30 +01:00
|
|
|
return window->ui->mapViewTab->currentIndex();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setMapViewTab(int index) {
|
|
|
|
if (this->getMainTab() != 0 || !window->ui->mapViewTab || index < 0 || index >= window->ui->mapViewTab->count())
|
2022-09-05 01:24:11 +01:00
|
|
|
return;
|
2022-09-05 16:53:30 +01:00
|
|
|
window->on_mapViewTab_tabBarClicked(index);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setGridVisibility(bool visible) {
|
|
|
|
window->ui->checkBox_ToggleGrid->setChecked(visible);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
bool ScriptUtility::getGridVisibility() {
|
|
|
|
return window->ui->checkBox_ToggleGrid->isChecked();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setBorderVisibility(bool visible) {
|
|
|
|
window->editor->toggleBorderVisibility(visible, false);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
bool ScriptUtility::getBorderVisibility() {
|
|
|
|
return window->ui->checkBox_ToggleBorder->isChecked();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setSmartPathsEnabled(bool visible) {
|
|
|
|
window->ui->checkBox_smartPaths->setChecked(visible);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
bool ScriptUtility::getSmartPathsEnabled() {
|
|
|
|
return window->ui->checkBox_smartPaths->isChecked();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
QList<QString> ScriptUtility::getCustomScripts() {
|
2023-09-05 22:02:35 +01:00
|
|
|
return userConfig.getCustomScriptPaths();
|
2022-09-05 16:53:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<int> ScriptUtility::getMetatileLayerOrder() {
|
2023-02-01 20:02:26 +00:00
|
|
|
if (!window || !window->editor || !window->editor->layout)
|
2022-09-05 01:24:11 +01:00
|
|
|
return QList<int>();
|
2023-02-01 20:02:26 +00:00
|
|
|
return window->editor->layout->metatileLayerOrder;
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setMetatileLayerOrder(QList<int> order) {
|
2023-02-01 20:02:26 +00:00
|
|
|
if (!window || !window->editor || !window->editor->layout)
|
2022-09-05 01:24:11 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
const int numLayers = 3;
|
|
|
|
int size = order.size();
|
|
|
|
if (size < numLayers) {
|
|
|
|
logError(QString("Metatile layer order has insufficient elements (%1), needs at least %2.").arg(size).arg(numLayers));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool invalid = false;
|
|
|
|
for (int i = 0; i < numLayers; i++) {
|
|
|
|
int layer = order.at(i);
|
|
|
|
if (layer < 0 || layer >= numLayers) {
|
|
|
|
logError(QString("'%1' is not a valid metatile layer order value, must be in range 0-%2.").arg(layer).arg(numLayers - 1));
|
|
|
|
invalid = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (invalid) return;
|
|
|
|
|
2023-02-01 20:02:26 +00:00
|
|
|
window->editor->layout->metatileLayerOrder = order;
|
2022-09-05 16:53:30 +01:00
|
|
|
window->refreshAfterPalettePreviewChange();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
QList<float> ScriptUtility::getMetatileLayerOpacity() {
|
2023-02-01 20:02:26 +00:00
|
|
|
if (!window || !window->editor || !window->editor->layout)
|
2022-09-05 01:24:11 +01:00
|
|
|
return QList<float>();
|
2023-02-01 20:02:26 +00:00
|
|
|
return window->editor->layout->metatileLayerOpacity;
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-09-05 16:53:30 +01:00
|
|
|
void ScriptUtility::setMetatileLayerOpacity(QList<float> order) {
|
2023-02-01 20:02:26 +00:00
|
|
|
if (!window || !window->editor || !window->editor->layout)
|
2022-09-05 01:24:11 +01:00
|
|
|
return;
|
2023-02-01 20:02:26 +00:00
|
|
|
window->editor->layout->metatileLayerOpacity = order;
|
2022-09-05 16:53:30 +01:00
|
|
|
window->refreshAfterPalettePreviewChange();
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-10-01 04:01:00 +01:00
|
|
|
QList<QString> ScriptUtility::getMapNames() {
|
2022-09-05 16:53:30 +01:00
|
|
|
if (!window || !window->editor || !window->editor->project)
|
2022-10-01 04:01:00 +01:00
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->mapNames;
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|
|
|
|
|
2022-10-01 04:01:00 +01:00
|
|
|
QList<QString> ScriptUtility::getTilesetNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->tilesetLabelsOrdered;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getPrimaryTilesetNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
2022-10-23 23:59:59 +01:00
|
|
|
return window->editor->project->primaryTilesetLabels;
|
2022-10-01 04:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getSecondaryTilesetNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
2022-10-23 23:59:59 +01:00
|
|
|
return window->editor->project->secondaryTilesetLabels;
|
2022-10-01 04:01:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getMetatileBehaviorNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->metatileBehaviorMap.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getSongNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->songNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getLocationNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->mapSectionNameToValue.keys();
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getWeatherNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->weatherNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getMapTypeNames() {
|
|
|
|
if (!window || !window->editor || !window->editor->project)
|
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->mapTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<QString> ScriptUtility::getBattleSceneNames() {
|
2022-09-05 16:53:30 +01:00
|
|
|
if (!window || !window->editor || !window->editor->project)
|
2022-10-01 04:01:00 +01:00
|
|
|
return QList<QString>();
|
|
|
|
return window->editor->project->mapBattleScenes;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptUtility::isPrimaryTileset(QString tilesetName) {
|
|
|
|
return getPrimaryTilesetNames().contains(tilesetName);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptUtility::isSecondaryTileset(QString tilesetName) {
|
|
|
|
return getSecondaryTilesetNames().contains(tilesetName);
|
2022-09-05 01:24:11 +01:00
|
|
|
}
|