Skip to content
Permalink
Browse files

ExpressionParser: Add deadzone function.

  • Loading branch information...
jordan-woyak committed Jan 26, 2019
1 parent 6a2096c commit bbd6b1848fcaeaafbe14babb3911557e1a503c76
Showing with 28 additions and 0 deletions.
  1. +28 −0 Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
@@ -27,6 +27,7 @@ class UnknownFunctionExpression : public FunctionExpression
std::string GetFuncName() const override { return "unknown"; }
};

// usage: !toggle(toggle_state_input, [clear_state_input])
class ToggleExpression : public FunctionExpression
{
private:
@@ -65,6 +66,7 @@ class ToggleExpression : public FunctionExpression
mutable bool m_state{};
};

// usage: !not(expression)
class NotExpression : public FunctionExpression
{
private:
@@ -78,6 +80,7 @@ class NotExpression : public FunctionExpression
std::string GetFuncName() const override { return ""; }
};

// usage: !sin(expression)
class SinExpression : public FunctionExpression
{
private:
@@ -91,6 +94,7 @@ class SinExpression : public FunctionExpression
std::string GetFuncName() const override { return "sin"; }
};

// usage: !timer(seconds)
class TimerExpression : public FunctionExpression
{
private:
@@ -134,6 +138,7 @@ class TimerExpression : public FunctionExpression
mutable Clock::time_point m_start_time = Clock::now();
};

// usage: !if(condition, true_expression, false_expression)
class IfExpression : public FunctionExpression
{
private:
@@ -152,6 +157,7 @@ class IfExpression : public FunctionExpression
std::string GetFuncName() const override { return "if"; }
};

// usage: !minus(expression)
class UnaryMinusExpression : public FunctionExpression
{
private:
@@ -170,6 +176,7 @@ class UnaryMinusExpression : public FunctionExpression
std::string GetFuncName() const override { return "minus"; }
};

// usage: !while(condition, expression)
class WhileExpression : public FunctionExpression
{
virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
@@ -200,6 +207,25 @@ class WhileExpression : public FunctionExpression
std::string GetFuncName() const override { return "while"; }
};

// usage: deadzone(input, amount)
class DeadzoneExpression : public FunctionExpression
{
virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
return 2 == args.size();
}

ControlState GetValue() const override
{
const ControlState val = GetArg(0).GetValue();
const ControlState deadzone = GetArg(1).GetValue();
return std::copysign(std::max(0.0, std::abs(val) - deadzone) / (1.0 - deadzone), val);
}

void SetValue(ControlState value) override {}
std::string GetFuncName() const override { return "deadzone"; }
};

std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name)
{
if (name.empty())
@@ -216,6 +242,8 @@ std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name)
return std::make_unique<WhileExpression>();
else if ("minus" == name)
return std::make_unique<UnaryMinusExpression>();
else if ("deadzone" == name)
return std::make_unique<DeadzoneExpression>();
else
return std::make_unique<UnknownFunctionExpression>();
}

0 comments on commit bbd6b18

Please sign in to comment.
You can’t perform that action at this time.