summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControlReference
diff options
context:
space:
mode:
authorJordan Woyak <jordan.woyak@gmail.com>2019-01-26 14:15:36 -0600
committerJordan Woyak <jordan.woyak@gmail.com>2019-10-11 18:12:17 -0500
commitf2e499d5873eebb8417527deaf694e26a06cb5a9 (patch)
treea13d87f00a1d7d5d37bc9fbe8e223742e70e33e7 /Source/Core/InputCommon/ControlReference
parent9e536382c44074ae8af620adb605fe107f3ef42c (diff)
ExpressionParser: Add !hold function that activates after input is held for N seconds.
Diffstat (limited to 'Source/Core/InputCommon/ControlReference')
-rw-r--r--Source/Core/InputCommon/ControlReference/FunctionExpression.cpp44
1 files changed, 44 insertions, 0 deletions
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<std::unique_ptr<Expression>>& 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<ControlState>;
+
+ if (std::chrono::duration_cast<FSec>(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<FunctionExpression> MakeFunctionExpression(std::string name)
{
if (name.empty())
@@ -291,6 +333,8 @@ std::unique_ptr<FunctionExpression> MakeFunctionExpression(std::string name)
return std::make_unique<DeadzoneExpression>();
else if ("smooth" == name)
return std::make_unique<SmoothExpression>();
+ else if ("hold" == name)
+ return std::make_unique<HoldExpression>();
else
return std::make_unique<UnknownFunctionExpression>();
}