Skip to content

Commit

Permalink
Support expressions presets/bundle
Browse files Browse the repository at this point in the history
Support extending the default JS functions.

Added 'clamp' as an example.
  • Loading branch information
rodlie committed Jan 26, 2025
1 parent 2d8279c commit a23c885
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/app/GUI/Expressions/expressiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ void addBasicDefs(QsciAPIs* const target) {
target->add("Math.tan(x)");
target->add("Math.tanh(x)");
target->add("Math.trunc(x)");

// load expressions bundle
const auto bundle = eSettings::instance().expressionsBundle;
for (const auto &fun : bundle) { target->add(fun.first); }
}

ExpressionDialog::ExpressionDialog(QrealAnimator* const target,
Expand Down
11 changes: 9 additions & 2 deletions src/core/Expressions/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "expression.h"

#include "exceptions.h"
#include "Private/esettings.h"

Expression::ResultTester Expression::sQrealAnimatorTester =
[](const QJSValue& val) {
Expand Down Expand Up @@ -60,8 +61,14 @@ void throwIfError(const QJSValue& value, const QString& name) {
}

void Expression::sAddDefinitionsTo(const QString& definitionsStr,
QJSEngine& e) {
const auto defRet = e.evaluate(definitionsStr);
QJSEngine& e)
{
QString defs;
const auto bundle = eSettings::instance().expressionsBundle;
for (const auto &fun : bundle) { defs.append(fun.second); }
defs.append(definitionsStr);

const auto defRet = e.evaluate(defs);
throwIfError(defRet, "Definitions");
}

Expand Down
1 change: 1 addition & 0 deletions src/core/Expressions/functions/clamp.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target="clamp(x, lower, upper)"
3 changes: 3 additions & 0 deletions src/core/Expressions/functions/clamp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function clamp(number, lower, upper) {
return Math.max(lower, Math.min(number, upper));
}
3 changes: 3 additions & 0 deletions src/core/Private/esettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class CORE_EXPORT eSettings : public QObject
QList<QAction*> fCommandPalette;
QList<QString> fCommandHistory;

// Expressions bundle
QList<QPair<QString,QString>> expressionsBundle = AppSupport::getExpressionsBundle();

signals:
void settingsChanged();

Expand Down
25 changes: 25 additions & 0 deletions src/core/appsupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,28 @@ const QString AppSupport::filterId(const QString &input)
{
return QString(input).simplified().replace(" ", "");
}

const QList<QPair<QString, QString> > AppSupport::getExpressionsBundle()
{
QStringList functions;
functions << "clamp";

QList<QPair<QString, QString> > result;
for (const auto &fun : functions) {
const QString funConf = QString(":/expressions/%1.conf").arg(fun);
const QString funJs = QString(":/expressions/%1.js").arg(fun);
if (!QFile::exists(funConf) || !QFile::exists(funJs)) { continue; }
QPair<QString,QString> expr;
QSettings conf(funConf, QSettings::IniFormat);
expr.first = conf.value("target").toString();
QFile funFile(funJs);
if (funFile.open(QIODevice::Text | QIODevice::ReadOnly)) {
expr.second = funFile.readAll();
funFile.close();
}
if (expr.first.isEmpty() || expr.second.isEmpty()) { continue; }
qDebug() << "adding expression function" << expr.first;
result << expr;
}
return result;
}
1 change: 1 addition & 0 deletions src/core/appsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class CORE_EXPORT AppSupport : public QObject
static const QList<QPair<QString,QString>> getEasingPresets();
static void handlePortableFirstRun();
static const QString filterId(const QString &input);
static const QList<QPair<QString,QString>> getExpressionsBundle();
};

#endif // APPSUPPORT_H
4 changes: 4 additions & 0 deletions src/core/coreresources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
<file alias="red.frag">colorshaders/red.frag</file>
<file alias="value.frag">colorshaders/value.frag</file>
</qresource>
<qresource prefix="/expressions">
<file alias="clamp.conf">Expressions/functions/clamp.conf</file>
<file alias="clamp.js">Expressions/functions/clamp.js</file>
</qresource>
</RCC>

0 comments on commit a23c885

Please sign in to comment.