From f5b9049421047f61cf9c139b7671564b04677de5 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Tue, 11 Oct 2022 15:13:33 -0500 Subject: [PATCH] Input: Add "abs" input expression function. --- .../Core/DolphinQt/Config/Mapping/IOWindow.cpp | 1 + .../ControlReference/FunctionExpression.cpp | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp b/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp index bf5d911afb..8bec82f015 100644 --- a/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp +++ b/Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp @@ -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")); diff --git a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp index 17ceae3bd0..46cfc37beb 100644 --- a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp +++ b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp @@ -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>& 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 MakeFunctionExpression(std::string_view name return std::make_unique(); if (name == "if") return std::make_unique(); + if (name == "abs") + return std::make_unique(); if (name == "sin") return std::make_unique(); if (name == "cos")