Add message boxes and user input windows to API

This commit is contained in:
GriffinR 2022-07-14 16:10:04 -04:00
parent 11661818b6
commit c8b28be0b4
4 changed files with 65 additions and 0 deletions

View file

@ -149,6 +149,14 @@ public:
Q_INVOKABLE void log(QString message);
Q_INVOKABLE void warn(QString message);
Q_INVOKABLE void error(QString message);
void runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon);
Q_INVOKABLE void showMessage(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showWarning(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE void showError(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE bool showQuestion(QString text, QString informativeText = "", QString detailedText = "");
Q_INVOKABLE QJSValue getInputText(QString title, QString label, QString defaultValue = "");
Q_INVOKABLE QJSValue getInputNumber(QString title, QString label, double defaultValue = 0, double min = INT_MIN, double max = INT_MAX, int decimals = 0, double step = 1);
Q_INVOKABLE QJSValue getInputItem(QString title, QString label, QStringList items, int defaultValue = 0, bool editable = false);
Q_INVOKABLE QList<int> getMetatileLayerOrder();
Q_INVOKABLE void setMetatileLayerOrder(QList<int> order);
Q_INVOKABLE QList<float> getMetatileLayerOpacity();

View file

@ -33,6 +33,7 @@ public:
static QJSValue position(int x, int y);
static QJSEngine *getEngine();
static QImage getImage(QString filepath);
static QJSValue dialogInput(QJSValue input, bool selectedOk);
static void init(MainWindow *mainWindow);
static void registerAction(QString functionName, QString actionName);
static int numRegisteredActions();

View file

@ -785,6 +785,55 @@ void MainWindow::error(QString message) {
logError(message);
}
void MainWindow::runMessageBox(QString text, QString informativeText, QString detailedText, QMessageBox::Icon icon) {
QMessageBox messageBox(this);
messageBox.setText(text);
messageBox.setInformativeText(informativeText);
messageBox.setDetailedText(detailedText);
messageBox.setIcon(icon);
messageBox.exec();
}
void MainWindow::showMessage(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Information);
}
void MainWindow::showWarning(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Warning);
}
void MainWindow::showError(QString text, QString informativeText, QString detailedText) {
this->runMessageBox(text, informativeText, detailedText, QMessageBox::Critical);
}
bool MainWindow::showQuestion(QString text, QString informativeText, QString detailedText) {
QMessageBox messageBox(this);
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 MainWindow::getInputText(QString title, QString label, QString defaultValue) {
bool ok;
QString input = QInputDialog::getText(this, title, label, QLineEdit::Normal, defaultValue, &ok);
return Scripting::dialogInput(input, ok);
}
QJSValue MainWindow::getInputNumber(QString title, QString label, double defaultValue, double min, double max, int decimals, double step) {
bool ok;
double input = QInputDialog::getDouble(this, title, label, defaultValue, min, max, decimals, &ok, Qt::WindowFlags(), step);
return Scripting::dialogInput(input, ok);
}
QJSValue MainWindow::getInputItem(QString title, QString label, QStringList items, int defaultValue, bool editable) {
bool ok;
QString input = QInputDialog::getItem(this, title, label, items, defaultValue, editable, &ok);
return Scripting::dialogInput(input, ok);
}
QList<int> MainWindow::getMetatileLayerOrder() {
if (!this->editor || !this->editor->map)
return QList<int>();

View file

@ -262,6 +262,13 @@ QJSValue Scripting::fromTile(Tile tile) {
return obj;
}
QJSValue Scripting::dialogInput(QJSValue input, bool selectedOk) {
QJSValue obj = instance->engine->newObject();
obj.setProperty("input", input);
obj.setProperty("ok", selectedOk);
return obj;
}
QJSEngine *Scripting::getEngine() {
return instance->engine;
}