Merge pull request #11158 from jordan-woyak/abs-function

Input: Add "abs" input expression function.
This commit is contained in:
Mai 2022-10-12 17:35:12 +00:00 committed by GitHub
commit 1f8b196d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -303,6 +303,7 @@ void IOWindow::CreateMainLayout()
m_functions_combo->addItem(QStringLiteral("min"));
m_functions_combo->addItem(QStringLiteral("max"));
m_functions_combo->addItem(QStringLiteral("clamp"));
m_functions_combo->addItem(QStringLiteral("abs"));
m_variables_combo = new QComboBoxWithMouseWheelDisabled(this);
m_variables_combo->addItem(tr("User Variables"));

View File

@ -69,6 +69,22 @@ private:
void SetValue(ControlState value) override { GetArg(0).SetValue(1.0 - value); }
};
// usage: abs(expression)
class AbsExpression final : public FunctionExpression
{
private:
ArgumentValidation
ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
if (args.size() == 1)
return ArgumentsAreValid{};
else
return ExpectedArguments{"expression"};
}
ControlState GetValue() const override { return std::abs(GetArg(0).GetValue()); }
};
// usage: sin(expression)
class SinExpression : public FunctionExpression
{
@ -641,6 +657,8 @@ std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string_view name
return std::make_unique<NotExpression>();
if (name == "if")
return std::make_unique<IfExpression>();
if (name == "abs")
return std::make_unique<AbsExpression>();
if (name == "sin")
return std::make_unique<SinExpression>();
if (name == "cos")