porymap/src/scriptapi/apiutility.cpp

300 lines
9.9 KiB
C++
Raw Normal View History

2022-09-05 01:24:11 +01:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "scripting.h"
#include "config.h"
ScriptUtility::ScriptUtility(MainWindow *mainWindow) {
this->window = mainWindow;
}
2023-02-10 16:46:19 +00:00
bool ScriptUtility::registerAction(QString functionName, QString actionName, QString shortcut) {
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
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;
}
if (this->registeredActions.size() == 0) {
QAction *section = window->ui->menuTools->addSection("Custom Actions");
2022-09-05 01:24:11 +01:00
this->registeredActions.append(section);
}
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
});
2022-09-05 01:24:11 +01:00
if (!shortcut.isEmpty()) {
action->setShortcut(QKeySequence(shortcut));
}
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
}
void ScriptUtility::clearActions() {
for (auto action : this->registeredActions) {
window->ui->menuTools->removeAction(action);
}
}
QString ScriptUtility::getActionFunctionName(int actionIndex) {
return this->actionMap.value(actionIndex);
}
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, [=](){
this->callTimeoutFunction(callback);
2022-09-05 01:24:11 +01:00
});
connect(timer, &QTimer::timeout, timer, &QTimer::deleteLater);
timer->setSingleShot(true);
timer->start(milliseconds);
}
void ScriptUtility::callTimeoutFunction(QJSValue callback) {
2022-09-05 01:24:11 +01:00
Scripting::tryErrorJS(callback.call());
}
void ScriptUtility::log(QString message) {
2022-09-05 01:24:11 +01:00
logInfo(message);
}
void ScriptUtility::warn(QString message) {
2022-09-05 01:24:11 +01:00
logWarn(message);
}
void ScriptUtility::error(QString message) {
2022-09-05 01:24:11 +01:00
logError(message);
}
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();
}
void ScriptUtility::showMessage(QString text, QString informativeText, QString detailedText) {
2022-09-05 01:24:11 +01:00
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Information);
}
void ScriptUtility::showWarning(QString text, QString informativeText, QString detailedText) {
2022-09-05 01:24:11 +01:00
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Warning);
}
void ScriptUtility::showError(QString text, QString informativeText, QString detailedText) {
2022-09-05 01:24:11 +01:00
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Critical);
}
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;
}
QJSValue ScriptUtility::getInputText(QString title, QString label, QString defaultValue) {
2022-09-05 01:24:11 +01:00
bool ok;
QString input = QInputDialog::getText(window, title, label, QLineEdit::Normal, defaultValue, &ok);
2022-09-05 01:24:11 +01:00
return Scripting::dialogInput(input, ok);
}
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;
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);
}
QJSValue ScriptUtility::getInputItem(QString title, QString label, QStringList items, int defaultValue, bool editable) {
2022-09-05 01:24:11 +01:00
bool ok;
QString input = QInputDialog::getItem(window, title, label, items, defaultValue, editable, &ok);
2022-09-05 01:24:11 +01:00
return Scripting::dialogInput(input, ok);
}
int ScriptUtility::getMainTab() {
if (!window || !window->ui || !window->ui->mainTabBar)
2022-09-05 01:24:11 +01:00
return -1;
return window->ui->mainTabBar->currentIndex();
2022-09-05 01:24:11 +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
if (index == 4 && !userConfig.getEncounterJsonActive())
2022-09-05 01:24:11 +01:00
return;
window->on_mainTabBar_tabBarClicked(index);
2022-09-05 01:24:11 +01:00
}
int ScriptUtility::getMapViewTab() {
if (!window || !window->ui || !window->ui->mapViewTab)
2022-09-05 01:24:11 +01:00
return -1;
return window->ui->mapViewTab->currentIndex();
2022-09-05 01:24:11 +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;
window->on_mapViewTab_tabBarClicked(index);
2022-09-05 01:24:11 +01:00
}
void ScriptUtility::setGridVisibility(bool visible) {
window->ui->checkBox_ToggleGrid->setChecked(visible);
2022-09-05 01:24:11 +01:00
}
bool ScriptUtility::getGridVisibility() {
return window->ui->checkBox_ToggleGrid->isChecked();
2022-09-05 01:24:11 +01:00
}
void ScriptUtility::setBorderVisibility(bool visible) {
window->editor->toggleBorderVisibility(visible, false);
2022-09-05 01:24:11 +01:00
}
bool ScriptUtility::getBorderVisibility() {
return window->ui->checkBox_ToggleBorder->isChecked();
2022-09-05 01:24:11 +01:00
}
void ScriptUtility::setSmartPathsEnabled(bool visible) {
window->ui->checkBox_smartPaths->setChecked(visible);
2022-09-05 01:24:11 +01:00
}
bool ScriptUtility::getSmartPathsEnabled() {
return window->ui->checkBox_smartPaths->isChecked();
2022-09-05 01:24:11 +01:00
}
QList<QString> ScriptUtility::getCustomScripts() {
return userConfig.getCustomScriptPaths();
}
QList<int> ScriptUtility::getMetatileLayerOrder() {
if (!window || !window->editor || !window->editor->layout)
2022-09-05 01:24:11 +01:00
return QList<int>();
return window->editor->layout->metatileLayerOrder;
2022-09-05 01:24:11 +01:00
}
void ScriptUtility::setMetatileLayerOrder(QList<int> order) {
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;
window->editor->layout->metatileLayerOrder = order;
window->refreshAfterPalettePreviewChange();
2022-09-05 01:24:11 +01:00
}
QList<float> ScriptUtility::getMetatileLayerOpacity() {
if (!window || !window->editor || !window->editor->layout)
2022-09-05 01:24:11 +01:00
return QList<float>();
return window->editor->layout->metatileLayerOpacity;
2022-09-05 01:24:11 +01:00
}
void ScriptUtility::setMetatileLayerOpacity(QList<float> order) {
if (!window || !window->editor || !window->editor->layout)
2022-09-05 01:24:11 +01:00
return;
window->editor->layout->metatileLayerOpacity = order;
window->refreshAfterPalettePreviewChange();
2022-09-05 01:24:11 +01:00
}
2022-10-01 04:01:00 +01:00
QList<QString> ScriptUtility::getMapNames() {
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() {
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
}