diff --git a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp index 8692d04c78..b7dfe23e02 100644 --- a/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp +++ b/Source/Core/InputCommon/ControlReference/FunctionExpression.cpp @@ -271,6 +271,48 @@ private: mutable Clock::time_point m_last_update = Clock::now(); }; +// usage: !hold(input, seconds) +class HoldExpression : public FunctionExpression +{ + virtual bool ValidateArguments(const std::vector>& args) override + { + return 2 == args.size(); + } + + ControlState GetValue() const override + { + const auto now = Clock::now(); + + const ControlState input = GetArg(0).GetValue(); + + if (input < CONDITION_THRESHOLD) + { + m_state = false; + m_start_time = Clock::now(); + } + else if (!m_state) + { + const auto hold_time = now - m_start_time; + + using FSec = std::chrono::duration; + + if (std::chrono::duration_cast(hold_time).count() >= GetArg(1).GetValue()) + m_state = true; + } + + return m_state; + } + + void SetValue(ControlState value) override {} + std::string GetFuncName() const override { return "smooth"; } + +private: + using Clock = std::chrono::steady_clock; + + mutable bool m_state = false; + mutable Clock::time_point m_start_time = Clock::now(); +}; + std::unique_ptr MakeFunctionExpression(std::string name) { if (name.empty()) @@ -291,6 +333,8 @@ std::unique_ptr MakeFunctionExpression(std::string name) return std::make_unique(); else if ("smooth" == name) return std::make_unique(); + else if ("hold" == name) + return std::make_unique(); else return std::make_unique(); }