Skip to content
Permalink
Browse files

ExpressionParser: Add function to smooth inputs.

  • Loading branch information...
jordan-woyak committed Jan 26, 2019
1 parent bbd6b18 commit 9e536382c44074ae8af620adb605fe107f3ef42c
Showing with 47 additions and 0 deletions.
  1. +47 −0 Source/Core/InputCommon/ControlReference/FunctionExpression.cpp
@@ -226,6 +226,51 @@ class DeadzoneExpression : public FunctionExpression
std::string GetFuncName() const override { return "deadzone"; }
};

// usage: smooth(input, seconds)
// seconds is seconds to change from 0.0 to 1.0
class SmoothExpression : public FunctionExpression
{
virtual bool ValidateArguments(const std::vector<std::unique_ptr<Expression>>& args) override
{
return 2 == args.size();
}

ControlState GetValue() const override
{
const auto now = Clock::now();
const auto elapsed = now - m_last_update;
m_last_update = now;

const ControlState desired_value = GetArg(0).GetValue();
const ControlState smooth = GetArg(1).GetValue();

using FSec = std::chrono::duration<ControlState>;

const ControlState max_move = std::chrono::duration_cast<FSec>(elapsed).count() / smooth;

if (std::isinf(max_move))
{
m_value = desired_value;
}
else
{
const ControlState diff = desired_value - m_value;
m_value += std::copysign(std::min(max_move, std::abs(diff)), diff);
}

return m_value;
}

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

private:
using Clock = std::chrono::steady_clock;

mutable ControlState m_value = 0.0;
mutable Clock::time_point m_last_update = Clock::now();
};

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

0 comments on commit 9e53638

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