Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11158 from jordan-woyak/abs-function
Input: Add "abs" input expression function.
  • Loading branch information
lioncash committed Oct 12, 2022
2 parents 0c09eb5 + f5b9049 commit 1f8b196
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp
Expand Up @@ -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"));
Expand Down
18 changes: 18 additions & 0 deletions Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
Expand Up @@ -69,6 +69,22 @@ class NotExpression : public FunctionExpression
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
{
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 1f8b196

Please sign in to comment.